ChartLine.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <script>
  2. import * as echarts from 'echarts'
  3. import { echartColors } from '@/utils/index'
  4. import merge from 'lodash.merge'
  5. export default {
  6. // 折线图
  7. name: 'ChartLine',
  8. components: {},
  9. props: {
  10. // 折线背景渐变色
  11. bgLinearGradient: {
  12. type: Boolean,
  13. default: false,
  14. },
  15. // X轴坐标数据
  16. xData: {
  17. type: Array,
  18. default: () => ['2021-12-01', '2021-12-02', '2021-12-03', '2021-12-03', '2021-12-04', '2021-12-05', '2021-12-06'],
  19. },
  20. // 图例; 单个图例可传字符串,实际转换成单元素数组[string]
  21. legend: {
  22. type: [Array, String],
  23. default: () => ['新增客户'],
  24. },
  25. // 展示数据集
  26. // 注:二位数组, 其一维数组顺序需要和图例顺序一致,其二维数组顺序需要和X轴数组顺序一致
  27. // 单条数据集:[120, 132, 101, 134, 90, 230, 210],其实会被转换成[[120, 132, 101, 134, 90, 230, 210]]
  28. // 多条数据集:
  29. // [
  30. // [120, 132, 101, 134, 90, 230, 210],
  31. // [220, 182, 191, 234, 290, 330, 310],
  32. // [150, 232, 201, 154, 190, 330, 410],
  33. // [320, 332, 301, 334, 390, 330, 320]
  34. // ]
  35. series: {
  36. type: Array,
  37. default: () => [120, 132, 101, 134, 90, 230, 210],
  38. },
  39. // 自定义图表配置项,使用loadsh.merge(origin, option)和原有的配置进行覆盖合并
  40. // loadsh.merge: https://www.html.cn/doc/lodash/#_mergeobject-sources
  41. option: {
  42. type: Object,
  43. default: null,
  44. },
  45. },
  46. data() {
  47. return {}
  48. },
  49. computed: {},
  50. watch: {
  51. series() {
  52. this.$nextTick(() => {
  53. this.drawChart()
  54. })
  55. },
  56. },
  57. created() {},
  58. mounted() {
  59. this.drawChart()
  60. },
  61. methods: {
  62. drawChart() {
  63. if (!(this.series && this.series.length)) {
  64. return
  65. }
  66. // eslint-disable-next-line
  67. this.myChart = echarts.init(this.$refs.chart)
  68. let option
  69. let series = []
  70. let legend = Array.isArray(this.legend) ? this.legend : [this.legend]
  71. let seriesData = this.series
  72. Array.isArray(this.series[0]) || (seriesData = [seriesData])
  73. seriesData.forEach((data, index) => {
  74. let obj = {
  75. name: legend[index],
  76. type: 'line',
  77. smooth: true,
  78. symbol: 'circle', // 数值点类型
  79. symbolSize: 6, // 数值点大小
  80. emphasis: {
  81. focus: 'series',
  82. },
  83. data,
  84. }
  85. this.bgLinearGradient &&
  86. Object.assign(obj, {
  87. areaStyle: {
  88. color: {
  89. type: 'linear',
  90. x: 0,
  91. y: 0,
  92. x2: 0,
  93. y2: 1,
  94. colorStops: [
  95. {
  96. offset: 0,
  97. color: echartColors[index], // 0% 处的颜色
  98. },
  99. {
  100. offset: 0.8,
  101. color: 'white', // 100% 处的颜色
  102. },
  103. ],
  104. global: false, // 缺省为 false
  105. },
  106. },
  107. })
  108. series.push(obj)
  109. })
  110. option = {
  111. color: echartColors,
  112. // title: {
  113. // text: 'Stacked Area Chart'
  114. // },
  115. tooltip: {
  116. trigger: 'axis',
  117. // textStyle: {
  118. // color: '#FFF', // 设置文字颜色
  119. // fontSize: 12,
  120. // },
  121. // backgroundColor: '#505050',
  122. // borderWidth: 0,
  123. // axisPointer: {
  124. // type: 'cross',
  125. // label: {
  126. // backgroundColor: '#6a7985',
  127. // },
  128. // },
  129. },
  130. legend: {
  131. data: legend,
  132. // lineStyle: {
  133. // width: 0
  134. // },
  135. // itemStyle: {
  136. // borderWidth: 10,
  137. // decal: {
  138. // symbol: 'rect'
  139. // }
  140. // }
  141. x: 'center',
  142. y: 'bottom',
  143. // selectedMode: false,
  144. type: 'scroll', // 分页类型
  145. icon: 'roundRect',
  146. itemWidth: 24,
  147. itemHeight: 4,
  148. itemGap: 14,
  149. tooltip: {
  150. show: true,
  151. },
  152. textStyle: {
  153. color: '#36363A',
  154. fontSize: 12,
  155. lineHeight: 14,
  156. },
  157. },
  158. grid: {
  159. left: '3%',
  160. right: '3%',
  161. bottom: '40px',
  162. top: '30px',
  163. containLabel: true,
  164. },
  165. xAxis: [
  166. {
  167. type: 'category',
  168. data: this.xData,
  169. // boundaryGap: false,
  170. offset: 5,
  171. axisLine: {
  172. lineStyle: {
  173. color: '#ccc',
  174. },
  175. },
  176. axisLabel: {
  177. // 坐标轴刻度标签的相关设置。
  178. // interval: 0,
  179. color: 'rgba(153, 153, 153, 1)', // 坐标字体颜色
  180. },
  181. axisTick: {
  182. // 横坐标刻度
  183. alignWithLabel: true,
  184. show: true,
  185. },
  186. },
  187. ],
  188. yAxis: [
  189. {
  190. type: 'value',
  191. axisLine: {
  192. lineStyle: {
  193. color: '#ccc',
  194. },
  195. show: false,
  196. },
  197. axisLabel: {
  198. // 坐标轴刻度标签的相关设置。
  199. // interval: 0,
  200. color: 'rgba(153, 153, 153, 1)', // 坐标字体颜色
  201. },
  202. },
  203. ],
  204. series,
  205. }
  206. this.option && merge(option, this.option)
  207. option && this.myChart.setOption(option)
  208. new ResizeObserver((entries) => {
  209. this.myChart.resize()
  210. }).observe(this.$refs.chart)
  211. },
  212. },
  213. }
  214. </script>
  215. <template>
  216. <div v-if="series && series.length" ref="chart" class="chart-line chart" key="1"></div>
  217. <div v-else class="chart-line chart" key="2"><div class="cc">暂无数据</div></div>
  218. </template>
  219. <style lang="scss" scoped>
  220. .chart {
  221. position: relative;
  222. height: 100%;
  223. min-width: 400px;
  224. min-height: 400px;
  225. }
  226. </style>