59 lines
1.2 KiB
Plaintext
Raw Normal View History

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