1、柱状图
1、1普通柱状图,给阴影部分添加点击事件
<template>
<div id="bar1"></div>
</template>
<script>
import echarts from "echarts";
import api from "../../api/sendRequest";
export default {
name: "charts",
data() {
return {
barData:{
"code": 1, //状态码
"msg": "查看成功!", //状态信息
"data": { //内容
"type": "bar", //类型
"title": "cpu使用率", //标题
"xUnit": "小时", //x轴单位
"yUnit": "%", //y轴单位
"xAxis": ["10","11","12","13","14","15","16","17","18","19","20","21","22","23"],//x轴内容
"yAxis": [9.64,6.9,6.76,6.17,5.49,7.42,9.37,8.62,15.99,14.92,8.31,4.36,4.52,4.52]//y轴内容
}
},
myChart:null,
searchForm:{},
barX1:[],
barY1:[],
}
},
mounted(){
let _this = this;
window.addEventListener("resize",()=>{
_this.myChart.resize()
})
this.getApiBar()
},
methods:{
//柱状图
getApiBar(){
let search = {
start_time:'',
end_time:''
}
search.start_time = this.searchForm.start_time
search.end_time = this.searchForm.end_time
api
.getApiBar(search)
.then(res => {
this.barX1 = []
this.barY1 = []
if(res.data.code === 1){
// console.log(res.data.data)
this.barData = res.data
this.barX1 = res.data.data.xAxis
this.barY1 = res.data.data.yAxis
this.getBar1()
}
}).catch(err => {
console.log(err);
});
},
//柱状图
getBar1(){
this.myChart = echarts.init(document.getElementById('bar1'))
let unit = this.barData.data.title
this.myChart.setOption({
title: {
text: this.barData.data.title,
left: "center",
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
top: "16%",
containLabel: true
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: function(params, ticket, callback) {
var res = params[0].name
return res +'时,' + unit+':' + params[0].value + '%'
}
},
xAxis: {
type: 'category',
name:this.barData.data.xUnit,
data: this.barX1,
axisTick: {show: false},
},
yAxis: {
type: 'value',
name:this.barData.data.yUnit,
splitLine: {
show: true,
lineStyle:{
color: ['#f0f0f0'],
width: 1,
type: 'solid'
}
},
axisTick: {show: false},
},
series: [{
data: this.barY1,
type: this.barData.data.type,
width:3,
itemStyle:
{
width:3,
},
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(139, 152, 233,1)'
}])
}]
})
//点击事件
var _this = this
this.myChart.on('click', function(params) {
// console.log(params);
})
//阴影点击
this.myChart.getZr().on('click',params=>{
var pointInPixel= [params.offsetX, params.offsetY]
var pointInGrid = this.myChart.convertFromPixel('grid', pointInPixel);
// console.log(pointInGrid)
if (this.myChart.containPixel('grid', pointInPixel)) {
this.chart_x = this.myChart.getOption().xAxis[0].data[pointInGrid[0]]
// console.log(this.chart_x)
}
})
},
},
}
</script>
<style scoped>
#bar1{
width:100%;
height: 50%;
float: left;
}
</style>
1、2叠加柱状图
<template>
<div id="bar2"></div>
</template>
<script>
import echarts from "echarts";
import api from "../../api/sendRequest";
export default {
name: "charts",
data() {
return {
barData:{
"code": 1, //状态码
"msg": "查看成功!", //状态信息
"data": { //内容
"type": "bar", //类型
"title": "cpu使用率", //标题
"xUnit": "小时", //x轴单位
"yUnit": "%", //y轴单位
"xAxis": ["10","11","12","13","14","15","16","17","18","19","20","21","22","23"],//x轴内容
"yAxis": [9.64,6.9,6.76,6.17,5.49,7.42,9.37,8.62,15.99,14.92,8.31,4.36,4.52,4.52]//y轴内容
}
},
myChart:null,
searchForm:{},
barX1:[],
barY1:{},
barY2:{}
}
},
mounted(){
let _this = this;
window.addEventListener("resize",()=>{
_this.myChart.resize()
})
this.getApiBar()
},
methods:{
//柱状图
getApiBar(){
let search = {
start_time:'',
end_time:''
}
search.start_time = this.searchForm.start_time
search.end_time = this.searchForm.end_time
api
.getApiBarMore(search)
.then(res => {
this.barX1 = []
if(res.data.code === 1){
// console.log(res.data.data)
this.barData = res.data
this.barX1 = res.data.data.xAxis
this.barY1 = res.data.data.yAxis[0]
this.barY2 = res.data.data.yAxis[1]
this.getbar2()
}
}).catch(err => {
console.log(err);
});
},
//柱状图
getbar2(){
this.myChart3 = echarts.init(document.getElementById('bar2'))
let unit = this.barData.data.title
this.myChart3.setOption({
title: {
text: this.barData.data.title,
left: "center",
},
grid: {
left: "3%",
right: "12%",
bottom: "3%",
top: "16%",
containLabel: true
},
legend: {
data: [],
x: "200px", // 对齐方式
itemGap: 200 // item之间的距离
},
xAxis: {
axisTick: {show: false},
name:this.barData.data.xUnit,
type: "category",
data: this.barX1,
splitLine: {
show: false
},
axisLabel: {
// nterval: 0,
// rotate: 40
}
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow"
}
},
yAxis: {
name:this.barData.data.yUnit,
axisTick: {show: false},
type: "value",
axisLabel: {
showMaxLabel: false
},
splitLine: {
show: false,
lineStyle:{
color:"#F2F2F2",
}
}
},
series: [
{
data: this.barY1.count,
type: this.barData.data.type,
name: this.barY1.projectName,
itemStyle: {
normal: {
barBorderRadius: 0,
color:"#8D95EC"
// color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [//渐变色
// {
// offset: 0,
// color: "#8D95EC"
// },
// {
// offset: 1,
// color: "#0844E4"
// }
// ])
}
}
},
{
// 背景色
type: "pictorialBar",
stack: "",
name: this.barY2.projectName,
symbol: "fixed",
z: -50,
data: this.barY2.count,
itemStyle: {
normal: {
color: "#92C8F3"
}
}
}
]
})
//点击事件
var _this = this
this.myChart3.on('click', function(params) {
// console.log(params);
})
//阴影点击
this.myChart3.getZr().on('click',params=>{
var pointInPixel= [params.offsetX, params.offsetY]
var pointInGrid = this.myChart3.convertFromPixel('grid', pointInPixel);
// console.log(pointInGrid)
if (this.myChart3.containPixel('grid', pointInPixel)) {
this.chart_x = this.myChart3.getOption().xAxis[0].data[pointInGrid[0]]
// console.log(this.chart_x)
}
})
},
},
}
</script>
<style scoped>
#bar2{
width:50%;
height: 50%;
float: left;
}
</style>
1、3 X,Y轴互换柱状图
<template>
<div id="bar3"></div>
</template>
<script>
import echarts from "echarts";
import api from "../../api/sendRequest";
export default {
name: "charts",
data() {
return {
barData:{
"code": 1, //状态码
"msg": "查看成功!", //状态信息
"data": { //内容
"type": "bar", //类型
"title": "cpu使用率", //标题
"xUnit": "小时", //x轴单位
"yUnit": "%", //y轴单位
"xAxis": ["10","11","12","13","14","15","16","17","18","19","20","21","22","23"],//x轴内容
"yAxis": [9.64,6.9,6.76,6.17,5.49,7.42,9.37,8.62,15.99,14.92,8.31,4.36,4.52,4.52]//y轴内容
}
},
myChart:null,
searchForm:{},
barX1:[],
barY1:[],
}
},
mounted(){
let _this = this;
window.addEventListener("resize",()=>{
_this.myChart.resize()
})
this.getApiBar()
},
methods:{
//柱状图
getApiBar(){
let search = {
start_time:'',
end_time:''
}
search.start_time = this.searchForm.start_time
search.end_time = this.searchForm.end_time
api
.getApiBar(search)
.then(res => {
this.barX1 = []
this.barY1 = []
if(res.data.code === 1){
// console.log(res.data.data)
this.barData = res.data
this.barX1 = res.data.data.xAxis
this.barY1 = res.data.data.yAxis
this.getbar3()
}
}).catch(err => {
console.log(err);
});
},
//柱状图
getbar3(){
this.myChart3 = echarts.init(document.getElementById('bar3'))
let unit = this.barData.data.title
this.myChart3.setOption({
title: {
text: this.barData.data.title,
left: "center",
},
grid: {
left: "3%",
right: "12%",
bottom: "3%",
top: "16%",
containLabel: true
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: function(params, ticket, callback) {
var res = params[0].name
return res +'时,' + unit+':' + params[0].value + '%'
}
},
xAxis: {
type: 'value',
name:this.barData.data.yUnit,
data: this.barY1,
splitLine: {
show: true,
lineStyle:{
color: ['#f0f0f0'],
width: 1,
type: 'solid'
}
},
axisTick: {show: false},
},
yAxis: {
name:this.barData.data.xUnit,
type: 'category',
data: this.barX1,
axisTick: {show: false},
},
series: [{
data: this.barY1,
type: this.barData.data.type,
width:3,
itemStyle:
{
width:3,
},
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(139, 152, 233,1)'
}])
}]
})
//点击事件
var _this = this
this.myChart3.on('click', function(params) {
// console.log(params);
})
//阴影点击
this.myChart3.getZr().on('click',params=>{
var pointInPixel= [params.offsetX, params.offsetY]
var pointInGrid = this.myChart3.convertFromPixel('grid', pointInPixel);
// console.log(pointInGrid)
if (this.myChart3.containPixel('grid', pointInPixel)) {
this.chart_x = this.myChart3.getOption().xAxis[0].data[pointInGrid[0]]
// console.log(this.chart_x)
}
})
},
},
}
</script>
<style scoped>
#bar3{
width:50%;
height: 50%;
float: left;
}
</style>
2、折线图,渐变色折线图
<template>
<div id="line3"></div>
</template>
<script>
import echarts from "echarts";
import api from "../../api/sendRequest";
export default {
name: "charts",
data() {
return {
lineData:{
"code": 1, //状态码
"msg": "查看成功!", //状态信息
"data": { //内容
"type": "line", //类型
"title": "cpu使用率", //标题
"xUnit": "小时", //x轴单位
"yUnit": "%", //y轴单位
"xAxis": ["10","11","12","13","14","15","16","17","18","19","20","21","22","23"],//x轴内容
"yAxis": [9.64,6.9,6.76,6.17,5.49,7.42,9.37,8.62,15.99,14.92,8.31,4.36,4.52,4.52]//y轴内容
}
},
myChart:null,
searchForm:{},
lineX1:[],
lineY1:{},
lineY2:{},
lineY3:{},
legendList:[]
}
},
mounted(){
let _this = this;
window.addEventListener("resize",()=>{
_this.myChart.resize()
})
this.getApiLine()
},
methods:{
//折线图
getApiLine(){
let search = {
start_time:'',
end_time:''
}
search.start_time = this.searchForm.start_time
search.end_time = this.searchForm.end_time
api
.getApiLineMore(search)
.then(res => {
this.lineX1 = []
if(res.data.code === 1){
// console.log(res.data.data)
this.lineData = res.data
this.lineX1 = res.data.data.xAxis
this.lineY1 = res.data.data.yAxis[0]
this.lineY2 = res.data.data.yAxis[1]
this.lineY3 = res.data.data.yAxis[2]
this.legendList = []
this.legendList.push(this.lineY1.projectName)
this.legendList.push(this.lineY2.projectName)
this.legendList.push(this.lineY3.projectName)
this.getLine()
}
}).catch(err => {
console.log(err);
});
},
//折线图
getLine(){
this.myChart = echarts.init(document.getElementById('line3'))
let unit = this.lineData.data.title
this.myChart.setOption({
title: {
text: this.lineData.data.title,
left: "center",
subtext:'最近15日,门宽监测最低100.00cm,最高100.00cm,最大偏差0.00cm,平均与基准偏差20.00cm',
},
grid: {
left: "3%",
right: "4%",
bottom: "10%",
top: "20%",
containLabel: true
},
legend: {
show: true,
data: this.legendList,
left: "right",
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
},
// formatter: function(params, ticket, callback) {
// var res = params[0].name
// return res +'时,' + unit+':' + params[0].value + '%'
// }
},
xAxis: {
type: 'category',
name:this.lineData.data.xUnit,
data: this.lineX1,
axisTick: {show: false},
},
yAxis: [
{
splitLine: { show: false },
axisTick: {show: false},
name: "楼高(层)",
type: "value",
position: "left",
axisLabel: {
formatter: "{value}"
},
},
{
splitLine: { show: false },
axisTick: {show: false},
name: "速度(m/s)",
nameLocation: "end",
min: 0,
max: 5,
type: "value",
position: "right",
axisLabel: {
formatter: "{value}"
},
}
],
series: [
{
data: this.lineY1.count,
name:this.lineY1.projectName,
type: this.lineData.data.type,
width:3,
smooth: true,
symbolSize:10,
lineStyle:{
width:2
},
itemStyle: {
color: 'rgba(139, 152, 233,100)',
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(139, 152, 233,100)'
}, {
offset: 1,
color: 'rgba(255,255, 255,0)'
}])
},
},
{
name:this.lineY2.projectName,
type: this.lineData.data.type,
width:3,
smooth: true,
symbolSize:10,
lineStyle:{
width:2
},
itemStyle: {
color: 'rgb(146, 201, 241)',
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(146, 201, 241)'
}, {
offset: 1,
color: 'rgba(255,255, 255,0)'
}])
},
markLine: {
symbol: "circle",
label: {
formatter: "额定最大速度:{c}",
position: "middle" //将警示值放在哪个位置,三个值“start”,"middle","end" 开始 中点 结束
},
lineStyle: {
width:3,
// normal: {
// color: "#00BFFF",
// type: "dashed"
// }
},
data: [{
yAxis: ['2','2','2','2','2','2','2','2','2','2'],
}]
},
data: this.lineY2.count,
},
{
name: this.lineY3.projectName,
type: "bar",
barGap: "-100%",
// barWidth: 20,
label: {
normal: {
color: "#EEEEEE"
}
},
itemStyle: {
normal: {
borderColor: "rgba(255, 255, 255, .1)",
borderWidth: "10",
barBorderRadius: 0,
color: "rgba(220, 220, 220, 0.3)"
},
},
z: -12,
data: this.lineY3.count,
},
]
})
//点击事件
var _this = this
this.myChart.on('click', function(params) {
// console.log(params);
})
},
},
}
</script>
<style scoped>
#line3{
width:100%;
height: 50%;
float: left;
}
</style>
3、饼图–内外圈带阴影
<template>
<div id="pie1"></div>
</template>
<script>
import echarts from "echarts";
import api from "../../api/sendRequest";
export default {
name: "charts",
data() {
return {
pieData:{
"code": 1, //状态码
"msg": "查询成功!", //状态信息
"data": { //内容
"type": "pie", //图形类别
"title": "饼图", //标题
"legend":["坤和求是里11栋","坤和求是里18栋","嘉里云荷廷12栋","嘉里云荷廷8栋","新昌人民医院"],//图例
"result_list": [ //列表内容
{"name": "坤和求是里11栋","value": 26},
{"name": "坤和求是里18栋","value": 23},
{"name": "嘉里云荷廷12栋","value": 21},
{"name": "嘉里云荷廷8栋","value": 21},
{"name": "新昌人民医院","value": 16}
]
}
},
myChart:null,
searchForm:{},
pieList:[],
title:[],
}
},
mounted(){
let _this = this;
window.addEventListener("resize",()=>{
_this.myChart.resize()
})
this.getPie()
},
methods:{
// 饼图
getPie(){
let search = {
start_time:'',
end_time:'',
limit:'5'
}
search.start_time = this.searchForm.start_time
search.end_time = this.searchForm.end_time
api
.getTopElevator(search)
.then(res => {
if(res.data.code === 1){
this.pieList = []
this.title = []
this.pieData = res.data
res.data.data.result_list.forEach((item) => {
this.pieList.push({value:item.count,name:item.name,id:item.device_id})
this.title.push(item.name)
})
this.getpie1()
}
}).catch(err => {
console.log(err);
});
},
// 饼图
getpie1(){
this.myChart = echarts.init(document.getElementById('pie1'))
this.myChart.setOption({
title : {
text: '五项目饼图',
x:'left'
},
tooltip : {
// trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
bottom: 10,
left: 'center',
icon: "circle", // 这个字段控制形状 类型包括 circle,rect ,roundRect,triangle,diamond,pin,arrow,none
itemWidth: 10, // 设置宽度
itemHeight: 10, // 设置高度
itemGap: 20, // 设置间距
data: this.title,
},
series : [
{
name:'',
type: this.pieData.data.type,
radius : ['35%', '60%'],
center: ['50%', '40%'],
label: {
normal: {
show: false,
// position: 'outer',
// formatter: '{d}%'
}
},
labelLine: {
normal: {
show: false
}
},
data:this.pieList,
itemStyle: {
color:function(params) {
var colorList = [
'#92C8F3','#5865F0','#C18FF5','#FCAA95','#8D95EC'
];
return colorList[params.dataIndex]
}
}
},
// 外边框的设置
{
radius : ['60%', '65%'],
center: ['50%', '40%'],
type: 'pie',
label: {
normal: {
show: false
},
emphasis: {
show: false
}
},
labelLine: {
normal: {
show: false
},
emphasis: {
show: false
}
},
animation: false,
tooltip: {
show: false
},
data: this.pieList,
itemStyle: {
color:function(params) {
var colorList = [
'#F4F9FE','#EEF0FE','#F9F4FE','#FFF7F4','#F4F4FD'
];
return colorList[params.dataIndex]
}
}
},
// 内边框的设置
{
radius : ['30%', '35%'],
center: ['50%', '40%'],
type: 'pie',
label: {
normal: {
show: false
},
emphasis: {
show: false
}
},
labelLine: {
normal: {
show: false
},
emphasis: {
show: false
}
},
animation: false,
tooltip: {
show: false
},
data: this.pieList,
itemStyle: {
color:function(params) {
var colorList = [
'#F4F9FE','#EEF0FE','#F9F4FE','#FFF7F4','#F4F4FD'
];
return colorList[params.dataIndex]
}
}
},
]
})
//点击事件
var _this = this
this.myChart.on('click', function(params) {
var id = params.data.id;
// console.log(id);
_this.$router.push({ path: '/LeftListTab', query: {device_id:id}})
})
},
},
}
</script>
<style scoped>
#pie1{
float: left;
width:50%;
height: 100%;
}
#pie1{
float: left;
width:50%;
height: 100%;
}
</style>