2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { tagName } from "@ember-decorators/component";
|
2016-04-14 13:46:01 +08:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@tagName("canvas")
|
|
|
|
export default class AdminGraph extends Component {
|
|
|
|
type = "line";
|
2019-03-29 11:54:49 +08:00
|
|
|
|
2016-04-14 13:46:01 +08:00
|
|
|
refreshChart() {
|
2019-07-16 18:45:15 +08:00
|
|
|
const ctx = this.element.getContext("2d");
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.model;
|
2016-04-14 13:46:01 +08:00
|
|
|
const rawData = this.get("model.data");
|
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let data = {
|
2016-04-14 13:46:01 +08:00
|
|
|
labels: rawData.map((r) => r.x),
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
data: rawData.map((r) => r.y),
|
|
|
|
label: model.get("title"),
|
2019-05-27 16:42:53 +08:00
|
|
|
backgroundColor: `rgba(200,220,240,${this.type === "bar" ? 1 : 0.3})`,
|
2016-04-14 13:46:01 +08:00
|
|
|
borderColor: "#08C",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const config = {
|
2019-05-27 16:15:39 +08:00
|
|
|
type: this.type,
|
2021-11-10 07:31:41 +08:00
|
|
|
data,
|
2016-04-14 13:46:01 +08:00
|
|
|
options: {
|
|
|
|
responsive: true,
|
2021-08-23 19:49:49 +08:00
|
|
|
plugins: {
|
|
|
|
tooltip: {
|
|
|
|
callbacks: {
|
|
|
|
title: (context) =>
|
|
|
|
moment(context[0].label, "YYYY-MM-DD").format("LL"),
|
|
|
|
},
|
2018-05-18 04:44:33 +08:00
|
|
|
},
|
|
|
|
},
|
2016-04-14 13:46:01 +08:00
|
|
|
scales: {
|
2021-08-23 19:49:49 +08:00
|
|
|
y: [
|
2016-04-14 13:46:01 +08:00
|
|
|
{
|
|
|
|
display: true,
|
|
|
|
ticks: {
|
2019-03-29 12:14:11 +08:00
|
|
|
stepSize: 1,
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2016-04-14 13:46:01 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-04-14 14:30:04 +08:00
|
|
|
this._chart = new window.Chart(ctx, config);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2016-04-14 13:46:01 +08:00
|
|
|
|
|
|
|
didInsertElement() {
|
2023-10-19 19:47:45 +08:00
|
|
|
super.didInsertElement(...arguments);
|
2016-04-14 13:46:01 +08:00
|
|
|
loadScript("/javascripts/Chart.min.js").then(() =>
|
|
|
|
this.refreshChart.apply(this)
|
|
|
|
);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
|
|
|
}
|