widgetBarDoubleYaxisChart.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. backgroundColor: optionsSetup.tooltipBackgroundColor,
  402. borderColor: optionsSetup.tooltipBorderColor,
  403. borderWidth: optionsSetup.tooltipBorderWidth,
  404. textStyle: {
  405. color: optionsSetup.tooltipColor,
  406. fontSize: optionsSetup.tooltipFontSize,
  407. fontWeight: optionsSetup.tooltipFontWeight,
  408. fontStyle: optionsSetup.tooltipFontStyle,
  409. fontFamily: optionsSetup.tooltipFontFamily,
  410. },
  411. };
  412. this.options.tooltip = tooltip;
  413. },
  414. // 边距设置
  415. setOptionsMargin() {
  416. const optionsSetup = this.optionsSetup;
  417. const grid = {
  418. left: optionsSetup.marginLeft,
  419. right: optionsSetup.marginRight,
  420. bottom: optionsSetup.marginBottom,
  421. top: optionsSetup.marginTop,
  422. containLabel: true,
  423. };
  424. this.options.grid = grid;
  425. },
  426. setOptionsLegend() {
  427. const optionsSetup = this.optionsSetup;
  428. const legend = {
  429. show: optionsSetup.isShowLegend,
  430. left: optionsSetup.lateralPosition,
  431. //right: optionsSetup.lateralPosition,
  432. top: optionsSetup.longitudinalPosition,
  433. //bottom: optionsSetup.longitudinalPosition,
  434. orient: optionsSetup.layoutFront,
  435. textStyle: {
  436. color: optionsSetup.legendColor,
  437. fontSize: optionsSetup.legendFontSize,
  438. fontWeight: optionsSetup.legendFontWeight,
  439. fontStyle: optionsSetup.legendFontStyle,
  440. fontFamily: optionsSetup.legendFontFamily,
  441. },
  442. itemHeight: optionsSetup.legendHeight,
  443. itemWidth: optionsSetup.legendWidth,
  444. };
  445. this.options.legend = legend;
  446. },
  447. // 图例名称设置
  448. setOptionsLegendName(name) {
  449. const optionsSetup = this.optionsSetup;
  450. const series = this.options.series;
  451. const legendName = optionsSetup.legendName;
  452. // 图例没有手动写则显示原值,写了则显示新值
  453. if (null == legendName || legendName == "") {
  454. for (let i = 0; i < name.length; i++) {
  455. series[i].name = name[i];
  456. }
  457. this.options.legend["data"] = name;
  458. } else {
  459. const arr = legendName.split("|");
  460. for (let i = 0; i < arr.length; i++) {
  461. series[i].name = arr[i];
  462. }
  463. this.options.legend["data"] = arr;
  464. }
  465. },
  466. // 图例颜色修改
  467. setOptionsColor() {
  468. const optionsSetup = this.optionsSetup;
  469. const customColor = optionsSetup.customColor;
  470. if (!customColor) return;
  471. const arrColor = [];
  472. for (let i = 0; i < customColor.length; i++) {
  473. arrColor.push(customColor[i].color);
  474. }
  475. this.options.color = arrColor;
  476. this.options = Object.assign({}, this.options);
  477. },
  478. // 数据处理
  479. setOptionsData(e, paramsConfig) {
  480. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  481. // 联动接收者逻辑开始
  482. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  483. const myDynamicData = optionsData.dynamicData;
  484. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  485. if (
  486. e &&
  487. optionsData.dataType !== "staticData" &&
  488. Object.keys(myDynamicData.contextData).length
  489. ) {
  490. const keyArr = Object.keys(myDynamicData.contextData);
  491. paramsConfig.forEach((conf) => {
  492. if (keyArr.includes(conf.targetKey)) {
  493. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  494. }
  495. });
  496. }
  497. // 联动接收者逻辑结束
  498. optionsData.dataType == "staticData"
  499. ? this.staticDataFn(optionsData.staticData)
  500. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  501. },
  502. staticDataFn(val) {
  503. const optionsSetup = this.optionsSetup;
  504. const series = this.options.series;
  505. let axis = [];
  506. let bar1 = [];
  507. let bar2 = [];
  508. for (const i in val) {
  509. axis[i] = val[i].axis;
  510. bar1[i] = val[i].bar1;
  511. bar2[i] = val[i].bar2;
  512. }
  513. // x轴
  514. this.options.xAxis.data = axis;
  515. // series
  516. series[0].data = bar1;
  517. series[1].data = bar2;
  518. const legendName = [];
  519. legendName.push("bar1");
  520. legendName.push("bar2");
  521. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  522. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / axis.length) / optionsSetup.textFontSizeX);
  523. const axisLabel = {
  524. show: optionsSetup.isShowAxisLabelX,
  525. interval: optionsSetup.textIntervalX,
  526. // 文字角度
  527. rotate: optionsSetup.textAngleX,
  528. textStyle: {
  529. // 坐标文字颜色
  530. color: optionsSetup.textColorX,
  531. fontSize: optionsSetup.textFontSizeX,
  532. fontWeight: optionsSetup.textFontWeightX,
  533. fontStyle: optionsSetup.textFontStyleX,
  534. fontFamily: optionsSetup.textFontFamilyX,
  535. },
  536. // 自动换行
  537. formatter: function (value, index) {
  538. const strs = value.split('');
  539. let str = ''
  540. for (let i = 0, s; s = strs[i++];) {
  541. str += s;
  542. if (!(i % rowsNum)) str += '\n';
  543. }
  544. return str
  545. }
  546. }
  547. if (optionsSetup.textRowsBreakAuto) {
  548. this.options.xAxis.axisLabel = axisLabel;
  549. }
  550. this.options.legend["data"] = legendName;
  551. this.setOptionsLegendName(legendName);
  552. },
  553. dynamicDataFn(val, refreshTime) {
  554. if (!val) return;
  555. if (this.ispreview) {
  556. this.getEchartData(val);
  557. this.flagInter = setInterval(() => {
  558. this.getEchartData(val);
  559. }, refreshTime);
  560. } else {
  561. this.getEchartData(val);
  562. }
  563. },
  564. getEchartData(val) {
  565. const data = this.queryEchartsData(val);
  566. data.then((res) => {
  567. this.renderingFn(res);
  568. });
  569. },
  570. renderingFn(val) {
  571. const optionsSetup = this.optionsSetup;
  572. this.options.xAxis.data = val.xAxis;
  573. // series
  574. const series = this.options.series;
  575. const legendName = [];
  576. let k = 0;
  577. for (const i in val.series) {
  578. if (val.series[i].type == "bar") {
  579. series[k]['data'] = val.series[i].data;
  580. k++
  581. legendName.push(val.series[i].name);
  582. }
  583. }
  584. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  585. const xAxisDataLength = val.length !== 0 ? val.xAxis.length : 1;
  586. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisDataLength) / optionsSetup.textFontSizeX);
  587. const axisLabel = {
  588. show: optionsSetup.isShowAxisLabelX,
  589. interval: optionsSetup.textIntervalX,
  590. // 文字角度
  591. rotate: optionsSetup.textAngleX,
  592. textStyle: {
  593. // 坐标文字颜色
  594. color: optionsSetup.textColorX,
  595. fontSize: optionsSetup.textFontSizeX,
  596. fontWeight: optionsSetup.textFontWeightX,
  597. fontStyle: optionsSetup.textFontStyleX,
  598. fontFamily: optionsSetup.textFontFamilyX,
  599. },
  600. // 自动换行
  601. formatter: function (value, index) {
  602. const strs = value.split('');
  603. let str = ''
  604. for (let i = 0, s; s = strs[i++];) {
  605. str += s;
  606. if (!(i % rowsNum)) str += '\n';
  607. }
  608. return str
  609. }
  610. }
  611. if (optionsSetup.textRowsBreakAuto) {
  612. this.options.xAxis.axisLabel = axisLabel;
  613. }
  614. this.options.legend["data"] = legendName;
  615. this.setOptionsLegendName(legendName);
  616. },
  617. },
  618. };
  619. </script>
  620. <style scoped lang="scss">
  621. .echarts {
  622. width: 100%;
  623. height: 100%;
  624. overflow: hidden;
  625. }
  626. </style>