首先NPM 安装 ECharts
你可以使用如下命令通过 npm 安装 ECharts
npm install echarts --save
在需要ECharts的页面上,script里面引入 ECharts(有时候不需要做)
这一步有时候是不需要的,我们这次的案例就不需要引入ECharts
import * as echarts from 'echarts';
template部分
<template>
<div id="main"></div>
</template>
script和style部分
<script>
// import * as echarts from 'echarts';
export default {
methods: {
drawChart() {
const myEchart = this.$echarts.init(document.getElementById('main'));
const option = {
title: {
text: 'Stacked Line',
},
tooltip: {
trigger: 'axis',
},
legend: {
data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'],
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true,
},
toolbox: {
feature: {
saveAsImage: {},
},
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
yAxis: {
type: 'value',
},
series: [
{
name: 'Email',
type: 'line',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210],
},
{
name: 'Union Ads',
type: 'line',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310],
},
{
name: 'Video Ads',
type: 'line',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410],
},
{
name: 'Direct',
type: 'line',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320],
},
{
name: 'Search Engine',
type: 'line',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320],
},
],
};
myEchart.setOption(option);
},
},
mounted() {
this.drawChart();
},
};
</script>
<style scoped>
#main {
width: 70%;
height:800px;
margin: auto;
}
</style>
效果
大家可以取到Echarts官网获取更多的示例图
1、 官网
https://echarts.apache.org/zh/index.html
2、 点击示例
3、就会看到很多示例
4、 随便点击一个示例,比如我点击这个示例
5、 就会看到这个页面
6、只需要修改第一个案例里面的script里面的option里面的内容就可以了
如下:
现在看的折线图的option如下:
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
};
将option修改之后,新的折线图就改好了