Define your own chart echart component based on vue2

Keywords: Front-end Vue JSON

First install the echarts cnpm i echarts -S, and then define the parent component

<template>
  <div>
       <echarts :option="echartOpion"></echarts>
  </div>
</template>
<script>
  import echarts from './echarts.vue' // Introduce subcomponents
  export default {
    data() {
      return {
        // The value passed to the subcomponent. Here, take the parameters of the histogram as an example. Please refer to the official website http://www.echartsjs.com/examples/
        echartOpion: {
          color: ['#3398DB'],
          tooltip: {
            trigger: 'axis',
            axisPointer: { // Axis indicator, axis trigger valid
              type: 'shadow' // The default is straight line, optional: 'line' | 'shadow'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value'
          }],
          series: [{
            name: 'Direct access',
            type: 'bar',
            barWidth: '60%',
            data: [10, 52, 200, 334, 390, 330, 220]
          }]
        },
        cloneOption: null
      }
    },
    components: {
      echarts //Import components
    },
    mounted() {
    }
  }
</script>

And then there are the subcomponents

<template>
  <div>
     <div style="height:500px;width:500px" ref="myEchart">
  </div>
  </div>
</template>
<script>
  import echarts from 'echarts'
  export default {
    data() {
      return {
        chart: null
      }
    },
    props: {
      // Whether the data of the chart is requested remotely
      mapdata: {
        type: String,
        default: ''
      },
      // Get directly from parent component
      option: Object
    },
    watch: {
      // Listen for the option value from the parent component
      option(val) {
        if(this.chart) {
          // If there is data, set the value otherwise clear
          // The setOption() and clear() methods here provide methods for echart, which can be found at http://www.echartsjs.com/api.html
          this.option ? this.chart.setOption(val) : this.chart.clear();
        }
      }
    },
    methods: {
      initChart() {
        // Initialization
        this.chart = echarts.init(this.$refs.myEchart);
        // Put configuration and data here
        this.chart.setOption(this.option);
      }
    },
    mounted() {
      // A mapdata is defined above. If it exists, set the value after sending the request. The method is the same
      this.initChart();
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose();// Destruction
      this.chart = null;
    }
  }
</script>

In this way, echart can be introduced, but there is a problem that different graph configurations are different, and we can use them in many places, so we need to put forward the configuration, that is, options, as a public part. Here, take the configuration of histogram as an example: first, create a new options.js file, and the file code is as follows:

export default {
  data() {
    return {
      // Exposed public configuration
      barOption: {
        color: ['#3398DB'],
        tooltip: {
          trigger: 'axis',
          axisPointer: { // Axis indicator, axis trigger valid
            type: 'shadow' // The default is straight line, optional: 'line' | 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value'
        }],
        series: [{
          name: 'Direct access',
          type: 'bar',
          barWidth: '60%',
          data: [10, 52, 200, 334, 390, 330, 220]
        }]
      }
    }
  },
  methods: {
    // Deep copy object or array
    clone(obj) {
      return JSON.parse(JSON.stringify(obj));
    }
  }
}

At this time, you only need to import this configuration where you need it. The parent file is modified as follows

<template>
  <div>
       <echarts :option="echartOpion"></echarts>
  </div>
</template>
<script>
  import echarts from './echarts.vue' // Introduce subcomponents
  import chartsOps from './options.js' // Public profile
  export default {
    mixins: [chartsOps], // The advantage of mixins is that it can directly call the methods and properties in the component
    data() {
      return {
        // The value passed to the subcomponent. Here, take the parameters of the histogram as an example. Please refer to the official website http://www.echartsjs.com/examples/
        echartOpion: {}
      }
    },
    components: {
      echarts
    },
    mounted() {
      // Copy the configuration and assign it to echartOpion. At this time, you can freely modify the properties in echartOpion and assign them to your own data:
     // For example, this.echartOpion.xAxis = xxx
      this.echartOpion = this.clone(this.barOption);
    }
  }
</script>

This is just to introduce the configuration of histogram. Other configurations, such as heat chart and pie chart, can be written in one file or in one file for each kind of chart, which is easy to distinguish

Posted by keyurjbhatt on Sat, 07 Dec 2019 23:57:48 -0800