@senx/discovery-widgets
Version:
Discovery Widgets Elements
464 lines (460 loc) • 24.7 kB
JavaScript
import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { b as DataModel } from './types.js';
import { P as Param, G as GTSLib, U as Utils, L as Logger } from './utils.js';
import { i as init } from './index2.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.options = new Param();
this.debug = false;
this.parsing = false;
this.rendering = false;
this.defOptions = new Param();
this.innerWidth = 0;
this.innerHeight = 0;
}
updateRes() {
this.chartOpts = this.convert(GTSLib.getData(this.result) || new DataModel());
this.setOpts(true);
}
optionsUpdate(newValue, oldValue) {
var _a, _b, _c;
(_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 (!Utils.deepEqual(opts, this.innerOptions)) {
this.innerOptions = Object.assign({}, opts);
if (this.myChart) {
this.chartOpts = this.convert((_b = this.result) !== null && _b !== void 0 ? _b : new DataModel());
this.setOpts(true);
}
(_c = this.LOG) === null || _c === void 0 ? void 0 : _c.debug(['optionsUpdate 2'], { options: this.innerOptions, newValue, oldValue }, this.chartOpts);
}
}
async resize() {
const dims = Utils.getContentBounds(this.el.parentElement);
const width = dims.w - 4;
const height = dims.h;
if (this.myChart && (this.innerWidth !== width || this.innerHeight !== dims.h)) {
this.innerWidth = width;
this.innerHeight = this.innerHeight !== dims.h ? height - this.el.parentElement.offsetTop : this.innerHeight;
this.myChart.resize({ width: this.innerWidth, height: this.innerHeight, silent: true });
}
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, _s, _t, _u, _v, _w, _x;
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: (_l = (((_k = data.params) !== null && _k !== void 0 ? _k : [])[i] || { key: undefined }).key) !== null && _l !== void 0 ? _l : GTSLib.serializeGtsMetadata(gts),
key: (_o = (((_m = data.params) !== null && _m !== void 0 ? _m : [])[i] || { key: undefined }).key) !== null && _o !== void 0 ? _o : GTSLib.serializeGtsMetadata(gts),
value,
max,
min,
});
}
else {
// custom data format
let max = (_p = this.innerOptions.maxValue) !== null && _p !== void 0 ? _p : 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((_q = (gts.hasOwnProperty('value') ? gts.value : gts)) !== null && _q !== void 0 ? _q : 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 = (_r = gts.value) !== null && _r !== void 0 ? _r : 0;
}
else {
value = gts !== null && gts !== void 0 ? gts : 0;
}
if ((_s = this.innerOptions.gauge) === null || _s === void 0 ? void 0 : _s.decimals) {
const dec = Math.pow(10, (_u = (_t = this.innerOptions.gauge) === null || _t === void 0 ? void 0 : _t.decimals) !== null && _u !== void 0 ? _u : 2);
value = Math.round(parseFloat(value + '') * dec) / dec;
}
dataStruct.push({ key: (_v = gts.key) !== null && _v !== void 0 ? _v : '', 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, _21, _22, _23, _24, _25;
if (i % 2 === 0) {
floor++;
}
const c = ColorLib.getColor(i, this.innerOptions.scheme);
const color = (_c = (_b = ((_a = data.params) !== null && _a !== void 0 ? _a : [])[i]) === null || _b === void 0 ? void 0 : _b.datasetColor) !== null && _c !== void 0 ? _c : c;
let axisLineColor;
if (((_f = (_e = ((_d = data.params) !== null && _d !== void 0 ? _d : [])[i]) === null || _e === void 0 ? void 0 : _e.gauge) === null || _f === void 0 ? void 0 : _f.color) || ((_g = this.innerOptions.gauge) === null || _g === void 0 ? void 0 : _g.color)) {
if (GTSLib.isArray(((_k = (_j = ((_h = data.params) !== null && _h !== void 0 ? _h : [])[i]) === null || _j === void 0 ? void 0 : _j.gauge) === null || _k === void 0 ? void 0 : _k.color) || ((_l = this.innerOptions.gauge) === null || _l === void 0 ? void 0 : _l.color))) {
axisLineColor = (_q = (_p = (_o = ((_m = data.params) !== null && _m !== void 0 ? _m : [])[i]) === null || _o === void 0 ? void 0 : _o.gauge) === null || _p === void 0 ? void 0 : _p.color) !== null && _q !== void 0 ? _q : (_r = this.innerOptions.gauge) === null || _r === void 0 ? void 0 : _r.color;
// eslint-disable-next-line no-unsafe-optional-chaining
}
else if (ColorLib.heatMaps[(((_u = (_t = ((_s = data.params) !== null && _s !== void 0 ? _s : [])[i]) === null || _t === void 0 ? void 0 : _t.gauge) === null || _u === void 0 ? void 0 : _u.color) || ((_v = this.innerOptions.gauge) === null || _v === void 0 ? void 0 : _v.color)).toString()]) {
// eslint-disable-next-line no-unsafe-optional-chaining
const heatMap = ColorLib.heatMaps[((_y = (_x = (_w = (data.params || [])[i]) === null || _w === void 0 ? void 0 : _w.gauge) === null || _x === void 0 ? void 0 : _x.color) !== null && _y !== void 0 ? _y : (_z = this.innerOptions.gauge) === null || _z === void 0 ? void 0 : _z.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: (_5 = (_2 = (_1 = (_0 = (data.params || [])[i]) === null || _0 === void 0 ? void 0 : _0.gauge) === null || _1 === void 0 ? void 0 : _1.width) !== null && _2 !== void 0 ? _2 : (_4 = (_3 = this.innerOptions) === null || _3 === void 0 ? void 0 : _3.gauge) === null || _4 === void 0 ? void 0 : _4.width) !== null && _5 !== void 0 ? _5 : 20,
},
}
: {
roundCap: false,
lineStyle: { width: (_11 = (_8 = (_7 = (_6 = (data.params || [])[i]) === null || _6 === void 0 ? void 0 : _6.gauge) === null || _7 === void 0 ? void 0 : _7.width) !== null && _8 !== void 0 ? _8 : (_10 = (_9 = this.innerOptions) === null || _9 === void 0 ? void 0 : _9.gauge) === null || _10 === void 0 ? void 0 : _10.width) !== null && _11 !== void 0 ? _11 : 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' ? {
hideOverlap: true,
color: Utils.getLabelColor(this.el),
distance: 0,
formatter: (value) => value === d.max ? '' : `${value}`,
} : { show: false }, progress: this.type === 'compass'
? { show: false }
: {
show: !(((_13 = (_12 = (data.params || [])[i]) === null || _12 === void 0 ? void 0 : _12.gauge) === null || _13 === void 0 ? void 0 : _13.pointer) || ((_15 = (_14 = this.innerOptions) === null || _14 === void 0 ? void 0 : _14.gauge) === null || _15 === void 0 ? void 0 : _15.pointer)),
roundCap: false,
width: (_21 = (_18 = (_17 = (_16 = (data.params || [])[i]) === null || _16 === void 0 ? void 0 : _16.gauge) === null || _17 === void 0 ? void 0 : _17.width) !== null && _18 !== void 0 ? _18 : (_20 = (_19 = this.innerOptions) === null || _19 === void 0 ? void 0 : _19.gauge) === null || _20 === void 0 ? void 0 : _20.width) !== null && _21 !== void 0 ? _21 : 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: ((_23 = (_22 = (data.params || [])[i]) === null || _22 === void 0 ? void 0 : _22.gauge) === null || _23 === void 0 ? void 0 : _23.pointer) || ((_25 = (_24 = this.innerOptions) === null || _24 === void 0 ? void 0 : _24.gauge) === null || _25 === void 0 ? void 0 : _25.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 }, ((_x = (_w = this.innerOptions) === null || _w === void 0 ? void 0 : _w.extra) === null || _x === void 0 ? void 0 : _x.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: 'e384b05a90a0652f60c6df33839589ebc19d6b51', style: { width: '100%', height: '100%' } }, h("div", { key: '51dcce8720f89304f97d8cab7ed298a6ea469c05', 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