MapComplete/src/UI/Popup/HistogramViz.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
2 KiB
TypeScript
Raw Normal View History

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"
import SvelteUIElement from "../Base/SvelteUIElement"
import Histogram from "../BigComponents/Histogram.svelte"
export class HistogramViz extends SpecialVisualization {
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 = []
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'
args = [
{
name: "key",
doc: "The key to be read and to generate a histogram from",
required: true,
}
2022-11-02 14:44:06 +01: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[]
) {
const values: Store<string[]> = tagSource.map((tags) => {
const value = tags[args[0]]
try {
if (value === "" || value === undefined) {
return undefined
}
if(Array.isArray(value)){
return value
}
return JSON.parse(value)
} catch (e) {
console.error("Could not load histogram: parsing of the list failed: ", e,"\nthe data to parse is",value)
return undefined
}
})
return new SvelteUIElement(Histogram, { values })
}
}