安装依赖包
npm i echarts -S
在组件内引入echarts
improt echarts from 'echarts'
html部分
<div id="chart" class="chart" style="height:300px;width:550px;"/>
js部分
methods: {
setChart() {
// 基于准备好的dom,初始化echarts实例
let chart = echarts.init(document.getElementById("chart"));
// 指定图表的配置项和数据
let option = {
// x轴是类目轴(离散数据),必须通过data设置类目数据
xAxis: {
type: "category",
data: ["一", "二", "三", "四", "五", "六", "日"], // 可以从数据库中取来
name: "日期"
},
// y轴是数据轴(连续数据)
yAxis: {
type: "value",
show: true,
name: "订单数"
},
// 系列列表。每个系列通过 type 决定自己的图表类型
series: [
{
// 系列中的数据内容数组
data: [100, 932, 901, 934, 1290, 1330, 1320], //数据都是从数据库中来
// 折线图
type: "bar" // line 线形图 bar 柱状图
}
],
title: {
text: "订单增长柱状图"
},
tooltip: {
trigger: "item"
}
};
chart.setOption(option);
},
},
mounted() {
this.setChart()
}
为什么不在created中调用方法,要在mounted中调用呢?