2022-06-17 09:01:34 -04:00
|
|
|
import Component from "@ember/component";
|
2021-02-18 06:06:22 -05:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2022-06-17 09:01:34 -04:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2021-02-18 06:06:22 -05:00
|
|
|
import themeColor from "../lib/themeColor";
|
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
export default Component.extend({
|
2021-02-18 06:06:22 -05:00
|
|
|
barsColor: themeColor("--tertiary"),
|
|
|
|
barsHoverColor: themeColor("--tertiary-high"),
|
|
|
|
gridColor: themeColor("--primary-low"),
|
|
|
|
labelsColor: themeColor("--primary-medium"),
|
|
|
|
chart: null,
|
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
@discourseComputed("data", "options")
|
2021-02-18 06:06:22 -05:00
|
|
|
config(data, options) {
|
|
|
|
return {
|
|
|
|
type: "bar",
|
|
|
|
data,
|
|
|
|
options,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
@discourseComputed("labels.[]", "values.[]", "datasetName")
|
2021-02-18 06:06:22 -05:00
|
|
|
data(labels, values, datasetName) {
|
|
|
|
return {
|
|
|
|
labels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: datasetName,
|
|
|
|
data: values,
|
|
|
|
backgroundColor: this.barsColor,
|
|
|
|
hoverBackgroundColor: this.barsHoverColor,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
@discourseComputed
|
2021-02-18 06:06:22 -05:00
|
|
|
options() {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._initChart();
|
|
|
|
},
|
|
|
|
|
|
|
|
didUpdate() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.chart.data = this.data;
|
|
|
|
this.chart.update();
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.chart.destroy();
|
|
|
|
},
|
|
|
|
|
|
|
|
_initChart() {
|
|
|
|
loadScript("/javascripts/Chart.min.js").then(() => {
|
|
|
|
const canvas = this.element.querySelector("canvas");
|
|
|
|
const context = canvas.getContext("2d");
|
|
|
|
const config = this.config;
|
|
|
|
// eslint-disable-next-line
|
|
|
|
this.chart = new Chart(context, config);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|