2022-11-02 14:44:06 +01:00
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
2023-03-28 05:13:48 +02:00
|
|
|
import { SpecialVisualization, SpecialVisualizationState } from "../SpecialVisualization"
|
2023-03-29 17:21:20 +02:00
|
|
|
import { Feature } from "geojson"
|
2025-07-12 15:53:51 +02:00
|
|
|
import SvelteUIElement from "../Base/SvelteUIElement"
|
|
|
|
|
import Histogram from "../BigComponents/Histogram.svelte"
|
2022-10-28 04:33:05 +02:00
|
|
|
|
2025-06-26 05:20:12 +02:00
|
|
|
export class HistogramViz extends SpecialVisualization {
|
2022-10-28 04:33:05 +02:00
|
|
|
funcName = "histogram"
|
|
|
|
|
docs = "Create a histogram for a list of given values, read from the properties."
|
2023-09-27 22:21:35 +02:00
|
|
|
needsUrls = []
|
|
|
|
|
|
2022-10-28 04:33:05 +02:00
|
|
|
example =
|
2023-03-29 17:21:20 +02:00
|
|
|
'`{histogram(\'some_key\')}` with properties being `{some_key: ["a","b","a","c"]} to create a histogram'
|
2022-10-28 04:33:05 +02:00
|
|
|
args = [
|
|
|
|
|
{
|
|
|
|
|
name: "key",
|
|
|
|
|
doc: "The key to be read and to generate a histogram from",
|
|
|
|
|
required: true,
|
2025-08-13 23:06:38 +02:00
|
|
|
},
|
2022-11-02 14:44:06 +01:00
|
|
|
]
|
2022-10-28 04:33:05 +02:00
|
|
|
|
2023-03-29 17:21:20 +02:00
|
|
|
structuredExamples(): { feature: Feature; args: string[] }[] {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
feature: <Feature>{
|
|
|
|
|
type: "Feature",
|
|
|
|
|
properties: { values: `["a","b","a","b","b","b","c","c","c","d","d"]` },
|
|
|
|
|
geometry: {
|
|
|
|
|
type: "Point",
|
|
|
|
|
coordinates: [0, 0],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
args: ["values"],
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 05:13:48 +02:00
|
|
|
constr(
|
|
|
|
|
state: SpecialVisualizationState,
|
|
|
|
|
tagSource: UIEventSource<Record<string, string>>,
|
|
|
|
|
args: string[]
|
|
|
|
|
) {
|
2025-07-12 15:53:51 +02:00
|
|
|
const values: Store<string[]> = tagSource.map((tags) => {
|
2025-07-11 04:26:56 +02:00
|
|
|
const value = tags[args[0]]
|
2022-10-28 04:33:05 +02:00
|
|
|
try {
|
|
|
|
|
if (value === "" || value === undefined) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
2025-08-13 23:06:38 +02:00
|
|
|
if (Array.isArray(value)) {
|
2025-07-11 04:26:56 +02:00
|
|
|
return value
|
|
|
|
|
}
|
2022-10-28 04:33:05 +02:00
|
|
|
return JSON.parse(value)
|
|
|
|
|
} catch (e) {
|
2025-08-13 23:06:38 +02:00
|
|
|
console.error(
|
|
|
|
|
"Could not load histogram: parsing of the list failed: ",
|
|
|
|
|
e,
|
|
|
|
|
"\nthe data to parse is",
|
|
|
|
|
value
|
|
|
|
|
)
|
2022-10-28 04:33:05 +02:00
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
})
|
2025-07-12 15:53:51 +02:00
|
|
|
return new SvelteUIElement(Histogram, { values })
|
2022-10-28 04:33:05 +02:00
|
|
|
}
|
|
|
|
|
}
|