<template>
|
<div
|
class="e-chart-root"
|
@dblclick="fullScreen"
|
:class="{ 'full-screen': fullScreenState }"
|
>
|
<div class="e-chart-container">
|
<div class="e-chart" :id="id" :ref="id"></div>
|
<div class="e-chart-tools" v-if="showTools1">
|
<i
|
class="iconfont el-icon-yanjingkejian"
|
:class="eleClass"
|
@click="changeEyeState"
|
></i>
|
</div>
|
</div>
|
</div>
|
</template>
|
|
<script>
|
// 引入 ECharts 主模块
|
import ECharts from "echarts/lib/echarts";
|
//引入折线图
|
import "echarts/lib/chart/bar";
|
//引入提示框
|
import "echarts/lib/component/tooltip";
|
//引入标题
|
import "echarts/lib/component/title";
|
//引入图例标志
|
import "echarts/lib/component/legend";
|
//区域缩放
|
import "echarts/lib/component/dataZoom";
|
|
//markeline
|
import "echarts/lib/component/markLine";
|
|
// 引入自定义主题
|
import "./theme/transparent";
|
|
export default {
|
chart: null,
|
props: {
|
id: {
|
type: String,
|
required: true,
|
},
|
unit: {
|
type: String,
|
default: "",
|
},
|
noFull: {
|
type: Boolean,
|
default: false
|
},
|
showTools: {
|
type: Boolean,
|
default: false,
|
},
|
showLabel: {
|
type: Boolean,
|
default: true,
|
},
|
maxColor: {
|
type: String,
|
default: "green",
|
},
|
minColor: {
|
type: String,
|
default: "red",
|
},
|
rightMenu: {
|
type: Boolean,
|
default: false,
|
},
|
},
|
data() {
|
return {
|
fullScreenState: false,
|
eye: true,
|
// 单体数过大 >=50
|
bigLength: false
|
};
|
},
|
methods: {
|
getOption(opt) {
|
let unit = this.unit;
|
let alarmVol = this.getAlarmVal(opt);
|
// 整体配置项
|
let option = {
|
animation: false,
|
color: this.getColor(opt),
|
title: this.getTitle(opt),
|
tooltip: {
|
trigger: "axis",
|
axisPointer: {
|
// 坐标轴指示器,坐标轴触发有效
|
type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
|
},
|
formatter(params) {
|
var res = params[0].name + "<br/>";
|
params.forEach((item) => {
|
res += item.marker;
|
res += item.seriesName;
|
res += " : " + item.data[1] + unit + "</br>";
|
});
|
return res;
|
},
|
},
|
grid: {
|
left: "1%",
|
right: "1%",
|
bottom: "2%",
|
containLabel: true,
|
},
|
xAxis: [
|
{
|
type: "category",
|
},
|
],
|
yAxis: [
|
{
|
type: "value",
|
splitLine: {
|
show: true,
|
},
|
min: function (data) {
|
let min = data.min;
|
if (min == Infinity) {
|
return 0;
|
}
|
if(alarmVol.low !== false && alarmVol.low<min) {
|
min = alarmVol.low;
|
}
|
return Number((min - min * 0.2).toFixed(2));
|
},
|
max: function (data) {
|
let max = data.max;
|
if (max == -Infinity) {
|
max = 1;
|
}
|
if(alarmVol.high !== false && alarmVol.high>max) {
|
max = alarmVol.high;
|
}
|
return Number((max + max * 0.2).toFixed(2));
|
},
|
},
|
],
|
series: this.getSeries(opt),
|
};
|
return option;
|
},
|
setOption(opt) {
|
let option = this.getOption(opt);
|
// 清理画布
|
this.$G.chartManage.get(this.id).clear();
|
// 设置配置项
|
this.$G.chartManage.get(this.id).setOption(option);
|
},
|
getAlarmVal(opt) {
|
let result = {
|
low: false,
|
high: false,
|
};
|
if (opt && opt.series && opt.series.length != 0) {
|
let markLine = opt.series[0].markLine;
|
if (opt.series && markLine && markLine.data) {
|
let markNumbers = markLine.data.map(v=>{
|
return v.yAxis;
|
});
|
|
result.low = Math.min.apply(null, markNumbers);
|
result.high = Math.max.apply(null, markNumbers);
|
}
|
}
|
return result;
|
},
|
getColor(opt) {
|
// 配置自定义颜色
|
// 未配置自定义颜色
|
if (!opt || !opt.color) {
|
return [];
|
}
|
|
// 返回颜色
|
return opt.color;
|
},
|
getTitle(opt) {
|
// 配置标题
|
// 未配置标题
|
if (!opt || !opt.title) {
|
return {
|
show: false,
|
};
|
}
|
|
// 返回标题
|
return opt.title;
|
},
|
getSeries(opt) {
|
// 设置series
|
// 未配置series
|
if (!opt || !opt.series) {
|
return [];
|
}
|
let minColor = this.minColor;
|
let maxColor = this.maxColor;
|
let bigLength = false;
|
// 设置配置项
|
let series = opt.series.map((item) => {
|
let max = this.getMax(item.data);
|
let min = this.getMin(item.data);
|
let rotate = item.data.length > 24 && item.data.length < 51 ? 30 : 0;
|
item.type = "bar";
|
|
if (item.data.length >= 51) {
|
bigLength = true;
|
}
|
// 显示数据
|
item.label = {
|
show: this.showChartLabel && item.data.length < 51,
|
position: "top",
|
color: "#fff",
|
rotate: rotate
|
};
|
// 设置颜色
|
if (item.hColor) {
|
// 设置背景
|
item.itemStyle = {
|
color: item.hColor,
|
};
|
} else {
|
if(this.$CFG.clientName.name === "cdsh"){//成都石化定制
|
// 设置背景
|
item.itemStyle = {
|
color: function (value) {
|
let val = value.value[1];
|
if (val > item.markLine.data[4].yAxis) {
|
return maxColor;
|
} else{
|
return minColor;
|
}
|
},
|
};
|
}else if(item.markLine && item.markLine.length>=4) { // 上限预告警,下限预告警 黄色 上限告警,下限告警 红色
|
// 设置背景
|
item.itemStyle = {
|
color: function (value) {
|
let val = value.value[1];
|
if ((val <= item.markLine.data[0].yAxis && val > item.markLine.data[1].yAxis)
|
|| (val >= item.markLine.data[2].yAxis && val < item.markLine.data[3].yAxis)) {
|
return "#f9b253";
|
} else if(val <= item.markLine.data[1].yAxis || val >= item.markLine.data[3].yAxis) {
|
return "#f83030";
|
}
|
},
|
};
|
}else{
|
// 设置背景
|
item.itemStyle = {
|
color: function (value) {
|
let val = value.value[1];
|
if (val == max) {
|
return maxColor;
|
} else if (val == min) {
|
return minColor;
|
}
|
},
|
};
|
}
|
|
}
|
return item;
|
});
|
this.bigLength = bigLength;
|
// 返回
|
return series;
|
},
|
getMax(list) {
|
let arr = list.map((item) => {
|
return item[1];
|
});
|
return Math.max.apply(null, arr);
|
},
|
getMin(list) {
|
let arr = list.map((item) => {
|
return item[1];
|
});
|
return Math.min.apply(null, arr);
|
},
|
fullScreen() {
|
// 判断是否可以全屏
|
if(this.noFull) {
|
return;
|
}
|
this.fullScreenState = this.fullScreenState ? false : true;
|
this.$nextTick(() => {
|
// 重置大小
|
this.$G.chartManage.get(this.id).resize();
|
});
|
},
|
resize() {
|
// 重置大小
|
this.$G.chartManage.get(this.id).resize();
|
},
|
changeEyeState() {
|
this.eye = this.eye ? false : true;
|
let option = this.$G.chartManage.get(this.id).getOption();
|
this.setOption(option);
|
},
|
},
|
computed: {
|
eleClass() {
|
return this.eye ? "el-icon-yanjingkejian" : "el-icon-yanjing-bukejian";
|
},
|
showChartLabel() {
|
return this.showLabel && this.eye ? true : false;
|
},
|
showTools1 () {
|
return this.showTools && !this.bigLength;
|
}
|
},
|
mounted() {
|
let self = this;
|
this.$refs[this.id].oncontextmenu = function () {
|
return false;
|
};
|
// 基于准备好的dom,初始化echarts实例
|
let chart = ECharts.init(this.$refs[this.id], "transparent");
|
this.$options.chart = chart;
|
// 将图表添加到图表管理
|
this.$G.chartManage.set(this.id, chart);
|
// 设置配置项
|
this.setOption();
|
|
// 点击事件
|
chart.getZr().on("mousedown", function (params) {
|
if (params.which == 3) {
|
let pointInPixel = [params.offsetX, params.offsetY];
|
if (chart.containPixel("grid", pointInPixel)) {
|
/*单击图标X轴数据,打开详情*/
|
let xIndex = chart.convertFromPixel(
|
{ seriesIndex: 0 },
|
pointInPixel
|
)[0];
|
self.$emit("right-click", {
|
x: params.event.clientX + 16,
|
y: params.event.clientY + 16,
|
xIndex: xIndex,
|
});
|
}
|
}
|
});
|
|
// 根据功能屏蔽右键菜单
|
if (this.rightMenu) {
|
document.getElementById(this.id) && (document.getElementById(this.id).oncontextmenu = function () {
|
return false;
|
});
|
}
|
},
|
};
|
</script>
|
|
<style scoped>
|
.e-chart-root,
|
.e-chart-container,
|
.e-chart {
|
height: 100%;
|
box-sizing: border-box;
|
}
|
|
.e-chart-root.full-screen .e-chart-container {
|
position: fixed;
|
top: 0;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
background-size: 100% 100%;
|
z-index: 9999;
|
}
|
|
.e-chart-tools {
|
position: absolute;
|
top: 16px;
|
right: 16px;
|
z-index: 9;
|
}
|
|
.e-chart-tools .iconfont {
|
margin-left: 8px;
|
font-size: 24px;
|
cursor: pointer;
|
color: #00fefe;
|
}
|
|
.e-chart-tools .iconfont:hover {
|
color: #04b1b1;
|
}
|
|
.e-chart-tools .iconfont:active {
|
color: #ff0000;
|
}
|
</style>
|