Skip to content

Commit

Permalink
🔧 🪲 better seriesRange cleanup and fixed stroke not appear when refre…
Browse files Browse the repository at this point in the history
…shed using higher layer number
  • Loading branch information
kunkoala committed Dec 10, 2024
1 parent 5304930 commit 4d1a03c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
9 changes: 5 additions & 4 deletions frontend/src/components/LineChartComponents/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ export default function LineChart({
const fillColor = line.fill ?? color(theme.palette.error.main);

return {
serieId: line.serieId ?? '',
threshold: horizontalYAxisThreshold,
fills: {
fill: fillColor, // change the fill of the range above threshold
Expand All @@ -390,8 +389,10 @@ export default function LineChart({
},
alternatingStrokes: {
stroke: lineColor,
strokeWidth: 2,
layer: 1,
strokeWidth: 2.5,

// somehow the layer needs to be set to a high number to be drawn on top of the other stroke, using values between 1-5 will not work and not refresh if we select another scenario
layer: 30,
strokeDasharray: [10, 10],
strokeOpacity: 1,
visible: line.stroke.visible ?? true,
Expand All @@ -415,7 +416,7 @@ export default function LineChart({

// set stroke settings from original line chart data below the threshold
series.strokes.template.setAll({
strokeWidth: seriesSettings?.stroke.strokeWidth ?? 2,
strokeWidth: seriesSettings?.stroke.strokeWidth ?? 2.5,
strokeDasharray: seriesSettings?.stroke.strokeDasharray ?? undefined,
});

Expand Down
35 changes: 16 additions & 19 deletions frontend/src/components/shared/LineChart/SeriesRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import {useLineSeriesList} from './LineSeries';
import {useLayoutEffect} from 'react';
import {AxisRenderer, ValueAxis} from '@amcharts/amcharts5/xy';
import {IGraphicsSettings} from '@amcharts/amcharts5';
import {ILineSeriesAxisRange} from '@amcharts/amcharts5/.internal/charts/xy/series/LineSeries';

export function useSeriesRange(
root: Root | null,
chart: XYChart | null,
settings: Array<ILineSeriesSettings>,
lineChartSettings: Array<ILineSeriesSettings>,
yAxis: ValueAxis<AxisRenderer> | null,
rangeSettings: Array<{
serieId?: string | number;
threshold?: number;
fills?: Partial<IGraphicsSettings>;
strokes?: Partial<IGraphicsSettings>;
Expand All @@ -25,7 +23,7 @@ export function useSeriesRange(
initializer?: (series: LineSeries, i: number) => void
) {
// Use the existing `useLineSeriesList` hook to create the series
const seriesList = useLineSeriesList(root, chart, settings, initializer);
const seriesList = useLineSeriesList(root, chart, lineChartSettings, initializer);

// Use `useLayoutEffect` to apply the series range logic after the series are created
useLayoutEffect(() => {
Expand All @@ -38,38 +36,37 @@ export function useSeriesRange(

const seriesRangeDataItem = yAxis.makeDataItem({
value: rangeSetting.threshold, // Start of the range
endValue: 1e6, // End value of the range (adjust as needed)
endValue: 1e6, // end value range (top of the chart)
});

// this creates a new range series with it's own styling for alternating strokes
if (rangeSetting.alternatingStrokes) {
const alternatingStrokeSeriesRange = series.createAxisRange(seriesRangeDataItem);
alternatingStrokeSeriesRange.strokes?.template.setAll(rangeSetting.alternatingStrokes);
}

const aboveThresholdRange = series.createAxisRange(seriesRangeDataItem);

// Apply the fill settings
// apply fill settings to aboveThresholdRange
if (rangeSetting.fills) {
aboveThresholdRange.fills?.template.setAll(rangeSetting.fills);
}

// Apply the stroke settings
// apply stroke settings to aboveThresholdRange
if (rangeSetting.strokes) {
aboveThresholdRange.strokes?.template.setAll(rangeSetting.strokes);
}

if (rangeSetting.alternatingStrokes) {
const alternatingStrokeSeriesRange = series.createAxisRange(seriesRangeDataItem);
alternatingStrokeSeriesRange.strokes?.template.setAll(rangeSetting.alternatingStrokes);
}
});

return () => {
// Dispose of the ranges when the component unmounts
seriesList.forEach((series: LineSeries) => {
series.axisRanges.each((range: ILineSeriesAxisRange) => {
if (series.axisRanges.contains(range)) {
series.axisRanges.removeValue(range);
}
});
// Clear the axis ranges when the component is unmounted
if (!series.isDisposed()) {
series.axisRanges.clear();
}
});
};
}, [seriesList, rangeSettings, yAxis]);
}, [seriesList, rangeSettings, yAxis, chart]);

return seriesList ?? null;
}

0 comments on commit 4d1a03c

Please sign in to comment.