MapComplete/src/UI/Base/ChartJs.ts

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

119 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-07-20 12:04:14 +02:00
import BaseUIElement from "../BaseUIElement"
import { Chart, ChartConfiguration, ChartType, DefaultDataPoint, registerables } from "chart.js"
Chart?.register(...(registerables ?? []))
2022-07-20 12:04:14 +02:00
export class ChartJsColours {
public static readonly unknownColor = "rgba(128, 128, 128, 0.2)"
public static readonly unknownBorderColor = "rgba(128, 128, 128, 0.2)"
public static readonly otherColor = "rgba(128, 128, 128, 0.2)"
public static readonly otherBorderColor = "rgba(128, 128, 255)"
public static readonly notApplicableColor = "rgba(128, 128, 128, 0.2)"
public static readonly notApplicableBorderColor = "rgba(255, 0, 0)"
public static readonly backgroundColors = [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)",
]
public static readonly borderColors = [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)",
]
}
2022-07-20 12:04:14 +02:00
export default class ChartJs<
TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>,
TLabel = unknown
> extends BaseUIElement {
private readonly _config: ChartConfiguration<TType, TData, TLabel>
2022-09-08 21:40:48 +02:00
2022-07-20 12:04:14 +02:00
constructor(config: ChartConfiguration<TType, TData, TLabel>) {
super()
this._config = config
}
2022-09-08 21:40:48 +02:00
2024-06-16 16:06:26 +02:00
public static ConstructDoughnut(data: Record<string, number>) {
const borderColor = [
2024-06-16 16:06:26 +02:00
// ChartJsColours.unkownBorderColor,
// ChartJsColours.otherBorderColor,
// ChartJsColours.notApplicableBorderColor,
]
const backgroundColor = [
2024-06-16 16:06:26 +02:00
// ChartJsColours.unkownColor,
// ChartJsColours.otherColor,
// ChartJsColours.notApplicableColor,
]
let i = 0
const borders = ChartJsColours.borderColors
const bg = ChartJsColours.backgroundColors
for (const key in data) {
2024-06-16 16:06:26 +02:00
if (key === "") {
borderColor.push(ChartJsColours.unknownBorderColor)
backgroundColor.push(ChartJsColours.unknownColor)
2024-06-16 16:06:26 +02:00
} else {
borderColor.push(borders[i % borders.length])
backgroundColor.push(bg[i % bg.length])
i++
}
}
const config = <ChartConfiguration>{
type: "doughnut",
data: {
labels: Object.keys(data),
datasets: [
{
data: Object.values(data),
backgroundColor,
borderColor,
borderWidth: 1,
label: undefined,
},
],
},
options: {
plugins: {
legend: {
display: false,
},
},
},
}
return new ChartJs(config)
}
2022-07-20 12:04:14 +02:00
protected InnerConstructElement(): HTMLElement {
const canvas = document.createElement("canvas")
2022-08-22 13:34:47 +02:00
// A bit exceptional: we apply the styles before giving them to 'chartJS'
if (this.style !== undefined) {
canvas.style.cssText = this.style
}
if (this.clss?.size > 0) {
try {
canvas.classList.add(...Array.from(this.clss))
} catch (e) {
console.error(
"Invalid class name detected in:",
Array.from(this.clss).join(" "),
"\nErr msg is ",
e
)
}
}
2022-07-20 12:04:14 +02:00
new Chart(canvas, this._config)
return canvas
}
}