Files
ketapk/frontend/js/chartManager.js
T
2026-08-01 18:22:30 +02:00

58 lines
1.7 KiB
JavaScript

import { CLINICAL_THRESHOLDS } from '../../pkCore.js';
/**
* Recalcule et applique les zones de couleur en fonction du mode et du Y max actuel
*/
export function updateChartAnnotations(chart, mode, currentYMax) {
const thresholds = CLINICAL_THRESHOLDS[mode];
const annotations = {};
thresholds.zones.forEach((zone, index) => {
if (zone.min < currentYMax) {
const zoneMax = zone.max === Infinity ? currentYMax : zone.max;
annotations[`zone_${index}`] = {
type: 'box',
yMin: zone.min,
yMax: Math.min(zoneMax, currentYMax),
backgroundColor: zone.color,
borderWidth: 0,
label: {
display: true,
content: zone.name,
color: zone.textHex,
font: { size: 11, weight: 'bold' },
position: 'center'
}
};
}
});
chart.options.plugins.annotation.annotations = annotations;
}
/**
* Ajuste l'échelle Y de manière dynamique :
* - Échelle par défaut (500 ou 1000)
* - Si un pic dépasse cette valeur, l'axe monte à 110 % du pic max.
*/
export function adjustChartYScale(chart, mode, datasets) {
const defaultMax = CLINICAL_THRESHOLDS[mode].maxScale;
let highestValue = 0;
// Trouve la valeur la plus haute parmi toutes les courbes actives
datasets.forEach(ds => {
ds.data.forEach(val => {
if (val > highestValue) highestValue = val;
});
});
// Calcul du Y max : 110 % de la valeur max si dépassement, sinon valeur par défaut
let targetYMax = defaultMax;
if (highestValue > defaultMax) {
targetYMax = Math.ceil(highestValue * 1.1);
}
chart.options.scales.y.max = targetYMax;
updateChartAnnotations(chart, mode, targetYMax);
}