1:安装echarts
cnpm install echarts --s
2:在需要用图表的地方引入
import echarts from 'echarts'
3.编写echarts.vue
<template>
<div id="index">
<div id="main" style="width: 100%;height: 400px;"></div>
</div>
</template>
<script>
import echarts from 'echarts'
export default {
name: 'index',
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>
4.接口写在mock里面
首先在mock里面新建一个echarts.json
{
"categories": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12"
],
"data": [
3,
2,
4,
4,
5
]
}
5:在build目录下找到webpack.dev.conf.js文件,编写以下代码
// mock code
const express = require('express')
const app = express()
const test = require('../mock/test.json')
const echarts = require('../mock/echarts.json')
const routes = express.Router()
app.use('/api', routes)
// 如果是post请求,那么将get改为post即可
devServer: {
...
before(app){
app.get('/api/test', (req, res) => {
res.json(test)
}),
app.get('/api/echarts', (req, res) => {
res.json(echarts)
})
}
6:浏览器输入http://localhost:8080/api/echarts
7.新代码
<template>
<div id="index">
<div id="main" style="width: 100%;height: 400px;"></div>
</div>
</template>
<script>
import axios from "axios";
import echarts from 'echarts'
export default{
name: "index",
data() {
return {
charts: '',
opinionData: []
}
},
mounted() {
this.getData();
},
methods: {
getData() {
axios.get('http://localhost:8080/api/echarts').then(response => {
console.log(response.data);
this.opinionData =response.data.data;
this.drawLine('main');
}, response => {
console.log("error");
});
},
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
}]
})
},
}
}</script>
网友评论