Refactoring: Histogram now uses chartJS, simplify code

This commit is contained in:
Pieter Vander Vennet 2025-07-12 15:53:51 +02:00
parent 3918e5ec04
commit 6ae258c48d
9 changed files with 92 additions and 314 deletions

View file

@ -0,0 +1,35 @@
<script lang="ts">
import { Utils } from "../../Utils"
import type { ChartConfiguration } from "chart.js"
import ChartJs from "../Base/ChartJs.svelte"
import { ChartJsUtils } from "../Base/ChartJsUtils"
export let values: Store<string[]>
let counts: Store<Map<string, number>> = values.map(
(values) => {
if (values === undefined) {
return undefined
}
values = Utils.NoNull(values)
const counts = new Map<string, number>()
for (const value of values) {
const c = counts.get(value) ?? 0
counts.set(value, c + 1)
}
return counts
})
let max: Store<number> = counts.mapD(counts => Math.max(...Array.from(counts.values())))
let keys: Store<string> = counts.mapD(counts => {
const keys = Utils.Dedup(counts.keys())
keys.sort(/*inplace sort*/)
return keys
})
let config: Store<ChartConfiguration> = keys.mapD(keys => ChartJsUtils.createHistogramConfig(keys, counts.data), [counts])
</script>
{#if $config}
<ChartJs config={$config} />
{/if}