-
Notifications
You must be signed in to change notification settings - Fork 4
/
BarChart.ts
88 lines (81 loc) · 2.09 KB
/
BarChart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import Control from "sap/ui/core/Control";
// https://www.chartjs.org/docs/latest/getting-started/integration.html#bundlers-webpack-rollup-etc
import Chart from "chart.js/auto";
import RenderManager from "sap/ui/core/RenderManager";
/**
* @name org.openui5.bestofui5.control.BarChart
*/
export default class BarChart extends Control {
private _chart: Chart;
// The following three lines were generated and should remain as-is to make TypeScript aware of the constructor signatures
constructor(idOrSettings?: string | $BarChartSettings);
constructor(id?: string, settings?: $BarChartSettings);
constructor(id?: string, settings?: $BarChartSettings) {
super(id, settings);
}
static readonly metadata = {
properties: {
title: "string",
color: "sap.ui.core.CSSColor",
},
aggregations: {
records: {
type: "org.openui5.bestofui5.control.ChartRecord",
},
},
defaultAggregation: "records",
};
static renderer = {
apiVersion: 2,
render: (rm: RenderManager, chart: BarChart) => {
rm.openStart("div", chart);
rm.style("color", chart.getColor());
rm.class("chartPadding");
rm.openEnd();
rm.openStart("canvas", chart.getId() + "-canvas");
rm.openEnd();
rm.close("canvas");
rm.close("div");
},
};
_getChartData() {
const aRecords = this.getRecords();
return {
labels: aRecords.map((record) => {
return record.getLabel();
}),
datasets: [
{
label: this.getTitle(),
backgroundColor: this.getColor(),
borderColor: this.getColor(),
data: aRecords.map((record) => {
return record.getValue();
}),
},
],
};
}
onAfterRendering() {
if (!this._chart) {
this._chart = new Chart(this.getDomRef("canvas") as HTMLCanvasElement, {
type: "bar",
data: this._getChartData(),
options: {
responsive: true,
animation: false,
scales: {
x: {
display: false,
},
},
},
});
} else {
this._chart.data = this._getChartData();
this._chart.update();
}
}
}