echarts折线图封装案例

一、封装代码

<template>
  <div class="echarts" ref="chart"></div>
</template>

<script>
const echarts = require("echarts");

import { mapGetters } from "vuex";
import "echarts/theme/macarons.js";
export default {
  name: "line_chart",
  props: {
    barData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data () {
    return {
      chart: null,
      xData: [],
      yData: [],
      sdata: [],
      legendData: []
    };
  },

  components: {},

  computed: {
    ...mapGetters(["isCollapse"])
  },
  watch: {
    barData: {
      handler () {
        this.initCharts();
      },
      deep: true
    },
    isCollapse: {
      handler () {
        setTimeout(() => {
          this.chart.resize();
        }, 500);
      },
      deep: true
    }
  },
  mounted () {
    this.initCharts();
    this.resizeChart();
  },
  beforeDestroy () {
    window.onresize = null;
  },
  methods: {
    resizeChart () {
      var that = this;
      window.addEventListener("resize", function () {
        that.chart.resize();
      });
    },
    initCharts () {
      this.chart = echarts.init(this.$refs.chart, "macarons");
      this.barData.xdata && (this.xData = this.barData.xdata);
      this.barData.ydata && (this.yData = this.barData.ydata);
      this.barData.legend && (this.legendData = this.barData.legend);
    //   this.changeCharts();
      this.setChartOption();
    },
    setChartOption () {
      //this.chart.setOption(option,true)第二个参数为true可以解决重新渲染问题
      this.chart.setOption(
        {
        title: {
        text: '电流趋势图'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['全电流有效值', '阻性电流有效值', '容性电流有效值']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        // feature: {
        //     saveAsImage: {}
        // }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['05-12', '05-14', '05-21', '05-22', '05-23', '05-27', '05-31']
    },
    yAxis: {
        type: 'value'
    },
    series: [
        {
            name: '全电流有效值',
            type: 'line',
            stack: '总量',
            data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
            name: '阻性电流有效值',
            type: 'line',
            stack: '总量',
            data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
            name: '容性电流有效值',
            type: 'line',
            stack: '总量',
            data: [150, 232, 201, 154, 190, 330, 410]
        },
    ]
        },
        true
      );
    }
  }
};
</script>
<style lang="scss" scoped>
.echarts {
  width: 100%;
  height: 100%;
}
</style>

二、使用

2.1、提取
import piechart from "./components/piechart"
import piechart1 from "./components/piechart2"
export default {
components:{
    piechart,
    piechart1
},
2.2、使用
  <piechart />  

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于echarts折线图封装,可以参考以下步骤: 1. 引入echarts库和封装所需的数据 2. 创建一个div容器,设置宽高 3. 初始化echarts实例,并设置基本配置项 4. 封装数据,包括x轴数据、y轴数据、图例名称等 5. 设置折线图的配置项,包括颜色、线条样式、标记点等 6. 将封装好的数据和配置项传入echarts实例中 7. 渲染折线图 以下是一个简单的示例代码: ```javascript // 引入echarts库和封装所需的数据 import echarts from 'echarts' import data from './data' // 创建一个div容器,设置宽高 const container = document.getElementById('chart') container.style.width = '600px' container.style.height = '400px' // 初始化echarts实例,并设置基本配置项 const chart = echarts.init(container) const option = { title: { text: '折线图' }, tooltip: {}, legend: { data: [] }, xAxis: { data: [] }, yAxis: {}, series: [] } // 封装数据,包括x轴数据、y轴数据、图例名称等 option.legend.data = data.legend option.xAxis.data = data.xAxis for (let i = 0; i < data.series.length; i++) { option.series.push({ name: data.legend[i], type: 'line', data: data.series[i] }) } // 设置折线图的配置项,包括颜色、线条样式、标记点等 option.series.forEach(item => { item.lineStyle = { color: '#1890ff' } item.itemStyle = { color: '#1890ff' } item.label = { show: true } }) // 将封装好的数据和配置项传入echarts实例中 chart.setOption(option) // 渲染折线图 chart.render() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值