<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="showTools">
|
<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: "",
|
},
|
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: 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;
|
}
|
min =
|
alarmVol.low == false
|
? min
|
: alarmVol.low < min
|
? alarmVol.low
|
: min;
|
return Number((min - min * 0.2).toFixed(2));
|
},
|
max: function (data) {
|
let max = data.max;
|
if (max == -Infinity) {
|
max = 1;
|
}
|
max =
|
alarmVol.high == false
|
? max
|
: alarmVol.high > max
|
? alarmVol.high
|
: max;
|
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) {
|
result.low = markLine.data[0].yAxis;
|
result.high = markLine.data[1].yAxis;
|
}
|
}
|
|
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 series = opt.series.map((item) => {
|
let max = this.getMax(item.data);
|
let min = this.getMin(item.data);
|
item.type = "bar";
|
|
// 显示数据
|
item.label = {
|
show: this.showChartLabel,
|
position: "top",
|
color: "#fff",
|
};
|
// 设置颜色
|
if (item.hColor) {
|
// 设置背景
|
item.itemStyle = {
|
color: item.hColor,
|
};
|
} else {
|
// 设置背景
|
item.itemStyle = {
|
color: function (value) {
|
let val = value.value[1];
|
if (val == max) {
|
return maxColor;
|
} else if (val == min) {
|
return minColor;
|
}
|
},
|
};
|
}
|
return item;
|
});
|
// 返回
|
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() {
|
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;
|
},
|
},
|
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).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>
|