echarts图表分别在vue2和vue3中的使用示例

1、vue2中的使用:

1、下载这个版本 npm install echarts@4.9.0

//2、在main.js中全局引用:

//import echarts from "echarts"

//Vue.prototype.$echarts=echarts

一般不糊在main中引用 ,应该在当前页面 

  import echarts from 'echarts'

3、 在页面中使用
 
下面的动态宽高是适配
 

<template>
  <div class="myCharts">
    <div class="myChart" ref="myChart" id="myChart" :style="{ width: '8rem', height: '8rem' }"
      _echarts_instance_="ec_1649752202234"></div>
  </div>
</template>

<script>
export default {
  mounted() { this.setChart() },
  methods: {
    setChart() {
      this.$nextTick(()=>{
        var myChart = this.$echarts.init(this.$refs.myChart)
        var option = {
          legend: {
            data: [],
          },
          radar: {
            shape: "circle",
            splitNumber: 5,
            indicator: [
              { name: "知觉辨别", max: 12, axisLabel: { show: true } },
              { name: "想象推理", max: 12 },
              { name: "系列关系", max: 12 },

              { name: "比较推理", max: 12 },
              { name: "类同比较", max: 12 },
            ],
            radius: [5, 120],

            splitArea: {
              areaStyle: {
                color: [
                  "#fefefe",
                  "#f6f8fc",
                  "#fefefe",
                  "#f6f8fc",
                  "#fefefe",
                  "#f6f8fc",
                ],
              },
            },
          },
          series: [
            {
              name: "",
              type: "radar",
              data: [],
            },
          ],
        };
        // 设置实例参数
        myChart.setOption(option);
      })
     
     
    }
  }

};
</script>

<style scoped>
</style>

2、在vue3中的使用:

1、下载echarts 

2、在main.js中全局引入

import * as echarts from "echarts";

// 全局挂载 echarts
app.config.globalProperties.$echarts = echarts;

3、在页面中使用

<template>
  <div class="myCharts">
    <div
      class="myChart"
      ref="myChart"
      id="myChart"
      :style="{ width: '8.4rem', height: '8.4rem' }"
      _echarts_instance_="ec_1649752202234"
    ></div>
  </div>
</template>

<script>
import { getCurrentInstance, onMounted } from "vue";
export default {
  // 雷达图表
  setup() {
    // 通过 internalInstance.appContext.config.globalProperties 获取全局属性或方法
    let internalInstance = getCurrentInstance();
    let echarts = internalInstance.appContext.config.globalProperties.$echarts;

    onMounted(() => {
      const dom = document.getElementById("myChart");
      const myChart = echarts.init(dom); // 初始化echarts实例
      const option = {
        legend: {
          data: [],
        }, 
        radar: {
          shape: "circle",
          splitNumber: 5,
          indicator: [
            { name: "知觉辨别", max: 12, axisLabel: { show: true } },
            { name: "想象推理", max: 12 },
            { name: "系列关系", max: 12 },

            { name: "比较推理", max: 12 },
            { name: "类同比较", max: 12 },
          ],
          radius: [5, 120],

          splitArea: {
            areaStyle: {
              color: [
                "#fefefe",
                "#f6f8fc",
                "#fefefe",
                "#f6f8fc",
                "#fefefe",
                "#f6f8fc",
              ],
            },
          },
        },
        series: [
          {
            name: "",
            type: "radar",
            data: [],
          },
        ],
      };
      // 设置实例参数
      myChart.setOption(option);
    });
    return {};
  },
};
</script>

<style scoped>
.myCharts .myChart {
  margin-left: 0.3rem;
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值