@record-evolution/widget-linechart 1.6.15 → 1.6.17

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": "Webcomponent widget-linechart following open-wc recommendations",
4
4
  "license": "MIT",
5
5
  "author": "widget-linechart",
6
- "version": "1.6.15",
6
+ "version": "1.6.17",
7
7
  "type": "module",
8
8
  "main": "dist/widget-linechart.js",
9
9
  "types": "dist/src/widget-linechart.d.ts",
@@ -38,7 +38,7 @@ type Theme = {
38
38
  theme_object: any
39
39
  }
40
40
 
41
- type SeriesOptionX = SeriesOption & { minDate?: number; maxDate?: number }
41
+ type SeriesOptionX = SeriesOption & { minDate?: number; maxDate?: number; drawOrder: number }
42
42
  @customElement('widget-linechart-versionplaceholder')
43
43
  export class WidgetLinechart extends LitElement {
44
44
  @property({ type: Object })
@@ -50,7 +50,13 @@ export class WidgetLinechart extends LitElement {
50
50
  @state()
51
51
  private canvasList: Map<
52
52
  string,
53
- { echart?: echarts.ECharts; series: SeriesOptionX[]; doomed?: boolean; element?: HTMLDivElement }
53
+ {
54
+ echart?: echarts.ECharts
55
+ series: SeriesOptionX[]
56
+ doomed?: boolean
57
+ element?: HTMLDivElement
58
+ drawing: boolean
59
+ }
54
60
  > = new Map()
55
61
 
56
62
  @state() private themeBgColor?: string
@@ -65,6 +71,8 @@ export class WidgetLinechart extends LitElement {
65
71
  version: string = 'versionplaceholder'
66
72
  chartContainer: HTMLDivElement | null | undefined
67
73
  resizeObserver?: ResizeObserver
74
+ lastUpdateTime: number = 0
75
+ updateThresholdMs: number = 300
68
76
 
69
77
  constructor() {
70
78
  super()
@@ -169,8 +177,13 @@ export class WidgetLinechart extends LitElement {
169
177
 
170
178
  update(changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
171
179
  if (changedProperties.has('inputData') && this.chartContainer) {
172
- this.transformData()
173
- this.applyData()
180
+ const drawingStates = Array.from(this.canvasList).map(([key, chart]) => chart.drawing)
181
+ if (drawingStates.every((d) => !d)) {
182
+ this.transformData()
183
+ this.applyData()
184
+ } else {
185
+ console.log('skipping linechart draw')
186
+ }
174
187
  }
175
188
 
176
189
  if (changedProperties.has('theme')) {
@@ -184,9 +197,9 @@ export class WidgetLinechart extends LitElement {
184
197
 
185
198
  protected firstUpdated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
186
199
  this.chartContainer = this.shadowRoot?.querySelector('.chart-container')
200
+ this.registerTheme(this.theme)
187
201
  this.transformData()
188
202
  this.applyData()
189
- this.registerTheme(this.theme)
190
203
  // Add ResizeObserver for chart container
191
204
  if (this.chartContainer) {
192
205
  this.resizeObserver = new ResizeObserver(() => {
@@ -287,7 +300,8 @@ export class WidgetLinechart extends LitElement {
287
300
  symbol: ds.styling?.pointStyle ?? 'circle',
288
301
  symbolSize: (d: any[]) => d[2] ?? 4,
289
302
  showSymbol: ds.styling?.pointStyle === 'none' ? false : true,
290
- data: data2 ?? []
303
+ data: data2 ?? [],
304
+ drawOrder: ds.advanced?.drawOrder ?? 0
291
305
  }
292
306
  let chartName = ds.advanced?.chartName ?? ''
293
307
  chartName = chartName.replace('#split#', prefix)
@@ -307,6 +321,16 @@ export class WidgetLinechart extends LitElement {
307
321
  })
308
322
 
309
323
  doomedCharts.forEach((label) => this.canvasList.delete(label))
324
+ this.canvasList = new Map(
325
+ [...this.canvasList.entries()].sort(([labelA, va], [labelB, vb]) => {
326
+ const orderA = va.series?.[0].drawOrder ?? 0
327
+ const orderB = vb.series?.[0].drawOrder ?? 0
328
+ if (orderA !== orderB) {
329
+ return orderA - orderB
330
+ }
331
+ return labelA.localeCompare(labelB)
332
+ })
333
+ )
310
334
  }
311
335
 
312
336
  xAxisType(): 'value' | 'log' | 'category' | 'time' | undefined {
@@ -324,7 +348,11 @@ export class WidgetLinechart extends LitElement {
324
348
 
325
349
  applyData() {
326
350
  const modifier = 1
327
- this.requestUpdate()
351
+ // Sort chartContainer children by drawOrder and label
352
+ if (!this.chartContainer) return
353
+ for (const canvas of this.canvasList.values()) {
354
+ if (canvas.element) this.chartContainer.appendChild(canvas.element)
355
+ }
328
356
  this.canvasList.forEach((chart, label) => {
329
357
  chart.series.sort((a, b) => ((a.name as string) > (b.name as string) ? 1 : -1))
330
358
 
@@ -363,6 +391,15 @@ export class WidgetLinechart extends LitElement {
363
391
  option.series = chart.series
364
392
  // console.log('Applying data to chart', label, option)
365
393
  if (chart.series.length <= 1) option.legend.show = false
394
+ const now = Date.now()
395
+ const timeSinceLastUpdate = now - this.lastUpdateTime
396
+ this.lastUpdateTime = now
397
+
398
+ const tooFast = timeSinceLastUpdate < this.updateThresholdMs
399
+ option.animation = !tooFast
400
+ option.animationDuration = tooFast ? 0 : this.updateThresholdMs
401
+ chart.echart?.on('finished', () => (chart.drawing = false))
402
+ chart.drawing = true
366
403
  chart.echart?.setOption(option, { notMerge })
367
404
  // chart.echart?.resize()
368
405
  })
@@ -394,7 +431,12 @@ export class WidgetLinechart extends LitElement {
394
431
  this.chartContainer.appendChild(newContainer)
395
432
 
396
433
  const newChart = echarts.init(newContainer, this.theme?.theme_name)
397
- const chart = { echart: newChart, series: [] as SeriesOption[], element: newContainer }
434
+ const chart = {
435
+ echart: newChart,
436
+ series: [] as SeriesOptionX[],
437
+ element: newContainer,
438
+ drawing: false
439
+ }
398
440
  this.canvasList.set(label, chart)
399
441
 
400
442
  return chart