multiple-line-chart.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. name: "multiple-line-chart",
  8. componentName: "multiple-line-chart",
  9. watch: {
  10. 'list': {
  11. deep: true,
  12. handler: function (json) {
  13. this.initChart()
  14. }
  15. }
  16. },
  17. props: {
  18. width: {
  19. type: String,
  20. default: "100%",
  21. },
  22. height: {
  23. type: String,
  24. default: "13.889vh",
  25. },
  26. // 数据
  27. list: {
  28. type: Array,
  29. default: () => [
  30. {
  31. title: "有功设定限值(MW)",
  32. yAxisIndex: 0,
  33. value: [],
  34. },
  35. {
  36. title: "实发有功(MW)",
  37. yAxisIndex: 0,
  38. value: [],
  39. },
  40. ],
  41. },
  42. colors: {
  43. type: Array,
  44. default: () => ["rgba(75, 85, 174, 1)", "rgba(05, 187, 76, 1)"]
  45. },
  46. // 单位
  47. units: {
  48. type: Array,
  49. default: () => ["(MW)", "(风速)"],
  50. },
  51. showLegend: {
  52. type: Boolean,
  53. default: false,
  54. },
  55. hoverType: {
  56. type: String,
  57. default: "item",
  58. },
  59. },
  60. data() {
  61. return {
  62. id: "",
  63. chart: null,
  64. // colors: ["#ffffff", "#ffffff"],
  65. };
  66. },
  67. computed: {
  68. datas() {
  69. return this.list.map((t) => {
  70. return t.value;
  71. });
  72. },
  73. legend() {
  74. return this.list.map((t) => {
  75. return t.title;
  76. });
  77. },
  78. xdata() {
  79. if (this.list.length > 0)
  80. return this.list[0].value.map((t) => {
  81. return t.text;
  82. });
  83. return [];
  84. },
  85. series() {
  86. let result = [];
  87. this.list.forEach((value, index) => {
  88. result.push({
  89. name: value.title,
  90. type: "line",
  91. smooth: true,
  92. showSymbol: false,
  93. zlevel: index,
  94. lineStyle: {
  95. normal: {
  96. color: this.colors[index],
  97. width: 1,
  98. },
  99. },
  100. itemStyle: {
  101. normal: {
  102. color: this.colors[index],
  103. },
  104. },
  105. tooltip: {
  106. show: true,
  107. position: [10, 10],
  108. },
  109. yAxisIndex: value.yAxisIndex,
  110. data: value.value.map((t) => {
  111. return t.value;
  112. }),
  113. });
  114. });
  115. return result;
  116. },
  117. yAxis() {
  118. let result = [];
  119. this.units.forEach((value, index) => {
  120. result.push({
  121. type: "value",
  122. name: value,
  123. axisLabel: {
  124. formatter: "{value}",
  125. fontSize: 14,
  126. },
  127. axisLine: {
  128. show: false,
  129. },
  130. //分格线
  131. splitLine: {
  132. show: index == 0,
  133. lineStyle: {
  134. color: '#606769',
  135. type: "dashed",
  136. },
  137. },
  138. });
  139. });
  140. return result;
  141. },
  142. },
  143. methods: {
  144. hexToRgba(hex, opacity) {
  145. let rgbaColor = "";
  146. let reg = /^#[\da-f]{6}$/i;
  147. if (reg.test(hex)) {
  148. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  149. }
  150. return rgbaColor;
  151. },
  152. resize() { },
  153. initChart() {
  154. let chart = echarts.init(this.$el);
  155. let option = {
  156. color: this.colors,
  157. tooltip: {
  158. trigger: this.hoverType,
  159. axisPointer:
  160. this.hoverType != "item"
  161. ? {
  162. type: "cross",
  163. }
  164. : {},
  165. backgroundColor: "rgba(0,0,0,0.4)",
  166. borderColor: '#606769',
  167. textStyle: {
  168. fontSize: 16,
  169. color: "#4B55AE",
  170. },
  171. },
  172. legend: {
  173. show: this.showLegend,
  174. data: this.legend,
  175. right: 56,
  176. icon: "circle",
  177. itemWidth: 6,
  178. inactiveColor: '#606769',
  179. textStyle: {
  180. color: '#B3BDC0',
  181. fontSize: 12,
  182. },
  183. },
  184. grid: {
  185. top: 32,
  186. left: 40,
  187. right: 40,
  188. bottom: 24,
  189. },
  190. xAxis: [
  191. {
  192. type: "category",
  193. boundaryGap: false,
  194. axisLabel: {
  195. interval: 60,
  196. showMinLabel: true,
  197. showMaxLabel: true,
  198. formatter: "{value}",
  199. fontSize: 14,
  200. textStyle: {
  201. color: '#606769',
  202. },
  203. },
  204. axisLine: {
  205. show: false,
  206. },
  207. data: this.xdata,
  208. },
  209. ],
  210. yAxis: this.yAxis,
  211. series: this.series,
  212. // series: [
  213. // {
  214. // name: this.list[0].title,
  215. // type: "line",
  216. // smooth: true,
  217. // showSymbol: false,
  218. // zlevel: 0,
  219. // lineStyle: {
  220. // normal: {
  221. // color: "#4B55AE",
  222. // width: 1,
  223. // },
  224. // },
  225. // tooltip: {
  226. // show: true,
  227. // position: [10, 10],
  228. // },
  229. // yAxisIndex: this.list[0].yAxisIndex,
  230. // data: this.list[0].value.map((t) => {
  231. // return t.value;
  232. // }),
  233. // },
  234. // {
  235. // name: this.list[1].title,
  236. // type: "line",
  237. // smooth: true,
  238. // showSymbol: false,
  239. // zlevel: 1,
  240. // lineStyle: {
  241. // normal: {
  242. // color: "#05BB4C",
  243. // width: 1,
  244. // },
  245. // },
  246. // tooltip: {
  247. // show: true,
  248. // position: [10, 10],
  249. // },
  250. // yAxisIndex: this.list[1].yAxisIndex,
  251. // data: this.list[1].value.map((t) => {
  252. // return t.value;
  253. // }),
  254. // }
  255. // ],
  256. };
  257. chart.clear();
  258. chart && option && chart.setOption(option);
  259. this.resize = function () {
  260. chart.resize();
  261. };
  262. window.addEventListener("resize", this.resize);
  263. chart.resize();
  264. },
  265. },
  266. created() {
  267. this.$nextTick(() => {
  268. this.id = "pie-chart-photo";
  269. });
  270. },
  271. mounted() {
  272. this.$nextTick(() => {
  273. this.$el.style.width = this.width;
  274. this.$el.style.height = this.height;
  275. this.initChart();
  276. });
  277. },
  278. updated() {
  279. this.$nextTick(() => {
  280. this.initChart();
  281. });
  282. },
  283. unmounted() {
  284. window.removeEventListener("resize", this.resize);
  285. },
  286. };
  287. </script>
  288. <style lang="less">
  289. .chart {
  290. width: 100%;
  291. height: 100%;
  292. display: inline-block;
  293. }
  294. </style>