Skip to content

Commit 8bc38a8

Browse files
author
Marko Petzold
committed
fix: Update widget-doughnut component to use dynamic versioning and improve chart disposal logic
1 parent 5abce85 commit 8bc38a8

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

demo/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@
2626
<script src="https://storage.googleapis.com/reswarm-images/ObjectRandomizer.js"></script>
2727

2828
<script type="module">
29-
import { html, render } from 'lit'
29+
import { render } from 'lit'
30+
import { html, unsafeStatic } from 'lit/static-html.js'
3031
import '../dist/widget-doughnut.js'
32+
const packageJson = await fetch('../package.json').then((res) => res.json())
33+
const tag = unsafeStatic(`widget-doughnut-${packageJson.version}`)
34+
3135
const data = await fetch('../src/default-data.json').then((res) => res.json())
3236
const themeObject = await fetch('themes/vintage.json').then((res) => res.json())
3337
const theme = { theme_name: 'vintage', theme_object: themeObject }
3438
render(
3539
html`
36-
<widget-doughnut-1.5.7 .theme="${theme}" .inputData=${data}>
40+
<${tag} .theme="${theme}" .inputData=${data}>
3741
web component is not registered. Make sure to use the tag with the correct version
3842
string from the package.json
39-
</widget-doughnut-1.5.7>
43+
</${tag}>
4044
`,
4145
document.getElementById('demo')
4246
)

src/widget-doughnut.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ export class WidgetDoughnut extends LitElement {
6666
this.resizeObserver.observe(this)
6767

6868
this.template = {
69-
title: {
70-
text: 'Pie',
71-
left: 'center',
72-
textStyle: {
73-
fontSize: 10
69+
title: [
70+
{
71+
text: 'Pie',
72+
left: 'center',
73+
textStyle: {
74+
fontSize: 10
75+
}
7476
}
75-
},
77+
],
7678
color: undefined,
7779
// toolbox: {
7880
// show: true,
@@ -158,14 +160,6 @@ export class WidgetDoughnut extends LitElement {
158160
this.themeTitleColor = cssTextColor || this.theme?.theme_object?.title?.textStyle?.color
159161
this.themeSubtitleColor =
160162
cssTextColor || this.theme?.theme_object?.title?.subtextStyle?.color || this.themeTitleColor
161-
console.log(
162-
'Theme colors',
163-
this.themeBgColor,
164-
this.themeTitleColor,
165-
this.themeSubtitleColor,
166-
cssBgColor,
167-
cssTextColor
168-
)
169163
if (!theme || !theme.theme_object || !theme.theme_name) return
170164

171165
echarts.registerTheme(theme.theme_name, theme.theme_object)
@@ -248,7 +242,10 @@ export class WidgetDoughnut extends LitElement {
248242
async transformData() {
249243
if (!this?.inputData?.dataseries?.length) return
250244
// reset all existing chart dataseries
251-
this.canvasList.forEach((chartM: ChartCombination) => (chartM.dataSets = []))
245+
this.canvasList.forEach((chartM) => {
246+
chartM.dataSets = []
247+
chartM.doomed = true
248+
})
252249
this.inputData.dataseries.forEach((ds) => {
253250
ds.label = ds.label ?? ''
254251

@@ -306,9 +303,17 @@ export class WidgetDoughnut extends LitElement {
306303
}
307304
})
308305
})
309-
// prevent duplicate transformation
310-
// this.inputData.dataseries = []
311-
// console.log('new doughnut datasets', this.canvasList)
306+
307+
const doomedCharts: string[] = []
308+
// remove all doomed charts
309+
this.canvasList.forEach((chart, label) => {
310+
if (!chart.doomed) return
311+
chart.echart?.dispose()
312+
chart.element?.remove()
313+
doomedCharts.push(label)
314+
})
315+
316+
doomedCharts.forEach((label) => this.canvasList.delete(label))
312317
}
313318

314319
async applyData() {
@@ -318,13 +323,13 @@ export class WidgetDoughnut extends LitElement {
318323
this.canvasList.forEach((chartM, label) => {
319324
for (const ds of chartM.dataSets) {
320325
// const option = this.canvasList[ds.label].getOption()
321-
const option: any = structuredClone(this.template)
326+
const option: any = chartM.echart?.getOption() ?? window.structuredClone(this.template)
322327
const series = option.series[0],
323328
series2 = option.series[1]
324329

325330
// Title
326-
option.title.text = ds.label
327-
option.title.textStyle.fontSize = 18 * modifier
331+
option.title[0].text = ds.label
332+
option.title[0].textStyle.fontSize = 18 * modifier
328333
// option.color = ds.sections?.[0]?.map((d) => d.color)
329334

330335
series.radius[0] = String(parseFloat(ds.settings?.cutout ?? '50%') * 0.6) + '%'
@@ -341,9 +346,7 @@ export class WidgetDoughnut extends LitElement {
341346
series2.label.fontSize = 14 * modifier
342347

343348
// Apply
344-
const oldOption: any = chartM.echart?.getOption() ?? {}
345-
const notMerge = oldOption.series?.length !== chartM.dataSets.length
346-
chartM.echart?.setOption(option, { notMerge })
349+
chartM.echart?.setOption(option)
347350
}
348351
})
349352
}
@@ -375,7 +378,6 @@ export class WidgetDoughnut extends LitElement {
375378

376379
const theme =
377380
this.theme?.theme_name === '---' || !this.theme?.theme_name ? 'light' : this.theme?.theme_name
378-
console.log('Using theme', theme)
379381
const newChart = echarts.init(newContainer, theme)
380382
const chart = {
381383
echart: newChart,

0 commit comments

Comments
 (0)