2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2016-04-14 13:46:01 +08:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2016-04-14 13:46:01 +08:00
|
|
|
tagName: "canvas",
|
2019-03-29 11:54:49 +08:00
|
|
|
type: "line",
|
|
|
|
|
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,
|
2016-04-14 13:46:01 +08:00
|
|
|
data: data,
|
|
|
|
options: {
|
|
|
|
responsive: true,
|
2018-05-18 04:44:33 +08:00
|
|
|
tooltips: {
|
|
|
|
callbacks: {
|
|
|
|
title: (context) =>
|
|
|
|
moment(context[0].xLabel, "YYYY-MM-DD").format("LL"),
|
|
|
|
},
|
|
|
|
},
|
2016-04-14 13:46:01 +08:00
|
|
|
scales: {
|
|
|
|
yAxes: [
|
|
|
|
{
|
|
|
|
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);
|
2016-04-14 13:46:01 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
loadScript("/javascripts/Chart.min.js").then(() =>
|
|
|
|
this.refreshChart.apply(this)
|
|
|
|
);
|
|
|
|
},
|
2016-04-14 14:30:04 +08:00
|
|
|
});
|