2020-03-17 18:27:46 +08:00
|
|
|
<template>
|
2020-04-22 19:45:10 +08:00
|
|
|
<div class="chart-room" @mouseenter="mouseEnterChart" @mouseleave="mouseLeaveChart">
|
2020-03-23 18:20:19 +08:00
|
|
|
<loading ref="loading"></loading>
|
2020-03-17 18:27:46 +08:00
|
|
|
|
2020-03-23 18:20:19 +08:00
|
|
|
<div class="chart-header">{{chartTitle}}</div>
|
|
|
|
|
|
2020-04-21 19:28:01 +08:00
|
|
|
<div class="chart-body" ref="chartBody" :id="chartId" ></div>
|
2020-03-17 18:27:46 +08:00
|
|
|
|
2020-04-21 19:28:01 +08:00
|
|
|
<div class="legend-container" id="legendArea" ref="legendArea" v-show="legend.length>0" v-scrollBar:legend>
|
|
|
|
|
<div v-for="(item, index) in legend" :title="item.alias?item.alias:item.name" @click="clickLegend(item.name,index)" class="legend-item" :class="{'ft-gr':item.isGray}" :key="'legend_' + item.name+'_'+index">
|
|
|
|
|
<span class="legend-shape" :style="{background:(item.isGray?'#D3D3D3':getBgColor(index))}"></span>{{item.alias?item.alias:item.name}}
|
|
|
|
|
<br/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-03-17 18:27:46 +08:00
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import echarts from 'echarts';
|
|
|
|
|
import chartDataFormat from "../../../charts/chartDataFormat";
|
|
|
|
|
import loading from "../../../common/loading";
|
2020-03-23 18:20:19 +08:00
|
|
|
import uuidv1 from "uuid/v1";
|
2020-03-17 18:27:46 +08:00
|
|
|
import chartConfig from './chartConfig'
|
2020-04-21 19:28:01 +08:00
|
|
|
import bus from "../../../../libs/bus";
|
|
|
|
|
import EleResize from "../../../common/js/divResize";
|
2020-03-17 18:27:46 +08:00
|
|
|
export default {
|
|
|
|
|
name: "chart",
|
2020-03-23 18:20:19 +08:00
|
|
|
components:{
|
|
|
|
|
'loading':loading,
|
|
|
|
|
},
|
|
|
|
|
props:{
|
2020-04-23 22:27:41 +08:00
|
|
|
name: {type: String},
|
2020-03-24 13:19:18 +08:00
|
|
|
chartTitle:{type:String},
|
2020-04-22 19:45:10 +08:00
|
|
|
showToolbox:{type:Boolean,default:true},
|
2020-03-24 13:19:18 +08:00
|
|
|
chartType:{type:String,default:'line'},
|
2020-04-21 19:28:01 +08:00
|
|
|
tooltipFormatter:Function,
|
|
|
|
|
yAxisFormatter:Function,
|
2020-03-30 21:09:34 +08:00
|
|
|
map:{}
|
2020-03-23 18:20:19 +08:00
|
|
|
},
|
2020-03-17 18:27:46 +08:00
|
|
|
data(){
|
|
|
|
|
return {
|
2020-03-23 18:20:19 +08:00
|
|
|
chart:null,
|
|
|
|
|
option:{},
|
2020-04-23 22:27:41 +08:00
|
|
|
chartId: this.name + new uuidv1()+'-'+new Date().getTime(),
|
2020-04-21 19:28:01 +08:00
|
|
|
legend:[],
|
2020-03-17 18:27:46 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
created() {
|
2020-04-22 19:45:10 +08:00
|
|
|
this.option=chartConfig.getOption(this.chartType);
|
|
|
|
|
if(this.showToolbox == false){
|
|
|
|
|
this.option.grid.top=10;
|
|
|
|
|
}
|
2020-03-17 18:27:46 +08:00
|
|
|
},
|
|
|
|
|
methods:{
|
2020-03-31 20:08:33 +08:00
|
|
|
modifyOption:function(target,name,obj){
|
|
|
|
|
if(this.option){
|
|
|
|
|
this.option=chartConfig.getOption(this.chartType);
|
2020-03-23 18:20:19 +08:00
|
|
|
}
|
2020-03-31 20:08:33 +08:00
|
|
|
this.$set(this.option[target],name,obj)
|
2020-03-23 18:20:19 +08:00
|
|
|
},
|
2020-04-21 19:28:01 +08:00
|
|
|
setLegend:function(legend){
|
|
|
|
|
this.legend=legend;
|
|
|
|
|
this.resize();
|
|
|
|
|
},
|
2020-03-23 18:20:19 +08:00
|
|
|
setSeries:function(series){
|
|
|
|
|
if(!this.chart){
|
2020-04-23 22:27:41 +08:00
|
|
|
this.chart = echarts.init(document.getElementById(this.chartId));
|
2020-03-23 18:20:19 +08:00
|
|
|
}
|
2020-04-23 22:27:41 +08:00
|
|
|
this.series = series;
|
2020-03-24 13:19:18 +08:00
|
|
|
if(this.chartType == 'map'){
|
2020-03-30 21:09:34 +08:00
|
|
|
if(this.map){
|
|
|
|
|
echarts.registerMap(this.map.name,this.map.geoJson);
|
|
|
|
|
chartConfig.setMap(this.map.name);
|
|
|
|
|
}else{
|
|
|
|
|
console.error('map chart need map data');
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 13:19:18 +08:00
|
|
|
}
|
2020-03-31 20:08:33 +08:00
|
|
|
if(this.option){
|
|
|
|
|
this.option=chartConfig.getOption(this.chartType);
|
|
|
|
|
}
|
2020-04-21 19:28:01 +08:00
|
|
|
|
|
|
|
|
this.modifyOption('tooltip','position',this.defaultTooltipPosition)
|
|
|
|
|
|
|
|
|
|
if (this.tooltipFormatter) {
|
|
|
|
|
this.modifyOption('tooltip', 'formatter', this.tooltipFormatter)
|
|
|
|
|
} else {
|
2020-04-23 22:27:41 +08:00
|
|
|
//this.modifyOption('tooltip', 'formatter', this.defaultTooltipFormatter)
|
2020-04-21 19:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.chartType == 'line'){
|
|
|
|
|
if(this.yAxisFormatter){
|
|
|
|
|
this.modifyOption('yAxis','formatter',this.yAxisFormatter)
|
|
|
|
|
}else{
|
|
|
|
|
this.modifyOption('yAxis','formatter',this.defaultYAxisFormatter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 20:25:16 +08:00
|
|
|
this.$set(this.option,'series',this.series);
|
|
|
|
|
|
2020-03-23 18:20:19 +08:00
|
|
|
this.chart.clear();
|
|
|
|
|
this.chart.setOption(this.option)
|
2020-04-21 19:28:01 +08:00
|
|
|
this.resize();
|
2020-03-23 18:20:19 +08:00
|
|
|
},
|
2020-03-25 16:27:00 +08:00
|
|
|
resizeChart:function(width,height){
|
|
|
|
|
if(this.chart){
|
|
|
|
|
this.chart.resize({width:width,height:height});
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-04-21 19:28:01 +08:00
|
|
|
resize:function(){
|
|
|
|
|
if(this.chart){
|
2020-04-22 19:45:10 +08:00
|
|
|
let height=this.$el.clientHeight;
|
2020-04-21 19:28:01 +08:00
|
|
|
let width=this.$el.clientWidth;
|
|
|
|
|
if(this.chartTitle){
|
|
|
|
|
height = height - 20;
|
|
|
|
|
}
|
|
|
|
|
if(this.legend && this.legend.length>0){
|
|
|
|
|
height = height - 80;
|
|
|
|
|
}
|
|
|
|
|
this.chart.resize({width:width,height:height});
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-04-22 19:45:10 +08:00
|
|
|
mouseEnterChart(){
|
|
|
|
|
if (this.chart&&this.showToolbox) {
|
|
|
|
|
this.chart.setOption({
|
|
|
|
|
toolbox: {
|
|
|
|
|
show:true,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mouseLeaveChart(){
|
|
|
|
|
if (this.chart) {
|
|
|
|
|
this.chart.setOption({
|
|
|
|
|
toolbox: {
|
|
|
|
|
show:false,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-04-21 19:28:01 +08:00
|
|
|
getBgColor:function(index){
|
|
|
|
|
let color=chartConfig.getBgColorList()[index];
|
|
|
|
|
return color;
|
|
|
|
|
},
|
|
|
|
|
clickLegend(legendName,index){
|
|
|
|
|
let curIsGrey=this.legend[index].isGray;
|
|
|
|
|
if(this.chart){
|
|
|
|
|
this.legend.forEach((item,i)=>{
|
|
|
|
|
let isGrey = item.isGray;
|
|
|
|
|
if(index != i){ //不是当前点击的
|
|
|
|
|
if(!curIsGrey && !isGrey){
|
|
|
|
|
this.chart.dispatchAction({
|
|
|
|
|
type: 'legendUnSelect',
|
|
|
|
|
name: item.name
|
|
|
|
|
});
|
|
|
|
|
item.isGray=true;
|
|
|
|
|
}else if(!curIsGrey && isGrey){
|
|
|
|
|
this.chart.dispatchAction({
|
|
|
|
|
type: 'legendSelect',
|
|
|
|
|
name: item.name
|
|
|
|
|
});
|
|
|
|
|
item.isGray=false;
|
|
|
|
|
}else{
|
|
|
|
|
this.chart.dispatchAction({
|
|
|
|
|
type: 'legendUnSelect',
|
|
|
|
|
name: item.name
|
|
|
|
|
});
|
|
|
|
|
item.isGray=true
|
|
|
|
|
}
|
|
|
|
|
}else {//当前点击的
|
|
|
|
|
this.chart.dispatchAction({
|
|
|
|
|
type: 'legendSelect',
|
|
|
|
|
name: item.name
|
|
|
|
|
});
|
|
|
|
|
if(item.isGray === true){
|
|
|
|
|
item.isGray = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
defaultTooltipPosition:function(point,params,dom,rect,size){
|
|
|
|
|
dom.style.transform = "translateZ(0)";
|
|
|
|
|
//提示框位置
|
|
|
|
|
var x=0;
|
|
|
|
|
var y=0;
|
|
|
|
|
//当前鼠标位置
|
|
|
|
|
var pointX = point[0];
|
|
|
|
|
var pointY = point[1];
|
|
|
|
|
//外层div大小
|
|
|
|
|
var viewWidth = size.viewSize[0];
|
|
|
|
|
var viewHeight = size.viewSize[1];
|
|
|
|
|
//提示框大小
|
|
|
|
|
var boxWidth = size.contentSize[0];
|
|
|
|
|
var boxHeight = size.contentSize[1];
|
|
|
|
|
let chartDom = document.getElementById(this.chartId);
|
|
|
|
|
if(chartDom){
|
|
|
|
|
let parTop = chartDom.offsetTop;
|
|
|
|
|
let parLeft = chartDom.offsetLeft;
|
|
|
|
|
|
|
|
|
|
let parent = chartDom.parentElement;
|
|
|
|
|
let parClientHeight = parent.clientHeight;//可视高度
|
|
|
|
|
let parClientWidth = parent.clientWidth;//可视宽度
|
|
|
|
|
let parScrollTop = parent.scrollTop;
|
|
|
|
|
if((parClientWidth-pointX-parLeft-20)>=boxWidth){//说明鼠标在左边放不下提示框
|
|
|
|
|
x=pointX+10;
|
|
|
|
|
}else{
|
|
|
|
|
x = pointX - boxWidth;
|
|
|
|
|
}
|
|
|
|
|
if((parClientHeight-pointY-(parTop-parScrollTop)-20)>=boxHeight){//说明鼠标上面放不下提示框
|
|
|
|
|
y = pointY+10;
|
|
|
|
|
}else {
|
|
|
|
|
y = pointY-boxHeight;
|
|
|
|
|
}
|
|
|
|
|
return [x,y];
|
|
|
|
|
}else {
|
|
|
|
|
x = pointX - boxWidth;
|
|
|
|
|
y = pointY+10;
|
|
|
|
|
return [x,y];
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
defaultTooltipFormatter:function(params){
|
|
|
|
|
let str = `<div>`;
|
|
|
|
|
params.forEach((item, i) => {
|
|
|
|
|
if(i===0){
|
|
|
|
|
let value=bus.computeTimezone(item.data[0])
|
|
|
|
|
let t_date = new Date(value);
|
|
|
|
|
str += [t_date.getFullYear(), t_date.getMonth() + 1, t_date.getDate()].join('-') + " "
|
|
|
|
|
+ [t_date.getHours(), t_date.getMinutes(),t_date.getSeconds()].join(':');
|
|
|
|
|
str +=`<br/>`;
|
|
|
|
|
}
|
|
|
|
|
let val = Number(item.data[1]);
|
|
|
|
|
str += `<div style="white-space:nowrap;overflow-x:hidden;text-overflow:ellipsis;display: flex; justify-content: space-between; min-width: 150px; max-width: 600px; line-height: 18px; font-size: 12px;">`;
|
|
|
|
|
str += `<div style="max-width: 500px;white-space:nowrap;overflow-x:hidden;text-overflow:ellipsis;"><span style='display:inline-block;margin-right:5px;border-radius:10px;width:15px;height:5px;background-color: ${item.color};}'></span>${item.seriesName}: </div>`;
|
|
|
|
|
str += `<div style="padding-left: 10px;">`;
|
|
|
|
|
str += chartDataFormat.getUnit(5).compute(val,null,2);
|
|
|
|
|
str += `</div>`;
|
|
|
|
|
str += `</div>`;
|
|
|
|
|
});
|
|
|
|
|
str +=`</div>`;
|
|
|
|
|
return str;
|
|
|
|
|
},
|
|
|
|
|
defaultYAxisFormatter:function(value,index){
|
|
|
|
|
let unit=chartDataFormat.getUnit(5);
|
|
|
|
|
return unit.compute(value,index);
|
|
|
|
|
},
|
2020-03-23 18:20:19 +08:00
|
|
|
startLoading:function(){
|
|
|
|
|
this.$refs.loading.startLoading();
|
|
|
|
|
},
|
|
|
|
|
endLoading:function(){
|
|
|
|
|
this.$refs.loading.endLoading();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted() {
|
|
|
|
|
this.chart=echarts.init(document.getElementById(this.chartId));
|
2020-04-21 19:28:01 +08:00
|
|
|
EleResize.on(this.$el,this.resize);
|
2020-03-17 18:27:46 +08:00
|
|
|
},
|
|
|
|
|
watch:{
|
|
|
|
|
|
2020-03-23 18:20:19 +08:00
|
|
|
},
|
|
|
|
|
destroyed() {
|
|
|
|
|
if(this.chart){
|
|
|
|
|
this.chart.clear();
|
|
|
|
|
}
|
2020-03-17 18:27:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2020-04-21 19:28:01 +08:00
|
|
|
.chart-room{
|
2020-04-22 19:45:10 +08:00
|
|
|
width: 100%;
|
2020-04-21 19:28:01 +08:00
|
|
|
height: 100%;
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
.chart-room .legend-container{
|
|
|
|
|
width: calc(100% - 30px);
|
|
|
|
|
/*max-height:80px;*/
|
|
|
|
|
/*min-height:40px;*/
|
|
|
|
|
height: 80px;
|
|
|
|
|
font-size:12px;
|
|
|
|
|
text-align:left;
|
|
|
|
|
margin:0 auto;
|
|
|
|
|
line-height: 18px;
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
.chart-room .legend-container .legend-item{
|
|
|
|
|
text-overflow:ellipsis;
|
|
|
|
|
white-space:nowrap;
|
|
|
|
|
width:100%;
|
|
|
|
|
overflow-x:hidden;
|
|
|
|
|
cursor:pointer;
|
|
|
|
|
display:inline-block;
|
|
|
|
|
float:left;
|
|
|
|
|
line-height: 20px;
|
|
|
|
|
}
|
|
|
|
|
.chart-room .ft-gr{
|
|
|
|
|
color:lightgray;
|
|
|
|
|
}
|
|
|
|
|
.chart-room .legend-shape{
|
|
|
|
|
display:inline-block;
|
|
|
|
|
margin-right:5px;
|
|
|
|
|
border-radius:10px;
|
|
|
|
|
width:15px;
|
|
|
|
|
height:5px;
|
|
|
|
|
vertical-align: middle;
|
|
|
|
|
}
|
2020-03-17 18:27:46 +08:00
|
|
|
</style>
|