VUE项目中封装Echart折线图的方法

本文实例为大家分享了VUE项目中封装Echart折线图的具体代码,供大家参考,具体内容如下

封装Echart折线图,可显示多条折线

1. 首先在项目中全局引入Echarts,main.js中:

import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts

2.components中新建组件baseLineChart.vue,以下代码直接复制:

<template>
    <div
      id="baseLineChart"
      ref="baseLineChart"
      :style="{ width: chartWidth, height: chartHeight }"
    />
</template>

<script>
export default {
  props: {
    chartData: {
      type: Array,
      default: () => []
    },
    timeX: {
      type: Array,
      default: () => []
    },
    chartName: {
      type: String,
      default: ''
    },
    chartWidth: {
      type: String,
      default: ''
    },
    chartHeight: {
      type: String,
      default: ''
    },
    seriesName: {
      type: Array,
      default: () => []
    },
    chartUnit: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      baseLineChart: null,
      newChartData: {}
    }
  },
  computed: {
    chartOptions() {
      const options = {
        grid: {
          left: '4%',
          bottom: '8%',
          top: '15%',
          right: '0'
        },
        tooltip: {
          trigger: 'axis'
        },
        color: ['#1879BD', '#03B8DF', '#7B879A'],
        legend: {
          show: true,
          right: '0',
          top: '-1%',
          icon: 'circle'
        },
        xAxis: [
          {
            type: 'category',
            axisTick: { show: false },
            data: []
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '',
            nameTextStyle: {
              padding: [0, 15, 0, 0]
            }
          }
        ],
        series: []
      }
      return options
    }
  },
  watch: {
    chartData: {
      handler(newValue, oldValue) {
        this.newChartData = newValue
        this.initData()
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      if (this.chartData) {
        this.initData()
      }
    })
  },
  methods: {
    initChart() {
      const _this = this
      _this.baseLineChart = _this.$echarts.init(this.$refs.baseLineChart)
      window.addEventListener('resize', function () {
        _this.baseLineChart.resize()
      })
    },
    initData() {
      let sData = []
      if (this.chartData) {
        sData = this.chartData.map((val, index) => {
          return {
            data: val,
            name: this.seriesName[index],
            type: 'line'
          }
        })
        this.chartOptions.series = sData
      }
      this.chartOptions.xAxis[0].data = this.timeX
      this.chartOptions.yAxis[0].name = `单位(${this.chartUnit})`
      this.baseLineChart.setOption(this.chartOptions, true)
    }
  }
}
</script>

配置项根据自身项目来定制。

3、在组件中引入:

<template>
  <div>
      <baseLine
        :chart-data="chartData"
         :time-x="timeX"
         chart-name="OEE"
         chart-width="1700px"
         chart-height="350px"
         :series-name="seriesName"
         chart-unit="%"
          />
    </div>
</template>
import baseLine from '@/components/charts/baseLineChart.vue'
<script>
export default {
 components: {
    baseLine
  },
 data() {
   return {
     timeX: [],
     chartData:[]
     seriesName: ['白班', '晚班']
   }
 },
}
</script>

chart-width 图表宽度
chart-height 图表高度
chart-unit 图表数据的显示单位
timeX为X轴数据,一般为时间数组 [‘1-1’,‘1-2’,‘1-3’ ];
chartData为折线数据,多条数据格式 [ [1,2,3],[4,5,6] ]
seriesName为每条折线对应名称

同理可封装其他图表

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • vue+echarts实现动态折线图的方法与注意

    之前公司有个绘制实时盈利率折线图的需求,实现的还不错,今天来分享下vue+echarts实现动态折线图的方法. 实现代码 <template> <div id="myChart"></div> </template> <script> import echarts from 'echarts' export default { name: 'DynamicLineChart', data () { return { // 实时

  • Vue+Echarts实现简单折线图

    本文实例为大家分享了Vue+Echarts实现简单折线图的具体代码,供大家参考,具体内容如下 Vue+Echarts实现一个折线图,打开vue的项目: 1.在项目里面安装echarts npm install echarts --save 2.在需要用图表的地方引入 import echarts from 'echarts' 3.打开my.vue 继续写代码,代码如下: <template>     <!--为echarts准备一个具备大小的容器dom-->     <div

  • vue使用ECharts实现折线图和饼图

    在开发后台管理项目时,需要统计后台用户数据,以折线图和饼图的形式显示,这边采用apache-echarts来实现需求. 1.安装echarts和引入 npm install echarts --save import * as echarts from 'echarts'; 2.使用echarts实现pie图 <template> <div id="chartPie" class="pie-wrap"></div> </te

  • Vue+Echarts实现柱状折线图

    本文实例为大家分享了Vue+Echarts实现柱状折线图的具体代码,供大家参考,具体内容如下 vue处理json数据渲染柱状折线图 HTML: <div id="lineCharts" style="width: 100%; height: 500px; margin-top: 20px"></div> JS: drawLineCharts() {         let data = {sid: "02fertdfg0221&qu

  • 在vue中使用echarts(折线图的demo,markline用法)

    公司最近在用vue开发项目,项目接近尾声了,趁休息时间写点demo-- vue引入echarts(折线图的demo) 主要是解决引入echarts,markline的使用(基准线) 这是demo的效果图: vue脚手架不多赘述 1.安装依赖 cnpm install echarts -S 2.在main.js中引入echarts import echarts from 'echarts' 3.在main.js中安装 Vue.prototype.echarts = echarts; 一般来说能正常

  • vue+echarts实现可拖动节点的折线图(支持拖动方向和上下限的设置)

    本篇文档主要是利用echarts实现可拖动节点的折线图,在echarts中找到了一个demo,传送门:https://echarts.baidu.com/examples/editor.html?c=line-draggable,但是不是用vue写的,并且在改写为vue组件的过程中遇到了很多问题,在百度过程中发现并没有相关的文档,所以决定自己开发,并在demo的基础上开发了一些实用的功能,所以把这个过程记录下来.文档中还有很多不够完善的地方,欢迎讨论哈! 需求:制作一个折线图用于显示当前24小时

  • vue实现折线图 可按时间查询

    本文实例为大家分享了vue实现可按时间查询的折线图的具体代码,供大家参考,具体内容如下 1.vue前端 //查询条件 <template> <el-date-picker v-model="listQuery.toptime" :picker-options="pickerOptions" style="width: 380px" type="daterange" clearable range-separa

  • vue+echarts实现多条折线图

    本文实例为大家分享了vue+echarts实现多条折线图的具体代码,供大家参考,具体内容如下 数据未使用json格式,直接写在页面 大致效果 页面代码: <template>     <!--为echarts准备一个具备大小的容器dom-->     <div id="main" style="width: 100%;height: 300px;"></div> </template> <script

  • 基于mpvue小程序使用echarts画折线图的方法示例

    第一次使用mpvue框架来写小程序,项目开发直接搬用mpvue-shop(一个仿网易严选的小程序开发项目),项目结构清楚,实现了大部分功能,对于初次使用mpvue的小伙伴们来说,是一个很好的选择. 关于组件的选择: 1.echarts-for-weixin,官方echarts的小程序版本.使用参考:echarts-for-weixin介绍,如果你是原生开发小程序版本,这个组件非常适合你,开发过程中可使用echarts官方提供的所有配置和Api,但并不适合mpvue项目. 2.wx-charts,

  • vue使用echarts实现折线图

    本文实例为大家分享了vue使用echarts实现折线图的具体代码,供大家参考,具体内容如下 效果图: 代码: <template>             <div class="proCharts" ref='charts'>             </div> </template> <script> import echarts from 'echarts'   //npm install echarts@4.9.0

随机推荐