widgetBarStackChart.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart ref="myVChart" :options="options" autoresize/>
  4. </div>
  5. </template>
  6. <script>
  7. import {targetWidgetLinkageLogic} from "@/views/bigscreenDesigner/designer/linkageLogic";
  8. export default {
  9. name: "WidgetBarStackchart",
  10. components: {},
  11. props: {
  12. value: Object,
  13. ispreview: Boolean,
  14. },
  15. data() {
  16. return {
  17. options: {
  18. grid: {},
  19. legend: {
  20. textStyle: {
  21. color: "#fff",
  22. },
  23. },
  24. xAxis: {
  25. type: "category",
  26. data: [],
  27. axisLabel: {
  28. show: true,
  29. textStyle: {
  30. color: "#fff",
  31. },
  32. },
  33. },
  34. yAxis: {
  35. type: "value",
  36. data: [],
  37. axisLabel: {
  38. show: true,
  39. textStyle: {
  40. color: "#fff",
  41. },
  42. },
  43. },
  44. series: [
  45. {
  46. data: [],
  47. name: "",
  48. type: "bar",
  49. barGap: "0%",
  50. itemStyle: {
  51. barBorderRadius: null,
  52. },
  53. },
  54. ],
  55. },
  56. optionsStyle: {}, // 样式
  57. optionsData: {}, // 数据
  58. optionsSetup: {},
  59. flagInter: null,
  60. };
  61. },
  62. computed: {
  63. styleObj() {
  64. return {
  65. position: this.ispreview ? "absolute" : "static",
  66. width: this.optionsStyle.width + "px",
  67. height: this.optionsStyle.height + "px",
  68. left: this.optionsStyle.left + "px",
  69. top: this.optionsStyle.top + "px",
  70. background: this.optionsSetup.background,
  71. };
  72. },
  73. allComponentLinkage() {
  74. return this.$store.state.designer.allComponentLinkage;
  75. },
  76. },
  77. watch: {
  78. value: {
  79. handler(val) {
  80. this.optionsStyle = val.position;
  81. this.optionsData = val.data;
  82. this.optionsCollapse = val.setup;
  83. this.optionsSetup = val.setup;
  84. this.editorOptions();
  85. },
  86. deep: true,
  87. },
  88. },
  89. mounted() {
  90. this.optionsStyle = this.value.position;
  91. this.optionsData = this.value.data;
  92. this.optionsCollapse = this.value.setup;
  93. this.optionsSetup = this.value.setup;
  94. this.editorOptions();
  95. targetWidgetLinkageLogic(this); // 联动-目标组件逻辑
  96. },
  97. methods: {
  98. // 修改图标options属性
  99. editorOptions() {
  100. this.setOptionsTitle();
  101. this.setOptionsX();
  102. this.setOptionsY();
  103. this.setOptionsTooltip();
  104. this.setOptionsMargin();
  105. this.setOptionsLegend();
  106. this.setOptionsData();
  107. },
  108. // 标题修改
  109. setOptionsTitle() {
  110. const optionsSetup = this.optionsSetup;
  111. const title = {};
  112. title.text = optionsSetup.titleText;
  113. title.show = optionsSetup.isNoTitle;
  114. title.left = optionsSetup.textAlign;
  115. title.textStyle = {
  116. color: optionsSetup.textColor,
  117. fontSize: optionsSetup.textFontSize,
  118. fontWeight: optionsSetup.textFontWeight,
  119. fontStyle: optionsSetup.textFontStyle,
  120. };
  121. title.subtext = optionsSetup.subText;
  122. title.subtextStyle = {
  123. color: optionsSetup.subTextColor,
  124. fontWeight: optionsSetup.subTextFontWeight,
  125. fontSize: optionsSetup.subTextFontSize,
  126. fontStyle: optionsSetup.subTextFontStyle,
  127. };
  128. this.options.title = title;
  129. },
  130. // X轴设置
  131. setOptionsX() {
  132. const optionsSetup = this.optionsSetup;
  133. const xAxis = {
  134. type: "category",
  135. // 坐标轴是否显示
  136. show: optionsSetup.hideX,
  137. // 坐标轴名称
  138. name: optionsSetup.nameX,
  139. nameTextStyle: {
  140. color: optionsSetup.nameColorX,
  141. fontSize: optionsSetup.nameFontSizeX,
  142. },
  143. // 轴反转
  144. inverse: optionsSetup.reversalX,
  145. axisLabel: {
  146. show: true,
  147. interval: optionsSetup.textInterval,
  148. // 文字角度
  149. rotate: optionsSetup.textAngleX,
  150. textStyle: {
  151. // 坐标文字颜色
  152. color: optionsSetup.colorX,
  153. fontSize: optionsSetup.fontSizeX,
  154. },
  155. },
  156. axisLine: {
  157. show: true,
  158. lineStyle: {
  159. color: optionsSetup.lineColorX,
  160. width: optionsSetup.lineWidthX,
  161. },
  162. },
  163. splitLine: {
  164. show: optionsSetup.isShowSplitLineX,
  165. lineStyle: {
  166. color: optionsSetup.splitLineColorX,
  167. width: optionsSetup.splitLineWidthX,
  168. },
  169. },
  170. };
  171. this.options.xAxis = xAxis;
  172. },
  173. // Y轴设置
  174. setOptionsY() {
  175. const optionsSetup = this.optionsSetup;
  176. const yAxis = {
  177. max: optionsSetup.maxY !== "" ? optionsSetup.maxY : null,
  178. type: "value",
  179. scale: optionsSetup.scale,
  180. // 均分
  181. splitNumber: optionsSetup.splitNumberY,
  182. // 坐标轴是否显示
  183. show: optionsSetup.isShowY,
  184. // 坐标轴名称
  185. name: optionsSetup.textNameY,
  186. nameTextStyle: {
  187. color: optionsSetup.nameColorY,
  188. fontSize: optionsSetup.nameFontSizeY,
  189. },
  190. // 轴反转
  191. inverse: optionsSetup.reversalY,
  192. axisLabel: {
  193. show: true,
  194. // 文字角度
  195. rotate: optionsSetup.textAngleY,
  196. textStyle: {
  197. // 坐标文字颜色
  198. color: optionsSetup.colorY,
  199. fontSize: optionsSetup.fontSizeY,
  200. },
  201. },
  202. axisLine: {
  203. show: true,
  204. lineStyle: {
  205. color: optionsSetup.lineColorY,
  206. width: optionsSetup.lineWidthY,
  207. },
  208. },
  209. splitLine: {
  210. show: optionsSetup.isShowSplitLineY,
  211. lineStyle: {
  212. color: optionsSetup.splitLineColorY,
  213. width: optionsSetup.splitLineWidthY,
  214. },
  215. },
  216. };
  217. this.options.yAxis = yAxis;
  218. },
  219. // tooltip 提示语设置,鼠标放置显示
  220. setOptionsTooltip() {
  221. const optionsSetup = this.optionsSetup;
  222. const tooltip = {
  223. trigger: "item",
  224. show: true,
  225. textStyle: {
  226. color: optionsSetup.tipsColor,
  227. fontSize: optionsSetup.tipsFontSize,
  228. },
  229. };
  230. this.options.tooltip = tooltip;
  231. },
  232. // 边距设置
  233. setOptionsMargin() {
  234. const optionsSetup = this.optionsSetup;
  235. const grid = {
  236. left: optionsSetup.marginLeft,
  237. right: optionsSetup.marginRight,
  238. bottom: optionsSetup.marginBottom,
  239. top: optionsSetup.marginTop,
  240. containLabel: true,
  241. };
  242. this.options.grid = grid;
  243. },
  244. // 图例操作 legend
  245. setOptionsLegend() {
  246. const optionsSetup = this.optionsSetup;
  247. const legend = this.options.legend;
  248. legend.show = optionsSetup.isShowLegend;
  249. legend.left = optionsSetup.lateralPosition;
  250. legend.top = optionsSetup.longitudinalPosition;
  251. legend.bottom = optionsSetup.longitudinalPosition;
  252. legend.orient = optionsSetup.layoutFront;
  253. legend.textStyle = {
  254. color: optionsSetup.legendColor,
  255. fontSize: optionsSetup.legendFontSize,
  256. };
  257. legend.itemWidth = optionsSetup.legendWidth;
  258. },
  259. // 图例名称设置
  260. setOptionsLegendName(name) {
  261. const optionsSetup = this.optionsSetup;
  262. const series = this.options.series;
  263. const legendName = optionsSetup.legendName;
  264. // 图例没有手动写则显示原值,写了则显示新值
  265. if (null == legendName || legendName == "") {
  266. for (let i = 0; i < name.length; i++) {
  267. series[i].name = name[i];
  268. }
  269. this.options.legend["data"] = name;
  270. } else {
  271. const arr = legendName.split("|");
  272. for (let i = 0; i < arr.length; i++) {
  273. series[i].name = arr[i];
  274. }
  275. this.options.legend["data"] = arr;
  276. }
  277. },
  278. // 数据解析
  279. setOptionsData(e, paramsConfig) {
  280. const optionsSetup = this.optionsSetup;
  281. // 数据类型 静态 or 动态
  282. const optionsData = this.optionsData;
  283. // 联动接收者逻辑开始
  284. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  285. const myDynamicData = optionsData.dynamicData;
  286. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  287. if (
  288. e &&
  289. optionsData.dataType !== "staticData" &&
  290. Object.keys(myDynamicData.contextData).length
  291. ) {
  292. const keyArr = Object.keys(myDynamicData.contextData);
  293. paramsConfig.forEach((conf) => {
  294. if (keyArr.includes(conf.targetKey)) {
  295. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  296. }
  297. });
  298. }
  299. // 联动接收者逻辑结束
  300. optionsData.dataType == "staticData"
  301. ? this.staticDataFn(optionsData.staticData, optionsSetup)
  302. : this.dynamicDataFn(
  303. optionsData.dynamicData,
  304. optionsData.refreshTime,
  305. optionsSetup
  306. );
  307. },
  308. //去重
  309. setUnique(arr) {
  310. let newArr = [];
  311. arr.forEach((item) => {
  312. return newArr.includes(item) ? "" : newArr.push(item);
  313. });
  314. return newArr;
  315. },
  316. //获取堆叠样式
  317. getStackStyle() {
  318. const optionsSetup = this.optionsSetup;
  319. let style = "";
  320. if (optionsSetup.stackStyle == "upDown") {
  321. style = "total";
  322. }
  323. return style;
  324. },
  325. //静态数据
  326. staticDataFn(val) {
  327. const optionsSetup = this.optionsSetup;
  328. //颜色
  329. const customColor = optionsSetup.customColor;
  330. const arrColor = [];
  331. for (let i = 0; i < customColor.length; i++) {
  332. arrColor.push(customColor[i].color);
  333. }
  334. //数据
  335. const series = [];
  336. let xAxisList = [];
  337. let yAxisList = [];
  338. const legendName = [];
  339. for (const i in val) {
  340. xAxisList[i] = val[i].axis;
  341. yAxisList[i] = val[i].name;
  342. }
  343. xAxisList = this.setUnique(xAxisList);
  344. yAxisList = this.setUnique(yAxisList);
  345. for (const i in yAxisList) {
  346. const data = new Array(xAxisList.length).fill(0);
  347. for (const j in xAxisList) {
  348. for (const k in val) {
  349. if (val[k].name == yAxisList[i]) {
  350. if (val[k].axis == xAxisList[j]) {
  351. data[j] = val[k].data;
  352. }
  353. }
  354. }
  355. }
  356. series.push({
  357. name: yAxisList[i],
  358. type: "bar",
  359. data: data,
  360. barGap: "0%",
  361. stack: this.getStackStyle(),
  362. barWidth: optionsSetup.maxWidth,
  363. label: {
  364. show: optionsSetup.isShow,
  365. position: "top",
  366. distance: 10,
  367. fontSize: optionsSetup.fontSize,
  368. color: optionsSetup.dataColor,
  369. fontWeight: optionsSetup.fontWeight,
  370. formatter: !!optionsSetup.percentSign ? '{c}%' : '{c}'
  371. },
  372. //颜色,圆角属性
  373. itemStyle: {
  374. normal: {
  375. color: arrColor[i],
  376. barBorderRadius: optionsSetup.radius,
  377. },
  378. },
  379. //柱体背景属性
  380. showBackground: optionsSetup.isShowBackground,
  381. backgroundStyle: {
  382. color: optionsSetup.backgroundStyleColor,
  383. borderColor: optionsSetup.backgroundStyleBorderColor,
  384. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  385. borderType: optionsSetup.backgroundStyleBorderType,
  386. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  387. shadowColor: optionsSetup.backgroundStyleShadowColor,
  388. opacity: optionsSetup.backgroundStyleOpacity / 100,
  389. }
  390. });
  391. legendName.push(yAxisList[i]);
  392. }
  393. this.options.series = series;
  394. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  395. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisList.length) / optionsSetup.fontSizeX);
  396. const axisLabel = {
  397. show: true,
  398. interval: optionsSetup.textInterval,
  399. // 文字角度
  400. rotate: optionsSetup.textAngleX,
  401. textStyle: {
  402. // 坐标文字颜色
  403. color: optionsSetup.colorX,
  404. fontSize: optionsSetup.fontSizeX,
  405. },
  406. // 自动换行
  407. formatter: function (value, index) {
  408. const strs = value.split('');
  409. let str = ''
  410. for (let i = 0, s; s = strs[i++];) {
  411. str += s;
  412. if (!(i % rowsNum)) str += '\n';
  413. }
  414. return str
  415. }
  416. }
  417. if (optionsSetup.verticalShow) {
  418. this.options.xAxis.data = [];
  419. this.options.yAxis.data = xAxisList;
  420. this.options.xAxis.type = "value";
  421. this.options.yAxis.type = "category";
  422. } else {
  423. this.options.xAxis.data = xAxisList;
  424. this.options.yAxis.data = [];
  425. this.options.xAxis.type = "category";
  426. this.options.yAxis.type = "value";
  427. if (optionsSetup.textRowsBreakAuto) {
  428. this.options.xAxis.axisLabel = axisLabel;
  429. }
  430. }
  431. this.options.legend["data"] = legendName;
  432. this.setOptionsLegendName(legendName);
  433. },
  434. // 动态数据
  435. dynamicDataFn(val, refreshTime, optionsSetup) {
  436. if (!val) return;
  437. if (this.ispreview) {
  438. this.getEchartData(val, optionsSetup);
  439. this.flagInter = setInterval(() => {
  440. this.getEchartData(val, optionsSetup);
  441. }, refreshTime);
  442. } else {
  443. this.getEchartData(val, optionsSetup);
  444. }
  445. },
  446. getEchartData(val, optionsSetup) {
  447. const data = this.queryEchartsData(val);
  448. data.then((res) => {
  449. this.renderingFn(optionsSetup, res);
  450. });
  451. },
  452. renderingFn(optionsSetup, val) {
  453. //颜色
  454. const customColor = optionsSetup.customColor;
  455. const arrColor = [];
  456. for (let i = 0; i < customColor.length; i++) {
  457. arrColor.push(customColor[i].color);
  458. }
  459. const series = [];
  460. const legendName = [];
  461. for (const i in val.series) {
  462. if (val.series[i].type == "bar") {
  463. series.push({
  464. name: val.series[i].name,
  465. type: "bar",
  466. data: val.series[i].data,
  467. barGap: "0%",
  468. stack: this.getStackStyle(),
  469. barWidth: optionsSetup.maxWidth,
  470. label: {
  471. show: optionsSetup.isShow,
  472. position: "top",
  473. distance: 10,
  474. fontSize: optionsSetup.fontSize,
  475. color: optionsSetup.dataColor,
  476. fontWeight: optionsSetup.fontWeight,
  477. formatter: !!optionsSetup.percentSign ? '{c}%' : '{c}'
  478. },
  479. //颜色,圆角属性
  480. itemStyle: {
  481. normal: {
  482. color: arrColor[i],
  483. barBorderRadius: optionsSetup.radius,
  484. },
  485. },
  486. //柱体背景属性
  487. showBackground: optionsSetup.isShowBackground,
  488. backgroundStyle: {
  489. color: optionsSetup.backgroundStyleColor,
  490. borderColor: optionsSetup.backgroundStyleBorderColor,
  491. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  492. borderType: optionsSetup.backgroundStyleBorderType,
  493. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  494. shadowColor: optionsSetup.backgroundStyleShadowColor,
  495. opacity: optionsSetup.backgroundStyleOpacity / 100,
  496. }
  497. });
  498. }
  499. legendName.push(val.series[i].name);
  500. }
  501. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  502. const xAxisDataLength = val.length !== 0 ? val.xAxis.length : 1;
  503. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisDataLength) / optionsSetup.fontSizeX);
  504. const axisLabel = {
  505. show: true,
  506. interval: optionsSetup.textInterval,
  507. // 文字角度
  508. rotate: optionsSetup.textAngleX,
  509. textStyle: {
  510. // 坐标文字颜色
  511. color: optionsSetup.colorX,
  512. fontSize: optionsSetup.fontSizeX,
  513. },
  514. // 自动换行
  515. formatter: function (value, index) {
  516. const strs = value.split('');
  517. let str = ''
  518. for (let i = 0, s; s = strs[i++];) {
  519. str += s;
  520. if (!(i % rowsNum)) str += '\n';
  521. }
  522. return str
  523. }
  524. }
  525. // x轴
  526. if (optionsSetup.verticalShow) {
  527. this.options.xAxis.data = [];
  528. this.options.yAxis.data = val.xAxis;
  529. this.options.xAxis.type = "value";
  530. this.options.yAxis.type = "category";
  531. } else {
  532. this.options.xAxis.data = val.xAxis;
  533. this.options.yAxis.data = [];
  534. this.options.xAxis.type = "category";
  535. this.options.yAxis.type = "value";
  536. if (optionsSetup.textRowsBreakAuto) {
  537. this.options.xAxis.axisLabel = axisLabel;
  538. }
  539. }
  540. this.options.series = series;
  541. this.options.legend["data"] = legendName;
  542. this.setOptionsLegendName(legendName);
  543. },
  544. },
  545. };
  546. </script>
  547. <style scoped lang="scss">
  548. .echarts {
  549. width: 100%;
  550. height: 100%;
  551. overflow: hidden;
  552. }
  553. </style>