封装的charts使用 vue2

//组件
<template>
  <div ref="chartRef" class="echart"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  data() {
    return {
      chart: null
    }
  },
  methods: {
    init() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chartRef)
      }
    },
    // 防抖
    debounce(fun, delay) {
      let timer
      return function () {
        if (timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          fun()
        }, delay)
      }
    },
    // 渲染图表
    setOptions(option) {
      if (!this.chart) return
      this.chart.setOption(option)
    },
    // 图表重绘
    setResize() {
      if (!this.chart) return
      this.chart.resize()
    }
  },
  mounted() {
    this.init()
    window.addEventListener('resize', () => this.debounce(this.setResize(), 500))
  },
  beforeDestroy() {
    if (!this.chart) return
    this.chart = null
  }
}
</script>

<style lang="scss" scoped>
.echart {
  width: 100%;
  height: 100%;
  min-height: 200px;
}
</style>

使用

<template>
  <div>
    <VueChart ref="lineChartRef" />
  </div>
</template>

<script>
import VueChart from './VueChart'

const lineOption = {
  color: ['#317FF6', '#00FF7E'],
  grid: {
    top: '16%',
    left: '2%',
    right: '2%',
    bottom: '2%',
    containLabel: true
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    top: 10,
    right: 10,
    textStyle: { color: '#FFFFFF' }
  },
  xAxis: {
    type: 'category',
    axisLabel: {
      color: '#C2D7E8',
      fontSize: 12,
      rotate: 45
    },
    axisLine: {
      show: true,
      lineStyle: { color: 'rgba(255, 255, 255, .2)' }
    },
    axisTick: { show: false },
    data: ['星期一', '星期一', '星期一', '星期一', '星期一', '星期一', '星期一', '星期一', '星期一', '星期一', '星期一'],
  },
  yAxis: {
    type: 'value',
    name: '单位:家',
    nameTextStyle: { color: '#C2D7E8' },
    axisLabel: {
      color: '#C2D7E8',
      fontSize: 14
    },
    axisLine: {
      show: true,
      lineStyle: { color: 'rgba(255, 255, 255, .2)' }
    },
    splitLine: {
      lineStyle: {
        type: 'dashed',
        color: 'rgba(255, 255, 255, .2)'
      }
    }
  },
  series: [
    {
      name: '总收入',
      type: 'line',
      smooth: true,
      showSymbol: false,
      areaStyle: {
        // opacity: 0.8,
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: 'rgba(49, 127, 246, .5)'},
            { offset: 1, color: 'rgba(43, 254, 192, 0)' }
          ],
          global: false
        }
      },
      data: [20, 120, 110, 90, 50, 30, 35, 40, 50, 100, 90]
    },
    {
      name: '纯收入',
      type: 'line',
      smooth: true,
      showSymbol: false,
      areaStyle: {
        // opacity: 0.8,
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: 'rgba(43, 254, 192, .5)'},
            { offset: 1, color: 'rgba(43, 254, 192, 0)' }
          ],
          global: false
        }
      },
      data: [90, 50, 30, 35, 20, 120, 110, 90, 30, 35, 10]
    }
  ]
}

export default {
  name: 'Income',
  components: {
    VueChart
  },
  data() {
    return {}
  },
  mounted() {
    this.$refs.lineChartRef.setOptions(lineOption)
  }
}
</script>

相关推荐

  1. charts使用 vue2

    2024-03-25 13:54:02       16 阅读
  2. Vue组件原理和好处

    2024-03-25 13:54:02       30 阅读
  3. Vue3 + TS + Element-Plus Dialog 弹窗组件

    2024-03-25 13:54:02       12 阅读
  4. Vue3 + TS + Element-Plus Table 表格组件

    2024-03-25 13:54:02       10 阅读
  5. Android 工具类

    2024-03-25 13:54:02       21 阅读
  6. 根据ElasticsearchRestTemplate工具类

    2024-03-25 13:54:02       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 13:54:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 13:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 13:54:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 13:54:02       20 阅读

热门阅读

  1. Springboot vue elementui 电子商城系统源码

    2024-03-25 13:54:02       17 阅读
  2. 蓝桥杯刷题--python-31-单调栈

    2024-03-25 13:54:02       19 阅读
  3. 2457. 美丽整数的最小增量

    2024-03-25 13:54:02       20 阅读
  4. 面试宝典:MySQL中索引为什么使用B+树的深度分析

    2024-03-25 13:54:02       15 阅读
  5. es同义词配置规则

    2024-03-25 13:54:02       16 阅读
  6. 天秀基础算法 - 二分查找和二分答案

    2024-03-25 13:54:02       18 阅读
  7. SpringCloud优缺点及适合场景

    2024-03-25 13:54:02       19 阅读
  8. npm 包管理工具:常用命令详解与使用指南

    2024-03-25 13:54:02       15 阅读
  9. kingbaseESV8分区表

    2024-03-25 13:54:02       16 阅读
  10. Github 2024-03-21 开源项目日报 Top10

    2024-03-25 13:54:02       15 阅读
  11. 计算方法(第3版)——学习笔记(一)

    2024-03-25 13:54:02       17 阅读
  12. Python之关键字传参(**kwargs)妙处

    2024-03-25 13:54:02       13 阅读