multiple-line-chart.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. import { right } from "@antv/x6/lib/registry/port-layout/main";
  9. export default {
  10. name: "multiple-line-chart",
  11. componentName: "multiple-line-chart",
  12. props: {
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. height: {
  18. type: String,
  19. default: "100%",
  20. },
  21. chartData: {
  22. type: Array,
  23. default: () => {},
  24. },
  25. // 单位
  26. units: {
  27. type: Array,
  28. default: () => ["MW", "m/s"],
  29. },
  30. // 颜色
  31. color: {
  32. type: Array,
  33. default: () => [
  34. "#323E6F",
  35. "#1DA0D7",
  36. "#02BB4C",
  37. "#DB5520",
  38. "#EDB32F",
  39. "#EDEB2F",
  40. ],
  41. },
  42. },
  43. data() {
  44. return {
  45. id: "",
  46. chart: null,
  47. chartName: [
  48. { name: "实际功率", code: "sjgl", color: "#4B55AE" },
  49. { name: "理论功率", code: "llgl", color: "#1C99FF" },
  50. { name: "预测功率", code: "ycgl", color: "#F8DE5B" },
  51. { name: "实际风速", code: "pjfs", color: "#05BB4C" },
  52. { name: "预测风速", code: "ycfs", color: "#FF8300" },
  53. ],
  54. };
  55. },
  56. computed: {
  57. series() {
  58. let series = [];
  59. if (this.chartData && this.chartData.xAxis.length) {
  60. for (let key in this.chartData) {
  61. this.chartName.forEach((ele) => {
  62. if (ele.code == key) {
  63. series.push({
  64. name: ele.name,
  65. type: "line",
  66. showAllSymbol: true,
  67. symbolSize: 0,
  68. smooth: true,
  69. lineStyle: {
  70. normal: {
  71. width: 1,
  72. color: ele.color,
  73. },
  74. // borderColor: "rgba(0,0,0,.4)",
  75. },
  76. itemStyle: {
  77. color: ele.color,
  78. },
  79. tooltip: {
  80. show: true,
  81. },
  82. data: this.chartData[key],
  83. });
  84. }
  85. });
  86. }
  87. }
  88. console.log(series);
  89. return series;
  90. },
  91. // lengend() {
  92. // let arr=[]
  93. // this.chartData.lengend.forEach((i) => {
  94. // if (this.chartName.some((item) => item.code == i)) {
  95. // i = item.name;
  96. // }
  97. // });
  98. // },
  99. },
  100. methods: {
  101. initChart() {
  102. // console.log(this.chartData);
  103. console.log(this.series);
  104. let chart = echarts.init(this.$el);
  105. //图表所需数据区
  106. var charts = {
  107. names: ["点击量", "访客"],
  108. lineX: [
  109. "2018-11-11 17:01",
  110. "2018-11-11 17:02",
  111. "2018-11-11 17:03",
  112. "2018-11-11 17:04",
  113. "2018-11-11 17:05",
  114. "2018-11-11 17:06",
  115. "2018-11-11 17:07",
  116. "2018-11-11 17:08",
  117. "2018-11-11 17:09",
  118. "2018-11-11 17:10",
  119. "2018-11-11 17:11",
  120. "2018-11-11 17:12",
  121. "2018-11-11 17:13",
  122. "2018-11-11 17:14",
  123. "2018-11-11 17:15",
  124. "2018-11-11 17:16",
  125. "2018-11-11 17:17",
  126. "2018-11-11 17:18",
  127. "2018-11-11 17:19",
  128. "2018-11-11 17:20",
  129. ],
  130. useNum: [
  131. 4.51, 3.52, 3.03, 5.34, 0.95, 2.36, 2.17, 3.28, 1.59, 1.51, 2.31,
  132. 1.92, 4.53, 5.24, 1.65, 2.36, 5.27, 3.28, 1.29, 5.3,
  133. ],
  134. visitorNum: [
  135. 360, 545, 80, 192, 330, 580, 192, 80, 250, 453, 352, 28, 625, 345, 65,
  136. 325, 468, 108, 253, 98,
  137. ],
  138. };
  139. var color = ["rgba(23, 255, 243", "rgba(255,100,97"];
  140. //y轴样式 arrIndex相关目标数组的index值,lineYData目标y轴值
  141. function getLineYSeries(arrIndex, lineYData) {
  142. var data = {
  143. name: charts.names[arrIndex],
  144. type: "line",
  145. yAxisIndex: arrIndex, //使用相对的y轴
  146. color: color[arrIndex] + ")",
  147. smooth: true,
  148. symbol: "circle",
  149. symbolSize: 5,
  150. data: lineYData,
  151. };
  152. return data;
  153. }
  154. var lineY = [getLineYSeries(0), getLineYSeries(1, charts.visitorNum)];
  155. //图表配置
  156. var option = {
  157. color: this.color,
  158. tooltip: {
  159. trigger: "axis",
  160. },
  161. legend: {
  162. data: this.chartName.map((item) => item.name),
  163. textStyle: {
  164. fontSize: 12,
  165. color: "#93989A",
  166. },
  167. top: "5%",
  168. right: "15%",
  169. },
  170. grid: {
  171. top: "22%",
  172. left: "4%",
  173. right: "4%",
  174. bottom: "12%",
  175. containLabel: true,
  176. },
  177. xAxis: {
  178. type: "category",
  179. boundaryGap: false,
  180. data: this.chartData?.xAxis || [],
  181. axisLabel: {
  182. textStyle: {
  183. color: "rgb(255,255,255)",
  184. },
  185. // formatter: function (params) {
  186. // return params.split(" ")[0] + "\n" + params.split(" ")[1];
  187. // },
  188. },
  189. splitLine: {
  190. show: false,
  191. },
  192. axisLine: {
  193. lineStyle: {
  194. color: "rgb(255,255,255)",
  195. },
  196. },
  197. },
  198. yAxis: [
  199. {
  200. name: "MW",
  201. type: "value",
  202. axisLabel: {
  203. formatter: "{value}",
  204. textStyle: {
  205. color: "rgb(255,255,255)",
  206. },
  207. },
  208. splitLine: {
  209. show: true,
  210. lineStyle: {
  211. color: "rgb(255,255,255,0.4)",
  212. type: "dashed",
  213. },
  214. },
  215. axisLine: {
  216. lineStyle: {
  217. color: "rgb(255,255,255)",
  218. },
  219. },
  220. },
  221. {
  222. name: "m/s",
  223. type: "value",
  224. axisLabel: {
  225. show: true,
  226. formatter: "{value}",
  227. textStyle: {
  228. color: "rgb(255,255,255)",
  229. },
  230. },
  231. splitLine: {
  232. show: false,
  233. },
  234. axisLine: {
  235. lineStyle: {
  236. color: "rgb(255,255,255)",
  237. },
  238. },
  239. },
  240. ],
  241. series: this.series || [],
  242. };
  243. chart.setOption(option);
  244. },
  245. },
  246. emits: {
  247. areaClick: null,
  248. },
  249. created() {
  250. this.id = "line-chart-" + util.newGUID();
  251. },
  252. mounted() {
  253. this.$nextTick(() => {
  254. this.$el.style.width = this.width;
  255. this.$el.style.height = this.height;
  256. this.initChart();
  257. });
  258. },
  259. updated() {
  260. this.$nextTick(() => {
  261. this.initChart();
  262. });
  263. },
  264. watch: {
  265. "$store.state.themeName"() {
  266. this.initChart();
  267. },
  268. },
  269. };
  270. </script>
  271. <style lang="less">
  272. .chart {
  273. width: 100%;
  274. height: 100%;
  275. display: inline-block;
  276. }
  277. </style>