您的位置:首页 > Web前端 > Vue.js

在vue模板中使用echarts实现一个图表

2020-06-03 05:58 609 查看

1.下载echarts模块

npm i cnpm install echarts --save

导入

在你想要使用echarts的页面里导入

import echarts from 'echarts'

使用

定义一个echarts容器

记得设置宽高

<div id="main" style="width: 600px;height: 400px;"></div>

实例化一个echarts对象

this.charts = echarts.init(document.getElementById('main'))
this.charts.setOption({
tooltip: {
trigger: 'axis'
},
legend: {
data: ['安全温度图']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},

toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ["1","2","3","4","5",]

},
yAxis: {
type: 'value'
},

series: [{
name: '安全温度图',
type: 'line',
stack: '总量',
data: this.opinionData
}]
})

在mounted中调用

mounted(){
this.$nextTick(function() {
this.drawLine()
})
}

代码

下面是这个dome的代码

<template>
<!--为echarts准备一个具备大小的容器dom-->
<div id="main" style="width: 600px;height: 400px;"></div></template>
<script>
import echarts from 'echarts'export default {
name: '',
data() {
return {
charts: '',
opinionData: ["3", "2", "4", "4", "5"]
}
},
methods: {
drawLine(id) {
this.charts = echarts.init(document.getElementById(id))
this.charts.setOption({
tooltip: {
trigger: 'axis'
},
legend: {
data: ['安全温度图']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},

toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ["1","2","3","4","5",]

},
yAxis: {
type: 'value'
},

series: [{
name: '安全温度图',
type: 'line',
stack: '总量',
data: this.opinionData
}]
})}
},
//调用
mounted() {
this.$nextTick(function() {
this.drawLine('main')
})
}
}
</script>
<style scoped>
* {
margin: 0;
padding: 0;
list-style: none;
}
</style>

效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: