fix: restore history chart initialization after flood bands change
This commit is contained in:
+17
-11
@@ -370,30 +370,36 @@
|
|||||||
level: { type: 'linear', position: 'right', grid: { drawOnChartArea: false } },
|
level: { type: 'linear', position: 'right', grid: { drawOnChartArea: false } },
|
||||||
x: { ticks: { maxTicksLimit: 12 } }
|
x: { ticks: { maxTicksLimit: 12 } }
|
||||||
},
|
},
|
||||||
|
plugins: {
|
||||||
|
legend: { display: true },
|
||||||
|
floodBands: { enabled: true }
|
||||||
|
}
|
||||||
|
},
|
||||||
plugins: [{
|
plugins: [{
|
||||||
id: 'flood-bands',
|
id: 'floodBands',
|
||||||
beforeDraw(chart) {
|
beforeDraw(chart, _args, pluginOptions) {
|
||||||
|
if (!pluginOptions.enabled || !chart.chartArea) return;
|
||||||
const levelScale = chart.scales?.level;
|
const levelScale = chart.scales?.level;
|
||||||
if (!levelScale || levelScale.min == null) return;
|
if (!levelScale || !Number.isFinite(levelScale.min) || !Number.isFinite(levelScale.max)) return;
|
||||||
|
const { ctx, chartArea } = chart;
|
||||||
const zones = [
|
const zones = [
|
||||||
{ min: levelScale.min, max: 3.0, color: 'rgba(30,139,96,.08)' },
|
{ min: levelScale.min, max: 3.0, color: 'rgba(30,139,96,.08)' },
|
||||||
{ min: 3.0, max: 4.5, color: 'rgba(217,144,24,.12)' },
|
{ min: 3.0, max: 4.5, color: 'rgba(217,144,24,.12)' },
|
||||||
{ min: 4.5, max: levelScale.max, color: 'rgba(204,75,55,.15)' },
|
{ min: 4.5, max: levelScale.max, color: 'rgba(204,75,55,.15)' },
|
||||||
];
|
];
|
||||||
zones.forEach((z) => {
|
|
||||||
const top = levelScale.getPixelForValue(Math.min(z.max, levelScale.max));
|
|
||||||
const bottom = levelScale.getPixelForValue(Math.max(z.min, levelScale.min));
|
|
||||||
if (bottom > top) {
|
|
||||||
const { ctx, chartArea } = chart;
|
|
||||||
ctx.save();
|
ctx.save();
|
||||||
|
zones.forEach((z) => {
|
||||||
|
const min = Math.max(z.min, levelScale.min);
|
||||||
|
const max = Math.min(z.max, levelScale.max);
|
||||||
|
if (max <= min) return;
|
||||||
|
const top = levelScale.getPixelForValue(max);
|
||||||
|
const bottom = levelScale.getPixelForValue(min);
|
||||||
ctx.fillStyle = z.color;
|
ctx.fillStyle = z.color;
|
||||||
ctx.fillRect(chartArea.left, top, chartArea.right - chartArea.left, bottom - top);
|
ctx.fillRect(chartArea.left, top, chartArea.right - chartArea.left, bottom - top);
|
||||||
|
});
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
}]
|
}]
|
||||||
}
|
|
||||||
});
|
});
|
||||||
$('history-status').textContent = rows.length ? `${rows.length} measurements (${sampled.length} daily) from PostgreSQL` : 'No PostgreSQL measurements in this period';
|
$('history-status').textContent = rows.length ? `${rows.length} measurements (${sampled.length} daily) from PostgreSQL` : 'No PostgreSQL measurements in this period';
|
||||||
$('history-card').scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
$('history-card').scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||||
|
|||||||
Reference in New Issue
Block a user