discourse/app/assets/javascripts/admin/components/admin-graph.js.es6

58 lines
1.2 KiB
Plaintext
Raw Normal View History

import Component from "@ember/component";
2018-06-15 11:03:24 -04:00
import loadScript from "discourse/lib/load-script";
export default Component.extend({
2018-06-15 11:03:24 -04:00
tagName: "canvas",
type: "line",
2018-06-15 11:03:24 -04:00
refreshChart() {
const ctx = this.element.getContext("2d");
const model = this.model;
const rawData = this.get("model.data");
var data = {
labels: rawData.map(r => r.x),
2018-06-15 11:03:24 -04:00
datasets: [
{
data: rawData.map(r => r.y),
label: model.get("title"),
backgroundColor: `rgba(200,220,240,${this.type === "bar" ? 1 : 0.3})`,
2018-06-15 11:03:24 -04:00
borderColor: "#08C"
}
]
};
const config = {
type: this.type,
data: data,
options: {
responsive: true,
tooltips: {
callbacks: {
2018-06-15 11:03:24 -04:00
title: context =>
moment(context[0].xLabel, "YYYY-MM-DD").format("LL")
}
},
scales: {
2018-06-15 11:03:24 -04:00
yAxes: [
{
display: true,
ticks: {
stepSize: 1
2018-06-15 11:03:24 -04:00
}
}
2018-06-15 11:03:24 -04:00
]
}
2018-06-15 11:03:24 -04:00
}
};
2016-04-14 02:30:04 -04:00
this._chart = new window.Chart(ctx, config);
},
2018-06-15 11:03:24 -04:00
didInsertElement() {
loadScript("/javascripts/Chart.min.js").then(() =>
this.refreshChart.apply(this)
);
}
2016-04-14 02:30:04 -04:00
});