widgetLinechart.vue 14 KB

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