@senx/discovery-plugin-marauder
Version:
Discovery plugin - Marauder's map
460 lines (456 loc) • 23.7 kB
JavaScript
import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { a as DataModel } from './types.js';
import { P as Param, G as GTSLib, l as lodash, L as Logger, U as Utils } from './utils.js';
import { i as init } from './index3.js';
import { C as ColorLib } from './color-lib.js';
import { d as defineCustomElement$1 } from './discovery-spinner2.js';
const discoveryGaugeCss = "/*!\n * Copyright 2022 SenX S.A.S.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */:host>div{width:100%;height:100%;position:relative}:host>div>div{width:100%;height:100%}:host .wv-tooltip{max-width:300px}:host .wv-tooltip div{word-wrap:anywhere}:host .discovery-chart-spinner{position:absolute;top:0;left:0;width:100%;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;height:100%;z-index:900}";
const DiscoveryGaugeStyle0 = discoveryGaugeCss;
const DiscoveryGauge = /*@__PURE__*/ proxyCustomElement(class DiscoveryGauge extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.draw = createEvent(this, "draw", 7);
this.dataPointOver = createEvent(this, "dataPointOver", 7);
this.dataPointSelected = createEvent(this, "dataPointSelected", 7);
this.defOptions = new Param();
this.result = undefined;
this.type = undefined;
this.options = new Param();
this.width = undefined;
this.height = undefined;
this.debug = false;
this.unit = undefined;
this.parsing = false;
this.rendering = false;
this.innerOptions = undefined;
}
updateRes() {
this.chartOpts = this.convert(GTSLib.getData(this.result) || new DataModel());
this.setOpts(true);
}
optionsUpdate(newValue, oldValue) {
var _a, _b;
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['optionsUpdate'], newValue, oldValue);
let opts = newValue;
if (!!newValue && typeof newValue === 'string') {
opts = JSON.parse(newValue);
}
if (!lodash.exports.isEqual(opts, this.innerOptions)) {
this.innerOptions = Object.assign({}, opts);
if (!!this.myChart) {
this.chartOpts = this.convert(this.result || new DataModel());
this.setOpts(true);
}
(_b = this.LOG) === null || _b === void 0 ? void 0 : _b.debug(['optionsUpdate 2'], { options: this.innerOptions, newValue, oldValue }, this.chartOpts);
}
}
async resize() {
if (this.myChart) {
this.myChart.resize();
}
return Promise.resolve();
}
async show(regexp) {
this.myChart.dispatchAction({
type: 'legendSelect',
batch: this.myChart.getOption().series.filter(s => new RegExp(regexp).test(s.name)),
});
return Promise.resolve();
}
async hide(regexp) {
this.myChart.dispatchAction({
type: 'legendUnSelect',
batch: this.myChart.getOption().series.filter(s => new RegExp(regexp).test(s.name)),
});
return Promise.resolve();
}
async hideById(id) {
if (this.myChart) {
this.myChart.dispatchAction({
type: 'legendUnSelect',
batch: this.myChart.getOption().series
.filter((s, i) => new RegExp(id.toString()).test((s.id || i).toString())),
});
}
return Promise.resolve();
}
async showById(id) {
if (this.myChart) {
this.myChart.dispatchAction({
type: 'legendSelect',
batch: this.myChart.getOption().series
.filter((s, i) => new RegExp(id.toString()).test((s.id || i).toString())),
});
}
return Promise.resolve();
}
// noinspection JSUnusedGlobalSymbols
componentWillLoad() {
var _a;
this.parsing = true;
this.LOG = new Logger(DiscoveryGauge, this.debug);
if (typeof this.options === 'string') {
this.innerOptions = JSON.parse(this.options);
}
else {
this.innerOptions = this.options;
}
this.chartOpts = this.convert(GTSLib.getData(this.result) || new DataModel());
this.setOpts();
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['componentWillLoad'], {
type: this.type,
options: this.innerOptions,
chartOpts: this.chartOpts,
});
}
setOpts(notMerge = false) {
var _a, _b, _c;
const series = [];
((_a = this.chartOpts) === null || _a === void 0 ? void 0 : _a.series).forEach(s => {
s.detail.fontSize = this.autoFontSize(this.chartOpts.series.length);
series.push(s);
});
this.chartOpts.series = series;
if (((_c = (_b = this.chartOpts) === null || _b === void 0 ? void 0 : _b.series) !== null && _c !== void 0 ? _c : []).length === 0) {
this.chartOpts.title = {
show: true,
textStyle: { color: Utils.getLabelColor(this.el), fontSize: 20 },
text: this.innerOptions.noDataLabel || '',
left: 'center',
top: 'center',
};
this.chartOpts.xAxis = { show: false };
this.chartOpts.yAxis = { show: false };
this.chartOpts.tooltip = { show: false };
}
else {
this.chartOpts.title = Object.assign(Object.assign({}, this.chartOpts.title || {}), { show: false });
}
setTimeout(() => {
if (this.myChart) {
this.myChart.setOption(this.chartOpts || {}, notMerge, true);
}
});
}
getCommonSeriesParam(color) {
const datasetNoAlpha = this.innerOptions.datasetNoAlpha;
return {
type: 'gauge',
animation: true,
large: true,
clip: false,
startAngle: 180,
endAngle: 0,
lineStyle: { color },
toolbox: {
show: this.innerOptions.showControls,
feature: {
saveAsImage: { type: 'png', excludeComponents: ['toolbox'] },
},
},
splitLine: { show: false },
splitNumber: 4, // The number of split segments on the axis
itemStyle: {
opacity: 0.8,
borderColor: color,
color: {
type: 'linear', x: 0, y: 0, x2: 1, y2: 1,
colorStops: [
{ offset: 0, color },
{ offset: 1, color: ColorLib.transparentize(color, datasetNoAlpha ? 1 : 0.4) },
],
global: false, // false by default
},
},
};
}
convert(data) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
let options = Utils.mergeDeep(this.defOptions, this.innerOptions || {});
options = Utils.mergeDeep(options || {}, data.globalParams);
this.innerOptions = Object.assign({}, options);
const series = [];
// noinspection JSUnusedAssignment
let gtsList = [];
if (GTSLib.isArray(data.data)) {
data.data = GTSLib.flatDeep(data.data);
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['convert', 'isArray']);
if (data.data.length > 0 && GTSLib.isGts(data.data[0])) {
(_b = this.LOG) === null || _b === void 0 ? void 0 : _b.debug(['convert', 'isArray 2']);
gtsList = GTSLib.flattenGtsIdArray(data.data, 0).res;
}
else {
(_c = this.LOG) === null || _c === void 0 ? void 0 : _c.debug(['convert', 'isArray 3']);
gtsList = data.data;
}
}
else {
(_d = this.LOG) === null || _d === void 0 ? void 0 : _d.debug(['convert', 'not array']);
gtsList = [data.data];
}
(_e = this.LOG) === null || _e === void 0 ? void 0 : _e.debug(['convert'], { options: this.innerOptions, gtsList });
const gtsCount = gtsList.length;
let overallMax = this.innerOptions.maxValue || Number.MIN_VALUE;
const dataStruct = [];
for (let i = 0; i < gtsCount; i++) {
const gts = gtsList[i];
if (GTSLib.isGts(gts)) {
let max = Number.MIN_VALUE;
const values = (gts.v || []);
const val = (_f = values[values.length - 1]) !== null && _f !== void 0 ? _f : [];
let value = 0;
if (val.length > 0) {
value = val[val.length - 1];
if ((_g = this.innerOptions.gauge) === null || _g === void 0 ? void 0 : _g.decimals) {
const dec = Math.pow(10, (_j = (_h = this.innerOptions.gauge) === null || _h === void 0 ? void 0 : _h.decimals) !== null && _j !== void 0 ? _j : 2);
value = Math.round(parseFloat(value + '') * dec) / dec;
}
}
if (!!data.params && !!data.params[i] && !!data.params[i].maxValue) {
max = data.params[i].maxValue;
}
else {
if (overallMax < value) {
overallMax = value;
}
}
let min = 0;
if (!!data.params && !!data.params[i] && !!data.params[i].minValue) {
min = data.params[i].minValue;
}
dataStruct.push({
id: gts.id,
name: ((data.params || [])[i] || { key: undefined }).key || GTSLib.serializeGtsMetadata(gts),
key: ((data.params || [])[i] || { key: undefined }).key || GTSLib.serializeGtsMetadata(gts),
value,
max,
min,
});
}
else {
// custom data format
let max = this.innerOptions.maxValue || Number.MIN_VALUE;
if (!!data.params && !!data.params[i] && !!data.params[i].maxValue) {
max = data.params[i].maxValue;
overallMax = Math.max(max, overallMax);
}
else {
overallMax = Math.max((gts.hasOwnProperty('value') ? gts.value : gts) || Number.MIN_VALUE, overallMax);
}
let min = 0;
if (!!data.params && !!data.params[i] && !!data.params[i].minValue) {
min = data.params[i].minValue;
}
let value = 0;
if (gts.hasOwnProperty('value')) {
value = (_k = gts.value) !== null && _k !== void 0 ? _k : 0;
}
else {
value = gts !== null && gts !== void 0 ? gts : 0;
}
if ((_l = this.innerOptions.gauge) === null || _l === void 0 ? void 0 : _l.decimals) {
const dec = Math.pow(10, (_o = (_m = this.innerOptions.gauge) === null || _m === void 0 ? void 0 : _m.decimals) !== null && _o !== void 0 ? _o : 2);
value = Math.round(parseFloat(value + '') * dec) / dec;
}
dataStruct.push({ key: (_p = gts.key) !== null && _p !== void 0 ? _p : '', value, max, min });
}
}
const radius = Math.round(100 / Math.ceil(gtsCount / 2)) * (this.type === 'compass' ? 0.8 : 0.8);
let floor = 1;
dataStruct.forEach((d, i) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20;
if (i % 2 === 0) {
floor++;
}
const c = ColorLib.getColor(i, this.innerOptions.scheme);
const color = ((data.params || [])[i] || { datasetColor: c }).datasetColor || c;
let axisLineColor;
if (((_b = (_a = (data.params || [])[i]) === null || _a === void 0 ? void 0 : _a.gauge) === null || _b === void 0 ? void 0 : _b.color) || ((_c = this.innerOptions.gauge) === null || _c === void 0 ? void 0 : _c.color)) {
if (GTSLib.isArray((_f = (_e = (_d = (data.params || [])[i]) === null || _d === void 0 ? void 0 : _d.gauge) === null || _e === void 0 ? void 0 : _e.color) !== null && _f !== void 0 ? _f : (_g = this.innerOptions.gauge) === null || _g === void 0 ? void 0 : _g.color)) {
axisLineColor = (_k = (_j = (_h = (data.params || [])[i]) === null || _h === void 0 ? void 0 : _h.gauge) === null || _j === void 0 ? void 0 : _j.color) !== null && _k !== void 0 ? _k : (_l = this.innerOptions.gauge) === null || _l === void 0 ? void 0 : _l.color;
}
else if (ColorLib.heatMaps[((_p = (_o = (_m = (data.params || [])[i]) === null || _m === void 0 ? void 0 : _m.gauge) === null || _o === void 0 ? void 0 : _o.color) !== null && _p !== void 0 ? _p : (_q = this.innerOptions.gauge) === null || _q === void 0 ? void 0 : _q.color).toString()]) {
const heatMap = ColorLib.heatMaps[((_t = (_s = (_r = (data.params || [])[i]) === null || _r === void 0 ? void 0 : _r.gauge) === null || _s === void 0 ? void 0 : _s.color) !== null && _t !== void 0 ? _t : (_u = this.innerOptions.gauge) === null || _u === void 0 ? void 0 : _u.color).toString()];
axisLineColor = heatMap.map((c, i) => [(i + 1) / heatMap.length, c]);
}
}
const unit = ((data.params || [])[i] || {}).unit || this.innerOptions.unit || this.unit || '';
const angles = DiscoveryGauge.getAngles(this.type);
series.push(Object.assign(Object.assign({}, this.getCommonSeriesParam(color)), { name: d.key, min: d.min, id: d.id, max: d.max === Number.MIN_VALUE ? overallMax : d.max, startAngle: angles.s, endAngle: angles.e, tooltip: {
backgroundColor: Utils.getCSSColor(this.el, '--warp-view-tooltip-bg-color', 'white'),
hideDelay: this.innerOptions.tooltipDelay || 100,
formatter: '{a} <br/>{b} : {c}%',
}, title: {
fontSize: 12,
offsetCenter: this.type === 'compass' ? [0, '110%'] : [0, 10],
color: Utils.getLabelColor(this.el),
}, axisLine: this.type === 'compass'
? { lineStyle: { color: [[1, Utils.getGridColor(this.el)]], width: 1 } }
: axisLineColor
? {
lineStyle: {
color: axisLineColor,
width: (_0 = (_x = (_w = (_v = (data.params || [])[i]) === null || _v === void 0 ? void 0 : _v.gauge) === null || _w === void 0 ? void 0 : _w.width) !== null && _x !== void 0 ? _x : (_z = (_y = this.innerOptions) === null || _y === void 0 ? void 0 : _y.gauge) === null || _z === void 0 ? void 0 : _z.width) !== null && _0 !== void 0 ? _0 : 20,
},
}
: {
roundCap: false,
lineStyle: { width: (_6 = (_3 = (_2 = (_1 = (data.params || [])[i]) === null || _1 === void 0 ? void 0 : _1.gauge) === null || _2 === void 0 ? void 0 : _2.width) !== null && _3 !== void 0 ? _3 : (_5 = (_4 = this.innerOptions) === null || _4 === void 0 ? void 0 : _4.gauge) === null || _5 === void 0 ? void 0 : _5.width) !== null && _6 !== void 0 ? _6 : 20 },
}, axisTick: this.type === 'compass' ? {
distance: 0,
length: 10,
lineStyle: { color: Utils.getGridColor(this.el) },
} : {
distance: 0,
splitNumber: 4,
lineStyle: { width: 1, color: Utils.getGridColor(this.el) },
}, axisLabel: this.type === 'compass' ? {
color: Utils.getLabelColor(this.el),
distance: 0,
formatter: (value) => value === d.max ? '' : `${value}`,
} : { show: false }, progress: this.type === 'compass'
? { show: false }
: {
show: !(((_8 = (_7 = (data.params || [])[i]) === null || _7 === void 0 ? void 0 : _7.gauge) === null || _8 === void 0 ? void 0 : _8.pointer) || ((_10 = (_9 = this.innerOptions) === null || _9 === void 0 ? void 0 : _9.gauge) === null || _10 === void 0 ? void 0 : _10.pointer)),
roundCap: false,
width: (_16 = (_13 = (_12 = (_11 = (data.params || [])[i]) === null || _11 === void 0 ? void 0 : _11.gauge) === null || _12 === void 0 ? void 0 : _12.width) !== null && _13 !== void 0 ? _13 : (_15 = (_14 = this.innerOptions) === null || _14 === void 0 ? void 0 : _14.gauge) === null || _15 === void 0 ? void 0 : _15.width) !== null && _16 !== void 0 ? _16 : 20,
}, data: [{ value: d.value, name: d.key }], anchor: this.type === 'compass' ? {
show: true,
size: 10,
itemStyle: { borderColor: color, borderWidth: 10 },
} : { show: false }, pointer: this.type === 'compass'
? {
offsetCenter: [0, '40%'],
length: '140%',
itemStyle: { color },
}
: {
show: ((_18 = (_17 = (data.params || [])[i]) === null || _17 === void 0 ? void 0 : _17.gauge) === null || _18 === void 0 ? void 0 : _18.pointer) || ((_20 = (_19 = this.innerOptions) === null || _19 === void 0 ? void 0 : _19.gauge) === null || _20 === void 0 ? void 0 : _20.pointer),
icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z',
length: '12%',
width: 20,
offsetCenter: [0, `-${radius - 10}%`],
itemStyle: { color: 'auto' },
}, radius: `${radius}%`, detail: {
formatter: '{value}' + unit,
fontSize: 12,
offsetCenter: [0, this.type === 'gauge' ? '-20%' : this.type === 'compass' ? 40 : 0],
color: Utils.getLabelColor(this.el),
}, center: [
(gtsCount === 1 ? '50' : i % 2 === 0 ? '25' : '75') + '%',
`${(gtsCount === 1
? (this.type === 'gauge' ? '65' : '50')
: (radius * (floor - 1) - radius / 2 + (floor > 2 ? 15 : 5)))}%`,
] }));
});
return Object.assign({ grid: {
left: 10, top: 10, bottom: 10, right: 10,
containLabel: true,
}, legend: { show: false }, series }, ((_r = (_q = this.innerOptions) === null || _q === void 0 ? void 0 : _q.extra) === null || _r === void 0 ? void 0 : _r.chartOpts) || {});
}
autoFontSize(size) {
if (this.el.getBoundingClientRect().height > 0) {
const count = size > 1;
return (this.el.getBoundingClientRect().height >= 700) ? 50 : (this.el.getBoundingClientRect().height / 10) / (count ? 4 : 1);
}
else {
return 12;
}
}
;
// noinspection JSUnusedGlobalSymbols
componentDidLoad() {
setTimeout(() => {
this.parsing = false;
this.rendering = true;
let initial = false;
this.myChart = init(this.graph, null, {
width: undefined,
height: this.height,
});
this.myChart.on('rendered', () => {
this.rendering = false;
if (initial) {
setTimeout(() => this.draw.emit());
initial = false;
}
});
this.myChart.on('mouseover', (event) => {
this.dataPointOver.emit({ date: event.value[0], name: event.seriesName, value: event.value[1], meta: {} });
});
this.el.addEventListener('mouseout', () => this.dataPointOver.emit({}));
this.myChart.on('click', (event) => {
this.dataPointSelected.emit({ date: event.value[0], name: event.seriesName, value: event.value[1], meta: {} });
});
this.setOpts();
initial = true;
});
}
async export(type = 'png') {
return Promise.resolve(this.myChart ? this.myChart.getDataURL({
type,
excludeComponents: ['toolbox'],
}) : undefined);
}
render() {
return h("div", { key: 'c8db45956b0998818edfc62f9a2f38700781eda9', style: { width: '100%', height: '100%' } }, h("div", { key: '9efa2741bdbf24e56c757b390052e1e5913970a6', ref: (el) => this.graph = el }), this.parsing ? h("div", { class: "discovery-chart-spinner" }, h("discovery-spinner", null, "Parsing data...")) : '', this.rendering ? h("div", { class: "discovery-chart-spinner" }, h("discovery-spinner", null, "Rendering data...")) : '');
}
static getAngles(type) {
switch (type) {
case 'compass':
return { s: 90, e: -270 };
case 'gauge':
return { s: 180, e: 0 };
default:
return { s: 270, e: -90 };
}
}
get el() { return this; }
static get watchers() { return {
"result": ["updateRes"],
"options": ["optionsUpdate"]
}; }
static get style() { return DiscoveryGaugeStyle0; }
}, [1, "discovery-gauge", {
"result": [1],
"type": [1],
"options": [1025],
"width": [2],
"height": [2],
"debug": [4],
"unit": [1],
"parsing": [32],
"rendering": [32],
"innerOptions": [32],
"resize": [64],
"show": [64],
"hide": [64],
"hideById": [64],
"showById": [64],
"export": [64]
}, undefined, {
"result": ["updateRes"],
"options": ["optionsUpdate"]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["discovery-gauge", "discovery-spinner"];
components.forEach(tagName => { switch (tagName) {
case "discovery-gauge":
if (!customElements.get(tagName)) {
customElements.define(tagName, DiscoveryGauge);
}
break;
case "discovery-spinner":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
export { DiscoveryGauge as D, defineCustomElement as d };
//# sourceMappingURL=discovery-gauge2.js.map