lineCharts1.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="countryLineChart" ref="countryLineChart"></div>
  3. </template>
  4. <script>
  5. // import * as ethis.charts from "ethis.charts";
  6. import config from "@/config/index";
  7. import * as echarts from "echarts/core";
  8. import { LineChart } from "echarts/charts";
  9. import {
  10. TitleComponent,
  11. TooltipComponent,
  12. LegendComponent,
  13. DataZoomComponent,
  14. GridComponent,
  15. } from "echarts/components";
  16. import { CanvasRenderer } from "echarts/renderers";
  17. echarts.use([
  18. TitleComponent,
  19. TooltipComponent,
  20. GridComponent,
  21. LineChart,
  22. CanvasRenderer,
  23. LegendComponent,
  24. DataZoomComponent,
  25. ]);
  26. let lineTypeThreeCharts = undefined;
  27. export default {
  28. name: "countryLineChart",
  29. props: ["list", "keys", "xName"],
  30. data: function() {
  31. return {
  32. leftList: [],
  33. rightList: [],
  34. };
  35. },
  36. filters: {},
  37. methods: {
  38. format(v) {
  39. if (isNaN(v)) return 0;
  40. let o = v.toFixed(4);
  41. if (v > 10000) {
  42. o = (v / 10000).toFixed(4) + "万";
  43. }
  44. return o;
  45. },
  46. formatte(list) {
  47. let values = [],
  48. k0 = this.keys[0],
  49. keys = (list[0] || []).map(v => v[this.xName]);
  50. for (let o = 0; o < list.length; o++) {
  51. const v = list[o];
  52. values.push(v.map(item => item[k0.key]));
  53. }
  54. return {
  55. keys,
  56. values,
  57. };
  58. },
  59. getTvName(tvId) {
  60. let li = config.channelNameList || [];
  61. let t = "";
  62. for (let i = 0; i < li.length; i++) {
  63. const v = li[i];
  64. if (v.value !== tvId) continue;
  65. t = v.label;
  66. break;
  67. }
  68. return t;
  69. },
  70. createChart() {
  71. if (!this.$refs.countryLineChart) return;
  72. lineTypeThreeCharts = echarts.init(this.$refs.countryLineChart);
  73. let chartData = this.formatte(this.list);
  74. lineTypeThreeCharts.resize({
  75. height: (this.$refs.countryLineChart.offsetWidth * 6) / 16,
  76. });
  77. var option = {
  78. tooltip: {
  79. trigger: "axis",
  80. confine: true,
  81. triggerOn: "mousemove",
  82. axisPointer: {
  83. type: "line",
  84. axis: "x",
  85. },
  86. formatter: params => {
  87. let out = "";
  88. let t = "";
  89. for (let i = 0; i < params.length; i++) {
  90. const par = params[i];
  91. if (t === "") {
  92. t = par.axisValueLabel;
  93. t += "<br />";
  94. }
  95. out +=
  96. par.seriesName + ": " + this.format(par.value) + "% <br />";
  97. }
  98. return t + out;
  99. },
  100. },
  101. legend: {
  102. data: this.keys.map(v => v.name),
  103. },
  104. grid: { left: 150, top: "10%", buttom: 0, right: 100 },
  105. xAxis: {
  106. type: "category",
  107. boundaryGap: true,
  108. data: chartData.keys,
  109. },
  110. yAxis: {
  111. type: "value",
  112. scale: true,
  113. },
  114. dataZoom: [
  115. {
  116. show: true,
  117. realtime: true,
  118. start: 30,
  119. end: 70,
  120. xAxisIndex: [0, 1],
  121. },
  122. {
  123. type: "inside",
  124. realtime: true,
  125. start: 30,
  126. end: 70,
  127. xAxisIndex: [0, 1],
  128. },
  129. ],
  130. series: chartData.values.map((v, i) => {
  131. return {
  132. type: "line",
  133. name: this.keys[i].name,
  134. data: v,
  135. showAllSymbol: false,
  136. smooth: true,
  137. };
  138. }),
  139. };
  140. option && lineTypeThreeCharts.setOption(option);
  141. },
  142. },
  143. watch: {
  144. list() {
  145. if (lineTypeThreeCharts) lineTypeThreeCharts.dispose();
  146. this.createChart();
  147. },
  148. },
  149. mounted() {
  150. lineTypeThreeCharts && lineTypeThreeCharts.dispose();
  151. this.createChart();
  152. },
  153. beforeUnmount: function() {
  154. lineTypeThreeCharts && lineTypeThreeCharts.dispose();
  155. },
  156. components: {},
  157. };
  158. </script>
  159. <style></style>