Vue下载Echarts
npm i echarts -S
echarts官网
快速上手 - Handbook - Apache ECharts2
引入:
import * as echarts from "echarts"
一、示例
EchartsController
@RestController
@RequestMapping("/echarts")
public class EchartsController {
@Resource
private UserService userService;
@RequestMapping("/example")
public Result get() {
Map<String, Object> map = new HashMap<>();
map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
return Result.success(map);
}
}
Home.vue
<template>
<div>
<el-row>
<el-col :span="12">
<div id="main" style="width: 500px;height: 400px"></div>
</el-col>
<el-col :span="12">
<div id="pie" style="width: 500px;height: 400px"></div>
</el-col>
</el-row>
</div>
</template>
<script>
import * as echarts from "echarts"
export default {
name: "Home",
data() {
return {}
},
//页面元素渲染之后再触发
mounted() {
var option = {
xAxis: {
type: 'category',
data: []
},
yAxis: {
type: 'value'
},
series: [
{
data: [],
type: 'line'
},
{
data: [],
type: 'bar'
}
]
};
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
this.request.get("/echarts/example").then(res =>{
//填空
option.xAxis.data = res.data.x
option.series[0].data = res.data.y
option.series[1].data = res.data.y
//数据准备完毕之后在set
myChart.setOption(option);
})
//饼图
var pieOption = {
title: {
text: 'Referer of a Website',
subtext: 'Fake Data',
left: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left'
},
series: [
{
name: 'Access From',
type: 'pie',
radius: '50%',
data: [
{value: 1048, name: 'Search Engine'},
{value: 735, name: 'Direct'},
{value: 580, name: 'Email'},
{value: 484, name: 'Union Ads'},
{value: 300, name: 'Video Ads'}
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
var pieDom = document.getElementById('pie');
var pieChart = echarts.init(pieDom);
pieChart.setOption(pieOption)
}
}
</script>
<style scoped>
</style>
二、折线图
EchartsController
@GetMapping("/members")
public Result members() {
List<User> list = userService.list();
//四个季度
int q1 = 0;
int q2 = 0;
int q3 = 0;
int q4 = 0;
for (User user : list) {
Date createTime = user.getCreateTime();
Quarter quarter = DateUtil.quarterEnum(createTime);
switch (quarter) {
case Q1:
q1 += 1;
break;
case Q2:
q2 += 1;
break;
case Q3:
q3 += 1;
break;
case Q4:
q4 += 1;
break;
default:
break;
}
}
return Result.success(CollUtil.newArrayList(q1, q2, q3, q4));
}
Home.vue
<el-col :span="12">
<div id="main" style="width: 500px;height: 400px"></div>
</el-col>
var option = {
title: {
text: '各季度会员数量趋势图',
subtext: '趋势图',
left: 'center'
},
xAxis: {
type: 'category',
data: ["第一季度","第二季度","第三季度","第四季度"]
},
yAxis: {
type: 'value'
},
series: [
{
data: [],
type: 'line'
},
{
data: [],
type: 'bar'
}
]
};
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
this.request.get("/echarts/members").then(res =>{
//填空
option.series[0].data = res.data
option.series[1].data = res.data
//数据准备完毕之后在set
myChart.setOption(option);
})
三、饼图
//饼图
var pieOption = {
title: {
text: '各季度会员数量比例图',
subtext: '比例图',
left: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left'
},
series: [
{
type: 'pie',
radius: '60%',
//饼图上的文本标签
label:{
normal:{
show:true,
//标签的位置
position:'inner',
textStyle:{
fontWeight:300,
fontSize:14,
color:'#fff'
},
formatter:'{d}%'
}
},
data: [],//填空
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
var pieDom = document.getElementById('pie');
var pieChart = echarts.init(pieDom);
this.request.get("/echarts/members").then(res =>{
pieOption.series[0].data = [
{name:'第一季度',value:res.data[0]},
{name:'第二季度',value:res.data[1]},
{name:'第三季度',value:res.data[2]},
{name:'第四季度',value:res.data[3]},
]
pieChart.setOption(pieOption)
})
}
四、构造
Home.vue
<template>
<div>
<el-row :gutter="10" style="margin-bottom: 60px">
<el-col :span="6">
<el-card style="color: #409EFF">
<div><i class="el-icon-user-solid"/>用户总数</div>
<div style="padding: 10px 0; text-align: center;font-weight: bold">
<el-tag type="primary">100</el-tag>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card style="color: #F56C6C">
<div><i class="el-icon-money"/>销售总量</div>
<div style="padding: 10px 0; text-align: center;font-weight: bold">
¥ 1000000
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card style="color: #67C23A">
<div><i class="el-icon-bank-card"/>收益总额</div>
<div style="padding: 10px 0; text-align: center;font-weight: bold">
¥ 300000
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card style="color: #E6A23C">
<div><i class="el-icon-s-shop"/>门店总数</div>
<div style="padding: 10px 0; text-align: center;font-weight: bold">
20
</div>
</el-card>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<div id="main" style="width: 500px;height: 450px"></div>
</el-col>
<el-col :span="12">
<div id="pie" style="width: 500px;height: 400px"></div>
</el-col>
</el-row>
</div>
</template>
<script>
import * as echarts from "echarts"
export default {
name: "Home",
data() {
return {}
},
//页面元素渲染之后再触发
mounted() {
var option = {
title: {
text: '各季度会员数量趋势图',
subtext: '趋势图',
left: 'center'
},
tooltip: {
trigger: "item"
},
xAxis: {
type: 'category',
data: ["第一季度", "第二季度", "第三季度", "第四季度"]
},
yAxis: {
type: 'value'
},
series: [
{
name:"星巴克",
data: [],
type: 'bar'
},
{
name:"星巴克",
data: [],
type: 'line'
},
{
name:"瑞星咖啡",
data: [],
type: 'bar'
},
{
name:"瑞星咖啡",
data: [],
type: 'line'
}
]
};
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
this.request.get("/echarts/members").then(res => {
//填空
option.series[0].data = res.data
option.series[1].data = res.data
option.series[2].data = [5,6,7,8]
option.series[3].data = [5,6,7,8]
//数据准备完毕之后在set
myChart.setOption(option);
})
//饼图
var pieOption = {
title: {
text: '各季度会员数量比例图',
subtext: '比例图',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter:'{a} <br/>{b}:{c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 'left'
},
series: [
{
name:'星巴克',
type: 'pie',
radius: '60%',
center:['25%','50%'],
//饼图上的文本标签
label: {
normal: {
show: true,
//标签的位置
position: 'inner',
textStyle: {
fontWeight: 300,
fontSize: 14,
color: '#fff'
},
formatter: '{c}/({d}%)'
}
},
data: [],//填空
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
},
{
name:'瑞星咖啡',
type: 'pie',
radius: '40%',
center:['75%','50%'],
//饼图上的文本标签
label: {
normal: {
show: true,
//标签的位置
position: 'inner',
textStyle: {
fontWeight: 300,
fontSize: 14,
color: '#fff'
},
formatter: '{d}%'
}
},
data: [
{name: '第一季度', value: 5},
{name: '第二季度', value: 6},
{name: '第三季度', value: 7},
{name: '第四季度', value: 8},
],//填空
}
]
};
var pieDom = document.getElementById('pie');
var pieChart = echarts.init(pieDom);
this.request.get("/echarts/members").then(res => {
pieOption.series[0].data = [
{name: '第一季度', value: res.data[0]},
{name: '第二季度', value: res.data[1]},
{name: '第三季度', value: res.data[2]},
{name: '第四季度', value: res.data[3]},
]
pieChart.setOption(pieOption)
})
}
}
</script>
<style scoped>
</style>