vue项目中使用echarts

1、安装

npm install echarts --save

2、在vue中引入(全局引入)

// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

3、在vue中的使用

1)定义一个盒子,设置宽高

<template>
  <div ref="chart" style="width: 100%; height: 200px"></div>
</template>

2)在data中(其他地方定义也可以,根据项目需求来)定义option数据

dt = [10,20,30,5,10]
 option = {
    // 提示框
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    color: ['#D9D9D9', '#0077FF', '#FFCD29', '#B37700', '#FF4455'],
    // 数据系列
    xAxis: [
      {
        type: 'category',
        data: ['未知', '低危', '中危', '高危', '超危'],
        axisTick: {
          alignWithLabel: true
        }
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: '数量',
        type: 'bar',  // type: bar 是柱形, category是折线
        barWidth: '60%',
        data: this.dt,
        colorBy: 'data',
        itemStyle: {
          normal: {
            label: {
              show: true,
              position: 'top'
            }
          }
        }
      }
    ]
  }

3)初始化echart实例以及数据

methods:{
	 initChart() {
    const dom = this.$refs['chart']  // 获取dom
    if (dom) {
      this.chart = this.$echarts.init(dom)  //  初始化echart, 加载配置
    }
  }
}
created(){
	this.initChart()
}

项目中完整版代码:

<template>
<!-- 给定一个盒子,设置宽高 -->
  <div ref="chart" style="width: 100%; height: 200px"></div>
</template>
<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import { EChartsType } from 'echarts'
export default class ChartTemplate extends ChartTemplateProps {
  $refs!: {
    chart: HTMLDivElement
  }
  dt = [10,20,30,5,10]

  chart = null as null | EChartsType

  image = ''
// 定义option配置对象,放入数据
  option = {
    // 提示框
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    color: ['#D9D9D9', '#0077FF', '#FFCD29', '#B37700', '#FF4455'],
    // 数据系列
    xAxis: [
      {
        type: 'category',
        data: ['未知', '低危', '中危', '高危', '超危'],
        axisTick: {
          alignWithLabel: true
        }
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: '数量',
        type: 'bar',  // type: bar 是柱形, category是折线
        barWidth: '60%',
        data: this.dt,
        colorBy: 'data',
        itemStyle: {
          normal: {
            label: {
              show: true,
              position: 'top'
            }
          }
        }
      }
    ]
  }

  initChart() {
    const dom = this.$refs['chart']  // 获取dom
    if (dom) {
      this.chart = this.$echarts.init(dom)  //  初始化echart, 加载配置
      this.chart.setOption(this.option) // 如果需要修改数据或者配置,调用setOption()方法,传入配置即可,不需要再次init()初始化
      this.transform2ImageUrl()
    }
  }

  updateChart() {
    if (this.chart) {
      this.chart.setOption({
        series: [{ data: this.dt }]
      })
      this.transform2ImageUrl()
    } else {
      this.initChart()
    }
  }

  transform2ImageUrl() {
    console.log('2222', this.chart)
    // 转换图片前重新定宽高,保证图片比例正确,必须异步执行
    setTimeout(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 0)
    setTimeout(() => {
      this.image = this.chart?.getDataURL({  // 导出echarts一般是将其转成图片,图片展示
        pixelRatio: 2,
        backgroundColor: '#fff'
      }) as string
    }, 2000)
  }
  mounted() {
    this.initChart()
  }
}
</script>

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue项目使用echarts图表,你需要先安装echarts库。可以通过npm或yarn来安装echarts: ```bash npm install echarts --save # 或者 yarn add echarts ``` 然后在Vue组件引入echarts使用它来绘制图表。以下是一个简单的例子: ```vue <template> <div class="chart-container"> <div ref="echarts" class="echarts"></div> </div> </template> <script> import echarts from 'echarts' export default { name: 'EchartsDemo', data() { return { chartData: [ { name: '一月', value: 100 }, { name: '二月', value: 200 }, { name: '三月', value: 300 }, { name: '四月', value: 400 }, { name: '五月', value: 500 }, { name: '六月', value: 600 } ] } }, mounted() { this.drawChart() }, methods: { drawChart() { const chartDom = this.$refs.echarts const chart = echarts.init(chartDom) const option = { xAxis: { type: 'category', data: this.chartData.map(item => item.name) }, yAxis: { type: 'value' }, series: [{ data: this.chartData.map(item => item.value), type: 'bar' }] } chart.setOption(option) } } } </script> <style scoped> .echarts { width: 100%; height: 400px; } </style> ``` 在这个例子,我们在组件的`mounted`生命周期钩子调用了`drawChart`方法来绘制图表。方法首先通过`this.$refs.echarts`获取到一个DOM元素,然后使用`echarts.init`方法初始化echarts实例。接着,我们通过设置`option`对象来定义图表的配置,最后调用`chart.setOption`方法来渲染图表。 上面的例子演示了如何使用echarts来绘制一个简单的柱状图。你可以根据需要调整`option`对象的配置来实现其他类型的图表,例如折线图、饼图等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值