直接来步骤
1,安装echarts依赖包
npm install echarts --save
2,在main.js中引入
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
3,在组件中使用
<template>
<div id="app">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template><script>
export default {
name: "app",
methods: {
drawChart() {
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("main"));
// 指定图表的配置项和数据
let option = {
title: {
text: "ECharts 入门示例"
},
tooltip: {},
legend: {
data: ["销量"]
},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
yAxis: {},
series: [
{
name: "销量",
type: "bar",
data: [5, 20, 36, 10, 10, 20]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
},
mounted() {
this.drawChart();
}
};
</script><style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
color: transparent;
}
</style>
4,如果出现init为undefined的时候,将main.js中的
import echarts from "echarts";
//改为
const echarts = require('echarts');
本文详细介绍了如何在Vue项目中使用ECharts库创建柱状图。首先通过npm安装echarts依赖,然后在main.js中全局注册echarts。接着在组件模板中定义绘图容器,并在组件的方法中初始化echarts实例,设置图表配置项和数据,最后调用setOption方法展示图表。若遇到初始化问题,可尝试使用require方式引入echarts。
1万+

被折叠的 条评论
为什么被折叠?



