简洁使用 ECharts
通过 npm 获取 echarts
1:第一步
npm install echarts --save,下载
详见“在 webpack 中使用 echarts”
引入 ECharts
通过标签方式直接引入构建好的 echarts 文件
2:第二步
.import echarts from "echarts" 引入
3:第三步
,<div id="main" style="width: 750px;height:400px;"></div>
4:第四步
export default {
methods: {
init: function() {
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("main"));
},
mounted() {
// 调用这个方法
this.init();
}
5 :第五步
// 指定图表的配置项和数据
let option = {
title: {
text: "用户来源"
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "cross",
label: {
backgroundColor: "#6a7985"
}
}
},
legend: {
data: ["华东", "华南", "华北", "西部", "其他"]
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true
},
xAxis: [
{
type: "category",
boundaryGap: false,
data: [
"2017-12-27",
"2017-12-28",
"2017-12-29",
"2017-12-30",
"2017-12-31",
"2018-1-1",
"2018-1-2"
]
}
],
yAxis: [
{
type: "value"
}
],
series: [
{
name: "华东",
type: "line",
stack: "总量",
areaStyle: {},
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: "华南",
type: "line",
stack: "总量",
areaStyle: {},
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: "华北",
type: "line",
stack: "总量",
areaStyle: {},
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: "西部",
type: "line",
stack: "总量",
areaStyle: {},
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: "其他",
type: "line",
stack: "总量",
label: {
normal: {
show: true,
position: "top"
}
},
areaStyle: {},
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
6:第六步
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);