赞
踩
折线图是用折线将各个数据点标志连接起来的图表,用于展现数据的变化趋势。折线图可以显示随时间而变化的连续数据,因此非常适用于显示在相等时间间隔下数据的趋势。在ECharts中,柱状图和折线图的配置有许多相似之处,例如x轴和y轴的设置,标记最值和平均值等场景基本雷同,至于图例(legend)、图表离容器的距离样式(grid)、提示框(tooltip)等设置基本适用于所有图表类型。由于这些内容在柱状图实现中已经介绍过了,所以本小节在这些方面不做过多赘述。在本小节中,主要记录ECharts折线图的基础设置,结合实际业务场景实现多折线图、阶梯折线图、双轴折线图、动态折线图等图表展现。
1.准备工作:(1)优化项目主页布局,由于后续要了解很多图表类型,所以主页只放文字链接,点击打开相应的图表路由页面。(2)针对各个图表组件,公共样式不需要每个组件都设置一遍,可以在页面上设置好,允许样式穿透(页面中去除scoped属性)。
主页HomeView.vue中,存放文字链接,点击跳转对应的路由页面,路由配置(router下的index.js)。
- import Vue from 'vue'
- import VueRouter from 'vue-router'
- import HomeView from '../views/HomeView.vue'
- import ChartBar from '../views/ChartBar.vue'
- import ChartLine from '../views/ChartLine.vue'
-
- Vue.use(VueRouter)
-
- const routes = [
- {
- path: '/',
- name: 'home',
- component: HomeView
- },
- {
- path: '/chartBar',
- name: 'ChartBar',
- component: ChartBar
- },
- {
- path: '/chartLine',
- name: 'ChartLine',
- component: ChartLine
- },
-
- ]
-
- const router = new VueRouter({
- mode: 'history',
- base: process.env.BASE_URL,
- routes
- })
-
- export default router
2.假设要展示的上半年各个月份的产品销售,那么可以用折线图来展示。修改前端前需要再加一个后端接口(RAP接口管理平台中新建接口提供上半年各月销量),然后复制出一个的图表组件ChartLineOne.vue放在ChartLine页图表1的位置上进行展示。参考代码:
- <template>
- <div class="mainDiv">
- <div class="titleDiv">上半年各月销量</div>
- <div class="chartDiv" id="chartLineOne"></div>
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- chartData: {},
- }
- },
- name: 'ChartLineOne',
- mounted() {
- this.getChartData().then(() => {
- // 数据请求到后渲染图表
- this.renderChart()
- })
- },
- methods: {
- // 请求数据回来
- async getChartData() {
- this.chartData = await this.$axios({ url: 'sales/month' })
- },
- // 渲染图表
- renderChart() {
- let myChart = this.$echarts.init(document.getElementById('chartLineOne'))
- let chartData = this.chartData
- var option = {
- tooltip: {
- show: true,
- },
- legend: {
- show: true,
- data: ['销量'],
- x: 'center',
- y: 'bottom',
- padding: [0, 0, 10, 0],
- textStyle: {
- fontStyle: 'normal',
- fontSize: 12,
- },
- },
- grid: {
- top: '8%',
- bottom: '10%',
- left: '4%',
- right: '8%',
- containLabel: true,
- },
- xAxis: {
- type: 'category',
- data: chartData.data.month,
- },
-
- yAxis: {
- type: 'value',
- },
- series: [
- {
- name: '销量',
- type: 'line',
- data: chartData.data.amount,
- },
- ],
- }
- // 使用刚指定的配置项和数据显示图表
- myChart.setOption(option)
- },
- },
- }
- </script>
-
- <style scoped>
- .mainDiv {
- border: 1px solid;
- height: 410px;
- border-radius: 5px;
- text-align: center;
- }
- .titleDiv {
- height: 30px;
- line-height: 30px;
- font-size: 12;
- }
- .chartDiv {
- height: 380px;
- }
- </style>
3.上图可以发现折线图不是从零开始的(默认折线从刻度中心开始),可以通过设置xAxis.boundaryGap参数让其从零开始。参考代码:
- xAxis: {
- type: 'category',
- boundaryGap: false,
- data: chartData.data.month,
- },
4.折线的线条过于刚直,能否设置为平滑折线,可以设置series.smooth参数实现。参考代码:
- series: [
- {
- name: '销量',
- type: 'line',
- data: chartData.data.amount,
- smooth: true,
- },
- ],
5.至此,基础设置相关介绍告一段落,启动项目,预览ChartLine页如下图:
1.假设要展示的各季度产品销量包括三大产品的销售-牛奶、面包和饼干,那么可以用多折线图来展示。修改前端前需要再加一个后端接口(RAP接口管理平台中新建接口提供产品销量),然后复制出一个的图表组件ChartBarTwo.vue放在ChartLine页图表2的位置上进行展示。参考代码:
系列图标(legend.data.icon)、折线连接点形状(series.symbol)属性设置说明:ECharts中类似标记的图形支持3种:(1)系统默认提供的几种类型;(2)直接放图片链接;(3)设置图标矢量路径。建议使用(1)或者(3)的方式,不用担心因为缩放而产生锯齿或模糊,而且可以设置为任意颜色,路径图形会自适应调整为合适的大小。此处使用了(3)的方式,为面包、牛奶、饼干设置了对应的矢量路径。
- <template>
- <div class="mainDiv">
- <div class="titleDiv">各季度产品销量</div>
- <div class="chartDiv" id="chartLineTwo"></div>
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- chartData: {},
- }
- },
- name: 'ChartLineTwo',
- mounted() {
- this.getChartData().then(() => {
- // 数据请求到后渲染图表
- this.renderChart()
- })
- },
- methods: {
- // 请求数据回来
- async getChartData() {
- this.chartData = await this.$axios({ url: 'sales/quarter' })
- },
- // 渲染图表
- renderChart() {
- let myChart = this.$echarts.init(document.getElementById('chartLineTwo'))
- let chartData = this.chartData
- var option = {
- tooltip: {
- show: true,
- textStyle: {
- color: '#000000',
- fontStyle: 'normal',
- fontSize: 14,
- align: 'left',
- },
- formatter: function (param) {
- // 设置提示显示当季度所有产品销量信息
- let message =
- param.name +
- '<br />' +
- '面包:' +
- chartData.data.breadAmount[param.dataIndex] +
- '<br />' +
- '牛奶:' +
- chartData.data.milkAmount[param.dataIndex] +
- '<br />' +
- '饼干:' +
- chartData.data.cookieAmount[param.dataIndex]
- return message
- },
- },
- legend: {
- show: true,
- x: 'center',
- y: 'bottom',
- padding: [0, 0, 10, 0],
- data: [
- {
- name: '面包',
- 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',
- },
- {
- name: '牛奶',
- 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',
- },
- {
- name: '饼干',
- 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',
- },
- ],
- },
- grid: {
- top: '8%',
- bottom: '10%',
- left: '4%',
- right: '6%',
- containLabel: true,
- },
- xAxis: {
- type: 'category',
- data: chartData.data.quarter,
- // 坐标轴两边留白策略(设置从0开始)
- boundaryGap: false,
- },
- yAxis: {
- type: 'value',
- axisLabel: {
- // 设置坐标轴文本和坐标轴的间距,以免第一个点的标签值和y轴的坐标轴文本有重叠
- margin: 15,
- },
- splitLine: {
- show: true,
- lineStyle: {
- color: ['#E5EAF3'],
- width: 1,
- type: 'dashed',
- },
- },
- },
- series: [
- {
- name: '面包',
- type: 'line',
- 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',
- symbolSize: 12,
- data: chartData.data.breadAmount,
- // 是否平滑曲线显示
- smooth: true,
- stack: '面包',
- itemStyle: {
- color: '#5470C6',
- },
- },
- {
- name: '牛奶',
- type: 'line',
- 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',
- symbolSize: 12,
- data: chartData.data.milkAmount,
- smooth: true,
- stack: '牛奶',
- itemStyle: {
- color: '#91CC75',
- },
- },
- {
- name: '饼干',
- type: 'line',
- 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',
- symbolSize: 12,
- data: chartData.data.cookieAmount,
- smooth: true,
- stack: '饼干',
- itemStyle: {
- color: '#FE9F9F',
- },
- },
- ],
- }
- // 使用刚指定的配置项和数据显示图表
- myChart.setOption(option)
- },
- },
- }
- </script>
-
- <style scoped>
- .mainDiv {
- border: 1px solid;
- height: 410px;
- border-radius: 5px;
- text-align: center;
- }
- .titleDiv {
- height: 30px;
- line-height: 30px;
- font-size: 12;
- }
- .chartDiv {
- height: 380px;
- }
- </style>
2.至此,多折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:
1.阶梯线图又称方波图,它使用水平和垂直的线来连接两个数据点,而普通折线图则直接将两个点连接起来。阶梯线图能够很好地表达数据的突变。同样展示上述各季度产品销量(牛奶、面包和饼干),复制出一个的图表组件ChartBarThree.vue放在ChartLine页图表3的位置上进行展示。此处主要通过series.step属性来控制阶梯线图的连接类型,共有三种取值:start、middle和end,分别表示在当前点,当前点与下个点的中间点,下个点拐弯。参考代码:
- series: [
- {
- name: '面包',
- type: 'line',
- step: 'start',
- ......
- },
- {
- name: '牛奶',
- type: 'line',
- step: 'end',
- ......
- },
- {
- name: '饼干',
- type: 'line',
- step: 'middle',
- ......
- },
- ],
2.至此,阶梯折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:
1.双轴折线图,一般适用于以下场景:(1)一个图表几个系列的数据单位不相同,相差较大。(2)希望y 轴可以设置左右两个维度,比如一个展示销量,一个展示毛利率。假设要展示的上半年各月产品销量和毛利率,那么可以用双轴折线图来展示。修改前端前需要再加一个后端接口(RAP接口管理平台中新建接口提供上半年各月产品销量毛利率数据),然后复制出一个的图表组件ChartLineFour.vue放在ChartLine页图表4的位置上进行展示。参考代码:
- <template>
- <div class="mainDiv">
- <div class="titleDiv">上半年各月销量和毛利率</div>
- <div class="chartDiv" id="chartLineFour"></div>
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- chartData: {},
- }
- },
- name: 'ChartLineFour',
- mounted() {
- this.getChartData().then(() => {
- // 数据请求到后渲染图表
- this.renderChart()
- })
- },
- methods: {
- // 请求数据回来
- async getChartData() {
- this.chartData = await this.$axios({ url: 'sales/month/amount' })
- },
- // 渲染图表
- renderChart() {
- let myChart = this.$echarts.init(document.getElementById('chartLineFour'))
- let chartData = this.chartData
- var option = {
- tooltip: {
- show: true,
- textStyle: {
- color: '#000000',
- fontStyle: 'normal',
- fontSize: 14,
- align: 'left',
- },
- formatter: function (param) {
- let message =
- param.name +
- '<br />' +
- '销售量:' +
- chartData.data.quantity[param.dataIndex] +
- '<br />' +
- '毛利率:' +
- chartData.data.rate[param.dataIndex] * 100 + '%'
- return message
- },
- },
- grid: {
- top: '8%',
- bottom: '8%',
- left: '6%',
- right: '6%',
- containLabel: true,
- },
- legend: {
- show: true,
- x: 'center',
- y: 'bottom',
- padding: [0, 0, 10, 0],
- },
- xAxis: {
- type: 'category',
- data: chartData.data.month,
- // 坐标轴两边留白策略(设置从0开始)
- boundaryGap: false,
- },
- yAxis: [
- {
- type: 'value',
- name: '销\n量\n︵\n件\n︶',
- nameLocation: 'center',
- nameRotate: 0,
- position: 'left',
- nameTextStyle: {
- fontSize: 12,
- padding: [0, 30, 0, 0],
- },
- },
- {
- type: 'value',
- name: '毛\n利\n率\n︵\n%\n︶',
- nameLocation: 'center',
- nameRotate: 0,
- position: 'right',
- nameTextStyle: {
- fontSize: 12,
- padding: [0, 0, 0, 15],
- },
- axisLabel: {
- formatter: function (param) {
- return param * 100
- },
- },
- },
- ],
- series: [
- {
- name: '销量',
- type: 'line',
- symbol: 'roundRect',
- symbolSize: 10,
- smooth: true,
- data: chartData.data.quantity,
- yAxisIndex: 0,
- },
- {
- name: '毛利率',
- type: 'line',
- symbol: '',
- symbolSize: 10,
- smooth: true,
- data: chartData.data.rate,
- yAxisIndex: 1,
- },
- ],
- }
- // 使用刚指定的配置项和数据显示图表
- myChart.setOption(option)
- },
- },
- }
- </script>
-
- <style scoped>
- .mainDiv {
- border: 1px solid;
- height: 410px;
- border-radius: 5px;
- text-align: center;
- }
- .titleDiv {
- height: 30px;
- line-height: 30px;
- font-size: 12;
- }
- .chartDiv {
- height: 380px;
- }
- </style>
2.至此,双轴折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:
1.ECharts动态折线图可以分为两类实现:一种为渲染静态数据,产生动画效果的折线图;另一种为动态渲染数据产生折线图(这点和上节中的动态排序柱状图实现原理类似)。我们可以利用第一种方式来实现上半年各月产品销量数据(面包、牛奶和饼干),主要是使用animationDuration动画属性以及series.endLabel末端标签显示属性,复制出一个的图表组件ChartLineFive.vue放在ChartLine页图表5的位置上进行展示。参考代码:
- <template>
- <div class="mainDiv">
- <div class="titleDiv">上半年各月产品销量</div>
- <div class="chartDiv" id="chartLineFive"></div>
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- chartData: {},
- }
- },
- name: 'ChartLineFive',
- mounted() {
- this.getChartData().then(() => {
- // 数据请求到后渲染图表
- this.renderChart()
- })
- },
- methods: {
- // 请求数据回来
- async getChartData() {
- this.chartData = await this.$axios({ url: 'sales/product/month' })
- },
- // 渲染图表
- renderChart() {
- let myChart = this.$echarts.init(document.getElementById('chartLineFive'))
- let chartData = this.chartData
- var option = {
- tooltip: {
- show: true,
- textStyle: {
- color: '#000000',
- fontStyle: 'normal',
- fontSize: 14,
- align: 'left',
- },
- formatter: function (param) {
- // 设置提示显示当季度所有产品销量信息
- let message =
- param.name +
- '<br />' +
- '面包:' +
- chartData.data.breadAmount[param.dataIndex] +
- '<br />' +
- '牛奶:' +
- chartData.data.milkAmount[param.dataIndex] +
- '<br />' +
- '饼干:' +
- chartData.data.cookieAmount[param.dataIndex]
- return message
- },
- },
- grid: {
- top: '8%',
- bottom: '8%',
- left: '4%',
- right: '4%',
- containLabel: true,
- },
- legend: {
- show: true,
- x: 'center',
- y: 'bottom',
- padding: [0, 0, 10, 0],
- data: [
- {
- name: '面包',
- 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',
- },
- {
- name: '牛奶',
- 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',
- },
- {
- name: '饼干',
- 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',
- },
- ],
- },
- xAxis: {
- type: 'category',
- data: chartData.data.month,
- },
- yAxis: {
- type: 'value',
- axisLabel: {
- // 设置坐标轴文本和坐标轴的间距,以免第一个点的标签值和y轴的坐标轴文本有重叠
- margin: 15,
- },
- splitLine: {
- show: true,
- lineStyle: {
- color: ['#E5EAF3'],
- width: 1,
- type: 'dashed',
- },
- },
- },
- animationDuration: 10000,
- series: [
- {
- name: '面包',
- type: 'line',
- 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',
- symbolSize: 12,
- data: chartData.data.breadAmount,
- itemStyle: {
- color: '#5470C6',
- },
- endLabel: {
- show: true,
- },
- },
- {
- name: '牛奶',
- type: 'line',
- 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',
- symbolSize: 12,
- data: chartData.data.milkAmount,
- itemStyle: {
- color: '#91CC75',
- },
- endLabel: {
- show: true,
- },
- },
- {
- name: '饼干',
- type: 'line',
- 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',
- symbolSize: 12,
- data: chartData.data.cookieAmount,
- itemStyle: {
- color: '#FE9F9F',
- },
- endLabel: {
- show: true,
- },
- },
- ],
- }
- // 使用刚指定的配置项和数据显示图表
- myChart.setOption(option)
- },
- },
- }
- </script>
-
- <style scoped>
- .mainDiv {
- border: 1px solid;
- height: 410px;
- border-radius: 5px;
- text-align: center;
- }
- .titleDiv {
- height: 30px;
- line-height: 30px;
- font-size: 12;
- }
- .chartDiv {
- height: 380px;
- }
- </style>
2.我们可以利用第二种方式来实现近2年各个月份产品销量数据(x轴和折线数据动态渲染),复制出一个的图表组件ChartLineSix.vue放在ChartLine页图表6的位置上进行展示。参考代码:
- <template>
- <div class="mainDiv">
- <div class="titleDiv">月度产品销量展示</div>
- <div class="chartDiv" id="chartLineSix"></div>
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- chartData: {},
- saleMonth: [],
- saleAmount: [],
- currentIndex: 0,
- currentMonth: [],
- currentAmount: [],
- }
- },
- name: 'ChartLineSix',
- mounted() {
- this.getChartData().then(() => {
- let myChart = this.$echarts.init(document.getElementById('chartLineSix'))
- // 数据请求到后渲染图表
- this.renderChart(myChart)
- // 更新图表option
- let that = this
- setInterval(function () {
- that.update(myChart)
- }, 1500)
- })
- },
- methods: {
- // 请求数据回来
- async getChartData() {
- this.chartData = await this.$axios({ url: 'sales/year/amount' })
- this.saleMonth = this.chartData.data.month
- this.saleAmount = this.chartData.data.amount
- this.currentMonth = this.saleMonth.slice(0, 6)
- this.currentAmount = this.saleAmount.slice(0, 6)
- },
- // 渲染图表
- renderChart(myChart) {
- let currentMonth = this.currentMonth
- let currentAmount = this.currentAmount
- var option = {
- tooltip: {
- show: true,
- },
- legend: {
- show: true,
- data: ['销量'],
- x: 'center',
- y: 'bottom',
- padding: [0, 0, 10, 0],
- textStyle: {
- fontStyle: 'normal',
- fontSize: 12,
- },
- },
- grid: {
- top: '8%',
- bottom: '10%',
- left: '4%',
- right: '8%',
- containLabel: true,
- },
- xAxis: {
- type: 'category',
- //boundaryGap: false,
- data: currentMonth,
- },
-
- yAxis: {
- type: 'value',
- },
- series: [
- {
- name: '销量',
- type: 'line',
- symbol: 'roundRect',
- symbolSize: 8,
- data: currentAmount,
- label: {
- show: true,
- position: 'top',
- fontSize: 10,
- },
- },
- ],
- }
- // 使用刚指定的配置项和数据显示图表
- myChart.setOption(option)
- },
- // 更新图表option
- update(myChart) {
- // 数据动态增长
- if (this.currentIndex < 18) {
- this.currentIndex = this.currentIndex + 1
- } else {
- this.currentIndex = 0
- }
- let index = this.currentIndex
- let newCurrentMonth = this.saleMonth.slice(index, index + 6)
- let newCurrentAmount = this.saleAmount.slice(index, index + 6)
- var newOption = {
- xAxis: {
- data: newCurrentMonth,
- },
- series: [
- {
- data: newCurrentAmount,
- },
- ],
- }
- myChart.setOption(newOption)
- },
- },
- }
- </script>
-
- <style scoped>
- </style>
3.至此,动态折线图相关介绍告一段落,启动项目,预览ChartLine页如下图:
折线图用于分析事物随时间或有序类别而变化的趋势。本小节主要记录了ECharts折线图的基础设置,并延伸介绍了如何实现多折线图、阶梯折线图、双轴折线图、动态折线图等可视化图表。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。