widgetBarStackChart.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. axisLine: {
  146. show: true,
  147. lineStyle: {
  148. color: optionsSetup.lineColorX,
  149. width: optionsSetup.lineWidthX,
  150. },
  151. },
  152. splitLine: {
  153. show: optionsSetup.isShowSplitLineX,
  154. lineStyle: {
  155. color: optionsSetup.splitLineColorX,
  156. width: optionsSetup.splitLineWidthX,
  157. },
  158. },
  159. };
  160. this.options.xAxis = xAxis;
  161. },
  162. // Y轴设置
  163. setOptionsY() {
  164. const optionsSetup = this.optionsSetup;
  165. const yAxis = {
  166. max: optionsSetup.maxY !== "" ? optionsSetup.maxY : null,
  167. type: "value",
  168. scale: optionsSetup.scale,
  169. // 均分
  170. splitNumber: optionsSetup.splitNumberY,
  171. // 坐标轴是否显示
  172. show: optionsSetup.isShowY,
  173. // 坐标轴名称
  174. name: optionsSetup.textNameY,
  175. nameTextStyle: {
  176. color: optionsSetup.nameColorY,
  177. fontSize: optionsSetup.nameFontSizeY,
  178. },
  179. // 轴反转
  180. inverse: optionsSetup.reversalY,
  181. axisLabel: {
  182. show: true,
  183. // 文字角度
  184. rotate: optionsSetup.textAngleY,
  185. textStyle: {
  186. // 坐标文字颜色
  187. color: optionsSetup.colorY,
  188. fontSize: optionsSetup.fontSizeY,
  189. },
  190. },
  191. axisLine: {
  192. show: true,
  193. lineStyle: {
  194. color: optionsSetup.lineColorY,
  195. width: optionsSetup.lineWidthY,
  196. },
  197. },
  198. splitLine: {
  199. show: optionsSetup.isShowSplitLineY,
  200. lineStyle: {
  201. color: optionsSetup.splitLineColorY,
  202. width: optionsSetup.splitLineWidthY,
  203. },
  204. },
  205. };
  206. this.options.yAxis = yAxis;
  207. },
  208. // tooltip 提示语设置,鼠标放置显示
  209. setOptionsTooltip() {
  210. const optionsSetup = this.optionsSetup;
  211. const tooltip = {
  212. trigger: "item",
  213. show: true,
  214. textStyle: {
  215. color: optionsSetup.tipsColor,
  216. fontSize: optionsSetup.tipsFontSize,
  217. },
  218. };
  219. this.options.tooltip = tooltip;
  220. },
  221. // 边距设置
  222. setOptionsMargin() {
  223. const optionsSetup = this.optionsSetup;
  224. const grid = {
  225. left: optionsSetup.marginLeft,
  226. right: optionsSetup.marginRight,
  227. bottom: optionsSetup.marginBottom,
  228. top: optionsSetup.marginTop,
  229. containLabel: true,
  230. };
  231. this.options.grid = grid;
  232. },
  233. // 图例操作 legend
  234. setOptionsLegend() {
  235. const optionsSetup = this.optionsSetup;
  236. const legend = this.options.legend;
  237. legend.show = optionsSetup.isShowLegend;
  238. legend.left = optionsSetup.lateralPosition;
  239. legend.top = optionsSetup.longitudinalPosition;
  240. legend.bottom = optionsSetup.longitudinalPosition;
  241. legend.orient = optionsSetup.layoutFront;
  242. legend.textStyle = {
  243. color: optionsSetup.legendColor,
  244. fontSize: optionsSetup.legendFontSize,
  245. };
  246. legend.itemWidth = optionsSetup.legendWidth;
  247. },
  248. // 图例名称设置
  249. setOptionsLegendName(name) {
  250. const optionsSetup = this.optionsSetup;
  251. const series = this.options.series;
  252. const legendName = optionsSetup.legendName;
  253. // 图例没有手动写则显示原值,写了则显示新值
  254. if (null == legendName || legendName == "") {
  255. for (let i = 0; i < name.length; i++) {
  256. series[i].name = name[i];
  257. }
  258. this.options.legend["data"] = name;
  259. } else {
  260. const arr = legendName.split("|");
  261. for (let i = 0; i < arr.length; i++) {
  262. series[i].name = arr[i];
  263. }
  264. this.options.legend["data"] = arr;
  265. }
  266. },
  267. // 数据解析
  268. setOptionsData(e, paramsConfig) {
  269. const optionsSetup = this.optionsSetup;
  270. // 数据类型 静态 or 动态
  271. const optionsData = this.optionsData;
  272. // 联动接收者逻辑开始
  273. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  274. const myDynamicData = optionsData.dynamicData;
  275. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  276. if (
  277. e &&
  278. optionsData.dataType !== "staticData" &&
  279. Object.keys(myDynamicData.contextData).length
  280. ) {
  281. const keyArr = Object.keys(myDynamicData.contextData);
  282. paramsConfig.forEach((conf) => {
  283. if (keyArr.includes(conf.targetKey)) {
  284. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  285. }
  286. });
  287. }
  288. // 联动接收者逻辑结束
  289. optionsData.dataType == "staticData"
  290. ? this.staticDataFn(optionsData.staticData, optionsSetup)
  291. : this.dynamicDataFn(
  292. optionsData.dynamicData,
  293. optionsData.refreshTime,
  294. optionsSetup
  295. );
  296. },
  297. //去重
  298. setUnique(arr) {
  299. let newArr = [];
  300. arr.forEach((item) => {
  301. return newArr.includes(item) ? "" : newArr.push(item);
  302. });
  303. return newArr;
  304. },
  305. //获取堆叠样式
  306. getStackStyle() {
  307. const optionsSetup = this.optionsSetup;
  308. let style = "";
  309. if (optionsSetup.stackStyle == "upDown") {
  310. style = "total";
  311. }
  312. return style;
  313. },
  314. //静态数据
  315. staticDataFn(val) {
  316. const optionsSetup = this.optionsSetup;
  317. //颜色
  318. const customColor = optionsSetup.customColor;
  319. const arrColor = [];
  320. for (let i = 0; i < customColor.length; i++) {
  321. arrColor.push(customColor[i].color);
  322. }
  323. //数据
  324. const series = [];
  325. let xAxisList = [];
  326. let yAxisList = [];
  327. const legendName = [];
  328. for (const i in val) {
  329. xAxisList[i] = val[i].axis;
  330. yAxisList[i] = val[i].name;
  331. }
  332. xAxisList = this.setUnique(xAxisList);
  333. yAxisList = this.setUnique(yAxisList);
  334. for (const i in yAxisList) {
  335. const data = new Array(xAxisList.length).fill(0);
  336. for (const j in xAxisList) {
  337. for (const k in val) {
  338. if (val[k].name == yAxisList[i]) {
  339. if (val[k].axis == xAxisList[j]) {
  340. data[j] = val[k].data;
  341. }
  342. }
  343. }
  344. }
  345. series.push({
  346. name: yAxisList[i],
  347. type: "bar",
  348. data: data,
  349. barGap: "0%",
  350. stack: this.getStackStyle(),
  351. barWidth: optionsSetup.maxWidth,
  352. label: {
  353. show: optionsSetup.isShow,
  354. position: "top",
  355. distance: 10,
  356. fontSize: optionsSetup.fontSize,
  357. color: optionsSetup.dataColor,
  358. fontWeight: optionsSetup.fontWeight,
  359. formatter: !!optionsSetup.percentSign ? '{c}%' : '{c}'
  360. },
  361. //颜色,圆角属性
  362. itemStyle: {
  363. normal: {
  364. color: arrColor[i],
  365. barBorderRadius: optionsSetup.radius,
  366. },
  367. },
  368. //柱体背景属性
  369. showBackground: optionsSetup.isShowBackground,
  370. backgroundStyle: {
  371. color: optionsSetup.backgroundStyleColor,
  372. borderColor: optionsSetup.backgroundStyleBorderColor,
  373. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  374. borderType: optionsSetup.backgroundStyleBorderType,
  375. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  376. shadowColor: optionsSetup.backgroundStyleShadowColor,
  377. opacity: optionsSetup.backgroundStyleOpacity / 100,
  378. }
  379. });
  380. legendName.push(yAxisList[i]);
  381. }
  382. this.options.series = series;
  383. if (optionsSetup.verticalShow) {
  384. this.options.xAxis.data = [];
  385. this.options.yAxis.data = xAxisList;
  386. this.options.xAxis.type = "value";
  387. this.options.yAxis.type = "category";
  388. } else {
  389. this.options.xAxis.data = xAxisList;
  390. this.options.yAxis.data = [];
  391. this.options.xAxis.type = "category";
  392. this.options.yAxis.type = "value";
  393. }
  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. this.options.xAxis.axisLabel = axisLabel;
  418. this.options.legend["data"] = legendName;
  419. this.setOptionsLegendName(legendName);
  420. },
  421. // 动态数据
  422. dynamicDataFn(val, refreshTime, optionsSetup) {
  423. if (!val) return;
  424. if (this.ispreview) {
  425. this.getEchartData(val, optionsSetup);
  426. this.flagInter = setInterval(() => {
  427. this.getEchartData(val, optionsSetup);
  428. }, refreshTime);
  429. } else {
  430. this.getEchartData(val, optionsSetup);
  431. }
  432. },
  433. getEchartData(val, optionsSetup) {
  434. const data = this.queryEchartsData(val);
  435. data.then((res) => {
  436. this.renderingFn(optionsSetup, res);
  437. });
  438. },
  439. renderingFn(optionsSetup, val) {
  440. //颜色
  441. const customColor = optionsSetup.customColor;
  442. const arrColor = [];
  443. for (let i = 0; i < customColor.length; i++) {
  444. arrColor.push(customColor[i].color);
  445. }
  446. // x轴
  447. if (optionsSetup.verticalShow) {
  448. this.options.xAxis.data = [];
  449. this.options.yAxis.data = val.xAxis;
  450. this.options.xAxis.type = "value";
  451. this.options.yAxis.type = "category";
  452. } else {
  453. this.options.xAxis.data = val.xAxis;
  454. this.options.yAxis.data = [];
  455. this.options.xAxis.type = "category";
  456. this.options.yAxis.type = "value";
  457. }
  458. const series = [];
  459. const legendName = [];
  460. for (const i in val.series) {
  461. if (val.series[i].type == "bar") {
  462. series.push({
  463. name: val.series[i].name,
  464. type: "bar",
  465. data: val.series[i].data,
  466. barGap: "0%",
  467. stack: this.getStackStyle(),
  468. barWidth: optionsSetup.maxWidth,
  469. label: {
  470. show: optionsSetup.isShow,
  471. position: "top",
  472. distance: 10,
  473. fontSize: optionsSetup.fontSize,
  474. color: optionsSetup.dataColor,
  475. fontWeight: optionsSetup.fontWeight,
  476. formatter: !!optionsSetup.percentSign ? '{c}%' : '{c}'
  477. },
  478. //颜色,圆角属性
  479. itemStyle: {
  480. normal: {
  481. color: arrColor[i],
  482. barBorderRadius: optionsSetup.radius,
  483. },
  484. },
  485. //柱体背景属性
  486. showBackground: optionsSetup.isShowBackground,
  487. backgroundStyle: {
  488. color: optionsSetup.backgroundStyleColor,
  489. borderColor: optionsSetup.backgroundStyleBorderColor,
  490. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  491. borderType: optionsSetup.backgroundStyleBorderType,
  492. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  493. shadowColor: optionsSetup.backgroundStyleShadowColor,
  494. opacity: optionsSetup.backgroundStyleOpacity / 100,
  495. }
  496. });
  497. }
  498. legendName.push(val.series[i].name);
  499. }
  500. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  501. let xAxisDataLength = 1;
  502. if (val.length !== 0) {
  503. xAxisDataLength = val.xAxis.length;
  504. }
  505. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisDataLength) / optionsSetup.fontSizeX);
  506. const axisLabel = {
  507. show: true,
  508. interval: optionsSetup.textInterval,
  509. // 文字角度
  510. rotate: optionsSetup.textAngleX,
  511. textStyle: {
  512. // 坐标文字颜色
  513. color: optionsSetup.colorX,
  514. fontSize: optionsSetup.fontSizeX,
  515. },
  516. // 自动换行
  517. formatter: function (value, index) {
  518. const strs = value.split('');
  519. let str = ''
  520. for (let i = 0, s; s = strs[i++];) {
  521. str += s;
  522. if (!(i % rowsNum)) str += '\n';
  523. }
  524. return str
  525. }
  526. }
  527. this.options.xAxis.axisLabel = axisLabel;
  528. this.options.series = series;
  529. this.options.legend["data"] = legendName;
  530. this.setOptionsLegendName(legendName);
  531. },
  532. },
  533. };
  534. </script>
  535. <style scoped lang="scss">
  536. .echarts {
  537. width: 100%;
  538. height: 100%;
  539. overflow: hidden;
  540. }
  541. </style>