widgetBarlinechart.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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: "WidgetBarlinechart",
  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: "line",
  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.setOptionsLine();
  143. this.setOptionsBar();
  144. this.setOptionsTooltip();
  145. this.setOptionsData();
  146. this.setOptionsMargin();
  147. this.setOptionsLegend();
  148. this.setOptionsColor();
  149. },
  150. // 标题修改
  151. setOptionsTitle() {
  152. const optionsSetup = this.optionsSetup;
  153. const title = {};
  154. title.text = optionsSetup.titleText;
  155. title.show = optionsSetup.isNoTitle;
  156. title.left = optionsSetup.textAlign;
  157. title.textStyle = {
  158. color: optionsSetup.textColor,
  159. fontSize: optionsSetup.textFontSize,
  160. fontWeight: optionsSetup.textFontWeight,
  161. fontStyle: optionsSetup.textFontStyle,
  162. };
  163. title.subtext = optionsSetup.subText;
  164. title.subtextStyle = {
  165. color: optionsSetup.subTextColor,
  166. fontWeight: optionsSetup.subTextFontWeight,
  167. fontSize: optionsSetup.subTextFontSize,
  168. fontStyle: optionsSetup.subTextFontStyle,
  169. };
  170. this.options.title = title;
  171. },
  172. // X轴设置
  173. setOptionsX() {
  174. const optionsSetup = this.optionsSetup;
  175. const xAxis = {
  176. type: "category",
  177. // 坐标轴是否显示
  178. show: optionsSetup.hideX,
  179. // 坐标轴名称
  180. name: optionsSetup.nameX,
  181. nameTextStyle: {
  182. color: optionsSetup.nameColorX,
  183. fontSize: optionsSetup.nameFontSizeX,
  184. },
  185. // 轴反转
  186. inverse: optionsSetup.reversalX,
  187. axisLabel: {
  188. show: true,
  189. interval: optionsSetup.textInterval,
  190. // 文字角度
  191. rotate: optionsSetup.textAngleX,
  192. textStyle: {
  193. // 坐标文字颜色
  194. color: optionsSetup.colorX,
  195. fontSize: optionsSetup.fontSizeX,
  196. },
  197. },
  198. axisLine: {
  199. show: true,
  200. lineStyle: {
  201. color: optionsSetup.lineColorX,
  202. width: optionsSetup.lineWidthX,
  203. },
  204. },
  205. splitLine: {
  206. show: optionsSetup.isShowSplitLineX,
  207. lineStyle: {
  208. color: optionsSetup.splitLineColorX,
  209. width: optionsSetup.splitLineWidthX,
  210. },
  211. },
  212. };
  213. this.options.xAxis = xAxis;
  214. },
  215. // Y轴设置
  216. setOptionsY() {
  217. const optionsSetup = this.optionsSetup;
  218. const yAxis = [
  219. {
  220. max: optionsSetup.maxYLeft !== "" ? optionsSetup.maxYLeft : null,
  221. type: "value",
  222. // 均分
  223. splitNumber: optionsSetup.splitNumberLeft,
  224. // 坐标轴是否显示
  225. show: optionsSetup.isShowYLeft,
  226. // 坐标轴名称
  227. name: optionsSetup.textNameYLeft,
  228. // 别名
  229. nameTextStyle: {
  230. color: optionsSetup.nameColorYLeft,
  231. fontSize: optionsSetup.nameFontSizeYLeft,
  232. },
  233. axisLabel: {
  234. show: true,
  235. // 文字角度
  236. rotate: optionsSetup.textAngleYLeft,
  237. textStyle: {
  238. // 坐标文字颜色
  239. color: optionsSetup.colorYLeft,
  240. fontSize: optionsSetup.fontSizeYLeft,
  241. },
  242. },
  243. axisTick: {
  244. // 刻度
  245. show: optionsSetup.tickLineYLeft,
  246. },
  247. axisLine: {
  248. show: optionsSetup.lineYLeft,
  249. lineStyle: {
  250. width: optionsSetup.lineWidthYLeft,
  251. color: optionsSetup.lineColorYLeft,
  252. },
  253. },
  254. splitLine: {
  255. show: optionsSetup.isShowSplitLineYLeft,
  256. lineStyle: {
  257. color: optionsSetup.splitLineColorYLeft,
  258. width: optionsSetup.splitLineFontWidthYLeft,
  259. },
  260. },
  261. },
  262. {
  263. max: optionsSetup.maxYRight !== "" ? optionsSetup.maxYRight : null,
  264. type: "value",
  265. // 均分
  266. splitNumber: optionsSetup.splitNumberRight,
  267. // 坐标轴是否显示
  268. show: optionsSetup.isShowYRight,
  269. // 坐标轴名称
  270. name: optionsSetup.textNameYRight,
  271. // 别名
  272. nameTextStyle: {
  273. color: optionsSetup.nameColorYRight,
  274. fontSize: optionsSetup.nameFontSizeYRight,
  275. },
  276. axisLabel: {
  277. show: true,
  278. // 文字角度
  279. rotate: optionsSetup.textAngleYRight,
  280. textStyle: {
  281. // 坐标文字颜色
  282. color: optionsSetup.colorYRight,
  283. fontSize: optionsSetup.fontSizeYRight,
  284. },
  285. },
  286. axisTick: {
  287. // 刻度
  288. show: optionsSetup.tickLineYRight,
  289. },
  290. axisLine: {
  291. show: optionsSetup.lineYRight,
  292. lineStyle: {
  293. width: optionsSetup.lineWidthYRight,
  294. color: optionsSetup.lineColorYRight,
  295. },
  296. },
  297. splitLine: {
  298. show: optionsSetup.isShowSplitLineYRight,
  299. lineStyle: {
  300. color: optionsSetup.splitLineColorYRight,
  301. width: optionsSetup.splitLineFontWidthYRight,
  302. },
  303. },
  304. },
  305. ];
  306. this.options.yAxis = yAxis;
  307. },
  308. // 折线设置 数值设置
  309. setOptionsLine() {
  310. const optionsSetup = this.optionsSetup;
  311. const series = this.options.series;
  312. for (const key in series) {
  313. if (series[key].type == "line") {
  314. series[key].symbol = optionsSetup.symbol;
  315. series[key].showSymbol = optionsSetup.markPoint;
  316. series[key].symbolSize = optionsSetup.pointSize;
  317. series[key].smooth = optionsSetup.smoothCurve;
  318. if (optionsSetup.area) {
  319. series[key].areaStyle = {
  320. opacity: optionsSetup.areaThickness / 100,
  321. };
  322. } else {
  323. series[key].areaStyle = {
  324. opacity: 0,
  325. };
  326. }
  327. series[key].lineStyle = {
  328. width: optionsSetup.lineWidth,
  329. };
  330. series[key].itemStyle.borderRadius = optionsSetup.radius;
  331. series[key].label = {
  332. show: optionsSetup.isShowLine,
  333. position: optionsSetup.fontPositionLine,
  334. distance: optionsSetup.distanceLine,
  335. fontSize: optionsSetup.fontSizeLine,
  336. color: optionsSetup.subTextColorLine,
  337. fontWeight: optionsSetup.fontWeightLine,
  338. formatter: !!optionsSetup.percentSignLine ? '{c}%' : '{c}'
  339. };
  340. }
  341. }
  342. this.options.series = series;
  343. },
  344. // 柱体设置 数值设置
  345. setOptionsBar() {
  346. const optionsSetup = this.optionsSetup;
  347. const series = this.options.series;
  348. for (const key in series) {
  349. if (series[key].type == "bar") {
  350. series[key].label = {
  351. show: optionsSetup.isShowBar,
  352. position: optionsSetup.fontPositionBar,
  353. distance: optionsSetup.distanceBar,
  354. fontSize: optionsSetup.fontSizeBar,
  355. color: optionsSetup.subTextColorBar,
  356. fontWeight: optionsSetup.fontWeightBar,
  357. formatter: !!optionsSetup.percentSignBar ? '{c}%' : '{c}'
  358. };
  359. //柱体背景属性
  360. series[key].showBackground = optionsSetup.isShowBackground;
  361. series[key].backgroundStyle = {
  362. color: optionsSetup.backgroundStyleColor,
  363. borderColor: optionsSetup.backgroundStyleBorderColor,
  364. borderWidth: optionsSetup.backgroundStyleBorderWidth,
  365. borderType: optionsSetup.backgroundStyleBorderType,
  366. shadowBlur: optionsSetup.backgroundStyleShadowBlur,
  367. shadowColor: optionsSetup.backgroundStyleShadowColor,
  368. opacity: optionsSetup.backgroundStyleOpacity / 100,
  369. };
  370. series[key].barWidth = optionsSetup.maxWidth;
  371. series[key].barMinHeight = optionsSetup.minHeight;
  372. series[key].itemStyle.barBorderRadius = optionsSetup.radius;
  373. }
  374. }
  375. this.options.series = series;
  376. },
  377. // tooltip 设置
  378. setOptionsTooltip() {
  379. const optionsSetup = this.optionsSetup;
  380. const tooltip = {
  381. show: optionsSetup.isShowTooltip,
  382. trigger: optionsSetup.tooltipTrigger,
  383. axisPointer: {
  384. type: optionsSetup.tooltipAxisPointerType,
  385. },
  386. textStyle: {
  387. color: optionsSetup.tipsColor,
  388. fontSize: optionsSetup.tipsFontSize,
  389. },
  390. };
  391. this.options.tooltip = tooltip;
  392. },
  393. // 边距设置
  394. setOptionsMargin() {
  395. const optionsSetup = this.optionsSetup;
  396. const grid = {
  397. left: optionsSetup.marginLeft,
  398. right: optionsSetup.marginRight,
  399. bottom: optionsSetup.marginBottom,
  400. top: optionsSetup.marginTop,
  401. containLabel: true,
  402. };
  403. this.options.grid = grid;
  404. },
  405. setOptionsLegend() {
  406. const optionsSetup = this.optionsSetup;
  407. const legend = this.options.legend;
  408. legend.show = optionsSetup.isShowLegend;
  409. legend.left = optionsSetup.lateralPosition;
  410. legend.top = optionsSetup.longitudinalPosition;
  411. legend.bottom = optionsSetup.longitudinalPosition;
  412. legend.orient = optionsSetup.layoutFront;
  413. legend.textStyle = {
  414. color: optionsSetup.legendColor,
  415. fontSize: optionsSetup.legendFontSize,
  416. };
  417. legend.itemWidth = optionsSetup.legendWidth;
  418. },
  419. // 图例名称设置
  420. setOptionsLegendName(name) {
  421. const optionsSetup = this.optionsSetup;
  422. const series = this.options.series;
  423. const legendName = optionsSetup.legendName;
  424. // 图例没有手动写则显示原值,写了则显示新值
  425. if (null == legendName || legendName == "") {
  426. for (let i = 0; i < name.length; i++) {
  427. series[i].name = name[i];
  428. }
  429. this.options.legend["data"] = name;
  430. } else {
  431. const arr = legendName.split("|");
  432. for (let i = 0; i < arr.length; i++) {
  433. series[i].name = arr[i];
  434. }
  435. this.options.legend["data"] = arr;
  436. }
  437. },
  438. // 图例颜色修改
  439. setOptionsColor() {
  440. const optionsSetup = this.optionsSetup;
  441. const customColor = optionsSetup.customColor;
  442. if (!customColor) return;
  443. const arrColor = [];
  444. for (let i = 0; i < customColor.length; i++) {
  445. arrColor.push(customColor[i].color);
  446. }
  447. this.options.color = arrColor;
  448. this.options = Object.assign({}, this.options);
  449. },
  450. // 数据处理
  451. setOptionsData(e, paramsConfig) {
  452. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  453. // 联动接收者逻辑开始
  454. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  455. const myDynamicData = optionsData.dynamicData;
  456. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  457. if (
  458. e &&
  459. optionsData.dataType !== "staticData" &&
  460. Object.keys(myDynamicData.contextData).length
  461. ) {
  462. const keyArr = Object.keys(myDynamicData.contextData);
  463. paramsConfig.forEach((conf) => {
  464. if (keyArr.includes(conf.targetKey)) {
  465. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  466. }
  467. });
  468. }
  469. // 联动接收者逻辑结束
  470. optionsData.dataType == "staticData"
  471. ? this.staticDataFn(optionsData.staticData)
  472. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  473. },
  474. staticDataFn(val) {
  475. const series = this.options.series;
  476. let axis = [];
  477. let bar = [];
  478. let line = [];
  479. for (const i in val) {
  480. axis[i] = val[i].axis;
  481. bar[i] = val[i].bar;
  482. line[i] = val[i].line;
  483. }
  484. // x轴
  485. this.options.xAxis.data = axis;
  486. // series
  487. for (const i in series) {
  488. if (series[i].type == "bar") {
  489. series[i].data = bar;
  490. } else {
  491. series[i].data = line;
  492. }
  493. }
  494. const legendName = [];
  495. legendName.push("bar");
  496. legendName.push("line");
  497. const optionsSetup = this.optionsSetup;
  498. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  499. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / axis.length) / optionsSetup.fontSizeX);
  500. const axisLabel = {
  501. show: true,
  502. interval: optionsSetup.textInterval,
  503. // 文字角度
  504. rotate: optionsSetup.textAngleX,
  505. textStyle: {
  506. // 坐标文字颜色
  507. color: optionsSetup.colorX,
  508. fontSize: optionsSetup.fontSizeX,
  509. },
  510. // 自动换行
  511. formatter: function (value, index) {
  512. const strs = value.split('');
  513. let str = ''
  514. for (let i = 0, s; s = strs[i++];) {
  515. str += s;
  516. if (!(i % rowsNum)) str += '\n';
  517. }
  518. return str
  519. }
  520. }
  521. if (optionsSetup.textRowsBreakAuto) {
  522. this.options.xAxis.axisLabel = axisLabel;
  523. }
  524. this.options.legend["data"] = legendName;
  525. this.setOptionsLegendName(legendName);
  526. },
  527. dynamicDataFn(val, refreshTime) {
  528. if (!val) return;
  529. if (this.ispreview) {
  530. this.getEchartData(val);
  531. this.flagInter = setInterval(() => {
  532. this.getEchartData(val);
  533. }, refreshTime);
  534. } else {
  535. this.getEchartData(val);
  536. }
  537. },
  538. getEchartData(val) {
  539. const data = this.queryEchartsData(val);
  540. data.then((res) => {
  541. this.renderingFn(res);
  542. });
  543. },
  544. renderingFn(val) {
  545. this.options.xAxis.data = val.xAxis;
  546. // series
  547. const series = this.options.series;
  548. const legendName = [];
  549. for (const i in series) {
  550. for (const j in val.series) {
  551. if (series[i].type == val.series[j].type) {
  552. series[i].data = val.series[j].data;
  553. legendName.push(val.series[j].name);
  554. }
  555. }
  556. }
  557. const optionsSetup = this.optionsSetup;
  558. // 根据图表的宽度 x轴的字体大小、长度来估算X轴的label能展示多少个字
  559. const xAxisDataLength = val.length !== 0 ? val.xAxis.length : 1;
  560. const rowsNum = optionsSetup.textRowsNum !== "" ? optionsSetup.textRowsNum : parseInt((this.optionsStyle.width / xAxisDataLength) / optionsSetup.fontSizeX);
  561. const axisLabel = {
  562. show: true,
  563. interval: optionsSetup.textInterval,
  564. // 文字角度
  565. rotate: optionsSetup.textAngleX,
  566. textStyle: {
  567. // 坐标文字颜色
  568. color: optionsSetup.colorX,
  569. fontSize: optionsSetup.fontSizeX,
  570. },
  571. // 自动换行
  572. formatter: function (value, index) {
  573. const strs = value.split('');
  574. let str = ''
  575. for (let i = 0, s; s = strs[i++];) {
  576. str += s;
  577. if (!(i % rowsNum)) str += '\n';
  578. }
  579. return str
  580. }
  581. }
  582. if (optionsSetup.textRowsBreakAuto) {
  583. this.options.xAxis.axisLabel = axisLabel;
  584. }
  585. this.options.legend["data"] = legendName;
  586. this.setOptionsLegendName(legendName);
  587. },
  588. },
  589. };
  590. </script>
  591. <style scoped lang="scss">
  592. .echarts {
  593. width: 100%;
  594. height: 100%;
  595. overflow: hidden;
  596. }
  597. </style>