2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-12-10 22:01:42 +08:00
|
|
|
import discourseDebounce from "discourse-common/lib/debounce";
|
2019-01-21 22:17:04 +08:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2019-11-02 01:06:50 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2019-01-21 22:17:04 +08:00
|
|
|
import { number } from "discourse/lib/formatter";
|
2020-12-10 22:01:42 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2019-01-21 22:17:04 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2019-01-21 22:17:04 +08:00
|
|
|
classNames: ["admin-report-chart", "admin-report-stacked-chart"],
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.resizeHandler = () =>
|
2020-12-10 22:01:42 +08:00
|
|
|
discourseDebounce(this, this._scheduleChartRendering, 500);
|
2019-01-21 22:17:04 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
$(window).on("resize.chart", this.resizeHandler);
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
$(window).off("resize.chart", this.resizeHandler);
|
|
|
|
|
|
|
|
this._resetChart();
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2020-12-10 22:01:42 +08:00
|
|
|
discourseDebounce(this, this._scheduleChartRendering, 100);
|
2019-01-21 22:17:04 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_scheduleChartRendering() {
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () => {
|
2019-10-03 05:54:30 +08:00
|
|
|
if (!this.element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:45:15 +08:00
|
|
|
this._renderChart(
|
|
|
|
this.model,
|
|
|
|
this.element.querySelector(".chart-canvas")
|
|
|
|
);
|
2019-01-21 22:17:04 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-07-16 18:45:15 +08:00
|
|
|
_renderChart(model, chartCanvas) {
|
2020-09-22 22:28:28 +08:00
|
|
|
if (!chartCanvas) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-21 22:17:04 +08:00
|
|
|
|
2019-07-16 18:45:15 +08:00
|
|
|
const context = chartCanvas.getContext("2d");
|
2019-01-21 22:17:04 +08:00
|
|
|
|
2019-11-02 01:57:22 +08:00
|
|
|
const chartData = makeArray(model.get("chartData") || model.get("data"));
|
2019-01-21 22:17:04 +08:00
|
|
|
|
|
|
|
const data = {
|
2019-12-11 22:49:17 +08:00
|
|
|
labels: chartData[0].data.mapBy("x"),
|
2019-01-21 22:17:04 +08:00
|
|
|
datasets: chartData.map((cd) => {
|
|
|
|
return {
|
2019-01-22 00:10:10 +08:00
|
|
|
label: cd.label,
|
2019-01-21 22:17:04 +08:00
|
|
|
stack: "pageviews-stack",
|
|
|
|
data: cd.data.map((d) => Math.round(parseFloat(d.y))),
|
|
|
|
backgroundColor: cd.color,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
loadScript("/javascripts/Chart.min.js").then(() => {
|
|
|
|
this._resetChart();
|
2019-12-11 22:49:17 +08:00
|
|
|
|
2019-01-21 22:17:04 +08:00
|
|
|
this._chart = new window.Chart(context, this._buildChartConfig(data));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildChartConfig(data) {
|
|
|
|
return {
|
|
|
|
type: "bar",
|
|
|
|
data,
|
|
|
|
options: {
|
2019-01-22 18:20:50 +08:00
|
|
|
responsive: true,
|
|
|
|
maintainAspectRatio: false,
|
2019-12-11 22:49:17 +08:00
|
|
|
responsiveAnimationDuration: 0,
|
2019-01-21 22:17:04 +08:00
|
|
|
hover: { mode: "index" },
|
2019-12-11 22:49:17 +08:00
|
|
|
animation: {
|
|
|
|
duration: 0,
|
|
|
|
},
|
2019-01-21 22:17:04 +08:00
|
|
|
tooltips: {
|
|
|
|
mode: "index",
|
|
|
|
intersect: false,
|
|
|
|
callbacks: {
|
2019-01-22 16:57:34 +08:00
|
|
|
beforeFooter: (tooltipItem) => {
|
|
|
|
let total = 0;
|
|
|
|
tooltipItem.forEach(
|
|
|
|
(item) => (total += parseInt(item.yLabel || 0, 10))
|
|
|
|
);
|
|
|
|
return `= ${total}`;
|
|
|
|
},
|
2019-01-21 22:17:04 +08:00
|
|
|
title: (tooltipItem) =>
|
|
|
|
moment(tooltipItem[0].xLabel, "YYYY-MM-DD").format("LL"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
padding: {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
yAxes: [
|
|
|
|
{
|
|
|
|
stacked: true,
|
|
|
|
display: true,
|
|
|
|
ticks: {
|
|
|
|
userCallback: (label) => {
|
2020-09-22 22:28:28 +08:00
|
|
|
if (Math.floor(label) === label) {
|
|
|
|
return label;
|
|
|
|
}
|
2019-01-21 22:17:04 +08:00
|
|
|
},
|
2019-12-11 22:49:17 +08:00
|
|
|
callback: (label) => number(label),
|
|
|
|
sampleSize: 5,
|
|
|
|
maxRotation: 25,
|
|
|
|
minRotation: 25,
|
2019-01-21 22:17:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
xAxes: [
|
|
|
|
{
|
|
|
|
display: true,
|
|
|
|
gridLines: { display: false },
|
|
|
|
type: "time",
|
2019-05-27 21:43:27 +08:00
|
|
|
offset: true,
|
2019-01-21 22:17:04 +08:00
|
|
|
time: {
|
|
|
|
parser: "YYYY-MM-DD",
|
|
|
|
minUnit: "day",
|
2019-12-11 22:49:17 +08:00
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
sampleSize: 5,
|
|
|
|
maxRotation: 50,
|
|
|
|
minRotation: 50,
|
2019-01-21 22:17:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
_resetChart() {
|
|
|
|
if (this._chart) {
|
|
|
|
this._chart.destroy();
|
|
|
|
this._chart = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|