第二十六节 Vue3集成并使用ECharts相关组件

亮子 2024-09-20 12:07:21 982 0 0 0

1、安装ECharts

npm install echarts --save

2、显示折线图

<template>
    <div>
        <el-button type="primary" @click="updateData">更新数据</el-button>
    </div>
    <div>
        <div id="main" style="width: 600px; height: 400px;"></div>
    </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';
import { onMounted, ref } from 'vue'

var myChart = null;

//
const updateData = () => {
    let xdata = ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'];
    let sdata = [5, 20, 36, 10, 10, 200];

    myChart.setOption({
    title: {
        text: 'ECharts 入门示例'
    },
    tooltip: {},
    xAxis: {
        data: xdata
    },
    yAxis: {},
    series: [
        {
        name: '销量',
        type: 'bar',
        data: sdata
        }
    ]
    });
}

//
onMounted(() => {
    // 基于准备好的dom,初始化echarts实例
    myChart = echarts.init(document.getElementById('main'));
    // 绘制图表
    myChart.setOption({
    title: {
        text: 'ECharts 入门示例'
    },
    tooltip: {},
    xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
    },
    yAxis: {},
    series: [
        {
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
        }
    ]
    });
});

</script>

<style scoped>

</style>