vue---图表-linechart

最近在做vue的图表功能,研究了好长时间,现将代码记录下来。

1、vue代码

<template>

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

</template>

<script>

import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme

import resize from './mixins/resize'

import { homeUserMonthChart } from "@/api/home";

export default {

  mixins: [resize],

  props: {

    className: {

      type: String,

      default: 'chart'

    },

    width: {

      type: String,

      default: '99%'

    },

    height: {

      type: String,

      default: '350px'

    },

    autoResize: {

      type: Boolean,

      default: true

    }

  },

  data() {

    return {

      chart: null,

      lineChartData: {},

      expectedData: [],

      actualData: [],

      monthData:[],

      name1: '总用户',

      name2: '月活'

    }

  },

  watch: {

    chartData: {

      deep: true,

      handler(val) {

        this.setOptions(val)

      }

    }

  },

  mounted() {

    // 等待一切加载完成后调用初始化方法

    this.$nextTick(() => {

      //this.initChart()

      this.getHomeUserChart();

    })

  },

  beforeDestroy() {

    if (!this.chart) {

      return

    }

    this.chart.dispose()

    this.chart = null

  },

  created() {

    // this.getHomeUserChart();

  },

  methods: {

    //折线图-总用户与月活 对比

    getHomeUserChart(){

      homeUserMonthChart(this.queryParams).then(response => {

        console.log(response.data);

        for (let i = 0;i < response.data.monthData.length;i++) {

          var mData=response.data.monthData[i];

          this.monthData.push(mData);

        }

        for (let i = 0;i < response.data.userData.length;i++) {

          var mData=response.data.userData[i];

          this.actualData.push(mData);

        }

        for (let i = 0;i < response.data.userAllData.length;i++) {

          var mData=response.data.userAllData[i];

          this.expectedData.push(mData);

        }

          //初始化 图形

          this.initChart();

      });

    },

    initChart() {

      this.chart = echarts.init(this.$el, 'macarons');

      this.setOptions();

    },

    setOptions() {

      console.log('x轴分区被初始化');

      this.chart.setOption({

        xAxis: {

          data: this.monthData,

          boundaryGap: false,

          axisTick: {

            show: false

          }

        },

        grid: {

          left: 30,

          right: 30,

          bottom: 20,

          top: 30,

          containLabel: true

        },

        tooltip: {

          trigger: 'axis',

          axisPointer: {

            type: 'cross'

          },

          padding: [5, 10]

        },

        yAxis: {

          axisTick: {

            show: false

          }

        },

        legend: {

          data: [this.name1, this.name2]

        },

        series: [{

          name: this.name1, itemStyle: {

            normal: {

              color: '#FF005A',

              lineStyle: {

                color: '#FF005A',

                width: 2

              }

            }

          },

          smooth: true,

          type: 'line',

          data: this.expectedData,

          animationDuration: 2800,

          animationEasing: 'cubicInOut'

        },

        {

          name: this.name2,

          smooth: true,

          type: 'line',

          itemStyle: {

            normal: {

              color: '#3888fa',

              lineStyle: {

                color: '#3888fa',

                width: 2

              },

              areaStyle: {

                color: '#f3f8ff'

              }

            }

          },

          data: this.actualData,

          animationDuration: 2800,

          animationEasing: 'quadraticOut'

        }]

      })

    }

  }

}

</script>

2、后端返回--数据格式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值