widgetBarDoubleYaxisChart.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart ref="myVChart" :option="options" autoresize/>
  4. </div>
  5. </template>
  6. <script>
  7. import {targetWidgetLinkageLogic} from "@/views/bigscreenDesigner/designer/linkageLogic";
  8. export default {
  9. name: "WidgetBarDoubleYaxisChart",
  10. components: {},
  11. props: {
  12. value: Object,
  13. ispreview: Boolean,
  14. },
  15. data() {
  16. return {
  17. options: {
  18. color: [],
  19. grid: {},
  20. title: {
  21. text: "",
  22. textStyle: {
  23. color: "#fff",
  24. },
  25. },
  26. tooltip: {
  27. trigger: "item",
  28. formatter: "{a} <br/>{b} : {c}%",
  29. },
  30. legend: {
  31. textStyle: {
  32. color: "#fff",
  33. },
  34. },
  35. xAxis: [
  36. {
  37. type: "category",
  38. data: [],
  39. axisLabel: {
  40. show: false,
  41. textStyle: {
  42. color: "#fff",
  43. },
  44. },
  45. },
  46. ],
  47. yAxis: [
  48. {
  49. type: "value",
  50. name: "",
  51. min: 0,
  52. max: 250,
  53. interval: 50,
  54. axisLabel: {
  55. show: true,
  56. textStyle: {
  57. color: "#fff",
  58. },
  59. },
  60. },
  61. {
  62. type: "value",
  63. name: "",
  64. min: 0,
  65. max: 25,
  66. interval: 5,
  67. axisLabel: {
  68. show: true,
  69. textStyle: {
  70. color: "#fff",
  71. },
  72. },
  73. },
  74. ],
  75. series: [
  76. {
  77. name: "",
  78. type: "bar",
  79. yAxisIndex: 0,
  80. data: [],
  81. itemStyle: {
  82. barBorderRadius: null,
  83. },
  84. },
  85. {
  86. name: "",
  87. type: "bar",
  88. yAxisIndex: 1,
  89. data: [],
  90. itemStyle: {},
  91. },
  92. ],
  93. },
  94. optionsStyle: {}, // 样式
  95. optionsData: {}, // 数据
  96. optionsCollapse: {}, // 图标属性
  97. optionsSetup: {},
  98. flagInter: null,
  99. };
  100. },
  101. computed: {
  102. styleObj() {
  103. return {
  104. position: this.ispreview ? "absolute" : "static",
  105. width: this.optionsStyle.width + "px",
  106. height: this.optionsStyle.height + "px",
  107. left: this.optionsStyle.left + "px",
  108. top: this.optionsStyle.top + "px",
  109. background: this.optionsSetup.background,
  110. };
  111. },
  112. allComponentLinkage() {
  113. return this.$store.state.designer.allComponentLinkage;
  114. },
  115. },
  116. watch: {
  117. value: {
  118. handler(val) {
  119. this.optionsStyle = val.position;
  120. this.optionsData = val.data;
  121. this.optionsCollapse = val.collapse;
  122. this.optionsSetup = val.setup;
  123. this.editorOptions();
  124. },
  125. deep: true,
  126. },
  127. },
  128. created() {
  129. this.optionsStyle = this.value.position;
  130. this.optionsData = this.value.data;
  131. this.optionsCollapse = this.value.collapse;
  132. this.optionsSetup = this.value.setup;
  133. this.editorOptions();
  134. targetWidgetLinkageLogic(this); // 联动-目标组件逻辑
  135. },
  136. methods: {
  137. // 修改图标options属性
  138. editorOptions() {
  139. this.setOptionsTitle();
  140. this.setOptionsX();
  141. this.setOptionsY();
  142. this.setOptionsBar();
  143. this.setOptionsTooltip();
  144. this.setOptionsData();
  145. this.setOptionsMargin();
  146. this.setOptionsLegend();
  147. this.setOptionsColor();
  148. },
  149. // 标题修改
  150. setOptionsTitle() {
  151. const optionsSetup = this.optionsSetup;
  152. const title = {
  153. text: optionsSetup.text,
  154. show: optionsSetup.isShowTitle,
  155. left: optionsSetup.titleLeft,
  156. top: optionsSetup.titleTop + "%",
  157. itemGap: optionsSetup.titleItemGap,
  158. textStyle: {
  159. color: optionsSetup.textColor,
  160. fontSize: optionsSetup.textFontSize,
  161. fontWeight: optionsSetup.textFontWeight,
  162. fontStyle: optionsSetup.textFontStyle,
  163. fontFamily: optionsSetup.textFontFamily,
  164. },
  165. subtext: optionsSetup.subtext,
  166. subtextStyle: {
  167. color: optionsSetup.subtextColor,
  168. fontWeight: optionsSetup.subtextFontWeight,
  169. fontSize: optionsSetup.subtextFontSize,
  170. fontStyle: optionsSetup.subtextFontStyle,
  171. fontFamily: optionsSetup.subtextFontFamily
  172. },
  173. };
  174. this.options.title = title;
  175. },
  176. // X轴设置
  177. setOptionsX() {
  178. const optionsSetup = this.optionsSetup;
  179. const xAxis = {
  180. type: "category",
  181. // 坐标轴是否显示
  182. show: optionsSetup.isShowX,
  183. position: optionsSetup.positionX,
  184. offset: optionsSetup.offsetX,
  185. // 坐标轴名称
  186. name: optionsSetup.nameX,
  187. nameLocation: optionsSetup.nameLocationX,
  188. nameTextStyle: {
  189. color: optionsSetup.nameColorX,
  190. fontSize: optionsSetup.nameFontSizeX,
  191. fontWeight: optionsSetup.nameFontWeightX,
  192. fontStyle: optionsSetup.nameFontStyleX,
  193. fontFamily: optionsSetup.nameFontFamilyX,
  194. },
  195. // 轴反转
  196. inverse: optionsSetup.reversalX,
  197. axisLabel: {
  198. show: optionsSetup.isShowAxisLabelX,
  199. interval: optionsSetup.textIntervalX,
  200. // 文字角度
  201. rotate: optionsSetup.textAngleX,
  202. textStyle: {
  203. // 坐标文字颜色
  204. color: optionsSetup.textColorX,
  205. fontSize: optionsSetup.textFontSizeX,
  206. fontWeight: optionsSetup.textFontWeightX,
  207. fontStyle: optionsSetup.textFontStyleX,
  208. fontFamily: optionsSetup.textFontFamilyX,
  209. },
  210. },
  211. axisLine: {
  212. show: optionsSetup.isShowAxisLineX,
  213. lineStyle: {
  214. color: optionsSetup.lineColorX,
  215. width: optionsSetup.lineWidthX,
  216. },
  217. },
  218. axisTick: {
  219. show: optionsSetup.isShowAxisLineX,
  220. lineStyle: {
  221. color: optionsSetup.lineColorX,
  222. width: optionsSetup.lineWidthX,
  223. },
  224. },
  225. splitLine: {
  226. show: optionsSetup.isShowSplitLineX,
  227. lineStyle: {
  228. color: optionsSetup.splitLineColorX,
  229. width: optionsSetup.splitLineWidthX,
  230. },
  231. },
  232. };
  233. this.options.xAxis = xAxis;
  234. },
  235. // Y轴设置
  236. setOptionsY() {
  237. const optionsSetup = this.optionsSetup;
  238. const yAxis = [
  239. {
  240. max: optionsSetup.maxYLeft !== "" ? optionsSetup.maxYLeft : null,
  241. type: "value",
  242. scale: optionsSetup.scaleYLeft,
  243. // 均分
  244. splitNumber: optionsSetup.splitNumberYLeft,
  245. // 坐标轴是否显示
  246. show: optionsSetup.isShowYLeft,
  247. position: optionsSetup.positionYLeft,
  248. offset: optionsSetup.offsetYLeft,
  249. // 坐标轴名称
  250. name: optionsSetup.textNameYLeft,
  251. nameLocation: optionsSetup.nameLocationYLeft,
  252. // 别名
  253. nameTextStyle: {
  254. color: optionsSetup.nameColorYLeft,
  255. fontSize: optionsSetup.nameFontSizeYLeft,
  256. fontWeight: optionsSetup.nameFontWeightYLeft,
  257. fontStyle: optionsSetup.nameFontStyleYLeft,
  258. fontFamily: optionsSetup.nameFontFamilyYLeft,
  259. },
  260. axisLabel: {
  261. show: optionsSetup.isShowAxisLabelYLeft,
  262. // 文字角度
  263. rotate: optionsSetup.textAngleYLeft,
  264. textStyle: {
  265. // 坐标文字颜色
  266. color: optionsSetup.textColorYLeft,
  267. fontSize: optionsSetup.textFontSizeYLeft,
  268. fontWeight: optionsSetup.textFontWeightYLeft,
  269. fontStyle: optionsSetup.textFontStyleYLeft,
  270. fontFamily: optionsSetup.textFontFamilyYLeft,
  271. },
  272. },
  273. axisLine: {
  274. show: optionsSetup.isShowAxisLineYLeft,
  275. lineStyle: {
  276. width: optionsSetup.lineWidthYLeft,
  277. color: optionsSetup.lineColorYLeft,
  278. },
  279. },
  280. axisTick: {
  281. // 刻度
  282. show: optionsSetup.isShowAxisLineYLeft,
  283. lineStyle: {
  284. width: optionsSetup.lineWidthYLeft,
  285. color: optionsSetup.lineColorYLeft,
  286. },
  287. },
  288. splitLine: {
  289. show: optionsSetup.isShowSplitLineYLeft,
  290. lineStyle: {
  291. color: optionsSetup.splitLineColorYLeft,
  292. width: optionsSetup.splitLineFontWidthYLeft,
  293. },
  294. },
  295. },
  296. {
  297. max: optionsSetup.maxYRight !== "" ? optionsSetup.maxYRight : null,
  298. type: "value",
  299. scale: optionsSetup.scaleYRight,
  300. // 均分
  301. splitNumber: optionsSetup.splitNumberYRight,
  302. // 坐标轴是否显示
  303. show: optionsSetup.isShowYRight,
  304. position: optionsSetup.positionYRight,
  305. offset: optionsSetup.offsetYRight,
  306. // 坐标轴名称
  307. name: optionsSetup.textNameYRight,
  308. nameLocation: optionsSetup.nameLocationYRight,
  309. // 别名
  310. nameTextStyle: {
  311. color: optionsSetup.nameColorYRight,
  312. fontSize: optionsSetup.nameFontSizeYRight,
  313. fontWeight: optionsSetup.nameFontWeightYRight,
  314. fontStyle: optionsSetup.nameFontStyleYRight,
  315. fontFamily: optionsSetup.nameFontFamilyYRight,
  316. },
  317. axisLabel: {
  318. show: optionsSetup.isShowAxisLabelYRight,
  319. // 文字角度
  320. rotate: optionsSetup.textAngleYRight,
  321. textStyle: {
  322. // 坐标文字颜色
  323. color: optionsSetup.textColorYRight,
  324. fontSize: optionsSetup.textFontSizeYRight,
  325. fontWeight: optionsSetup.textFontWeightYRight,
  326. fontStyle: optionsSetup.textFontStyleYRight,
  327. fontFamily: optionsSetup.textFontFamilyYRight,
  328. },
  329. },
  330. axisLine: {
  331. show: optionsSetup.isShowAxisLineYRight,
  332. lineStyle: {
  333. width: optionsSetup.lineWidthYRight,
  334. color: optionsSetup.lineColorYRight,
  335. },
  336. },
  337. axisTick: {
  338. // 刻度
  339. show: optionsSetup.isShowAxisLineYRight,
  340. lineStyle: {
  341. width: optionsSetup.lineWidthYRight,
  342. color: optionsSetup.lineColorYRight,
  343. }
  344. },
  345. splitLine: {
  346. show: optionsSetup.isShowSplitLineYRight,
  347. lineStyle: {
  348. color: optionsSetup.splitLineColorYRight,
  349. width: optionsSetup.splitLineFontWidthYRight,
  350. },
  351. },
  352. },
  353. ];
  354. this.options.yAxis = yAxis;
  355. },
  356. // 柱体设置 数值设置
  357. setOptionsBar() {
  358. const optionsSetup = this.optionsSetup;
  359. const series = this.options.series;
  360. for (const key in series) {
  361. if (series[key].type == "bar") {
  362. series[key].barGap = optionsSetup.barGap + "%";
  363. series[key].label = {
  364. show: optionsSetup.isShow,
  365. position: optionsSetup.fontPosition,
  366. distance: optionsSetup.fontDistance,
  367. fontSize: optionsSetup.fontSize,
  368. color: optionsSetup.fontColor,
  369. fontWeight: optionsSetup.fontWeight,
  370. formatter: !!optionsSetup.percentSign ? '{c}%' : '{c}',
  371. fontStyle: optionsSetup.fontStyle,
  372. fontFamily: optionsSetup.fontFamily,
  373. };
  374. series[key].barWidth = optionsSetup.maxWidth;
  375. series[key].barMinHeight = optionsSetup.minHeight;
  376. series[key].itemStyle.barBorderRadius = optionsSetup.radius;
  377. //柱体背景属性
  378. series[key].showBackground = optionsSetup.isShowBackground;
  379. series[key].backgroundStyle = {
  380. color: optionsSetup.backgroundStyleColor,
  381. borderColor: optionsSetup.backgroundStyleBorderColor,
  382. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  383. borderType: optionsSetup.backgroundStyleBorderType,
  384. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  385. shadowColor: optionsSetup.backgroundStyleShadowColor,
  386. opacity: optionsSetup.backgroundStyleOpacity / 100,
  387. };
  388. }
  389. }
  390. this.options.series = series;
  391. },
  392. // tooltip 设置
  393. setOptionsTooltip() {
  394. const optionsSetup = this.optionsSetup;
  395. const tooltip = {
  396. show: optionsSetup.isShowTooltip,
  397. trigger: optionsSetup.tooltipTrigger,
  398. axisPointer: {
  399. type: optionsSetup.tooltipAxisPointerType,
  400. },
  401. textStyle: {
  402. color: optionsSetup.tooltipColor,
  403. fontSize: optionsSetup.tooltipFontSize,
  404. fontWeight: optionsSetup.tooltipFontWeight,
  405. fontStyle: optionsSetup.tooltipFontStyle,
  406. fontFamily: optionsSetup.tooltipFontFamily,
  407. },
  408. };
  409. this.options.tooltip = tooltip;
  410. },
  411. // 边距设置
  412. setOptionsMargin() {
  413. const optionsSetup = this.optionsSetup;
  414. const grid = {
  415. left: optionsSetup.marginLeft,
  416. right: optionsSetup.marginRight,
  417. bottom: optionsSetup.marginBottom,
  418. top: optionsSetup.marginTop,
  419. containLabel: true,
  420. };
  421. this.options.grid = grid;
  422. },
  423. setOptionsLegend() {
  424. const optionsSetup = this.optionsSetup;
  425. const legend = {
  426. show: optionsSetup.isShowLegend,
  427. left: optionsSetup.lateralPosition,
  428. //right: optionsSetup.lateralPosition,
  429. top: optionsSetup.longitudinalPosition,
  430. //bottom: optionsSetup.longitudinalPosition,
  431. orient: optionsSetup.layoutFront,
  432. textStyle: {
  433. color: optionsSetup.legendColor,
  434. fontSize: optionsSetup.legendFontSize,
  435. fontWeight: optionsSetup.legendFontWeight,
  436. fontStyle: optionsSetup.legendFontStyle,
  437. fontFamily: optionsSetup.legendFontFamily,
  438. },
  439. itemHeight: optionsSetup.legendHeight,
  440. itemWidth: optionsSetup.legendWidth,
  441. };
  442. this.options.legend = legend;
  443. },
  444. // 图例名称设置
  445. setOptionsLegendName(name) {
  446. const optionsSetup = this.optionsSetup;
  447. const series = this.options.series;
  448. const legendName = optionsSetup.legendName;
  449. // 图例没有手动写则显示原值,写了则显示新值
  450. if (null == legendName || legendName == "") {
  451. for (let i = 0; i < name.length; i++) {
  452. series[i].name = name[i];
  453. }
  454. this.options.legend["data"] = name;
  455. } else {
  456. const arr = legendName.split("|");
  457. for (let i = 0; i < arr.length; i++) {
  458. series[i].name = arr[i];
  459. }
  460. this.options.legend["data"] = arr;
  461. }
  462. },
  463. // 图例颜色修改
  464. setOptionsColor() {
  465. const optionsSetup = this.optionsSetup;
  466. const customColor = optionsSetup.customColor;
  467. if (!customColor) return;
  468. const arrColor = [];
  469. for (let i = 0; i < customColor.length; i++) {
  470. arrColor.push(customColor[i].color);
  471. }
  472. this.options.color = arrColor;
  473. this.options = Object.assign({}, this.options);
  474. },
  475. // 数据处理
  476. setOptionsData(e, paramsConfig) {
  477. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  478. // 联动接收者逻辑开始
  479. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  480. const myDynamicData = optionsData.dynamicData;
  481. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  482. if (
  483. e &&
  484. optionsData.dataType !== "staticData" &&
  485. Object.keys(myDynamicData.contextData).length
  486. ) {
  487. const keyArr = Object.keys(myDynamicData.contextData);
  488. paramsConfig.forEach((conf) => {
  489. if (keyArr.includes(conf.targetKey)) {
  490. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  491. }
  492. });
  493. }
  494. // 联动接收者逻辑结束
  495. optionsData.dataType == "staticData"
  496. ? this.staticDataFn(optionsData.staticData)
  497. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  498. },
  499. staticDataFn(val) {
  500. const optionsSetup = this.optionsSetup;
  501. const series = this.options.series;
  502. let axis = [];
  503. let bar1 = [];
  504. let bar2 = [];
  505. for (const i in val) {
  506. axis[i] = val[i].axis;
  507. bar1[i] = val[i].bar1;
  508. bar2[i] = val[i].bar2;
  509. }
  510. // x轴
  511. this.options.xAxis.data = axis;
  512. // series
  513. series[0].data = bar1;
  514. series[1].data = bar2;
  515. const legendName = [];
  516. legendName.push("bar1");
  517. legendName.push("bar2");
  518. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  519. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / axis.length) / optionsSetup.textFontSizeX);
  520. const axisLabel = {
  521. show: optionsSetup.isShowAxisLabelX,
  522. interval: optionsSetup.textIntervalX,
  523. // 文字角度
  524. rotate: optionsSetup.textAngleX,
  525. textStyle: {
  526. // 坐标文字颜色
  527. color: optionsSetup.textColorX,
  528. fontSize: optionsSetup.textFontSizeX,
  529. fontWeight: optionsSetup.textFontWeightX,
  530. fontStyle: optionsSetup.textFontStyleX,
  531. fontFamily: optionsSetup.textFontFamilyX,
  532. },
  533. // 自动换行
  534. formatter: function (value, index) {
  535. const strs = value.split('');
  536. let str = ''
  537. for (let i = 0, s; s = strs[i++];) {
  538. str += s;
  539. if (!(i % rowsNum)) str += '\n';
  540. }
  541. return str
  542. }
  543. }
  544. if (optionsSetup.textRowsBreakAuto) {
  545. this.options.xAxis.axisLabel = axisLabel;
  546. }
  547. this.options.legend["data"] = legendName;
  548. this.setOptionsLegendName(legendName);
  549. },
  550. dynamicDataFn(val, refreshTime) {
  551. if (!val) return;
  552. if (this.ispreview) {
  553. this.getEchartData(val);
  554. this.flagInter = setInterval(() => {
  555. this.getEchartData(val);
  556. }, refreshTime);
  557. } else {
  558. this.getEchartData(val);
  559. }
  560. },
  561. getEchartData(val) {
  562. const data = this.queryEchartsData(val);
  563. data.then((res) => {
  564. this.renderingFn(res);
  565. });
  566. },
  567. renderingFn(val) {
  568. const optionsSetup = this.optionsSetup;
  569. this.options.xAxis.data = val.xAxis;
  570. // series
  571. const series = this.options.series;
  572. const legendName = [];
  573. let k = 0;
  574. for (const i in val.series) {
  575. if (val.series[i].type == "bar") {
  576. series[k]['data'] = val.series[i].data;
  577. k++
  578. legendName.push(val.series[i].name);
  579. }
  580. }
  581. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  582. const xAxisDataLength = val.length !== 0 ? val.xAxis.length : 1;
  583. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisDataLength) / optionsSetup.textFontSizeX);
  584. const axisLabel = {
  585. show: optionsSetup.isShowAxisLabelX,
  586. interval: optionsSetup.textIntervalX,
  587. // 文字角度
  588. rotate: optionsSetup.textAngleX,
  589. textStyle: {
  590. // 坐标文字颜色
  591. color: optionsSetup.textColorX,
  592. fontSize: optionsSetup.textFontSizeX,
  593. fontWeight: optionsSetup.textFontWeightX,
  594. fontStyle: optionsSetup.textFontStyleX,
  595. fontFamily: optionsSetup.textFontFamilyX,
  596. },
  597. // 自动换行
  598. formatter: function (value, index) {
  599. const strs = value.split('');
  600. let str = ''
  601. for (let i = 0, s; s = strs[i++];) {
  602. str += s;
  603. if (!(i % rowsNum)) str += '\n';
  604. }
  605. return str
  606. }
  607. }
  608. if (optionsSetup.textRowsBreakAuto) {
  609. this.options.xAxis.axisLabel = axisLabel;
  610. }
  611. this.options.legend["data"] = legendName;
  612. this.setOptionsLegendName(legendName);
  613. },
  614. },
  615. };
  616. </script>
  617. <style scoped lang="scss">
  618. .echarts {
  619. width: 100%;
  620. height: 100%;
  621. overflow: hidden;
  622. }
  623. </style>