widgetBarLineStackChart.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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: "widgetBarLineStackChart",
  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. {
  36. type: "value",
  37. axisLine: {
  38. show: true,
  39. lineStyle: {
  40. color: "#cdd5e2",
  41. },
  42. },
  43. splitLine: {
  44. show: false,
  45. },
  46. axisLabel: {
  47. textStyle: {
  48. color: "#666666",
  49. },
  50. },
  51. },
  52. {
  53. type: "value",
  54. nameTextStyle: {
  55. color: "#666666",
  56. },
  57. axisLine: {
  58. lineStyle: {
  59. color: "#cdd5e2",
  60. },
  61. },
  62. splitLine: {
  63. show: false,
  64. },
  65. axisLabel: {
  66. show: true,
  67. textStyle: {
  68. color: "#666666",
  69. },
  70. },
  71. },
  72. ],
  73. series: [
  74. {
  75. data: [],
  76. name: "",
  77. type: "bar",
  78. barGap: "0%",
  79. itemStyle: {
  80. barBorderRadius: null,
  81. },
  82. },
  83. {
  84. data: [],
  85. name: "",
  86. type: "line",
  87. itemStyle: {},
  88. },
  89. ],
  90. },
  91. optionsStyle: {}, // 样式
  92. optionsData: {}, // 数据
  93. optionsSetup: {},
  94. flagInter: null,
  95. };
  96. },
  97. computed: {
  98. styleObj() {
  99. return {
  100. position: this.ispreview ? "absolute" : "static",
  101. width: this.optionsStyle.width + "px",
  102. height: this.optionsStyle.height + "px",
  103. left: this.optionsStyle.left + "px",
  104. top: this.optionsStyle.top + "px",
  105. background: this.optionsSetup.background,
  106. };
  107. },
  108. allComponentLinkage() {
  109. return this.$store.state.designer.allComponentLinkage;
  110. },
  111. },
  112. watch: {
  113. value: {
  114. handler(val) {
  115. this.optionsStyle = val.position;
  116. this.optionsData = val.data;
  117. this.optionsCollapse = val.setup;
  118. this.optionsSetup = val.setup;
  119. this.editorOptions();
  120. },
  121. deep: true,
  122. },
  123. },
  124. mounted() {
  125. this.optionsStyle = this.value.position;
  126. this.optionsData = this.value.data;
  127. this.optionsCollapse = this.value.setup;
  128. this.optionsSetup = this.value.setup;
  129. this.editorOptions();
  130. targetWidgetLinkageLogic(this); // 联动-目标组件逻辑
  131. },
  132. methods: {
  133. // 修改图标options属性
  134. editorOptions() {
  135. this.setOptionsTitle();
  136. this.setOptionsX();
  137. this.setOptionsY();
  138. this.setOptionsTooltip();
  139. this.setOptionsMargin();
  140. this.setOptionsLegend();
  141. this.setOptionsData();
  142. },
  143. // 标题修改
  144. setOptionsTitle() {
  145. const optionsSetup = this.optionsSetup;
  146. const title = {
  147. text: optionsSetup.text,
  148. show: optionsSetup.isShowTitle,
  149. left: optionsSetup.titleLeft,
  150. top: optionsSetup.titleTop + "%",
  151. itemGap: optionsSetup.titleItemGap,
  152. textStyle: {
  153. color: optionsSetup.textColor,
  154. fontSize: optionsSetup.textFontSize,
  155. fontWeight: optionsSetup.textFontWeight,
  156. fontStyle: optionsSetup.textFontStyle,
  157. fontFamily: optionsSetup.textFontFamily,
  158. },
  159. subtext: optionsSetup.subtext,
  160. subtextStyle: {
  161. color: optionsSetup.subtextColor,
  162. fontWeight: optionsSetup.subtextFontWeight,
  163. fontSize: optionsSetup.subtextFontSize,
  164. fontStyle: optionsSetup.subtextFontStyle,
  165. fontFamily: optionsSetup.subtextFontFamily
  166. },
  167. };
  168. this.options.title = title;
  169. },
  170. // X轴设置
  171. setOptionsX() {
  172. const optionsSetup = this.optionsSetup;
  173. const xAxis = {
  174. type: "category",
  175. // 坐标轴是否显示
  176. show: optionsSetup.isShowX,
  177. position: optionsSetup.positionX,
  178. offset: optionsSetup.offsetX,
  179. // 坐标轴名称
  180. name: optionsSetup.nameX,
  181. nameLocation: optionsSetup.nameLocationX,
  182. nameTextStyle: {
  183. color: optionsSetup.nameColorX,
  184. fontSize: optionsSetup.nameFontSizeX,
  185. fontWeight: optionsSetup.nameFontWeightX,
  186. fontStyle: optionsSetup.nameFontStyleX,
  187. fontFamily: optionsSetup.nameFontFamilyX,
  188. },
  189. // 轴反转
  190. inverse: optionsSetup.reversalX,
  191. axisLabel: {
  192. show: optionsSetup.isShowAxisLabelX,
  193. interval: optionsSetup.textIntervalX,
  194. // 文字角度
  195. rotate: optionsSetup.textAngleX,
  196. textStyle: {
  197. // 坐标文字颜色
  198. color: optionsSetup.textColorX,
  199. fontSize: optionsSetup.textFontSizeX,
  200. fontWeight: optionsSetup.textFontWeightX,
  201. fontStyle: optionsSetup.textFontStyleX,
  202. fontFamily: optionsSetup.textFontFamilyX,
  203. },
  204. },
  205. // X轴线
  206. axisLine: {
  207. show: optionsSetup.isShowAxisLineX,
  208. lineStyle: {
  209. color: optionsSetup.lineColorX,
  210. width: optionsSetup.lineWidthX,
  211. },
  212. },
  213. // X轴刻度线
  214. axisTick: {
  215. show: optionsSetup.isShowAxisLineX,
  216. lineStyle: {
  217. color: optionsSetup.lineColorX,
  218. width: optionsSetup.lineWidthX,
  219. },
  220. },
  221. // X轴分割线
  222. splitLine: {
  223. show: optionsSetup.isShowSplitLineX,
  224. lineStyle: {
  225. color: optionsSetup.splitLineColorX,
  226. width: optionsSetup.splitLineWidthX,
  227. },
  228. },
  229. };
  230. this.options.xAxis = xAxis;
  231. },
  232. // Y轴设置
  233. setOptionsY() {
  234. const optionsSetup = this.optionsSetup;
  235. const yAxis = [
  236. {
  237. max: optionsSetup.maxYLeft !== "" ? optionsSetup.maxYLeft : null,
  238. type: "value",
  239. scale: optionsSetup.scaleYLeft,
  240. // 均分
  241. splitNumber: optionsSetup.splitNumberYLeft,
  242. // 坐标轴是否显示
  243. show: optionsSetup.isShowYLeft,
  244. position: optionsSetup.positionYLeft,
  245. offset: optionsSetup.offsetYLeft,
  246. // 坐标轴名称
  247. name: optionsSetup.textNameYLeft,
  248. nameLocation: optionsSetup.nameLocationYLeft,
  249. // 别名
  250. nameTextStyle: {
  251. color: optionsSetup.nameColorYLeft,
  252. fontSize: optionsSetup.nameFontSizeYLeft,
  253. fontWeight: optionsSetup.nameFontWeightYLeft,
  254. fontStyle: optionsSetup.nameFontStyleYLeft,
  255. fontFamily: optionsSetup.nameFontFamilyYLeft,
  256. },
  257. axisLabel: {
  258. show: optionsSetup.isShowAxisLabelYLeft,
  259. // 文字角度
  260. rotate: optionsSetup.textAngleYLeft,
  261. textStyle: {
  262. // 坐标文字颜色
  263. color: optionsSetup.textColorYLeft,
  264. fontSize: optionsSetup.textFontSizeYLeft,
  265. fontWeight: optionsSetup.textFontWeightYLeft,
  266. fontStyle: optionsSetup.textFontStyleYLeft,
  267. fontFamily: optionsSetup.textFontFamilyYLeft,
  268. },
  269. },
  270. axisLine: {
  271. show: optionsSetup.isShowAxisLineYLeft,
  272. lineStyle: {
  273. width: optionsSetup.lineWidthYLeft,
  274. color: optionsSetup.lineColorYLeft,
  275. },
  276. },
  277. axisTick: {
  278. // 刻度
  279. show: optionsSetup.isShowAxisLineYLeft,
  280. lineStyle: {
  281. width: optionsSetup.lineWidthYLeft,
  282. color: optionsSetup.lineColorYLeft,
  283. },
  284. },
  285. splitLine: {
  286. show: optionsSetup.isShowSplitLineYLeft,
  287. lineStyle: {
  288. color: optionsSetup.splitLineColorYLeft,
  289. width: optionsSetup.splitLineFontWidthYLeft,
  290. },
  291. },
  292. },
  293. {
  294. max: optionsSetup.maxYRight !== "" ? optionsSetup.maxYRight : null,
  295. type: "value",
  296. scale: optionsSetup.scaleYRight,
  297. // 均分
  298. splitNumber: optionsSetup.splitNumberYRight,
  299. // 坐标轴是否显示
  300. show: optionsSetup.isShowYRight,
  301. position: optionsSetup.positionYRight,
  302. offset: optionsSetup.offsetYRight,
  303. // 坐标轴名称
  304. name: optionsSetup.textNameYRight,
  305. nameLocation: optionsSetup.nameLocationYRight,
  306. // 别名
  307. nameTextStyle: {
  308. color: optionsSetup.nameColorYRight,
  309. fontSize: optionsSetup.nameFontSizeYRight,
  310. fontWeight: optionsSetup.nameFontWeightYRight,
  311. fontStyle: optionsSetup.nameFontStyleYRight,
  312. fontFamily: optionsSetup.nameFontFamilyYRight,
  313. },
  314. axisLabel: {
  315. show: optionsSetup.isShowAxisLabelYRight,
  316. // 文字角度
  317. rotate: optionsSetup.textAngleYRight,
  318. textStyle: {
  319. // 坐标文字颜色
  320. color: optionsSetup.textColorYRight,
  321. fontSize: optionsSetup.textFontSizeYRight,
  322. fontWeight: optionsSetup.textFontWeightYRight,
  323. fontStyle: optionsSetup.textFontStyleYRight,
  324. fontFamily: optionsSetup.textFontFamilyYRight,
  325. },
  326. },
  327. axisLine: {
  328. show: optionsSetup.isShowAxisLineYRight,
  329. lineStyle: {
  330. width: optionsSetup.lineWidthYRight,
  331. color: optionsSetup.lineColorYRight,
  332. },
  333. },
  334. axisTick: {
  335. // 刻度
  336. show: optionsSetup.isShowAxisLineYRight,
  337. lineStyle: {
  338. width: optionsSetup.lineWidthYRight,
  339. color: optionsSetup.lineColorYRight,
  340. }
  341. },
  342. splitLine: {
  343. show: optionsSetup.isShowSplitLineYRight,
  344. lineStyle: {
  345. color: optionsSetup.splitLineColorYRight,
  346. width: optionsSetup.splitLineFontWidthYRight,
  347. },
  348. },
  349. },
  350. ];
  351. this.options.yAxis = yAxis;
  352. },
  353. // tooltip 提示语设置,鼠标放置显示
  354. setOptionsTooltip() {
  355. const optionsSetup = this.optionsSetup;
  356. const tooltip = {
  357. show: optionsSetup.isShowTooltip,
  358. trigger: optionsSetup.tooltipTrigger,
  359. axisPointer: {
  360. type: optionsSetup.tooltipAxisPointerType,
  361. },
  362. textStyle: {
  363. color: optionsSetup.tooltipColor,
  364. fontSize: optionsSetup.tooltipFontSize,
  365. fontWeight: optionsSetup.tooltipFontWeight,
  366. fontStyle: optionsSetup.tooltipFontStyle,
  367. fontFamily: optionsSetup.tooltipFontFamily,
  368. },
  369. };
  370. this.options.tooltip = tooltip;
  371. },
  372. // 边距设置
  373. setOptionsMargin() {
  374. const optionsSetup = this.optionsSetup;
  375. const grid = {
  376. left: optionsSetup.marginLeft,
  377. right: optionsSetup.marginRight,
  378. bottom: optionsSetup.marginBottom,
  379. top: optionsSetup.marginTop,
  380. containLabel: true,
  381. };
  382. this.options.grid = grid;
  383. },
  384. // 图例操作 legend
  385. setOptionsLegend() {
  386. const optionsSetup = this.optionsSetup;
  387. const legend = {
  388. show: optionsSetup.isShowLegend,
  389. left: optionsSetup.lateralPosition,
  390. //right: optionsSetup.lateralPosition,
  391. top: optionsSetup.longitudinalPosition,
  392. //bottom: optionsSetup.longitudinalPosition,
  393. orient: optionsSetup.layoutFront,
  394. textStyle: {
  395. color: optionsSetup.legendColor,
  396. fontSize: optionsSetup.legendFontSize,
  397. fontWeight: optionsSetup.legendFontWeight,
  398. fontStyle: optionsSetup.legendFontStyle,
  399. fontFamily: optionsSetup.legendFontFamily,
  400. },
  401. itemHeight: optionsSetup.legendHeight,
  402. itemWidth: optionsSetup.legendWidth,
  403. };
  404. this.options.legend = legend;
  405. },
  406. // 图例名称设置
  407. setOptionsLegendName(name) {
  408. const optionsSetup = this.optionsSetup;
  409. const series = this.options.series;
  410. const legendName = optionsSetup.legendName;
  411. // 图例没有手动写则显示原值,写了则显示新值
  412. if (null == legendName || legendName == "") {
  413. for (let i = 0; i < name.length; i++) {
  414. series[i].name = name[i];
  415. }
  416. this.options.legend["data"] = name;
  417. } else {
  418. const arr = legendName.split("|");
  419. for (let i = 0; i < arr.length; i++) {
  420. series[i].name = arr[i];
  421. }
  422. this.options.legend["data"] = arr;
  423. }
  424. },
  425. //获取堆叠样式
  426. getStackStyle() {
  427. const optionsSetup = this.optionsSetup;
  428. let style = "";
  429. if (optionsSetup.stackStyle == "upDown") {
  430. style = "total";
  431. }
  432. return style;
  433. },
  434. // 数据解析
  435. setOptionsData(e, paramsConfig) {
  436. const optionsSetup = this.optionsSetup;
  437. // 数据类型 静态 or 动态
  438. const optionsData = this.optionsData;
  439. // 联动接收者逻辑开始
  440. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  441. const myDynamicData = optionsData.dynamicData;
  442. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  443. if (
  444. e &&
  445. optionsData.dataType !== "staticData" &&
  446. Object.keys(myDynamicData.contextData).length
  447. ) {
  448. const keyArr = Object.keys(myDynamicData.contextData);
  449. paramsConfig.forEach((conf) => {
  450. if (keyArr.includes(conf.targetKey)) {
  451. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  452. }
  453. });
  454. }
  455. // 联动接收者逻辑结束
  456. optionsData.dataType == "staticData"
  457. ? this.staticDataFn(optionsData.staticData, optionsSetup)
  458. : this.dynamicDataFn(
  459. optionsData.dynamicData,
  460. optionsData.refreshTime,
  461. optionsSetup
  462. );
  463. },
  464. //去重
  465. setUnique(arr) {
  466. let newArr = [];
  467. arr.forEach((item) => {
  468. return newArr.includes(item) ? "" : newArr.push(item);
  469. });
  470. return newArr;
  471. },
  472. getStaticSeriesData(xAxisList, yAxis, data, type) {
  473. const typeData = new Array(xAxisList.length).fill(0);
  474. for (const i in xAxisList) {
  475. for (const j in data) {
  476. if (data[j].name == yAxis && data[j].axis == xAxisList[i]) {
  477. typeData[i] = data[j][type];
  478. }
  479. }
  480. }
  481. return typeData;
  482. },
  483. //静态数据
  484. staticDataFn(val) {
  485. const optionsSetup = this.optionsSetup;
  486. //颜色
  487. const customColor = optionsSetup.customColor;
  488. const arrColor = [];
  489. for (let i = 0; i < customColor.length; i++) {
  490. arrColor.push(customColor[i].color);
  491. }
  492. //数据
  493. const series = [];
  494. let xAxisList = [];
  495. let yAxisList = [];
  496. for (const i in val) {
  497. xAxisList[i] = val[i].axis;
  498. yAxisList[i] = val[i].name;
  499. }
  500. xAxisList = this.setUnique(xAxisList);
  501. yAxisList = this.setUnique(yAxisList);
  502. const legendName = yAxisList;
  503. for (let i = 0; i < yAxisList.length; i++) {
  504. const bar = this.getStaticSeriesData(
  505. xAxisList,
  506. yAxisList[i],
  507. val,
  508. "bar"
  509. );
  510. series.push({
  511. name: yAxisList[i],
  512. type: "bar",
  513. data: bar,
  514. barGap: optionsSetup.barGap + "%",
  515. barMinHeight : optionsSetup.minHeight,
  516. stack: this.getStackStyle(),
  517. barWidth: optionsSetup.maxWidth,
  518. label: {
  519. show: optionsSetup.isShowBar,
  520. position: optionsSetup.fontPositionBar,
  521. distance: optionsSetup.fontDistanceBar,
  522. fontSize: optionsSetup.fontSizeBar,
  523. color: optionsSetup.fontColorBar,
  524. fontWeight: optionsSetup.fontWeightBar,
  525. formatter: !!optionsSetup.percentSignBar ? '{c}%' : '{c}',
  526. fontStyle: optionsSetup.fontStyleBar,
  527. fontFamily: optionsSetup.fontFamilyBar,
  528. },
  529. //颜色,圆角属性
  530. itemStyle: {
  531. normal: {
  532. color: arrColor[i],
  533. barBorderRadius: optionsSetup.radius,
  534. },
  535. },
  536. //柱体背景属性
  537. showBackground: optionsSetup.isShowBackground,
  538. backgroundStyle: {
  539. color: optionsSetup.backgroundStyleColor,
  540. borderColor: optionsSetup.backgroundStyleBorderColor,
  541. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  542. borderType: optionsSetup.backgroundStyleBorderType,
  543. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  544. shadowColor: optionsSetup.backgroundStyleShadowColor,
  545. opacity: optionsSetup.backgroundStyleOpacity / 100,
  546. }
  547. });
  548. }
  549. for (let i = 0; i < yAxisList.length; i++) {
  550. const line = this.getStaticSeriesData(
  551. xAxisList,
  552. yAxisList[i],
  553. val,
  554. "line"
  555. );
  556. series.push({
  557. name: yAxisList[i],
  558. type: "line",
  559. data: line,
  560. yAxisIndex: 1,
  561. symbol: optionsSetup.symbol,
  562. showSymbol: optionsSetup.markPoint,
  563. symbolSize: optionsSetup.pointSize,
  564. smooth: optionsSetup.smoothCurve,
  565. itemStyle: {
  566. normal: {
  567. color: arrColor[i],
  568. },
  569. },
  570. // 线条
  571. lineStyle: {
  572. color: arrColor[i],
  573. width: optionsSetup.lineWidth,
  574. },
  575. areaStyle: {
  576. opacity: optionsSetup.area ? optionsSetup.areaThickness / 100 : 0,
  577. },
  578. label: {
  579. show: optionsSetup.isShowLine,
  580. position: optionsSetup.fontPositionLine,
  581. distance: optionsSetup.fontDistanceLine,
  582. fontSize: optionsSetup.fontSizeLine,
  583. color: optionsSetup.fontColorLine,
  584. fontWeight: optionsSetup.fontWeightLine,
  585. formatter: !!optionsSetup.percentSignLine ? '{c}%' : '{c}',
  586. fontStyle: optionsSetup.fontStyleLine,
  587. fontFamily: optionsSetup.fontFamilyLine,
  588. },
  589. });
  590. }
  591. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  592. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisList.length) / optionsSetup.textFontSizeX);
  593. const axisLabel = {
  594. show: optionsSetup.isShowAxisLabelX,
  595. interval: optionsSetup.textIntervalX,
  596. // 文字角度
  597. rotate: optionsSetup.textAngleX,
  598. textStyle: {
  599. // 坐标文字颜色
  600. color: optionsSetup.textColorX,
  601. fontSize: optionsSetup.textFontSizeX,
  602. fontWeight: optionsSetup.textFontWeightX,
  603. fontStyle: optionsSetup.textFontStyleX,
  604. fontFamily: optionsSetup.textFontFamilyX,
  605. },
  606. // 自动换行
  607. formatter: function (value, index) {
  608. const strs = value.split('');
  609. let str = ''
  610. for (let i = 0, s; s = strs[i++];) {
  611. str += s;
  612. if (!(i % rowsNum)) str += '\n';
  613. }
  614. return str
  615. }
  616. }
  617. if (optionsSetup.textRowsBreakAuto) {
  618. this.options.xAxis.axisLabel = axisLabel;
  619. }
  620. this.options.series = series;
  621. this.options.xAxis.data = xAxisList;
  622. this.options.yAxis.data = [];
  623. this.options.xAxis.type = "category";
  624. this.options.yAxis.type = "value";
  625. this.options.legend["data"] = legendName;
  626. this.setOptionsLegendName(legendName);
  627. },
  628. // 动态数据
  629. dynamicDataFn(val, refreshTime, optionsSetup) {
  630. if (!val) return;
  631. if (this.ispreview) {
  632. this.getEchartData(val, optionsSetup);
  633. this.flagInter = setInterval(() => {
  634. this.getEchartData(val, optionsSetup);
  635. }, refreshTime);
  636. } else {
  637. this.getEchartData(val, optionsSetup);
  638. }
  639. },
  640. getEchartData(val, optionsSetup) {
  641. const data = this.queryEchartsData(val);
  642. data.then((res) => {
  643. this.renderingFn(optionsSetup, res);
  644. });
  645. },
  646. renderingFn(optionsSetup, val) {
  647. //颜色
  648. const customColor = optionsSetup.customColor;
  649. const arrColor = [];
  650. for (let i = 0; i < customColor.length; i++) {
  651. arrColor.push(customColor[i].color);
  652. }
  653. this.options.xAxis.data = val.xAxis;
  654. this.options.yAxis.data = [];
  655. this.options.xAxis.type = "category";
  656. this.options.yAxis.type = "value";
  657. const series = [];
  658. let legendName = [];
  659. for (const i in val.series) {
  660. legendName.push(val.series[i].name);
  661. }
  662. legendName = this.setUnique(legendName);
  663. for (let i = 0; i < legendName.length; i++) {
  664. const bar = this.getDynamicSeriesData(legendName[i], val.series, "bar");
  665. series.push({
  666. name: legendName[i],
  667. type: "bar",
  668. data: bar,
  669. barGap: optionsSetup.barGap + "%",
  670. barMinHeight : optionsSetup.minHeight,
  671. stack: this.getStackStyle(),
  672. barWidth: optionsSetup.maxWidth,
  673. label: {
  674. show: optionsSetup.isShowBar,
  675. position: optionsSetup.fontPositionBar,
  676. distance: optionsSetup.fontDistanceBar,
  677. fontSize: optionsSetup.fontSizeBar,
  678. color: optionsSetup.fontColorBar,
  679. fontWeight: optionsSetup.fontWeightBar,
  680. formatter: !!optionsSetup.percentSignBar ? '{c}%' : '{c}',
  681. fontStyle: optionsSetup.fontStyleBar,
  682. fontFamily: optionsSetup.fontFamilyBar,
  683. },
  684. //颜色,圆角属性
  685. itemStyle: {
  686. normal: {
  687. color: arrColor[i],
  688. barBorderRadius: optionsSetup.radius,
  689. },
  690. },
  691. //柱体背景属性
  692. showBackground: optionsSetup.isShowBackground,
  693. backgroundStyle: {
  694. color: optionsSetup.backgroundStyleColor,
  695. borderColor: optionsSetup.backgroundStyleBorderColor,
  696. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  697. borderType: optionsSetup.backgroundStyleBorderType,
  698. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  699. shadowColor: optionsSetup.backgroundStyleShadowColor,
  700. opacity: optionsSetup.backgroundStyleOpacity / 100,
  701. }
  702. });
  703. }
  704. for (let i = 0; i < legendName.length; i++) {
  705. const line = this.getDynamicSeriesData(
  706. legendName[i],
  707. val.series,
  708. "line"
  709. );
  710. series.push({
  711. name: legendName[i],
  712. type: "line",
  713. data: line,
  714. yAxisIndex: 1,
  715. symbol: optionsSetup.symbol,
  716. showSymbol: optionsSetup.markPoint,
  717. symbolSize: optionsSetup.pointSize,
  718. smooth: optionsSetup.smoothCurve,
  719. itemStyle: {
  720. normal: {
  721. color: arrColor[i],
  722. },
  723. },
  724. // 线条
  725. lineStyle: {
  726. color: arrColor[i],
  727. width: optionsSetup.lineWidth,
  728. },
  729. areaStyle: {
  730. opacity: optionsSetup.area ? optionsSetup.areaThickness / 100 : 0,
  731. },
  732. label: {
  733. show: optionsSetup.isShowLine,
  734. position: optionsSetup.fontPositionLine,
  735. distance: optionsSetup.fontDistanceLine,
  736. fontSize: optionsSetup.fontSizeLine,
  737. color: optionsSetup.fontColorLine,
  738. fontWeight: optionsSetup.fontWeightLine,
  739. formatter: !!optionsSetup.percentSignLine ? '{c}%' : '{c}',
  740. fontStyle: optionsSetup.fontStyleLine,
  741. fontFamily: optionsSetup.fontFamilyLine,
  742. },
  743. });
  744. }
  745. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  746. const xAxisDataLength = val.length !== 0 ? val.xAxis.length : 1;
  747. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisDataLength) / optionsSetup.textFontSizeX);
  748. const axisLabel = {
  749. show: optionsSetup.isShowAxisLabelX,
  750. interval: optionsSetup.textIntervalX,
  751. // 文字角度
  752. rotate: optionsSetup.textAngleX,
  753. textStyle: {
  754. // 坐标文字颜色
  755. color: optionsSetup.textColorX,
  756. fontSize: optionsSetup.textFontSizeX,
  757. fontWeight: optionsSetup.textFontWeightX,
  758. fontStyle: optionsSetup.textFontStyleX,
  759. fontFamily: optionsSetup.textFontFamilyX,
  760. },
  761. // 自动换行
  762. formatter: function (value, index) {
  763. const strs = value.split('');
  764. let str = ''
  765. for (let i = 0, s; s = strs[i++];) {
  766. str += s;
  767. if (!(i % rowsNum)) str += '\n';
  768. }
  769. return str
  770. }
  771. }
  772. if (optionsSetup.textRowsBreakAuto) {
  773. this.options.xAxis.axisLabel = axisLabel;
  774. }
  775. this.options.series = series;
  776. this.options.legend["data"] = legendName;
  777. this.setOptionsLegendName(legendName);
  778. },
  779. getDynamicSeriesData(legend, series, type) {
  780. let data = [];
  781. for (const i in series) {
  782. if (series[i].name == legend && series[i].type == type) {
  783. data = series[i].data;
  784. }
  785. }
  786. return data;
  787. },
  788. },
  789. };
  790. </script>
  791. <style scoped lang="scss">
  792. .echarts {
  793. width: 100%;
  794. height: 100%;
  795. overflow: hidden;
  796. }
  797. </style>