Align chart stripe on every dataset update

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev committed 2021-02-01 16:05:28 +03:00
1 parent 79234dcbf9
commit ba97235dd6
2 files changed
+17 -8

No files matched your search

+2 -1
View File
@@ -136,7 +136,8 @@ export class BarChart extends React.Component<Props> {
},
plugins: {
ZebraStripes: {
stripeColor: chartStripesColor
stripeColor: chartStripesColor,
interval: chartData.datasets.length ? chartData.datasets[0].data.length : 61
}
}
};
@@ -7,7 +7,6 @@ import get from "lodash/get";
const defaultOptions = {
interval: 61,
stripeMinutes: 10,
stripeColor: "#ffffff08",
};
@@ -36,12 +35,23 @@ export const ZebraStripes = {
chart.canvas.parentElement.removeChild(elem);
},
updateOptions(chart: ChartJS) {
this.options = {
...defaultOptions,
...this.getOptions(chart)
};
},
getStripeMinutes() {
return this.options.interval < 10 ? 0 : 10;
},
renderStripes(chart: ChartJS) {
if (!chart.data.datasets.length) return;
const { interval, stripeMinutes, stripeColor } = this.options;
const { interval, stripeColor } = this.options;
const { top, left, bottom, right } = chart.chartArea;
const step = (right - left) / interval;
const stripeWidth = step * stripeMinutes;
const stripeWidth = step * this.getStripeMinutes();
const cover = document.createElement("div");
const styles = cover.style;
@@ -61,14 +71,12 @@ export const ZebraStripes = {
afterInit(chart: ChartJS) {
if (!chart.data.datasets.length) return;
this.options = {
...defaultOptions,
...this.getOptions(chart)
};
this.updateOptions(chart);
this.updated = this.getLastUpdate(chart);
},
afterUpdate(chart: ChartJS) {
this.updateOptions(chart);
this.renderStripes(chart);
},