安装方法:npm install echarts -S
import Echarts from 'echarts'
Vue.use(Echarts);
Vue.prototype.$echarts = Echarts;
<template><div></div></template>
中添加一个div标签,内容如下(style中的width和height是折线图的宽高):<div id="myLine" style="width: 700px; height: 350px"></div>
<script></script>
标签中添加如下代码:export default {
data() {
return {
myLine_option: {
title: {
text: '5分钟平均负载(近一小时)', // 图表标题
x: '100px' // 标题位置,可选值有'center','left','right',{number}(x坐标,单位px)
},
tooltip: {trigger: 'axis'}, // 提示框选项:axis,item,none
// legend是每条折线的配置
legend: {
orient: 'horizontal', // 设置布局方式,默认水平布局,可选值:'horizontal'(水平),'vertical'(垂直)
x: 'right', // 设置水平安放位置,默认全图居中,可选值:'center','left','right',{number}(x坐标,单位px)
y: 'top', // 设置垂直安放位置,默认全图顶端,可选值:'top','bottom','center',{number}(y坐标,单位px)
data: ['5分钟平均负载(近一小时)', '10分钟平均负载(近一小时)'] // 每条折线的标题
},
// 图表距边框的距离,可选值:'百分比',{number}(单位px)
grid: {
top: '16%', // 等价于 y: '16%'
left: '8%',
right: '8%',
bottom: '3%',
containLabel: true
},
xAxis: {
name: '时间', // x轴标题
type: 'category',
data: ['2019-3-4 15:19:20', '2019-3-4 15:20:20', '2019-3-4 15:21:20', '2019-3-4 15:22:20', '2019-3-4 15:23:20', '2019-3-4 15:24:20', '2019-3-4 15:25:20'], // x轴数据
axisLabel: {
rotate: 30, // 旋转角度
interval: 0 // 设置X轴数据间隔几个显示一个,为0表示都显示
},
},
yAxis: {},
// 要展示的数据
series: [{
name: '5分钟平均负载(近一小时)', // 要与legend中data里面的内容一致,否则会导致无法显示折线标题
type: 'line', // 选项有:折线'line', 柱状'bar'
data: [16.7, 18.9, 15.4, 17.3, 13.6, 11.4, 14.9]
},
{
name: '10分钟平均负载(近一小时)',
type: 'line',
data: [17.3, 14.9, 15.4, 13.6, 16.7, 11.4, 18.9]
}
]
},
}
},
methods: {
initChart() {
var myLine = this.$echarts.init(document.getElementById('myLine'));
myLine.setOption(this.myLine_option);
},
},
mounted() {
this.initChart()
}
}
<template>
<div>
<div id="myLine" style="width: 700px; height: 350px"></div>
</div>
</template>
<script>
export default {
data() {
return {
myLine_option: {
title: {
text: '5分钟平均负载(近一小时)',
x: '100px'
},
tooltip: {trigger: 'axis'},
legend: {
orient: 'horizontal',
x: 'right',
y: 'top',
data: ['5分钟平均负载(近一小时)', '10分钟平均负载(近一小时)']
},
grid: {
top: '16%',
left: '8%',
right: '8%',
bottom: '3%',
containLabel: true
},
xAxis: {
name: '时间',
type: 'category',
data: ['2019-3-4 15:19:20', '2019-3-4 15:20:20', '2019-3-4 15:21:20', '2019-3-4 15:22:20', '2019-3-4 15:23:20', '2019-3-4 15:24:20', '2019-3-4 15:25:20'],
axisLabel: {
rotate: 30,
interval: 0
},
},
yAxis: {},
series: [{
name: '5分钟平均负载(近一小时)',
type: 'line',
data: [16.7, 18.9, 15.4, 17.3, 13.6, 11.4, 14.9]
},
{
name: '10分钟平均负载(近一小时)',
type: 'bar',
data: [17.3, 14.9, 15.4, 13.6, 16.7, 11.4, 18.9]
}
]
},
}
},
methods: {
initChart() {
var myLine = this.$echarts.init(document.getElementById('myLine'));
myLine.setOption(this.myLine_option);
},
},
mounted() {
this.initChart()
}
}
</script>
<style>
</style>