multiple-y-line-chart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "multiple-y-line-chart",
  10. componentName: "multiple-y-line-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "13.889vh",
  19. },
  20. // 数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. title: "机舱震动x方向",
  26. yAxisIndex: 0,
  27. value: [
  28. ["2021/07/01 00:00:00", 68],
  29. ["2021/07/01 05:00:00", 74],
  30. ["2021/07/01 10:00:00", 69],
  31. ["2021/07/01 15:00:00", 134],
  32. ["2021/07/01 20:00:00", 262],
  33. ],
  34. },
  35. {
  36. title: "机舱震动y方向",
  37. yAxisIndex: 1,
  38. value: [
  39. ["2021/07/01 00:00:00", 110],
  40. ["2021/07/01 05:00:00", 175],
  41. ["2021/07/01 10:00:00", 290],
  42. ["2021/07/01 15:00:00", 140],
  43. ["2021/07/01 20:00:00", 203],
  44. ],
  45. },
  46. {
  47. title: "机舱震动最大偏移值",
  48. yAxisIndex: 2,
  49. value: [
  50. ["2021/07/01 00:00:00", 312],
  51. ["2021/07/01 05:00:00", 203],
  52. ["2021/07/01 10:00:00", 231],
  53. ["2021/07/01 15:00:00", 305],
  54. ["2021/07/01 20:00:00", 230],
  55. ],
  56. },
  57. {
  58. title: "风速1",
  59. yAxisIndex: 3,
  60. value: [
  61. ["2021/07/01 00:00:00", 277],
  62. ["2021/07/01 05:00:00", 311],
  63. ["2021/07/01 10:00:00", 296],
  64. ["2021/07/01 15:00:00", 87],
  65. ["2021/07/01 20:00:00", 273],
  66. ],
  67. },
  68. {
  69. title: "风速2",
  70. yAxisIndex: 4,
  71. value: [
  72. ["2021/07/01 00:00:00", 214],
  73. ["2021/07/01 05:00:00", 224],
  74. ["2021/07/01 10:00:00", 313],
  75. ["2021/07/01 15:00:00", 230],
  76. ["2021/07/01 20:00:00", 264],
  77. ],
  78. }
  79. ],
  80. },
  81. showLegend: {
  82. type: Boolean,
  83. default: false,
  84. },
  85. // 轴
  86. yAxises: {
  87. type: Array,
  88. default: () => [
  89. {
  90. name: '机舱震动x方向',
  91. min: 50,
  92. max: 370,
  93. unit: "",
  94. position: "left",
  95. },
  96. {
  97. name: '机舱震动y方向',
  98. min: 60,
  99. max: 320,
  100. unit: "",
  101. position: "right",
  102. },
  103. {
  104. name: '机舱震动最大偏移值',
  105. min: 90,
  106. max: 330,
  107. unit: "",
  108. position: "left",
  109. },
  110. {
  111. name: '风速1',
  112. min: 30,
  113. max: 350,
  114. unit: "",
  115. position: "right",
  116. },
  117. {
  118. name: '风速2',
  119. min: 100,
  120. max: 360,
  121. unit: "",
  122. position: "left",
  123. },
  124. ],
  125. },
  126. // 标线
  127. markLine: {
  128. type: Array,
  129. default: () => [
  130. {
  131. yAxisIndex: 0,
  132. data: ["2021/07/01 10:00:00", "2021/07/01 20:00:00", "2021/07/05 15:00:00", "2021/07/15 15:00:00"]
  133. }
  134. ]
  135. }
  136. },
  137. data() {
  138. return {
  139. id: "",
  140. chart: null,
  141. color: ["#323E6F", "#1DA0D7", "#05BB4C", "#EDB32F", "#DB5520"],
  142. };
  143. },
  144. computed: {
  145. legend() {
  146. return this.list.map((t) => {
  147. return t.title;
  148. });
  149. },
  150. xdata() {
  151. return this.list[0].value.map((t) => {
  152. return t.text;
  153. });
  154. },
  155. yAxis() {
  156. let themeName = this.$store.state.themeName.split(' ')[1];
  157. let result = [];
  158. let p = {left: 0, right: 0};
  159. this.yAxises.forEach((item, index) => {
  160. result.push({
  161. type: "value",
  162. name: `${item.name}${item.unit}`,
  163. nameLocation: p[item.position] % 2 == 0 ? "end" : "start",
  164. min: item.min,
  165. max: item.max,
  166. position: item.position,
  167. offset: p[item.position] * 60,
  168. axisLabel: {
  169. formatter: "{value}",
  170. fontSize: util.vh(14),
  171. },
  172. //分格线
  173. splitLine: {
  174. lineStyle: {
  175. color: themeName === "dark"? partten.getColor("gray"): "#000",
  176. type: "dashed",
  177. },
  178. },
  179. axisLine: {
  180. show: true,
  181. },
  182. axisTick: {
  183. show: true
  184. }
  185. });
  186. p[item.position]++;
  187. });
  188. return result;
  189. },
  190. series() {
  191. let result = [];
  192. this.list.forEach((value, index) => {
  193. let ser = {
  194. name: value.title,
  195. type: "line",
  196. smooth: true,
  197. zlevel: index,
  198. lineStyle: {
  199. normal: {
  200. color: this.color[index],
  201. width: 1,
  202. },
  203. },
  204. yAxisIndex: value.yAxisIndex,
  205. // data: value.value.map((t) => {
  206. // return t.value;
  207. // }),
  208. data: value.value,
  209. };
  210. let iMarkLine = this.markLine.find(t => t.yAxisIndex == index);
  211. if (iMarkLine) {
  212. ser.markLine = {
  213. silent: true,
  214. data: iMarkLine.data.map(t => {
  215. return { xAxis: t }
  216. }),
  217. lineStyle: {
  218. color: '#ff0000',
  219. },
  220. label: {
  221. color: 'rgba(204,187,225,0.5)'
  222. }
  223. }
  224. }
  225. result.push(ser);
  226. });
  227. return result;
  228. },
  229. },
  230. methods: {
  231. resize() {},
  232. initChart() {
  233. let themeName = this.$store.state.themeName.split(' ')[1];
  234. const chart = echarts.init(this.$el);
  235. let option = this.option(themeName);
  236. console.log(option)
  237. chart.clear();
  238. chart.setOption(option);
  239. this.resize = function() {
  240. chart.resize();
  241. };
  242. window.addEventListener("resize", this.resize);
  243. },
  244. option: function (themeName) {
  245. return {
  246. color: this.color,
  247. tooltip: {
  248. trigger: "axis",
  249. backgroundColor: themeName === "dark"? "rgba(0,0,0,0.4)": "rgba(255,255,255,0.5)",
  250. borderColor: themeName === "dark"? partten.getColor("gray"): "#000",
  251. textStyle: {
  252. color: themeName === "dark" ? "#fff" : "#000",
  253. fontSize: 14,
  254. },
  255. },
  256. legend: {
  257. show: this.showLegend,
  258. data: this.legend,
  259. right: 56,
  260. icon: "circle",
  261. itemWidth: 6,
  262. inactiveColor: themeName === "dark"? partten.getColor("gray"): "#000",
  263. textStyle: {
  264. color: themeName === "dark"? partten.getColor("grayl"): "#000",
  265. fontSize: 12,
  266. },
  267. },
  268. dataZoom: [{
  269. show: true,
  270. height: 30,
  271. xAxisIndex: [0],
  272. bottom: 30,
  273. "start": 0,
  274. "end": 5,
  275. handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
  276. handleSize: '110%',
  277. handleStyle: {
  278. color: themeName === "dark"?"#05bb4c": partten.getColor("deepblue"),
  279. },
  280. textStyle:{
  281. color: themeName === "dark"? "rgba(204,187,225,0.5)": "#000",
  282. },
  283. fillerColor:themeName === "dark"?"rgba(5,187,76,0.4)":"rgba(54, 52, 142,0.4)",
  284. borderColor:themeName === "dark"?"rgba(5,187,76,0.5)":"rgba(54, 52, 142,0.5)",
  285. }, {
  286. type: "inside",
  287. show: true,
  288. height: 15,
  289. start: 1,
  290. end: 35
  291. }],
  292. grid: {
  293. top: util.vh(80),
  294. left: util.vh(80),
  295. right: util.vh(80),
  296. bottom: util.vh(64),
  297. containLabel: true,
  298. },
  299. xAxis: [
  300. {
  301. type: "time",
  302. boundaryGap: false,
  303. axisLabel: {
  304. // formatter: "{value}",
  305. fontSize: util.vh(14),
  306. textStyle: {
  307. color: themeName === "dark"? partten.getColor("gray"): "#000",
  308. },
  309. },
  310. // data: this.xdata,
  311. },
  312. ],
  313. yAxis: this.yAxis,
  314. series: this.series,
  315. };
  316. },
  317. datazoom: function (start, end) {
  318. const chart = echarts.getInstanceByDom(this.$el);
  319. chart.dispatchAction({
  320. type: 'dataZoom',
  321. start: start,
  322. end: end,
  323. });
  324. },
  325. reload: function () {
  326. const chart = echarts.getInstanceByDom(this.$el);
  327. chart.clear();
  328. let option = this.option();
  329. chart.setOption(option);
  330. },
  331. },
  332. created() {
  333. this.id = "pie-chart-" + util.newGUID();
  334. },
  335. mounted() {
  336. this.$nextTick(() => {
  337. this.$el.style.width = this.width;
  338. this.$el.style.height = this.height;
  339. this.initChart();
  340. });
  341. },
  342. updated() {
  343. this.$nextTick(() => {
  344. this.initChart();
  345. });
  346. },
  347. unmounted() {
  348. window.removeEventListener("resize", this.resize);
  349. },
  350. watch: {
  351. "$store.state.themeName"() {
  352. this.initChart();
  353. },
  354. },
  355. };
  356. </script>
  357. <style lang="less">
  358. .chart {
  359. width: 100%;
  360. height: 100%;
  361. display: inline-block;
  362. }
  363. </style>