当前位置:   article > 正文

ECharts折线图

echarts折线图

1.小节概述

    折线图是用折线将各个数据点标志连接起来的图表,用于展现数据的变化趋势。折线图可以显示随时间而变化的连续数据,因此非常适用于显示在相等时间间隔下数据的趋势。在ECharts中,柱状图和折线图的配置有许多相似之处,例如x轴和y轴的设置,标记最值和平均值等场景基本雷同,至于图例(legend)、图表离容器的距离样式(grid)、提示框(tooltip)等设置基本适用于所有图表类型。由于这些内容在柱状图实现中已经介绍过了,所以本小节在这些方面不做过多赘述。在本小节中,主要记录ECharts折线图的基础设置,结合实际业务场景实现多折线图、阶梯折线图、双轴折线图、动态折线图等图表展现。

2.操作步骤

2.1.基础设置

1.准备工作:(1)优化项目主页布局,由于后续要了解很多图表类型,所以主页只放文字链接,点击打开相应的图表路由页面。(2)针对各个图表组件,公共样式不需要每个组件都设置一遍,可以在页面上设置好,允许样式穿透(页面中去除scoped属性)。

主页HomeView.vue中,存放文字链接,点击跳转对应的路由页面,路由配置(router下的index.js)。

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import HomeView from '../views/HomeView.vue'
  4. import ChartBar from '../views/ChartBar.vue'
  5. import ChartLine from '../views/ChartLine.vue'
  6. Vue.use(VueRouter)
  7. const routes = [
  8. {
  9. path: '/',
  10. name: 'home',
  11. component: HomeView
  12. },
  13. {
  14. path: '/chartBar',
  15. name: 'ChartBar',
  16. component: ChartBar
  17. },
  18. {
  19. path: '/chartLine',
  20. name: 'ChartLine',
  21. component: ChartLine
  22. },
  23. ]
  24. const router = new VueRouter({
  25. mode: 'history',
  26. base: process.env.BASE_URL,
  27. routes
  28. })
  29. export default router

 

2.假设要展示的上半年各个月份的产品销售,那么可以用折线图来展示。修改前端前需要再加一个后端接口(RAP接口管理平台中新建接口提供上半年各月销量),然后复制出一个的图表组件ChartLineOne.vue放在ChartLine页图表1的位置上进行展示。参考代码:

  1. <template>
  2. <div class="mainDiv">
  3. <div class="titleDiv">上半年各月销量</div>
  4. <div class="chartDiv" id="chartLineOne"></div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. chartData: {},
  12. }
  13. },
  14. name: 'ChartLineOne',
  15. mounted() {
  16. this.getChartData().then(() => {
  17. // 数据请求到后渲染图表
  18. this.renderChart()
  19. })
  20. },
  21. methods: {
  22. // 请求数据回来
  23. async getChartData() {
  24. this.chartData = await this.$axios({ url: 'sales/month' })
  25. },
  26. // 渲染图表
  27. renderChart() {
  28. let myChart = this.$echarts.init(document.getElementById('chartLineOne'))
  29. let chartData = this.chartData
  30. var option = {
  31. tooltip: {
  32. show: true,
  33. },
  34. legend: {
  35. show: true,
  36. data: ['销量'],
  37. x: 'center',
  38. y: 'bottom',
  39. padding: [0, 0, 10, 0],
  40. textStyle: {
  41. fontStyle: 'normal',
  42. fontSize: 12,
  43. },
  44. },
  45. grid: {
  46. top: '8%',
  47. bottom: '10%',
  48. left: '4%',
  49. right: '8%',
  50. containLabel: true,
  51. },
  52. xAxis: {
  53. type: 'category',
  54. data: chartData.data.month,
  55. },
  56. yAxis: {
  57. type: 'value',
  58. },
  59. series: [
  60. {
  61. name: '销量',
  62. type: 'line',
  63. data: chartData.data.amount,
  64. },
  65. ],
  66. }
  67. // 使用刚指定的配置项和数据显示图表
  68. myChart.setOption(option)
  69. },
  70. },
  71. }
  72. </script>
  73. <style scoped>
  74. .mainDiv {
  75. border: 1px solid;
  76. height: 410px;
  77. border-radius: 5px;
  78. text-align: center;
  79. }
  80. .titleDiv {
  81. height: 30px;
  82. line-height: 30px;
  83. font-size: 12;
  84. }
  85. .chartDiv {
  86. height: 380px;
  87. }
  88. </style>

3.上图可以发现折线图不是从零开始的(默认折线从刻度中心开始),可以通过设置xAxis.boundaryGap参数让其从零开始。参考代码:

  1. xAxis: {
  2. type: 'category',
  3. boundaryGap: false,
  4. data: chartData.data.month,
  5. },

4.折线的线条过于刚直,能否设置为平滑折线,可以设置series.smooth参数实现。参考代码:

  1. series: [
  2. {
  3. name: '销量',
  4. type: 'line',
  5. data: chartData.data.amount,
  6. smooth: true,
  7. },
  8. ],

5.至此,基础设置相关介绍告一段落,启动项目,预览ChartLine页如下图:

2.2.多折线图

1.假设要展示的各季度产品销量包括三大产品的销售-牛奶、面包和饼干,那么可以用多折线图来展示。修改前端前需要再加一个后端接口(RAP接口管理平台中新建接口提供产品销量),然后复制出一个的图表组件ChartBarTwo.vue放在ChartLine页图表2的位置上进行展示。参考代码:

系列图标(legend.data.icon)、折线连接点形状(series.symbol)属性设置说明:ECharts中类似标记的图形支持3种:(1)系统默认提供的几种类型;(2)直接放图片链接;(3)设置图标矢量路径。建议使用(1)或者(3)的方式,不用担心因为缩放而产生锯齿或模糊,而且可以设置为任意颜色,路径图形会自适应调整为合适的大小。此处使用了(3)的方式,为面包、牛奶、饼干设置了对应的矢量路径。

  1. <template>
  2. <div class="mainDiv">
  3. <div class="titleDiv">各季度产品销量</div>
  4. <div class="chartDiv" id="chartLineTwo"></div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. chartData: {},
  12. }
  13. },
  14. name: 'ChartLineTwo',
  15. mounted() {
  16. this.getChartData().then(() => {
  17. // 数据请求到后渲染图表
  18. this.renderChart()
  19. })
  20. },
  21. methods: {
  22. // 请求数据回来
  23. async getChartData() {
  24. this.chartData = await this.$axios({ url: 'sales/quarter' })
  25. },
  26. // 渲染图表
  27. renderChart() {
  28. let myChart = this.$echarts.init(document.getElementById('chartLineTwo'))
  29. let chartData = this.chartData
  30. var option = {
  31. tooltip: {
  32. show: true,
  33. textStyle: {
  34. color: '#000000',
  35. fontStyle: 'normal',
  36. fontSize: 14,
  37. align: 'left',
  38. },
  39. formatter: function (param) {
  40. // 设置提示显示当季度所有产品销量信息
  41. let message =
  42. param.name +
  43. '<br />' +
  44. '面包:' +
  45. chartData.data.breadAmount[param.dataIndex] +
  46. '<br />' +
  47. '牛奶:' +
  48. chartData.data.milkAmount[param.dataIndex] +
  49. '<br />' +
  50. '饼干:' +
  51. chartData.data.cookieAmount[param.dataIndex]
  52. return message
  53. },
  54. },
  55. legend: {
  56. show: true,
  57. x: 'center',
  58. y: 'bottom',
  59. padding: [0, 0, 10, 0],
  60. data: [
  61. {
  62. name: '面包',
  63. icon: 'path://M66.19,738.03c2.39,41.18,33.76,75.58,75.05,80.8,42.68,5.4,106.69-1.05,197.53-43.32,0,0,416.31-181.69,540.54-378.14,0,0,49-82.43,51.96-152.97,1.24-29.57-13.29-57.64-37.94-74.36-52.25-35.45-176.24-91.82-392.54-38.99,0,0-343.32,67.7-430.61,448.07,0,0.02-10.13,53.16-3.99,158.91z M584.5,115.17s-3.06,154.95-44.02,169.16c0,0-23.56-131.75-60.42-147.99-0.01,0,58.84-16.35,104.44-21.17z M183.48,336.63s83.05,140.79,70.01,185.83c0,0-86.83-101.91-126.95-97.72,0.01,0,27.18-52.68,56.94-88.11z M365.11,184.25s62.22,183.34,29.41,211.49c0,0-77.84-151.04-118.15-152.69,0-0.01,48.13-37.69,88.74-58.8z M141.64,495.33s-71.05,88.49-61.12,196.48,61.12-196.48,61.12-196.48z M909.55,184.25c3.61,69.01-305.31,450.06-618.91,611.35,0,0,342.16-141.32,497.43-294.66-0.01,0.01,229.55-205.04,121.48-316.69z',
  64. },
  65. {
  66. name: '牛奶',
  67. icon: 'path://M726.65,184a19.09,19.09,0,0,0-19.09-19.09H316.44A19.09,19.09,0,0,0,297.35,184v50.46a10,10,0,0,0,10,10h409.3a10,10,0,0,0,10-10zM257.9,754.85a10,10,0,0,0-10,10V840a19.09,19.09,0,0,0,19.1,19.13h490A19.09,19.09,0,0,0,776.1,840v-75a10,10,0,0,0-10.05-10zM723.42,267.33H300.58a10,10,0,0,0-9.32,6.37L258.48,358a10,10,0,0,0,9.32,13.63h488.4a10,10,0,0,0,9.32-13.63l-32.78-84.3a10,10,0,0,0-9.32-6.37zM616.75,624.24c-27-10.12-48.09,1.94-72.55,15.89-21.35,12.19-43.42,24.77-70.08,21-25.46-3.64-49.29-19.13-63.76-41.44s-18.29-48.72-10.9-74.58c5.42-19-6.85-31.72-20.15-37.33-16.81-7.07-40.64-4.74-53.15,17.16-13.23,23.13-29.36,51.31-64.44,50.54-0.8,0-1.82-0.08-2.94-0.18a10,10,0,0,0-10.88,10v128.43A18.21,18.21,0,0,0,266.11,732H646a18.22,18.22,0,0,0,18.15-16.61c4.31-48.27-11.06-77.52-47.4-91.15zM541.06,455c10.92,13.43,30.4,23.33,52.28,10.82,31.66-18.09,60.1-21.68,80-10.1,15.55,9,24.11,26.44,24.11,49.05,0,18.93,12.27,38.52,29.84,47.64,13.52,7,28.41,7.06,42.89,0.44a10,10,0,0,0,5.88-9.12V404.52a10,10,0,0,0-10-10H544.58a10.11,10.11,0,0,0-8.72,5.09c-11.38,19.68-6.29,41.24,5.2,55.39z M716.77,572.72c-25.24-13.11-42.19-40.42-42.19-68,0-10-2.2-23.15-12.7-29.24-12.41-7.21-33.26-3.49-57.18,10.18-27.83,15.91-60.56,9.39-81.41-16.26-12.86-15.8-20.37-39.21-14.79-62.58a9.92,9.92,0,0,0-9.57-12.33h-241a10,10,0,0,0-10,10V542.1a10,10,0,0,0,8.68,9.91c2,0.27,3.87,0.5,4.82,0.53,21,0.82,30.69-14.18,44.86-38.95,18.58-32.51,55.28-38.13,81.94-26.92,27.65,11.65,41,37.66,33.29,64.73a67.26,67.26,0,0,0,8.09,55.83c10.93,16.83,28.79,28.5,47.78,31.21s36.61-7.44,55.46-18.2,39.7-22.66,64.1-22.66a78.44,78.44,0,0,1,27.84,5.22c47.36,17.75,68.07,57.63,61.77,118.68a9.81,9.81,0,0,0,10,11l69.59-0.44a10,10,0,0,0,9.93-10V588.79a10,10,0,0,0-12.15-9.79,69.13,69.13,0,0,1-47.16-6.28z',
  68. },
  69. {
  70. name: '饼干',
  71. icon: 'path://M537.6,130.133333c-46.933333-4.266667-91.733333,0-132.266667,10.666667-14.933333,4.266667-21.333333,19.2-14.933333,34.133333,10.666667,23.466667,14.933333,51.2,12.8,81.066667-6.4,70.4-59.733333,130.133333-130.133333,142.933333-6.4,2.133333-12.8,2.133333-17.066667,2.133334-17.066667,2.133333-27.733333,19.2-19.2,34.133333,6.4,17.066667,10.666667,34.133333,10.666667,53.333333,0,57.6-38.4,106.666667-89.6,125.866667-12.8,4.266667-19.2,19.2-14.933334,32,51.2,145.066667,192,249.6,358.4,249.6,219.733333,0,396.8-183.466667,384-407.466667-12.8-187.733333-162.133333-341.333333-347.733333-358.4z M584.533333,379.733333m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M443.733333,640m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M704,635.733333m-61.866667,0a61.866667,61.866667,0,1,0,123.733334,0,61.866667,61.866667,0,1,0-123.733334,0Z',
  72. },
  73. ],
  74. },
  75. grid: {
  76. top: '8%',
  77. bottom: '10%',
  78. left: '4%',
  79. right: '6%',
  80. containLabel: true,
  81. },
  82. xAxis: {
  83. type: 'category',
  84. data: chartData.data.quarter,
  85. // 坐标轴两边留白策略(设置从0开始)
  86. boundaryGap: false,
  87. },
  88. yAxis: {
  89. type: 'value',
  90. axisLabel: {
  91. // 设置坐标轴文本和坐标轴的间距,以免第一个点的标签值和y轴的坐标轴文本有重叠
  92. margin: 15,
  93. },
  94. splitLine: {
  95. show: true,
  96. lineStyle: {
  97. color: ['#E5EAF3'],
  98. width: 1,
  99. type: 'dashed',
  100. },
  101. },
  102. },
  103. series: [
  104. {
  105. name: '面包',
  106. type: 'line',
  107. symbol: 'path://M66.19,738.03c2.39,41.18,33.76,75.58,75.05,80.8,42.68,5.4,106.69-1.05,197.53-43.32,0,0,416.31-181.69,540.54-378.14,0,0,49-82.43,51.96-152.97,1.24-29.57-13.29-57.64-37.94-74.36-52.25-35.45-176.24-91.82-392.54-38.99,0,0-343.32,67.7-430.61,448.07,0,0.02-10.13,53.16-3.99,158.91z M584.5,115.17s-3.06,154.95-44.02,169.16c0,0-23.56-131.75-60.42-147.99-0.01,0,58.84-16.35,104.44-21.17z M183.48,336.63s83.05,140.79,70.01,185.83c0,0-86.83-101.91-126.95-97.72,0.01,0,27.18-52.68,56.94-88.11z M365.11,184.25s62.22,183.34,29.41,211.49c0,0-77.84-151.04-118.15-152.69,0-0.01,48.13-37.69,88.74-58.8z M141.64,495.33s-71.05,88.49-61.12,196.48,61.12-196.48,61.12-196.48z M909.55,184.25c3.61,69.01-305.31,450.06-618.91,611.35,0,0,342.16-141.32,497.43-294.66-0.01,0.01,229.55-205.04,121.48-316.69z',
  108. symbolSize: 12,
  109. data: chartData.data.breadAmount,
  110. // 是否平滑曲线显示
  111. smooth: true,
  112. stack: '面包',
  113. itemStyle: {
  114. color: '#5470C6',
  115. },
  116. },
  117. {
  118. name: '牛奶',
  119. type: 'line',
  120. symbol: 'path://M726.65,184a19.09,19.09,0,0,0-19.09-19.09H316.44A19.09,19.09,0,0,0,297.35,184v50.46a10,10,0,0,0,10,10h409.3a10,10,0,0,0,10-10zM257.9,754.85a10,10,0,0,0-10,10V840a19.09,19.09,0,0,0,19.1,19.13h490A19.09,19.09,0,0,0,776.1,840v-75a10,10,0,0,0-10.05-10zM723.42,267.33H300.58a10,10,0,0,0-9.32,6.37L258.48,358a10,10,0,0,0,9.32,13.63h488.4a10,10,0,0,0,9.32-13.63l-32.78-84.3a10,10,0,0,0-9.32-6.37zM616.75,624.24c-27-10.12-48.09,1.94-72.55,15.89-21.35,12.19-43.42,24.77-70.08,21-25.46-3.64-49.29-19.13-63.76-41.44s-18.29-48.72-10.9-74.58c5.42-19-6.85-31.72-20.15-37.33-16.81-7.07-40.64-4.74-53.15,17.16-13.23,23.13-29.36,51.31-64.44,50.54-0.8,0-1.82-0.08-2.94-0.18a10,10,0,0,0-10.88,10v128.43A18.21,18.21,0,0,0,266.11,732H646a18.22,18.22,0,0,0,18.15-16.61c4.31-48.27-11.06-77.52-47.4-91.15zM541.06,455c10.92,13.43,30.4,23.33,52.28,10.82,31.66-18.09,60.1-21.68,80-10.1,15.55,9,24.11,26.44,24.11,49.05,0,18.93,12.27,38.52,29.84,47.64,13.52,7,28.41,7.06,42.89,0.44a10,10,0,0,0,5.88-9.12V404.52a10,10,0,0,0-10-10H544.58a10.11,10.11,0,0,0-8.72,5.09c-11.38,19.68-6.29,41.24,5.2,55.39z M716.77,572.72c-25.24-13.11-42.19-40.42-42.19-68,0-10-2.2-23.15-12.7-29.24-12.41-7.21-33.26-3.49-57.18,10.18-27.83,15.91-60.56,9.39-81.41-16.26-12.86-15.8-20.37-39.21-14.79-62.58a9.92,9.92,0,0,0-9.57-12.33h-241a10,10,0,0,0-10,10V542.1a10,10,0,0,0,8.68,9.91c2,0.27,3.87,0.5,4.82,0.53,21,0.82,30.69-14.18,44.86-38.95,18.58-32.51,55.28-38.13,81.94-26.92,27.65,11.65,41,37.66,33.29,64.73a67.26,67.26,0,0,0,8.09,55.83c10.93,16.83,28.79,28.5,47.78,31.21s36.61-7.44,55.46-18.2,39.7-22.66,64.1-22.66a78.44,78.44,0,0,1,27.84,5.22c47.36,17.75,68.07,57.63,61.77,118.68a9.81,9.81,0,0,0,10,11l69.59-0.44a10,10,0,0,0,9.93-10V588.79a10,10,0,0,0-12.15-9.79,69.13,69.13,0,0,1-47.16-6.28z',
  121. symbolSize: 12,
  122. data: chartData.data.milkAmount,
  123. smooth: true,
  124. stack: '牛奶',
  125. itemStyle: {
  126. color: '#91CC75',
  127. },
  128. },
  129. {
  130. name: '饼干',
  131. type: 'line',
  132. symbol: 'path://M537.6,130.133333c-46.933333-4.266667-91.733333,0-132.266667,10.666667-14.933333,4.266667-21.333333,19.2-14.933333,34.133333,10.666667,23.466667,14.933333,51.2,12.8,81.066667-6.4,70.4-59.733333,130.133333-130.133333,142.933333-6.4,2.133333-12.8,2.133333-17.066667,2.133334-17.066667,2.133333-27.733333,19.2-19.2,34.133333,6.4,17.066667,10.666667,34.133333,10.666667,53.333333,0,57.6-38.4,106.666667-89.6,125.866667-12.8,4.266667-19.2,19.2-14.933334,32,51.2,145.066667,192,249.6,358.4,249.6,219.733333,0,396.8-183.466667,384-407.466667-12.8-187.733333-162.133333-341.333333-347.733333-358.4z M584.533333,379.733333m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M443.733333,640m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M704,635.733333m-61.866667,0a61.866667,61.866667,0,1,0,123.733334,0,61.866667,61.866667,0,1,0-123.733334,0Z',
  133. symbolSize: 12,
  134. data: chartData.data.cookieAmount,
  135. smooth: true,
  136. stack: '饼干',
  137. itemStyle: {
  138. color: '#FE9F9F',
  139. },
  140. },
  141. ],
  142. }
  143. // 使用刚指定的配置项和数据显示图表
  144. myChart.setOption(option)
  145. },
  146. },
  147. }
  148. </script>
  149. <style scoped>
  150. .mainDiv {
  151. border: 1px solid;
  152. height: 410px;
  153. border-radius: 5px;
  154. text-align: center;
  155. }
  156. .titleDiv {
  157. height: 30px;
  158. line-height: 30px;
  159. font-size: 12;
  160. }
  161. .chartDiv {
  162. height: 380px;
  163. }
  164. </style>

2.至此,多折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:

2.3.阶梯折线图

1.阶梯线图又称方波图,它使用水平和垂直的线来连接两个数据点,而普通折线图则直接将两个点连接起来。阶梯线图能够很好地表达数据的突变。同样展示上述各季度产品销量(牛奶、面包和饼干),复制出一个的图表组件ChartBarThree.vue放在ChartLine页图表3的位置上进行展示。此处主要通过series.step属性来控制阶梯线图的连接类型,共有三种取值:start、middle和end,分别表示在当前点,当前点与下个点的中间点,下个点拐弯。参考代码:

  1. series: [
  2. {
  3. name: '面包',
  4. type: 'line',
  5. step: 'start',
  6. ......
  7. },
  8. {
  9. name: '牛奶',
  10. type: 'line',
  11. step: 'end',
  12. ......
  13. },
  14. {
  15. name: '饼干',
  16. type: 'line',
  17. step: 'middle',
  18. ......
  19. },
  20. ],

2.至此,阶梯折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:

2.4.双轴折线图

1.双轴折线图,一般适用于以下场景:(1)一个图表几个系列的数据单位不相同,相差较大。(2)希望y 轴可以设置左右两个维度,比如一个展示销量,一个展示毛利率。假设要展示的上半年各月产品销量和毛利率,那么可以用双轴折线图来展示。修改前端前需要再加一个后端接口(RAP接口管理平台中新建接口提供上半年各月产品销量毛利率数据),然后复制出一个的图表组件ChartLineFour.vue放在ChartLine页图表4的位置上进行展示。参考代码:

  1. <template>
  2. <div class="mainDiv">
  3. <div class="titleDiv">上半年各月销量和毛利率</div>
  4. <div class="chartDiv" id="chartLineFour"></div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. chartData: {},
  12. }
  13. },
  14. name: 'ChartLineFour',
  15. mounted() {
  16. this.getChartData().then(() => {
  17. // 数据请求到后渲染图表
  18. this.renderChart()
  19. })
  20. },
  21. methods: {
  22. // 请求数据回来
  23. async getChartData() {
  24. this.chartData = await this.$axios({ url: 'sales/month/amount' })
  25. },
  26. // 渲染图表
  27. renderChart() {
  28. let myChart = this.$echarts.init(document.getElementById('chartLineFour'))
  29. let chartData = this.chartData
  30. var option = {
  31. tooltip: {
  32. show: true,
  33. textStyle: {
  34. color: '#000000',
  35. fontStyle: 'normal',
  36. fontSize: 14,
  37. align: 'left',
  38. },
  39. formatter: function (param) {
  40. let message =
  41. param.name +
  42. '<br />' +
  43. '销售量:' +
  44. chartData.data.quantity[param.dataIndex] +
  45. '<br />' +
  46. '毛利率:' +
  47. chartData.data.rate[param.dataIndex] * 100 + '%'
  48. return message
  49. },
  50. },
  51. grid: {
  52. top: '8%',
  53. bottom: '8%',
  54. left: '6%',
  55. right: '6%',
  56. containLabel: true,
  57. },
  58. legend: {
  59. show: true,
  60. x: 'center',
  61. y: 'bottom',
  62. padding: [0, 0, 10, 0],
  63. },
  64. xAxis: {
  65. type: 'category',
  66. data: chartData.data.month,
  67. // 坐标轴两边留白策略(设置从0开始)
  68. boundaryGap: false,
  69. },
  70. yAxis: [
  71. {
  72. type: 'value',
  73. name: '销\n量\n︵\n件\n︶',
  74. nameLocation: 'center',
  75. nameRotate: 0,
  76. position: 'left',
  77. nameTextStyle: {
  78. fontSize: 12,
  79. padding: [0, 30, 0, 0],
  80. },
  81. },
  82. {
  83. type: 'value',
  84. name: '毛\n利\n率\n︵\n%\n︶',
  85. nameLocation: 'center',
  86. nameRotate: 0,
  87. position: 'right',
  88. nameTextStyle: {
  89. fontSize: 12,
  90. padding: [0, 0, 0, 15],
  91. },
  92. axisLabel: {
  93. formatter: function (param) {
  94. return param * 100
  95. },
  96. },
  97. },
  98. ],
  99. series: [
  100. {
  101. name: '销量',
  102. type: 'line',
  103. symbol: 'roundRect',
  104. symbolSize: 10,
  105. smooth: true,
  106. data: chartData.data.quantity,
  107. yAxisIndex: 0,
  108. },
  109. {
  110. name: '毛利率',
  111. type: 'line',
  112. symbol: '',
  113. symbolSize: 10,
  114. smooth: true,
  115. data: chartData.data.rate,
  116. yAxisIndex: 1,
  117. },
  118. ],
  119. }
  120. // 使用刚指定的配置项和数据显示图表
  121. myChart.setOption(option)
  122. },
  123. },
  124. }
  125. </script>
  126. <style scoped>
  127. .mainDiv {
  128. border: 1px solid;
  129. height: 410px;
  130. border-radius: 5px;
  131. text-align: center;
  132. }
  133. .titleDiv {
  134. height: 30px;
  135. line-height: 30px;
  136. font-size: 12;
  137. }
  138. .chartDiv {
  139. height: 380px;
  140. }
  141. </style>

2.至此,双轴折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:

2.5.动态折线图

1.ECharts动态折线图可以分为两类实现:一种为渲染静态数据,产生动画效果的折线图;另一种为动态渲染数据产生折线图(这点和上节中的动态排序柱状图实现原理类似)。我们可以利用第一种方式来实现上半年各月产品销量数据(面包、牛奶和饼干),主要是使用animationDuration动画属性以及series.endLabel末端标签显示属性,复制出一个的图表组件ChartLineFive.vue放在ChartLine页图表5的位置上进行展示。参考代码:

  1. <template>
  2. <div class="mainDiv">
  3. <div class="titleDiv">上半年各月产品销量</div>
  4. <div class="chartDiv" id="chartLineFive"></div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. chartData: {},
  12. }
  13. },
  14. name: 'ChartLineFive',
  15. mounted() {
  16. this.getChartData().then(() => {
  17. // 数据请求到后渲染图表
  18. this.renderChart()
  19. })
  20. },
  21. methods: {
  22. // 请求数据回来
  23. async getChartData() {
  24. this.chartData = await this.$axios({ url: 'sales/product/month' })
  25. },
  26. // 渲染图表
  27. renderChart() {
  28. let myChart = this.$echarts.init(document.getElementById('chartLineFive'))
  29. let chartData = this.chartData
  30. var option = {
  31. tooltip: {
  32. show: true,
  33. textStyle: {
  34. color: '#000000',
  35. fontStyle: 'normal',
  36. fontSize: 14,
  37. align: 'left',
  38. },
  39. formatter: function (param) {
  40. // 设置提示显示当季度所有产品销量信息
  41. let message =
  42. param.name +
  43. '<br />' +
  44. '面包:' +
  45. chartData.data.breadAmount[param.dataIndex] +
  46. '<br />' +
  47. '牛奶:' +
  48. chartData.data.milkAmount[param.dataIndex] +
  49. '<br />' +
  50. '饼干:' +
  51. chartData.data.cookieAmount[param.dataIndex]
  52. return message
  53. },
  54. },
  55. grid: {
  56. top: '8%',
  57. bottom: '8%',
  58. left: '4%',
  59. right: '4%',
  60. containLabel: true,
  61. },
  62. legend: {
  63. show: true,
  64. x: 'center',
  65. y: 'bottom',
  66. padding: [0, 0, 10, 0],
  67. data: [
  68. {
  69. name: '面包',
  70. icon: 'path://M66.19,738.03c2.39,41.18,33.76,75.58,75.05,80.8,42.68,5.4,106.69-1.05,197.53-43.32,0,0,416.31-181.69,540.54-378.14,0,0,49-82.43,51.96-152.97,1.24-29.57-13.29-57.64-37.94-74.36-52.25-35.45-176.24-91.82-392.54-38.99,0,0-343.32,67.7-430.61,448.07,0,0.02-10.13,53.16-3.99,158.91z M584.5,115.17s-3.06,154.95-44.02,169.16c0,0-23.56-131.75-60.42-147.99-0.01,0,58.84-16.35,104.44-21.17z M183.48,336.63s83.05,140.79,70.01,185.83c0,0-86.83-101.91-126.95-97.72,0.01,0,27.18-52.68,56.94-88.11z M365.11,184.25s62.22,183.34,29.41,211.49c0,0-77.84-151.04-118.15-152.69,0-0.01,48.13-37.69,88.74-58.8z M141.64,495.33s-71.05,88.49-61.12,196.48,61.12-196.48,61.12-196.48z M909.55,184.25c3.61,69.01-305.31,450.06-618.91,611.35,0,0,342.16-141.32,497.43-294.66-0.01,0.01,229.55-205.04,121.48-316.69z',
  71. },
  72. {
  73. name: '牛奶',
  74. icon: 'path://M726.65,184a19.09,19.09,0,0,0-19.09-19.09H316.44A19.09,19.09,0,0,0,297.35,184v50.46a10,10,0,0,0,10,10h409.3a10,10,0,0,0,10-10zM257.9,754.85a10,10,0,0,0-10,10V840a19.09,19.09,0,0,0,19.1,19.13h490A19.09,19.09,0,0,0,776.1,840v-75a10,10,0,0,0-10.05-10zM723.42,267.33H300.58a10,10,0,0,0-9.32,6.37L258.48,358a10,10,0,0,0,9.32,13.63h488.4a10,10,0,0,0,9.32-13.63l-32.78-84.3a10,10,0,0,0-9.32-6.37zM616.75,624.24c-27-10.12-48.09,1.94-72.55,15.89-21.35,12.19-43.42,24.77-70.08,21-25.46-3.64-49.29-19.13-63.76-41.44s-18.29-48.72-10.9-74.58c5.42-19-6.85-31.72-20.15-37.33-16.81-7.07-40.64-4.74-53.15,17.16-13.23,23.13-29.36,51.31-64.44,50.54-0.8,0-1.82-0.08-2.94-0.18a10,10,0,0,0-10.88,10v128.43A18.21,18.21,0,0,0,266.11,732H646a18.22,18.22,0,0,0,18.15-16.61c4.31-48.27-11.06-77.52-47.4-91.15zM541.06,455c10.92,13.43,30.4,23.33,52.28,10.82,31.66-18.09,60.1-21.68,80-10.1,15.55,9,24.11,26.44,24.11,49.05,0,18.93,12.27,38.52,29.84,47.64,13.52,7,28.41,7.06,42.89,0.44a10,10,0,0,0,5.88-9.12V404.52a10,10,0,0,0-10-10H544.58a10.11,10.11,0,0,0-8.72,5.09c-11.38,19.68-6.29,41.24,5.2,55.39z M716.77,572.72c-25.24-13.11-42.19-40.42-42.19-68,0-10-2.2-23.15-12.7-29.24-12.41-7.21-33.26-3.49-57.18,10.18-27.83,15.91-60.56,9.39-81.41-16.26-12.86-15.8-20.37-39.21-14.79-62.58a9.92,9.92,0,0,0-9.57-12.33h-241a10,10,0,0,0-10,10V542.1a10,10,0,0,0,8.68,9.91c2,0.27,3.87,0.5,4.82,0.53,21,0.82,30.69-14.18,44.86-38.95,18.58-32.51,55.28-38.13,81.94-26.92,27.65,11.65,41,37.66,33.29,64.73a67.26,67.26,0,0,0,8.09,55.83c10.93,16.83,28.79,28.5,47.78,31.21s36.61-7.44,55.46-18.2,39.7-22.66,64.1-22.66a78.44,78.44,0,0,1,27.84,5.22c47.36,17.75,68.07,57.63,61.77,118.68a9.81,9.81,0,0,0,10,11l69.59-0.44a10,10,0,0,0,9.93-10V588.79a10,10,0,0,0-12.15-9.79,69.13,69.13,0,0,1-47.16-6.28z',
  75. },
  76. {
  77. name: '饼干',
  78. icon: 'path://M537.6,130.133333c-46.933333-4.266667-91.733333,0-132.266667,10.666667-14.933333,4.266667-21.333333,19.2-14.933333,34.133333,10.666667,23.466667,14.933333,51.2,12.8,81.066667-6.4,70.4-59.733333,130.133333-130.133333,142.933333-6.4,2.133333-12.8,2.133333-17.066667,2.133334-17.066667,2.133333-27.733333,19.2-19.2,34.133333,6.4,17.066667,10.666667,34.133333,10.666667,53.333333,0,57.6-38.4,106.666667-89.6,125.866667-12.8,4.266667-19.2,19.2-14.933334,32,51.2,145.066667,192,249.6,358.4,249.6,219.733333,0,396.8-183.466667,384-407.466667-12.8-187.733333-162.133333-341.333333-347.733333-358.4z M584.533333,379.733333m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M443.733333,640m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M704,635.733333m-61.866667,0a61.866667,61.866667,0,1,0,123.733334,0,61.866667,61.866667,0,1,0-123.733334,0Z',
  79. },
  80. ],
  81. },
  82. xAxis: {
  83. type: 'category',
  84. data: chartData.data.month,
  85. },
  86. yAxis: {
  87. type: 'value',
  88. axisLabel: {
  89. // 设置坐标轴文本和坐标轴的间距,以免第一个点的标签值和y轴的坐标轴文本有重叠
  90. margin: 15,
  91. },
  92. splitLine: {
  93. show: true,
  94. lineStyle: {
  95. color: ['#E5EAF3'],
  96. width: 1,
  97. type: 'dashed',
  98. },
  99. },
  100. },
  101. animationDuration: 10000,
  102. series: [
  103. {
  104. name: '面包',
  105. type: 'line',
  106. symbol: 'path://M66.19,738.03c2.39,41.18,33.76,75.58,75.05,80.8,42.68,5.4,106.69-1.05,197.53-43.32,0,0,416.31-181.69,540.54-378.14,0,0,49-82.43,51.96-152.97,1.24-29.57-13.29-57.64-37.94-74.36-52.25-35.45-176.24-91.82-392.54-38.99,0,0-343.32,67.7-430.61,448.07,0,0.02-10.13,53.16-3.99,158.91z M584.5,115.17s-3.06,154.95-44.02,169.16c0,0-23.56-131.75-60.42-147.99-0.01,0,58.84-16.35,104.44-21.17z M183.48,336.63s83.05,140.79,70.01,185.83c0,0-86.83-101.91-126.95-97.72,0.01,0,27.18-52.68,56.94-88.11z M365.11,184.25s62.22,183.34,29.41,211.49c0,0-77.84-151.04-118.15-152.69,0-0.01,48.13-37.69,88.74-58.8z M141.64,495.33s-71.05,88.49-61.12,196.48,61.12-196.48,61.12-196.48z M909.55,184.25c3.61,69.01-305.31,450.06-618.91,611.35,0,0,342.16-141.32,497.43-294.66-0.01,0.01,229.55-205.04,121.48-316.69z',
  107. symbolSize: 12,
  108. data: chartData.data.breadAmount,
  109. itemStyle: {
  110. color: '#5470C6',
  111. },
  112. endLabel: {
  113. show: true,
  114. },
  115. },
  116. {
  117. name: '牛奶',
  118. type: 'line',
  119. symbol: 'path://M726.65,184a19.09,19.09,0,0,0-19.09-19.09H316.44A19.09,19.09,0,0,0,297.35,184v50.46a10,10,0,0,0,10,10h409.3a10,10,0,0,0,10-10zM257.9,754.85a10,10,0,0,0-10,10V840a19.09,19.09,0,0,0,19.1,19.13h490A19.09,19.09,0,0,0,776.1,840v-75a10,10,0,0,0-10.05-10zM723.42,267.33H300.58a10,10,0,0,0-9.32,6.37L258.48,358a10,10,0,0,0,9.32,13.63h488.4a10,10,0,0,0,9.32-13.63l-32.78-84.3a10,10,0,0,0-9.32-6.37zM616.75,624.24c-27-10.12-48.09,1.94-72.55,15.89-21.35,12.19-43.42,24.77-70.08,21-25.46-3.64-49.29-19.13-63.76-41.44s-18.29-48.72-10.9-74.58c5.42-19-6.85-31.72-20.15-37.33-16.81-7.07-40.64-4.74-53.15,17.16-13.23,23.13-29.36,51.31-64.44,50.54-0.8,0-1.82-0.08-2.94-0.18a10,10,0,0,0-10.88,10v128.43A18.21,18.21,0,0,0,266.11,732H646a18.22,18.22,0,0,0,18.15-16.61c4.31-48.27-11.06-77.52-47.4-91.15zM541.06,455c10.92,13.43,30.4,23.33,52.28,10.82,31.66-18.09,60.1-21.68,80-10.1,15.55,9,24.11,26.44,24.11,49.05,0,18.93,12.27,38.52,29.84,47.64,13.52,7,28.41,7.06,42.89,0.44a10,10,0,0,0,5.88-9.12V404.52a10,10,0,0,0-10-10H544.58a10.11,10.11,0,0,0-8.72,5.09c-11.38,19.68-6.29,41.24,5.2,55.39z M716.77,572.72c-25.24-13.11-42.19-40.42-42.19-68,0-10-2.2-23.15-12.7-29.24-12.41-7.21-33.26-3.49-57.18,10.18-27.83,15.91-60.56,9.39-81.41-16.26-12.86-15.8-20.37-39.21-14.79-62.58a9.92,9.92,0,0,0-9.57-12.33h-241a10,10,0,0,0-10,10V542.1a10,10,0,0,0,8.68,9.91c2,0.27,3.87,0.5,4.82,0.53,21,0.82,30.69-14.18,44.86-38.95,18.58-32.51,55.28-38.13,81.94-26.92,27.65,11.65,41,37.66,33.29,64.73a67.26,67.26,0,0,0,8.09,55.83c10.93,16.83,28.79,28.5,47.78,31.21s36.61-7.44,55.46-18.2,39.7-22.66,64.1-22.66a78.44,78.44,0,0,1,27.84,5.22c47.36,17.75,68.07,57.63,61.77,118.68a9.81,9.81,0,0,0,10,11l69.59-0.44a10,10,0,0,0,9.93-10V588.79a10,10,0,0,0-12.15-9.79,69.13,69.13,0,0,1-47.16-6.28z',
  120. symbolSize: 12,
  121. data: chartData.data.milkAmount,
  122. itemStyle: {
  123. color: '#91CC75',
  124. },
  125. endLabel: {
  126. show: true,
  127. },
  128. },
  129. {
  130. name: '饼干',
  131. type: 'line',
  132. symbol: 'path://M537.6,130.133333c-46.933333-4.266667-91.733333,0-132.266667,10.666667-14.933333,4.266667-21.333333,19.2-14.933333,34.133333,10.666667,23.466667,14.933333,51.2,12.8,81.066667-6.4,70.4-59.733333,130.133333-130.133333,142.933333-6.4,2.133333-12.8,2.133333-17.066667,2.133334-17.066667,2.133333-27.733333,19.2-19.2,34.133333,6.4,17.066667,10.666667,34.133333,10.666667,53.333333,0,57.6-38.4,106.666667-89.6,125.866667-12.8,4.266667-19.2,19.2-14.933334,32,51.2,145.066667,192,249.6,358.4,249.6,219.733333,0,396.8-183.466667,384-407.466667-12.8-187.733333-162.133333-341.333333-347.733333-358.4z M584.533333,379.733333m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M443.733333,640m-74.666666,0a74.666667,74.666667,0,1,0,149.333333,0,74.666667,74.666667,0,1,0-149.333333,0Z M704,635.733333m-61.866667,0a61.866667,61.866667,0,1,0,123.733334,0,61.866667,61.866667,0,1,0-123.733334,0Z',
  133. symbolSize: 12,
  134. data: chartData.data.cookieAmount,
  135. itemStyle: {
  136. color: '#FE9F9F',
  137. },
  138. endLabel: {
  139. show: true,
  140. },
  141. },
  142. ],
  143. }
  144. // 使用刚指定的配置项和数据显示图表
  145. myChart.setOption(option)
  146. },
  147. },
  148. }
  149. </script>
  150. <style scoped>
  151. .mainDiv {
  152. border: 1px solid;
  153. height: 410px;
  154. border-radius: 5px;
  155. text-align: center;
  156. }
  157. .titleDiv {
  158. height: 30px;
  159. line-height: 30px;
  160. font-size: 12;
  161. }
  162. .chartDiv {
  163. height: 380px;
  164. }
  165. </style>

2.我们可以利用第二种方式来实现近2年各个月份产品销量数据(x轴和折线数据动态渲染),复制出一个的图表组件ChartLineSix.vue放在ChartLine页图表6的位置上进行展示。参考代码:

  1. <template>
  2. <div class="mainDiv">
  3. <div class="titleDiv">月度产品销量展示</div>
  4. <div class="chartDiv" id="chartLineSix"></div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. chartData: {},
  12. saleMonth: [],
  13. saleAmount: [],
  14. currentIndex: 0,
  15. currentMonth: [],
  16. currentAmount: [],
  17. }
  18. },
  19. name: 'ChartLineSix',
  20. mounted() {
  21. this.getChartData().then(() => {
  22. let myChart = this.$echarts.init(document.getElementById('chartLineSix'))
  23. // 数据请求到后渲染图表
  24. this.renderChart(myChart)
  25. // 更新图表option
  26. let that = this
  27. setInterval(function () {
  28. that.update(myChart)
  29. }, 1500)
  30. })
  31. },
  32. methods: {
  33. // 请求数据回来
  34. async getChartData() {
  35. this.chartData = await this.$axios({ url: 'sales/year/amount' })
  36. this.saleMonth = this.chartData.data.month
  37. this.saleAmount = this.chartData.data.amount
  38. this.currentMonth = this.saleMonth.slice(0, 6)
  39. this.currentAmount = this.saleAmount.slice(0, 6)
  40. },
  41. // 渲染图表
  42. renderChart(myChart) {
  43. let currentMonth = this.currentMonth
  44. let currentAmount = this.currentAmount
  45. var option = {
  46. tooltip: {
  47. show: true,
  48. },
  49. legend: {
  50. show: true,
  51. data: ['销量'],
  52. x: 'center',
  53. y: 'bottom',
  54. padding: [0, 0, 10, 0],
  55. textStyle: {
  56. fontStyle: 'normal',
  57. fontSize: 12,
  58. },
  59. },
  60. grid: {
  61. top: '8%',
  62. bottom: '10%',
  63. left: '4%',
  64. right: '8%',
  65. containLabel: true,
  66. },
  67. xAxis: {
  68. type: 'category',
  69. //boundaryGap: false,
  70. data: currentMonth,
  71. },
  72. yAxis: {
  73. type: 'value',
  74. },
  75. series: [
  76. {
  77. name: '销量',
  78. type: 'line',
  79. symbol: 'roundRect',
  80. symbolSize: 8,
  81. data: currentAmount,
  82. label: {
  83. show: true,
  84. position: 'top',
  85. fontSize: 10,
  86. },
  87. },
  88. ],
  89. }
  90. // 使用刚指定的配置项和数据显示图表
  91. myChart.setOption(option)
  92. },
  93. // 更新图表option
  94. update(myChart) {
  95. // 数据动态增长
  96. if (this.currentIndex < 18) {
  97. this.currentIndex = this.currentIndex + 1
  98. } else {
  99. this.currentIndex = 0
  100. }
  101. let index = this.currentIndex
  102. let newCurrentMonth = this.saleMonth.slice(index, index + 6)
  103. let newCurrentAmount = this.saleAmount.slice(index, index + 6)
  104. var newOption = {
  105. xAxis: {
  106. data: newCurrentMonth,
  107. },
  108. series: [
  109. {
  110. data: newCurrentAmount,
  111. },
  112. ],
  113. }
  114. myChart.setOption(newOption)
  115. },
  116. },
  117. }
  118. </script>
  119. <style scoped>
  120. </style>

3.至此,动态折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:

3.小节总结

    折线图用于分析事物随时间或有序类别而变化的趋势。本小节主要记录了ECharts折线图的基础设置,并延伸介绍了如何实现多折线图、阶梯折线图、双轴折线图、动态折线图等可视化图表。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/208720
推荐阅读