Vue绘制折线图并渲染数据

本文讲解vue项目中使用ECharts快速绘制折线图,并将服务器传来的数据渲染上去,提供源代码,可以直接复制粘贴使用。

效果展示: 

df98f8bcbedd403fa06b3856e892882b.png

 Vue 通常使用ECharts 生成图表,官网地址:

Apache ECharts

1.Vue中要使用EChart需要先安装:

npm install echarts --save

 更多安装方法请移步:

ECharts 安装 | 菜鸟教程

 8b75cd1b3455461b993b66b018df086d.png

2.本例将折线图写在子组件LineChart中,父组件进行引入:

7f077d4f3ef84d738efc685df687c67c.png 

 3.子组件LineChart.vue(绘制折线图)

<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { getUserCountList } from '@/api/chartData'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      lineChartData: [],
      xdata: [],
      ydata: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getChartData()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getXYData() {
      // console.log("lineChartData.length:"+this.lineChartData.length)
      for (let i = 0; i < this.lineChartData.length; i++) {
        this.xdata.push(this.lineChartData[i].createTime)
        this.ydata.push(this.lineChartData[i].count)
      }
    },
    getChartData() {
      const _this = this
      getUserCountList().then(response => {
        // console.log("zhexiantu:")
        _this.lineChartData = response.data
        // console.log(_this.lineChartData)
        _this.initChart()
      }).catch(error => {
        console.log(error)
      })
    },
    initChart() {
      // console.log("initChart")
      // console.log(this.lineChartData)
      this.getXYData()
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        xAxis: {
          data: this.xdata,
          boundaryGap: false,
          axisLabel: {
            interval: 0,
            rotate: 45
          },
          axisTick: {
            show: false
          }
        },
        grid: {
          left: '2%',
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['注册用户(人数)']
        },
        series: [{
          name: '注册用户(人数)',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: this.ydata,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>

对以上代码的分析:
161548d4aea8429abb1c19b00315c5ad.png

4a72ac899cef4c57b8908ef35d0aa99b.png a67f77118c6a4d57b96903dfb8810926.png

366e09327adb4b9fa284dee2a1085ba9.png af3ec2a941064108980b5c501b6e1a8b.png

3c7a2dcb78d441df838614ce0f998847.png

29fce41023bc4593afcffb39b9d937a2.png 

猜你喜欢

转载自blog.csdn.net/weixin_46019681/article/details/127396397