widgetLinechart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. axisLabel: {
  160. show: true,
  161. // 文字间隔
  162. interval: optionsSetup.textInterval,
  163. // 文字角度
  164. rotate: optionsSetup.textAngleX,
  165. textStyle: {
  166. // 坐标文字颜色
  167. color: optionsSetup.colorX,
  168. fontSize: optionsSetup.fontSizeX,
  169. },
  170. },
  171. axisLine: {
  172. show: true,
  173. lineStyle: {
  174. color: optionsSetup.lineColorX,
  175. width: optionsSetup.lineWidthX,
  176. },
  177. },
  178. splitLine: {
  179. show: optionsSetup.isShowSplitLineX,
  180. lineStyle: {
  181. color: optionsSetup.splitLineColorX,
  182. width: optionsSetup.splitLineWidthX,
  183. },
  184. },
  185. };
  186. this.options.xAxis = xAxis;
  187. },
  188. // Y轴设置
  189. setOptionsY() {
  190. const optionsSetup = this.optionsSetup;
  191. const yAxis = {
  192. type: "value",
  193. scale: optionsSetup.scale,
  194. // 均分
  195. splitNumber: optionsSetup.splitNumberY,
  196. // 坐标轴是否显示
  197. show: optionsSetup.isShowY,
  198. // 坐标轴名称
  199. name: optionsSetup.textNameY,
  200. nameTextStyle: {
  201. color: optionsSetup.nameColorY,
  202. fontSize: optionsSetup.nameFontSizeY,
  203. },
  204. // 轴反转
  205. inverse: optionsSetup.reversalY,
  206. axisLabel: {
  207. show: true,
  208. // 文字角度
  209. rotate: optionsSetup.textAngleY,
  210. textStyle: {
  211. // 坐标文字颜色
  212. color: optionsSetup.colorY,
  213. fontSize: optionsSetup.fontSizeY,
  214. },
  215. },
  216. axisLine: {
  217. show: true,
  218. lineStyle: {
  219. color: optionsSetup.lineColorY,
  220. width: optionsSetup.lineWidthY,
  221. },
  222. },
  223. splitLine: {
  224. show: optionsSetup.isShowSplitLineY,
  225. lineStyle: {
  226. color: optionsSetup.splitLineColorY,
  227. width: optionsSetup.splitLineWidthY,
  228. },
  229. },
  230. };
  231. this.options.yAxis = yAxis;
  232. },
  233. // tooltip 设置
  234. setOptionsTooltip() {
  235. const optionsSetup = this.optionsSetup;
  236. const tooltip = {
  237. trigger: "item",
  238. show: true,
  239. textStyle: {
  240. color: optionsSetup.tipsColor,
  241. fontSize: optionsSetup.tipsFontSize,
  242. },
  243. };
  244. this.options.tooltip = tooltip;
  245. },
  246. // 边距设置
  247. setOptionsMargin() {
  248. const optionsSetup = this.optionsSetup;
  249. const grid = {
  250. left: optionsSetup.marginLeft,
  251. right: optionsSetup.marginRight,
  252. bottom: optionsSetup.marginBottom,
  253. top: optionsSetup.marginTop,
  254. containLabel: true,
  255. };
  256. this.options.grid = grid;
  257. },
  258. // 图例
  259. setOptionsLegend() {
  260. const optionsSetup = this.optionsSetup;
  261. const legend = this.options.legend;
  262. legend.show = optionsSetup.isShowLegend;
  263. legend.left = optionsSetup.lateralPosition;
  264. legend.top = optionsSetup.longitudinalPosition;
  265. legend.bottom = optionsSetup.longitudinalPosition;
  266. legend.orient = optionsSetup.layoutFront;
  267. legend.textStyle = {
  268. color: optionsSetup.legendColor,
  269. fontSize: optionsSetup.legendFontSize,
  270. };
  271. legend.itemWidth = optionsSetup.legendWidth;
  272. },
  273. // 图例名称设置
  274. setOptionsLegendName(name) {
  275. const optionsSetup = this.optionsSetup;
  276. const series = this.options.series;
  277. const legendName = optionsSetup.legendName;
  278. // 图例没有手动写则显示原值,写了则显示新值
  279. if (null == legendName || legendName == "") {
  280. for (let i = 0; i < name.length; i++) {
  281. series[i].name = name[i];
  282. }
  283. this.options.legend["data"] = name;
  284. } else {
  285. const arr = legendName.split("|");
  286. for (let i = 0; i < arr.length; i++) {
  287. series[i].name = arr[i];
  288. }
  289. this.options.legend["data"] = arr;
  290. }
  291. },
  292. // 处理数据
  293. setOptionsData(e, paramsConfig) {
  294. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  295. optionsData.dynamicData = optionsData.dynamicData || {}; // 兼容 dynamicData undefined
  296. const myDynamicData = optionsData.dynamicData;
  297. clearInterval(this.flagInter); // 不管咋,先干掉上一次的定时任务,避免多跑
  298. if (
  299. e &&
  300. optionsData.dataType !== "staticData" &&
  301. Object.keys(myDynamicData.contextData).length
  302. ) {
  303. const keyArr = Object.keys(myDynamicData.contextData);
  304. paramsConfig.forEach((conf) => {
  305. if (keyArr.includes(conf.targetKey)) {
  306. myDynamicData.contextData[conf.targetKey] = e[conf.originKey];
  307. }
  308. });
  309. }
  310. optionsData.dataType == "staticData"
  311. ? this.staticDataFn(optionsData.staticData)
  312. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  313. },
  314. staticDataFn(val) {
  315. const optionsSetup = this.optionsSetup;
  316. //颜色
  317. const customColor = optionsSetup.customColor;
  318. const arrColor = [];
  319. for (let i = 0; i < customColor.length; i++) {
  320. arrColor.push(customColor[i].color);
  321. }
  322. const series = this.options.series;
  323. let axis = [];
  324. let data = [];
  325. for (const i in val) {
  326. axis[i] = val[i].axis;
  327. data[i] = val[i].data;
  328. }
  329. const legendName = [];
  330. legendName.push("line");
  331. // x轴
  332. this.options.xAxis.data = axis;
  333. // series
  334. for (const i in series) {
  335. if (series[i].type == "line") {
  336. series[i].symbol = optionsSetup.symbol;
  337. series[i].showSymbol = optionsSetup.markPoint;
  338. series[i].symbolSize = optionsSetup.pointSize;
  339. series[i].smooth = optionsSetup.smoothCurve;
  340. if (optionsSetup.area) {
  341. series[i].areaStyle = {
  342. opacity: optionsSetup.areaThickness / 100,
  343. };
  344. } else {
  345. series[i].areaStyle = {
  346. opacity: 0,
  347. };
  348. }
  349. series[i].itemStyle = {
  350. normal: {
  351. color: arrColor[i],
  352. },
  353. };
  354. series[i].lineStyle = {
  355. color: arrColor[i],
  356. width: optionsSetup.lineWidth,
  357. };
  358. series[i].label = {
  359. show: optionsSetup.isShow,
  360. position: "top",
  361. distance: 10,
  362. fontSize: optionsSetup.fontSize,
  363. color: optionsSetup.dataColor,
  364. fontWeight: optionsSetup.fontWeight,
  365. };
  366. series[i].data = data;
  367. this.options.legend["data"] = legendName;
  368. this.setOptionsLegendName(legendName);
  369. }
  370. }
  371. },
  372. dynamicDataFn(val, refreshTime) {
  373. if (!val) return;
  374. if (this.ispreview) {
  375. this.getEchartData(val);
  376. this.flagInter = setInterval(() => {
  377. this.getEchartData(val);
  378. }, refreshTime);
  379. } else {
  380. this.getEchartData(val);
  381. }
  382. },
  383. getEchartData(val) {
  384. const data = this.queryEchartsData(val);
  385. data.then((res) => {
  386. this.renderingFn(res);
  387. });
  388. },
  389. renderingFn(val) {
  390. const optionsSetup = this.optionsSetup;
  391. //颜色
  392. const customColor = optionsSetup.customColor;
  393. const arrColor = [];
  394. for (let i = 0; i < customColor.length; i++) {
  395. arrColor.push(customColor[i].color);
  396. }
  397. // x轴
  398. this.options.xAxis.data = val.xAxis;
  399. const series = [];
  400. const legendName = [];
  401. for (const i in val.series) {
  402. legendName.push(val.series[i].name);
  403. const obj = {};
  404. if (val.series[i].type == 'line') {
  405. obj.type = 'line';
  406. obj.symbol = optionsSetup.symbol;
  407. obj.showSymbol = optionsSetup.markPoint;
  408. obj.symbolSize = optionsSetup.pointSize;
  409. obj.smooth = optionsSetup.smoothCurve;
  410. if (optionsSetup.area) {
  411. obj.areaStyle = {
  412. opacity: optionsSetup.areaThickness / 100,
  413. };
  414. } else {
  415. obj.areaStyle = {
  416. opacity: 0,
  417. };
  418. }
  419. obj.itemStyle = {
  420. normal: {
  421. color: arrColor[i],
  422. },
  423. };
  424. obj.lineStyle = {
  425. color: arrColor[i],
  426. width: optionsSetup.lineWidth,
  427. };
  428. obj.label = {
  429. show: optionsSetup.isShow,
  430. position: "top",
  431. distance: 10,
  432. fontSize: optionsSetup.fontSize,
  433. color: optionsSetup.dataColor,
  434. fontWeight: optionsSetup.fontWeight,
  435. };
  436. obj.data = val.series[i].data;
  437. series.push(obj);
  438. }
  439. }
  440. this.options.series = series;
  441. this.options.legend["data"] = legendName;
  442. this.setOptionsLegendName(legendName);
  443. },
  444. },
  445. };
  446. </script>
  447. <style scoped lang="scss">
  448. .echarts {
  449. width: 100%;
  450. height: 100%;
  451. overflow: hidden;
  452. }
  453. </style>