widgetBarlinechart.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart :options="options" autoresize />
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "WidgetBarlinechart",
  9. components: {},
  10. props: {
  11. value: Object,
  12. ispreview: Boolean,
  13. },
  14. data() {
  15. return {
  16. options: {
  17. color: [],
  18. grid: {},
  19. title: {
  20. text: "",
  21. textStyle: {
  22. color: "#fff",
  23. },
  24. },
  25. tooltip: {
  26. trigger: "item",
  27. formatter: "{a} <br/>{b} : {c}%",
  28. },
  29. legend: {
  30. textStyle: {
  31. color: "#fff",
  32. },
  33. },
  34. xAxis: [
  35. {
  36. type: "category",
  37. data: [],
  38. axisLabel: {
  39. show: false,
  40. textStyle: {
  41. color: "#fff",
  42. },
  43. },
  44. },
  45. ],
  46. yAxis: [
  47. {
  48. type: "value",
  49. name: "",
  50. min: 0,
  51. max: 250,
  52. interval: 50,
  53. axisLabel: {
  54. show: true,
  55. textStyle: {
  56. color: "#fff",
  57. },
  58. },
  59. },
  60. {
  61. type: "value",
  62. name: "",
  63. min: 0,
  64. max: 25,
  65. interval: 5,
  66. axisLabel: {
  67. show: true,
  68. textStyle: {
  69. color: "#fff",
  70. },
  71. },
  72. },
  73. ],
  74. series: [
  75. {
  76. name: "",
  77. type: "bar",
  78. yAxisIndex: 0,
  79. data: [],
  80. itemStyle: {
  81. barBorderRadius: null,
  82. },
  83. },
  84. {
  85. name: "",
  86. type: "line",
  87. yAxisIndex: 1,
  88. data: [],
  89. itemStyle: {},
  90. },
  91. ],
  92. },
  93. optionsStyle: {}, // 样式
  94. optionsData: {}, // 数据
  95. optionsCollapse: {}, // 图标属性
  96. optionsSetup: {},
  97. };
  98. },
  99. computed: {
  100. styleObj() {
  101. return {
  102. position: this.ispreview ? "absolute" : "static",
  103. width: this.optionsStyle.width + "px",
  104. height: this.optionsStyle.height + "px",
  105. left: this.optionsStyle.left + "px",
  106. top: this.optionsStyle.top + "px",
  107. background: this.optionsSetup.background,
  108. };
  109. },
  110. },
  111. watch: {
  112. value: {
  113. handler(val) {
  114. this.optionsStyle = val.position;
  115. this.optionsData = val.data;
  116. this.optionsCollapse = val.collapse;
  117. this.optionsSetup = val.setup;
  118. this.editorOptions();
  119. },
  120. deep: true,
  121. },
  122. },
  123. created() {
  124. this.optionsStyle = this.value.position;
  125. this.optionsData = this.value.data;
  126. this.optionsCollapse = this.value.collapse;
  127. this.optionsSetup = this.value.setup;
  128. this.editorOptions();
  129. },
  130. methods: {
  131. // 修改图标options属性
  132. editorOptions() {
  133. this.setOptionsTitle();
  134. this.setOptionsX();
  135. this.setOptionsY();
  136. this.setOptionsLine();
  137. this.setOptionsBar();
  138. this.setOptionsTooltip();
  139. this.setOptionsData();
  140. this.setOptionsMargin();
  141. this.setOptionsLegend();
  142. this.setOptionsColor();
  143. },
  144. // 标题修改
  145. setOptionsTitle() {
  146. const optionsSetup = this.optionsSetup;
  147. const title = {};
  148. title.text = optionsSetup.titleText;
  149. title.show = optionsSetup.isNoTitle;
  150. title.left = optionsSetup.textAlign;
  151. title.textStyle = {
  152. color: optionsSetup.textColor,
  153. fontSize: optionsSetup.textFontSize,
  154. fontWeight: optionsSetup.textFontWeight,
  155. fontStyle: optionsSetup.textFontStyle,
  156. };
  157. title.subtext = optionsSetup.subText;
  158. title.subtextStyle = {
  159. color: optionsSetup.subTextColor,
  160. fontWeight: optionsSetup.subTextFontWeight,
  161. fontSize: optionsSetup.subTextFontSize,
  162. fontStyle: optionsSetup.subTextFontStyle,
  163. };
  164. this.options.title = title;
  165. },
  166. // X轴设置
  167. setOptionsX() {
  168. const optionsSetup = this.optionsSetup;
  169. const xAxis = {
  170. type: "category",
  171. // 坐标轴是否显示
  172. show: optionsSetup.hideX,
  173. // 坐标轴名称
  174. name: optionsSetup.nameX,
  175. nameTextStyle: {
  176. color: optionsSetup.nameColorX,
  177. fontSize: optionsSetup.nameFontSizeX,
  178. },
  179. // 轴反转
  180. inverse: optionsSetup.reversalX,
  181. axisLabel: {
  182. show: true,
  183. // 文字间隔
  184. interval: optionsSetup.textInterval,
  185. // 文字角度
  186. rotate: optionsSetup.textAngleX,
  187. textStyle: {
  188. // 坐标文字颜色
  189. color: optionsSetup.colorX,
  190. fontSize: optionsSetup.fontSizeX,
  191. },
  192. },
  193. axisLine: {
  194. show: true,
  195. lineStyle: {
  196. color: optionsSetup.lineColorX,
  197. width: optionsSetup.lineWidthX,
  198. },
  199. },
  200. splitLine: {
  201. show: optionsSetup.isShowSplitLineX,
  202. lineStyle: {
  203. color: optionsSetup.splitLineColorX,
  204. width: optionsSetup.splitLineWidthX,
  205. },
  206. },
  207. };
  208. this.options.xAxis = xAxis;
  209. },
  210. // Y轴设置
  211. setOptionsY() {
  212. const optionsSetup = this.optionsSetup;
  213. const yAxis = [
  214. {
  215. type: "value",
  216. // 均分
  217. splitNumber: optionsSetup.splitNumberLeft,
  218. // 坐标轴是否显示
  219. show: optionsSetup.isShowYLeft,
  220. // 坐标轴名称
  221. name: optionsSetup.textNameYLeft,
  222. // 别名
  223. nameTextStyle: {
  224. color: optionsSetup.nameColorYLeft,
  225. fontSize: optionsSetup.nameFontSizeYLeft,
  226. },
  227. axisLabel: {
  228. show: true,
  229. // 文字角度
  230. rotate: optionsSetup.textAngleYLeft,
  231. textStyle: {
  232. // 坐标文字颜色
  233. color: optionsSetup.colorYLeft,
  234. fontSize: optionsSetup.fontSizeYLeft,
  235. },
  236. },
  237. axisTick: {
  238. // 刻度
  239. show: optionsSetup.tickLineYLeft,
  240. },
  241. axisLine: {
  242. show: optionsSetup.lineYLeft,
  243. lineStyle: {
  244. width: optionsSetup.lineWidthYLeft,
  245. color: optionsSetup.lineColorYLeft,
  246. },
  247. },
  248. splitLine: {
  249. show: optionsSetup.isShowSplitLineYLeft,
  250. lineStyle: {
  251. color: optionsSetup.splitLineColorYLeft,
  252. width: optionsSetup.splitLineFontWidthYLeft,
  253. },
  254. },
  255. },
  256. {
  257. type: "value",
  258. // 均分
  259. splitNumber: optionsSetup.splitNumberRight,
  260. // 坐标轴是否显示
  261. show: optionsSetup.isShowYRight,
  262. // 坐标轴名称
  263. name: optionsSetup.textNameYRight,
  264. // 别名
  265. nameTextStyle: {
  266. color: optionsSetup.nameColorYRight,
  267. fontSize: optionsSetup.nameFontSizeYRight,
  268. },
  269. axisLabel: {
  270. show: true,
  271. // 文字角度
  272. rotate: optionsSetup.textAngleYRight,
  273. textStyle: {
  274. // 坐标文字颜色
  275. color: optionsSetup.colorYRight,
  276. fontSize: optionsSetup.fontSizeYRight,
  277. },
  278. },
  279. axisTick: {
  280. // 刻度
  281. show: optionsSetup.tickLineYRight,
  282. },
  283. axisLine: {
  284. show: optionsSetup.lineYRight,
  285. lineStyle: {
  286. width: optionsSetup.lineWidthYRight,
  287. color: optionsSetup.lineColorYRight,
  288. },
  289. },
  290. splitLine: {
  291. show: optionsSetup.isShowSplitLineYRight,
  292. lineStyle: {
  293. color: optionsSetup.splitLineColorYRight,
  294. width: optionsSetup.splitLineFontWidthYRight,
  295. },
  296. },
  297. },
  298. ];
  299. this.options.yAxis = yAxis;
  300. },
  301. // 折线设置 数值设置
  302. setOptionsLine() {
  303. const optionsSetup = this.optionsSetup;
  304. const series = this.options.series;
  305. for (const key in series) {
  306. if (series[key].type == "line") {
  307. series[key].symbol = optionsSetup.symbol;
  308. series[key].showSymbol = optionsSetup.markPoint;
  309. series[key].symbolSize = optionsSetup.pointSize;
  310. series[key].smooth = optionsSetup.smoothCurve;
  311. if (optionsSetup.area) {
  312. series[key].areaStyle = {
  313. opacity: optionsSetup.areaThickness / 100,
  314. };
  315. } else {
  316. series[key].areaStyle = {
  317. opacity: 0,
  318. };
  319. }
  320. series[key].lineStyle = {
  321. width: optionsSetup.lineWidth,
  322. };
  323. series[key].itemStyle.borderRadius = optionsSetup.radius;
  324. series[key].label = {
  325. show: optionsSetup.isShowLine,
  326. position: "top",
  327. distance: optionsSetup.distanceLine,
  328. fontSize: optionsSetup.fontSizeLine,
  329. color: optionsSetup.subTextColorLine,
  330. fontWeight: optionsSetup.fontWeightLine,
  331. };
  332. }
  333. }
  334. this.options.series = series;
  335. },
  336. // 柱体设置 数值设置
  337. setOptionsBar() {
  338. const optionsSetup = this.optionsSetup;
  339. const series = this.options.series;
  340. for (const key in series) {
  341. if (series[key].type == "bar") {
  342. series[key].label = {
  343. show: optionsSetup.isShowBar,
  344. position: "top",
  345. distance: optionsSetup.distanceBar,
  346. fontSize: optionsSetup.fontSizeBar,
  347. color: optionsSetup.subTextColorBar,
  348. fontWeight: optionsSetup.fontWeightBar,
  349. };
  350. series[key].barWidth = optionsSetup.maxWidth;
  351. series[key].barMinHeight = optionsSetup.minHeight;
  352. series[key].itemStyle.barBorderRadius = optionsSetup.radius;
  353. }
  354. }
  355. this.options.series = series;
  356. },
  357. // tooltip 设置
  358. setOptionsTooltip() {
  359. const optionsSetup = this.optionsSetup;
  360. const tooltip = {
  361. trigger: "item",
  362. show: true,
  363. textStyle: {
  364. color: optionsSetup.tipsColor,
  365. fontSize: optionsSetup.tipsFontSize,
  366. },
  367. };
  368. this.options.tooltip = tooltip;
  369. },
  370. // 边距设置
  371. setOptionsMargin() {
  372. const optionsSetup = this.optionsSetup;
  373. const grid = {
  374. left: optionsSetup.marginLeft,
  375. right: optionsSetup.marginRight,
  376. bottom: optionsSetup.marginBottom,
  377. top: optionsSetup.marginTop,
  378. containLabel: true,
  379. };
  380. this.options.grid = grid;
  381. },
  382. setOptionsLegend() {
  383. const optionsSetup = this.optionsSetup;
  384. const legend = this.options.legend;
  385. legend.show = optionsSetup.isShowLegend;
  386. legend.left = optionsSetup.lateralPosition;
  387. legend.top = optionsSetup.longitudinalPosition;
  388. legend.bottom = optionsSetup.longitudinalPosition;
  389. legend.orient = optionsSetup.layoutFront;
  390. legend.textStyle = {
  391. color: optionsSetup.legendColor,
  392. fontSize: optionsSetup.legendFontSize,
  393. };
  394. legend.itemWidth = optionsSetup.legendWidth;
  395. },
  396. // 图例名称设置
  397. setOptionsLegendName(name) {
  398. const optionsSetup = this.optionsSetup;
  399. const series = this.options.series;
  400. const legendName = optionsSetup.legendName;
  401. // 图例没有手动写则显示原值,写了则显示新值
  402. if (null == legendName || legendName == "") {
  403. for (let i = 0; i < name.length; i++) {
  404. series[i].name = name[i];
  405. }
  406. this.options.legend["data"] = name;
  407. } else {
  408. const arr = legendName.split("|");
  409. for (let i = 0; i < arr.length; i++) {
  410. series[i].name = arr[i];
  411. }
  412. this.options.legend["data"] = arr;
  413. }
  414. },
  415. // 图例颜色修改
  416. setOptionsColor() {
  417. const optionsSetup = this.optionsSetup;
  418. const customColor = optionsSetup.customColor;
  419. if (!customColor) return;
  420. const arrColor = [];
  421. for (let i = 0; i < customColor.length; i++) {
  422. arrColor.push(customColor[i].color);
  423. }
  424. this.options.color = arrColor;
  425. this.options = Object.assign({}, this.options);
  426. },
  427. // 数据处理
  428. setOptionsData() {
  429. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  430. optionsData.dataType == "staticData"
  431. ? this.staticDataFn(optionsData.staticData)
  432. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  433. },
  434. staticDataFn(val) {
  435. const series = this.options.series;
  436. let axis = [];
  437. let bar = [];
  438. let line = [];
  439. for (const i in val) {
  440. axis[i] = val[i].axis;
  441. bar[i] = val[i].bar;
  442. line[i] = val[i].line;
  443. }
  444. // x轴
  445. this.options.xAxis.data = axis;
  446. // series
  447. for (const i in series) {
  448. if (series[i].type == "bar") {
  449. series[i].data = bar;
  450. } else {
  451. series[i].data = line;
  452. }
  453. }
  454. const legendName = [];
  455. legendName.push("bar");
  456. legendName.push("line");
  457. this.options.legend["data"] = legendName;
  458. this.setOptionsLegendName(legendName);
  459. },
  460. dynamicDataFn(val, refreshTime) {
  461. if (!val) return;
  462. if (this.ispreview) {
  463. this.getEchartData(val);
  464. this.flagInter = setInterval(() => {
  465. this.getEchartData(val);
  466. }, refreshTime);
  467. } else {
  468. this.getEchartData(val);
  469. }
  470. },
  471. getEchartData(val) {
  472. const data = this.queryEchartsData(val);
  473. data.then((res) => {
  474. this.renderingFn(res);
  475. });
  476. },
  477. renderingFn(val) {
  478. this.options.xAxis.data = val.xAxis;
  479. // series
  480. const series = this.options.series;
  481. const legendName = [];
  482. for (const i in series) {
  483. for (const j in val.series) {
  484. if (series[i].type == val.series[j].type) {
  485. series[i].data = val.series[j].data;
  486. legendName.push(val.series[j].name);
  487. }
  488. }
  489. }
  490. this.options.legend["data"] = legendName;
  491. this.setOptionsLegendName(legendName);
  492. },
  493. },
  494. };
  495. </script>
  496. <style scoped lang="scss">
  497. .echarts {
  498. width: 100%;
  499. height: 100%;
  500. overflow: hidden;
  501. }
  502. </style>