@record-evolution/widget-gauge 1.7.1 → 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "REWidget widget-gauge",
4
4
  "license": "MIT",
5
5
  "author": "widget-gauge",
6
- "version": "1.7.1",
6
+ "version": "1.7.3",
7
7
  "type": "module",
8
8
  "main": "dist/widget-gauge.js",
9
9
  "types": "dist/src/widget-gauge.d.ts",
@@ -19,8 +19,6 @@
19
19
  "link": "npm run build && npm link && cd ../RESWARM/frontend && npm link @record-evolution/widget-gauge",
20
20
  "unlink": "npm unlink --global && cd ../RESWARM/frontend && npm unlink @record-evolution/widget-gauge && npm i @record-evolution/widget-gauge",
21
21
  "types": "cat src/definition-schema.json | json2ts --style.tabWidth=4 > src/definition-schema.d.ts",
22
- "lint": "eslint --ext .ts,.html . --ignore-path .gitignore && prettier \"**/*.ts\" --check --ignore-path .gitignore",
23
- "format": "eslint --ext .ts,.html . --fix --ignore-path .gitignore && prettier \"**/*.ts\" --write --ignore-path .gitignore",
24
22
  "release": "npm run build && npm run types && npm version patch --tag-version-prefix='' && git push && git push --tag"
25
23
  },
26
24
  "dependencies": {
@@ -5,36 +5,37 @@ import { GaugeChartConfiguration } from './definition-schema.js'
5
5
  import * as echarts from 'echarts/core'
6
6
  import { TooltipComponent } from 'echarts/components'
7
7
  import { GaugeChart, GaugeSeriesOption } from 'echarts/charts'
8
- import { CanvasRenderer, SVGRenderer } from 'echarts/renderers'
8
+ import { CanvasRenderer } from 'echarts/renderers'
9
9
  import { EChartsOption, SeriesOption } from 'echarts'
10
10
 
11
11
  echarts.use([TooltipComponent, GaugeChart, CanvasRenderer])
12
12
 
13
13
  type Dataseries = Exclude<GaugeChartConfiguration['dataseries'], undefined>[number]
14
14
  type Data = Exclude<Dataseries['data'], undefined>[number]
15
-
15
+ type Theme = {
16
+ theme_name: string
17
+ theme_object: any
18
+ }
16
19
  @customElement('widget-gauge-versionplaceholder')
17
20
  export class WidgetGauge extends LitElement {
18
21
  @property({ type: Object })
19
22
  inputData?: GaugeChartConfiguration
20
23
 
21
24
  @property({ type: Object })
22
- themeObject?: any
23
-
24
- @property({ type: String })
25
- themeName?: string
25
+ theme?: Theme
26
26
 
27
27
  @state()
28
28
  private dataSets: Dataseries[] = []
29
29
 
30
30
  @state()
31
- private canvasList: any = {}
32
-
33
- @state()
34
- private themeBgColor?: string
31
+ private canvasList: Map<
32
+ string,
33
+ { echart?: echarts.ECharts; title?: HTMLHeadingElement; wrapper?: HTMLDivElement }
34
+ > = new Map()
35
35
 
36
- @state()
37
- private themeColor?: string
36
+ @state() private themeBgColor?: string
37
+ @state() private themeTitleColor?: string
38
+ @state() private themeSubtitleColor?: string
38
39
 
39
40
  private resizeObserver: ResizeObserver
40
41
 
@@ -48,7 +49,10 @@ export class WidgetGauge extends LitElement {
48
49
 
49
50
  constructor() {
50
51
  super()
51
- this.resizeObserver = new ResizeObserver(this.adjustSizes.bind(this))
52
+ this.resizeObserver = new ResizeObserver(() => {
53
+ this.adjustSizes()
54
+ this.applyData()
55
+ })
52
56
 
53
57
  this.template = {
54
58
  title: {
@@ -156,16 +160,20 @@ export class WidgetGauge extends LitElement {
156
160
  update(changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
157
161
  if (changedProperties.has('inputData') && this.gaugeContainer) {
158
162
  this.transformData()
163
+ this.setupCharts()
164
+ this.sizingSetup()
165
+ this.adjustSizes()
166
+ this.applyData()
159
167
  }
160
168
 
161
- if (changedProperties.has('themeObject')) {
162
- this.registerTheme(this.themeName, this.themeObject)
163
- }
164
-
165
- if (changedProperties.has('themeName')) {
166
- this.registerTheme(this.themeName, this.themeObject)
169
+ if (changedProperties.has('theme')) {
170
+ this.registerTheme(this.theme)
167
171
  this.deleteCharts()
172
+ this.transformData()
168
173
  this.setupCharts()
174
+ this.sizingSetup()
175
+ this.adjustSizes()
176
+ this.applyData()
169
177
  }
170
178
  super.update(changedProperties)
171
179
  }
@@ -174,12 +182,23 @@ export class WidgetGauge extends LitElement {
174
182
  this.resizeObserver.observe(this.shadowRoot?.querySelector('.wrapper') as HTMLDivElement)
175
183
  this.gaugeContainer = this.shadowRoot?.querySelector('.gauge-container')
176
184
  this.transformData()
185
+ this.setupCharts()
186
+ this.sizingSetup()
187
+ this.adjustSizes()
188
+ this.applyData()
177
189
  }
178
190
 
179
- registerTheme(themeName?: string, themeObject?: any) {
180
- if (!themeObject || !themeName) return
181
-
182
- echarts.registerTheme(themeName, this.themeObject)
191
+ registerTheme(theme?: Theme) {
192
+ console.log('Registering theme', theme)
193
+ if (!theme || !theme.theme_object || !theme.theme_name) return
194
+
195
+ echarts.registerTheme(theme.theme_name, theme.theme_object)
196
+ const cssTextColor = getComputedStyle(this).getPropertyValue('--re-text-color').trim()
197
+ const cssBgColor = getComputedStyle(this).getPropertyValue('--re-background-color').trim()
198
+ this.themeBgColor = cssBgColor || this.theme?.theme_object?.backgroundColor
199
+ this.themeTitleColor = cssTextColor || this.theme?.theme_object?.title?.textStyle?.color
200
+ this.themeSubtitleColor =
201
+ cssTextColor || this.theme?.theme_object?.title?.subtextStyle?.color || this.themeTitleColor
183
202
  }
184
203
 
185
204
  sizingSetup() {
@@ -248,10 +267,9 @@ export class WidgetGauge extends LitElement {
248
267
 
249
268
  this.modifier = modifier
250
269
 
251
- for (const canvas in this.canvasList) {
252
- this.canvasList[canvas].echart.resize()
253
- }
254
- this.applyData()
270
+ this.canvasList.forEach((canvasObj) => {
271
+ canvasObj.echart?.resize()
272
+ })
255
273
  }
256
274
 
257
275
  async transformData() {
@@ -279,12 +297,10 @@ export class WidgetGauge extends LitElement {
279
297
  this.dataSets.push(pds)
280
298
  })
281
299
  })
282
-
283
- this.setupCharts()
284
- this.adjustSizes()
285
300
  }
286
301
 
287
302
  applyData() {
303
+ if (!this.gaugeContainer) return
288
304
  const modifier = this.modifier
289
305
  this.dataSets.forEach((d) => {
290
306
  d.label ??= ''
@@ -312,7 +328,7 @@ export class WidgetGauge extends LitElement {
312
328
  if (isNaN(ds.range as number)) ds.range = 100
313
329
  ds.ranges = ds.sections?.sectionLimits?.map((v, i, a) => v - (a?.[i - 1] ?? 0)).slice(1) ?? []
314
330
 
315
- // const option = this.canvasList[ds.label].getOption()
331
+ // const option = this.canvasList.get(ds.label)?.echart.getOption()
316
332
  const option = window.structuredClone(this.template)
317
333
  const seriesArr = option.series as GaugeSeriesOption[]
318
334
  const ga: any = seriesArr?.[0],
@@ -329,8 +345,8 @@ export class WidgetGauge extends LitElement {
329
345
  ga.data[0].value = ds.needleValue
330
346
  ga.data[0].name = ds.unit
331
347
  ga.title.fontSize = 25 * modifier
332
- ga.title.color = ds.valueColor ?? this.themeColor
333
- ga.detail.color = ds.valueColor ?? this.themeColor
348
+ ga.title.color = ds.valueColor ?? this.themeTitleColor
349
+ ga.detail.color = ds.valueColor ?? this.themeTitleColor
334
350
  ga.detail.fontSize = 40 * modifier
335
351
  ga.detail.formatter = (val: number) =>
336
352
  isNaN(val) ? '-' : val.toFixed(Math.floor(ds.precision ?? 0))
@@ -370,37 +386,39 @@ export class WidgetGauge extends LitElement {
370
386
  ga.progress.itemStyle.color = progressColor
371
387
  ga.progress.width = 60 * modifier
372
388
  // Apply
373
- const titleElement = this.canvasList[ds.label]?.title
374
- titleElement.style.fontSize = String(20 * modifier) + 'px'
375
- titleElement.style.maxWidth = String(300 * modifier) + 'px'
376
- titleElement.style.height = String(25 * modifier) + 'px'
377
- titleElement.textContent = ds.label ?? ''
389
+ const titleElement = this.canvasList.get(ds.label)?.title
390
+ if (titleElement) {
391
+ titleElement.style.fontSize = String(20 * modifier) + 'px'
392
+ titleElement.style.maxWidth = String(300 * modifier) + 'px'
393
+ titleElement.style.height = String(25 * modifier) + 'px'
394
+ titleElement.textContent = ds.label ?? ''
395
+ }
378
396
 
379
- this.canvasList[ds.label]?.echart.setOption(option)
397
+ this.canvasList.get(ds.label)?.echart?.setOption(option)
380
398
  }
381
399
  }
382
-
383
400
  deleteCharts() {
384
- for (const label in this.canvasList) {
385
- this.canvasList[label].echart.dispose()
386
- this.canvasList[label].wrapper?.remove()
387
- delete this.canvasList[label]
388
- }
401
+ this.canvasList.forEach((canvasObj, label) => {
402
+ canvasObj.echart?.dispose()
403
+ canvasObj.wrapper?.remove()
404
+ })
405
+ this.canvasList.clear()
389
406
  }
390
407
 
391
408
  setupCharts() {
409
+ if (!this.gaugeContainer) return
392
410
  // remove the gauge canvases of non provided data series
393
- for (const label in this.canvasList) {
411
+ this.canvasList.forEach((canvasObj, label) => {
394
412
  const ex = this.dataSets.find((ds) => ds.label === label)
395
413
  if (!ex) {
396
- this.canvasList[label].echart.dispose()
397
- this.canvasList[label].wrapper?.remove()
398
- delete this.canvasList[label]
414
+ canvasObj.echart?.dispose()
415
+ canvasObj.wrapper?.remove()
416
+ this.canvasList.delete(label)
399
417
  }
400
- }
418
+ })
401
419
 
402
420
  this.dataSets.forEach((ds) => {
403
- if (this.canvasList[ds.label ?? '']) return
421
+ if (this.canvasList.has(ds.label ?? '')) return
404
422
  const newWrapper = document.createElement('div')
405
423
  newWrapper.setAttribute('class', 'chart-wrapper')
406
424
  const newTitle = document.createElement('h3')
@@ -417,21 +435,14 @@ export class WidgetGauge extends LitElement {
417
435
  newWrapper!.appendChild(newCanvas)
418
436
  this.gaugeContainer!.appendChild(newWrapper)
419
437
 
420
- const newChart = echarts.init(newCanvas, this.themeName)
421
- this.canvasList[ds.label ?? ''] = { echart: newChart, title: newTitle, wrapper: newWrapper }
422
- // this.canvasList[ds.label ?? ''].setOption(structuredClone(this.template))
423
- //@ts-ignore
424
- this.themeBgColor = newChart?._theme?.backgroundColor
425
- //@ts-ignore
426
- this.themeColor = newChart?._theme?.gauge?.title?.color
438
+ const newChart = echarts.init(newCanvas, this.theme?.theme_name)
439
+ this.canvasList.set(ds.label ?? '', { echart: newChart, title: newTitle, wrapper: newWrapper })
427
440
  })
428
- this.sizingSetup()
429
441
  }
430
442
 
431
443
  static styles = css`
432
444
  :host {
433
445
  display: block;
434
- color: var(--re-text-color, #000);
435
446
  font-family: sans-serif;
436
447
  box-sizing: border-box;
437
448
  position: relative;
@@ -449,7 +460,6 @@ export class WidgetGauge extends LitElement {
449
460
  width: 100%;
450
461
  padding: 16px;
451
462
  box-sizing: border-box;
452
- color: var(--re-text-color, #000);
453
463
  gap: 12px;
454
464
  }
455
465
 
@@ -497,7 +507,6 @@ export class WidgetGauge extends LitElement {
497
507
 
498
508
  .no-data {
499
509
  font-size: 20px;
500
- color: var(--re-text-color, #000);
501
510
  display: flex;
502
511
  height: 100%;
503
512
  width: 100%;
@@ -509,10 +518,19 @@ export class WidgetGauge extends LitElement {
509
518
 
510
519
  render() {
511
520
  return html`
512
- <div class="wrapper" style="background-color: ${this.themeBgColor}; color: ${this.themeColor}">
521
+ <div
522
+ class="wrapper"
523
+ style="background-color: ${this.themeBgColor}; color: ${this.themeTitleColor}"
524
+ >
513
525
  <header class="paging" ?active=${this.inputData?.title || this.inputData?.subTitle}>
514
526
  <h3 class="paging" ?active=${this.inputData?.title}>${this.inputData?.title}</h3>
515
- <p class="paging" ?active=${this.inputData?.subTitle}>${this.inputData?.subTitle}</p>
527
+ <p
528
+ class="paging"
529
+ ?active=${this.inputData?.subTitle}
530
+ style="color: ${this.themeSubtitleColor}"
531
+ >
532
+ ${this.inputData?.subTitle}
533
+ </p>
516
534
  </header>
517
535
  <div class="paging no-data" ?active=${!this.dataSets.length}>No Data</div>
518
536
  <div class="gauge-container"></div>