vue3中引入echarts示例

vue3中引入echarts

  • 环境
"vue": "^3.2.13",
"echarts": "^5.4.1",
  • 安装
npm install echarts --save
  • 配置
  1. 在main.js中配置
import { createApp } from 'vue'
import App from './App.vue'
import * as echarts from "echarts";

const app = createApp(App)
app.echarts = echarts
app.config.globalProperties.$echarts = echarts
app.mount('#app')
  • 折线图使用实例(没有安装axios和less自行安装)
<template>
  <div class="home">
    <div id="main" class="main"></div>
  </div>
</template>
<script>
import axios from 'axios';

export default {
  mounted() {
    var ROOT_PATH = 'https://echarts.apache.org/examples';

    var myChart = this.$echarts.init(document.getElementById('main'));
    console.log(this.$echarts);

    axios.get(ROOT_PATH + "/data/asset/data/life-expectancy-table.json").then((_rawData) => {
      let { data } = _rawData
      run(data);
    });
    
    let run =  (_rawData) => {
      // var countries = ['Australia', 'Canada', 'China', 'Cuba', 'Finland', 'France', 'Germany', 'Iceland', 'India', 'Japan', 'North Korea', 'South Korea', 'New Zealand', 'Norway', 'Poland', 'Russia', 'Turkey', 'United Kingdom', 'United States'];
      const countries = [
        'Finland',
        'France',
        'Germany',
        'Iceland',
        'Norway',
        'Poland',
        'Russia',
        'United Kingdom'
      ];
      const datasetWithFilters = [];
      const seriesList = [];
      this.$echarts.util.each(countries, (country) => {
        var datasetId = 'dataset_' + country;
        datasetWithFilters.push({
          id: datasetId,
          fromDatasetId: 'dataset_raw',
          transform: {
            type: 'filter',
            config: {
              and: [
                { dimension: 'Year', gte: 1950 },
                { dimension: 'Country', '=': country }
              ]
            }
          }
        });
        seriesList.push({
          type: 'line',
          datasetId: datasetId,
          showSymbol: false,
          name: country,
          endLabel: {
            show: true,
            formatter:  (params) => {
              return params.value[3] + ': ' + params.value[0];
            }
          },
          labelLayout: {
            moveOverlap: 'shiftY'
          },
          emphasis: {
            focus: 'series'
          },
          encode: {
            x: 'Year',
            y: 'Income',
            label: ['Country', 'Income'],
            itemName: 'Year',
            tooltip: ['Income']
          }
        });
      });
      myChart.setOption({
        animationDuration: 1000,
        dataset: [
          {
            id: 'dataset_raw',
            source: _rawData
          },
          ...datasetWithFilters
        ],
        title: {
          text: 'Income of Germany and France since 1950'
        },
        tooltip: {
          order: 'valueDesc',
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          nameLocation: 'middle'
        },
        yAxis: {
          name: 'Income'
        },
        grid: {
          right: 140
        },
        series: seriesList
      });
    }
  },
}
</script>

<style lang="less" scoped>
.main {
  margin: 0 auto;
  width: 1200px;
  height: 400px;
}
</style>
  • 实例效果图(实际开发可以把mounted中的代码封装为函数)
    请添加图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值