1.安装
npm install echarts --save
2.引入 在需要使用echarts的页面引入 import * as echarts from 'echarts’
import * as echarts from "echarts";
import { onMounted } from "vue";
echarts 5.0版本接口更新后,echarts 引入方式有变动
不能使用 import echarts from ‘echarts’ ,会报错
3.整体代码
<template>
<div class="echarts">
<div id="barchart" :style="{ width: '380px', height: '300px' }"></div>
</div>
</template>
import * as echarts from "echarts";
import { onMounted } from "vue";
export default {
setup() {
onMounted(() => {
var myChart = echarts.init(document.getElementById("barchart"));
// 指定图表的配置项和数据
var option = {
title: {
text: "ECharts入门",
},
legend: {
data: ["销量"],
},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
},
yAxis: {},
series: [
{
name: "销量",
type: "bar",
data: [13, 25, 46, 21, 40, 20],
},
],
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
return {}
}
}
4.效果图
参考原博文地址:https://blog.youkuaiyun.com/qq_39279706/article/details/112245678