2022-12-16 12:52:00 -05:00
|
|
|
import Component from "@glimmer/component";
|
2023-12-01 10:47:54 -05:00
|
|
|
import { action } from "@ember/object";
|
2021-02-18 06:06:22 -05:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2022-12-16 12:52:00 -05:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
2023-12-01 10:47:54 -05:00
|
|
|
import themeColor from "../lib/themeColor";
|
2021-02-18 06:06:22 -05:00
|
|
|
|
2022-12-16 12:52:00 -05:00
|
|
|
export default class DataExplorerBarChart extends Component {
|
|
|
|
chart;
|
|
|
|
barsColor = themeColor("--tertiary");
|
|
|
|
barsHoverColor = themeColor("--tertiary-high");
|
|
|
|
gridColor = themeColor("--primary-low");
|
|
|
|
labelsColor = themeColor("--primary-medium");
|
2021-02-18 06:06:22 -05:00
|
|
|
|
2024-11-20 09:45:48 -05:00
|
|
|
willDestroy() {
|
|
|
|
super.willDestroy(...arguments);
|
|
|
|
this.chart.destroy();
|
|
|
|
}
|
|
|
|
|
2022-12-16 12:52:00 -05:00
|
|
|
get config() {
|
|
|
|
const data = this.data;
|
|
|
|
const options = this.options;
|
2021-02-18 06:06:22 -05:00
|
|
|
return {
|
|
|
|
type: "bar",
|
|
|
|
data,
|
|
|
|
options,
|
|
|
|
};
|
2022-12-16 12:52:00 -05:00
|
|
|
}
|
2021-02-18 06:06:22 -05:00
|
|
|
|
2022-12-16 12:52:00 -05:00
|
|
|
get data() {
|
|
|
|
const labels = this.args.labels;
|
2021-02-18 06:06:22 -05:00
|
|
|
return {
|
|
|
|
labels,
|
|
|
|
datasets: [
|
|
|
|
{
|
2022-12-16 12:52:00 -05:00
|
|
|
label: this.args.datasetName,
|
|
|
|
data: this.args.values,
|
2021-02-18 06:06:22 -05:00
|
|
|
backgroundColor: this.barsColor,
|
|
|
|
hoverBackgroundColor: this.barsHoverColor,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2022-12-16 12:52:00 -05:00
|
|
|
}
|
2021-02-18 06:06:22 -05:00
|
|
|
|
2022-12-16 12:52:00 -05:00
|
|
|
get options() {
|
2021-02-18 06:06:22 -05:00
|
|
|
return {
|
|
|
|
scales: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
fontColor: this.labelsColor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
xAxes: [
|
|
|
|
{
|
|
|
|
gridLines: {
|
|
|
|
color: this.gridColor,
|
|
|
|
zeroLineColor: this.gridColor,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
fontColor: this.labelsColor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
yAxes: [
|
|
|
|
{
|
|
|
|
gridLines: {
|
|
|
|
color: this.gridColor,
|
|
|
|
zeroLineColor: this.gridColor,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
beginAtZero: true,
|
|
|
|
fontColor: this.labelsColor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
2022-12-16 12:52:00 -05:00
|
|
|
}
|
2021-02-18 06:06:22 -05:00
|
|
|
|
2022-12-16 12:52:00 -05:00
|
|
|
@bind
|
|
|
|
async initChart(canvas) {
|
|
|
|
await loadScript("/javascripts/Chart.min.js");
|
|
|
|
const context = canvas.getContext("2d");
|
|
|
|
// eslint-disable-next-line
|
|
|
|
this.chart = new Chart(context, this.config);
|
|
|
|
}
|
2021-02-18 06:06:22 -05:00
|
|
|
|
2022-12-16 12:52:00 -05:00
|
|
|
@action
|
|
|
|
updateChartData() {
|
2021-02-18 06:06:22 -05:00
|
|
|
this.chart.data = this.data;
|
|
|
|
this.chart.update();
|
2022-12-16 12:52:00 -05:00
|
|
|
}
|
|
|
|
}
|