vue2 Echart 设置图表、resize并自定义tooltip

【关键思路】

1.在data中设置一个 chartArr 变量来存放每一个echart Init的实例,后面再想操作各个图表的 resize 的时候就可以直接循环 chartArr 数组来实现

2.在写 drawChart 方法的时候,将id和option抽离出去(同理后面也可以把数据data等抽出去,自己组合想要的内容),然后在mounted里调用各个表的id和option。如  this.drawChart("chartBarLine", optionBarLine);  即可

3. tooltip中的icon需要在 iconfont 中自行下载

【完整代码】

<template>
  <div>
    <div id="chartBarLine" :style="{ width: '100%', height: '200px', marginTop: '100px' }"></div>
    <div id="chartBarLine2" :style="{ width: '100%', height: '200px', marginTop: '100px' }"></div>
  </div>
</template>

<script>

// 柱状折线图
const optionBarLine = {
  tooltip: {
    show: true,
    trigger: "axis",
    backgroundColor: "#fff",
    textStyle: {
      color: "#333",
      fontSize: 12
    },
    padding: [0, 8, 8, 8],
    extraCssText: 'box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);',
    formatter: function (params) {
      var html = [];
      html.push('<div style="font-size:12px;">');
      html.push('<div style="margin :15px 0 6px  0;">' + params[0].name + '</div>');
      for (var i = 0; i < params.length; i++) {
        html.push('<div style="margin :0 0 8px  0; min-width:120px" ><span style="display: inline-block;vertical-align: middle;margin-right: 8px; width: 6px;height: 6px;background: ' + params[i].color + ';border-radius: 50%;"></span><span style="display: inline-block; margin-right: 3px;color: #666;">' + params[i].seriesName + '</span>');
        html.push('<span>' + params[i].value + '</span>');
        html.push('<span style="display: inline-block; margin: 0 8px 0 16px;color: #666;">text2: </span>');
        html.push('<span>' + params[i].value + '%</span> <i class="iconfont icon-decline" style="color: #e14f1b; font-size: 12px;"></i> <i class="iconfont icon-rise" style="color: #0cbb6b; font-size: 12px;"></i>');
        html.push("</div>");
      }
      html.push("</div>");
      console.log(params);
      return html.join("");
    },
  },
  legend: {
    data: ['Evaporation', 'Precipitation', 'Temperature']
  },
  xAxis: [
    {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisPointer: {
        type: 'shadow'
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: 'Precipitation',
      min: 0,
      max: 250,
      interval: 50,
      axisLabel: {
        formatter: '{value} ml'
      }
    },
    {
      type: 'value',
      name: 'Temperature',
      min: 0,
      max: 25,
      interval: 5,
      axisLabel: {
        formatter: '{value} °C'
      }
    }
  ],
  series: [
    {
      name: 'Evaporation',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' ml';
        }
      },
      data: [
        2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
      ]
    },
    {
      name: 'Precipitation',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' ml';
        }
      },
      data: [
        2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
      ]
    },
    {
      name: 'Temperature',
      type: 'line',
      yAxisIndex: 1,
      tooltip: {
        valueFormatter: function (value) {
          return value + ' °C';
        }
      },
      data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
    }
  ]
};
// 柱状折线图 END

export default {
  data() {
    return {
      chartArr: [],
    }
  },
  mounted() {
    this.drawChart("chartBarLine", optionBarLine);
    this.drawChart("chartBarLine2", optionBarLine);
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
    if (this.chartArr) {
      for (var i = 0; i < this.chartArr.length; i++) {
        this.chartArr[i].dispose()// 清理图表实例
      }
    }
  },
  methods: {
    drawChart(name, optionName) {
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById(name))
      this.chartArr.push(myChart)
      // 配置 option
      let option = optionName
      // 绘制图表
      myChart.setOption(option)

    },
    handleResize() {
      if (this.chartArr) {
        for (var i = 0; i < this.chartArr.length; i++) {
          this.chartArr[i].resize()
        }
      }
    }
  }
}
</script>

<style scoped lang="less"></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Vue3 中画 Echarts 环形图表,可以按照以下步骤进行: 1. 安装 EchartsVue-Echarts: ``` npm install echarts vue-echarts@5.0.0-beta.5 ``` 2. 在需要使用的组件中引入 EchartsVue-Echarts: ```javascript import * as echarts from 'echarts' import { use } from 'echarts/core' import { CanvasRenderer } from 'echarts/renderers' import { PieChart } from 'echarts/charts' import { LegendComponent, TooltipComponent } from 'echarts/components' import VueECharts from 'vue-echarts' use([CanvasRenderer, PieChart, LegendComponent, TooltipComponent]) export default { components: { VueECharts } } ``` 3. 在组件的模板中使用 Vue-Echarts 组件,并传入需要渲染的数据和 Echarts 实例: ```vue <template> <div> <vue-echarts :options="options" :auto-resize="true" :renderer="'canvas'" :chart="chart"></vue-echarts> </div> </template> <script> export default { data() { return { chart: null, options: { title: { text: '环形图示例' }, tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', left: 10, data: ['数据1', '数据2', '数据3', '数据4', '数据5'] }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ { value: 335, name: '数据1' }, { value: 310, name: '数据2' }, { value: 234, name: '数据3' }, { value: 135, name: '数据4' }, { value: 1548, name: '数据5' } ] } ] } } }, mounted() { this.chart = echarts.init(this.$refs.chart) } } </script> ``` 这样就可以在 Vue3 中画出一个简单的 Echarts 环形图表了。注意,在组件 mounted 钩子函数中,需要使用 Echarts.init 方法初始化 Echarts 实例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值