LineChart.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div id="lineChart" ref="line"></div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "LineChart",
  7. data() {
  8. return {
  9. lineChart: null
  10. };
  11. },
  12. props: {
  13. categoryData: {
  14. type: Array,
  15. default() {
  16. return [];
  17. }
  18. },
  19. valueData: {
  20. type: Array,
  21. default() {
  22. return [];
  23. }
  24. },
  25. unit: {
  26. type: String,
  27. default() {
  28. return "";
  29. }
  30. },
  31. title: {
  32. type: String,
  33. default() {
  34. return "";
  35. }
  36. }
  37. },
  38. watch: {
  39. valueData: {
  40. handler(val) {
  41. this.$nextTick(() => {
  42. if (this.valueData) {
  43. this.initEchart(this.categoryData, this.valueData);
  44. }
  45. });
  46. },
  47. deep: true,
  48. immediate: true
  49. }
  50. },
  51. mounted() {
  52. this.$nextTick(() => {
  53. if (this.valueData) {
  54. this.initEchart(this.categoryData, this.valueData);
  55. }
  56. });
  57. },
  58. methods: {
  59. initEchart(xData, seriesData) {
  60. if (!this.lineChart) {
  61. this.lineChart = this.$Echarts.init(this.$refs.line);
  62. }
  63. let unit = this.unit;
  64. let title = this.title;
  65. let dataMin = Math.min(...seriesData);
  66. let dataMax = Math.max(...seriesData);
  67. let homeSpecialTown = this.$store.state.homeSpecialTown;
  68. let option = {
  69. tooltip: {
  70. show: true,
  71. trigger: "axis",
  72. formatter: function (params) {
  73. return (
  74. params[0].marker +
  75. params[0].name +
  76. "年" +
  77. "(" +
  78. homeSpecialTown +
  79. ")" +
  80. "<br />" +
  81. title +
  82. ":" +
  83. (unit == "条" || unit == "个" ? parseFloat(params[0].data).toFixed(0) : parseFloat(params[0].data).toFixed(2)) +
  84. unit
  85. );
  86. }
  87. },
  88. grid: {
  89. left: "3%",
  90. right: "4%",
  91. bottom: "3%",
  92. top: "20px",
  93. containLabel: true
  94. },
  95. xAxis: {
  96. type: "category",
  97. data: xData,
  98. axisTick: {
  99. show: false // 不显示坐标轴刻度线
  100. },
  101. axisLine: {
  102. show: true,
  103. lineStyle: {
  104. color: "rgba(219,225,255,1)",
  105. width: 1,
  106. type: "solid"
  107. }
  108. },
  109. axisLabel: {
  110. //x轴文字的配置
  111. show: true,
  112. color: "rgba(219,225,255,1)"
  113. }
  114. },
  115. yAxis: {
  116. min:
  117. dataMax === dataMin
  118. ? dataMax
  119. : parseFloat(dataMin - (dataMax - dataMin) / 2).toFixed(2) < 0
  120. ? 0
  121. : unit == "条" || unit == "个"
  122. ? dataMin
  123. : parseFloat(dataMin - (dataMax - dataMin) / 2).toFixed(2),
  124. max: unit == "条" || unit == "个" ? parseFloat(dataMax).toFixed(0) : parseFloat(dataMax).toFixed(2),
  125. interval:
  126. dataMax === dataMin
  127. ? parseFloat(dataMax).toFixed(0)
  128. : parseFloat(dataMax - dataMin).toFixed(2) < 0
  129. ? 0
  130. : unit == "条" || unit == "个"
  131. ? parseFloat(dataMax - dataMin).toFixed(0)
  132. : parseFloat(dataMax - dataMin).toFixed(2),
  133. type: "value",
  134. scale: true,
  135. splitLine: {
  136. show: false // 不显示网格线
  137. },
  138. axisLine: {
  139. show: false // 不显示坐标轴线
  140. },
  141. axisLabel: {
  142. //y轴文字的配置
  143. color: "rgba(219,225,255,1)",
  144. margin: 15
  145. }
  146. },
  147. series: [
  148. {
  149. data: seriesData,
  150. type: "line",
  151. smooth: true,
  152. itemStyle: {
  153. color: "#00aaff"
  154. },
  155. areaStyle: {
  156. color: new this.$Echarts.graphic.LinearGradient(0, 0, 0, 1, [
  157. {
  158. offset: 0,
  159. color: "#00aaff"
  160. },
  161. {
  162. offset: 1,
  163. color: "#00aaff00"
  164. }
  165. ])
  166. }
  167. }
  168. ]
  169. };
  170. this.lineChart.setOption(option);
  171. }
  172. }
  173. };
  174. </script>
  175. <style>
  176. #lineChart {
  177. width: 100%;
  178. height: 100%;
  179. }
  180. </style>