Skip to content

Commit

Permalink
Merge pull request #2953 from andrewbaldwin44/bugfix/redraw-request-l…
Browse files Browse the repository at this point in the history
…ines

Webui Echarts Redraw Request Lines if Changed
  • Loading branch information
cyberw authored Oct 24, 2024
2 parents 262dfd5 + 9635162 commit 0b57d3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
20 changes: 14 additions & 6 deletions locust/webui/src/components/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface IBaseChartType extends ILineChartMarkers, ILineChartTimeAxis {
[key: string]: any;
}

interface ILineChartProps<ChartType extends IBaseChartType> extends ILineChart<ChartType> {
shouldReplaceMergeLines?: boolean;
}

export default function LineChart<ChartType extends IBaseChartType>({
charts,
title,
Expand All @@ -28,7 +32,8 @@ export default function LineChart<ChartType extends IBaseChartType>({
splitAxis,
yAxisLabels,
scatterplot,
}: ILineChart<ChartType>) {
shouldReplaceMergeLines = false,
}: ILineChartProps<ChartType>) {
const [chart, setChart] = useState<ECharts | null>(null);
const isDarkMode = useSelector(({ theme: { isDarkMode } }) => isDarkMode);

Expand Down Expand Up @@ -77,11 +82,11 @@ export default function LineChart<ChartType extends IBaseChartType>({
...echartsOptions,
data: charts[key],
...(splitAxis ? { yAxisIndex: yAxisIndex || index } : {}),
...(index === 0 ? { markLine: createMarkLine<ChartType>(charts, isDarkMode) } : {}),
...(index === 0 ? { markLine: createMarkLine<ChartType>(charts) } : {}),
})),
});
}
}, [charts, chart, lines, isDarkMode]);
}, [charts, chart, lines]);

useEffect(() => {
if (chart) {
Expand Down Expand Up @@ -110,9 +115,12 @@ export default function LineChart<ChartType extends IBaseChartType>({

useEffect(() => {
if (chart) {
chart.setOption({
series: getSeriesData<ChartType>({ charts, lines, scatterplot }),
});
chart.setOption(
{
series: getSeriesData<ChartType>({ charts, lines, scatterplot }),
},
shouldReplaceMergeLines ? { replaceMerge: ['series'] } : undefined,
);
}
}, [lines]);

Expand Down
7 changes: 1 addition & 6 deletions locust/webui/src/components/LineChart/LineChart.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ScatterSeriesOption,
} from 'echarts';

import { CHART_THEME } from 'components/LineChart/LineChart.constants';
import {
ILineChart,
ILineChartZoomEvent,
Expand Down Expand Up @@ -142,16 +141,12 @@ export const createOptions = <ChartType extends Pick<ICharts, 'time'>>({
},
});

export const createMarkLine = <ChartType extends Pick<ICharts, 'markers'>>(
charts: ChartType,
isDarkMode: boolean,
) => ({
export const createMarkLine = <ChartType extends Pick<ICharts, 'markers'>>(charts: ChartType) => ({
symbol: 'none',
label: {
formatter: (params: DefaultLabelFormatterCallbackParams) => `Run #${params.dataIndex + 1}`,
padding: [0, 0, 8, 0],
},
lineStyle: { color: isDarkMode ? CHART_THEME.DARK.axisColor : CHART_THEME.LIGHT.axisColor },
data: (charts.markers || []).map((timeMarker: string) => ({ xAxis: timeMarker })),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DefaultLabelFormatterCallbackParams, ECharts } from 'echarts';
import { describe, expect, test, vi } from 'vitest';

import { CHART_THEME } from 'components/LineChart/LineChart.constants';
import { ILineChartTooltipFormatterParams } from 'components/LineChart/LineChart.types';
import {
getSeriesData,
Expand Down Expand Up @@ -188,11 +187,10 @@ describe('createMarkLine', () => {
markers: [mockTimestamps[1]],
};

const options = createMarkLine(markChartsWithMarkers, false);
const options = createMarkLine(markChartsWithMarkers);

expect(options.symbol).toBe('none');
expect(options.data).toEqual([{ xAxis: markChartsWithMarkers.markers[0] }]);
expect(options.lineStyle.color).toBe(CHART_THEME.LIGHT.axisColor);
});

test('should create multiple mark lines', () => {
Expand All @@ -201,7 +199,7 @@ describe('createMarkLine', () => {
markers: [mockTimestamps[1], mockTimestamps[3]],
};

const options = createMarkLine(markChartsWithMarkers, false);
const options = createMarkLine(markChartsWithMarkers);

expect(options.data).toEqual([
{ xAxis: markChartsWithMarkers.markers[0] },
Expand All @@ -214,22 +212,12 @@ describe('createMarkLine', () => {
...mockCharts,
markers: [mockTimestamps[1]],
};
const options = createMarkLine(markChartsWithMarkers, false);
const options = createMarkLine(markChartsWithMarkers);

expect(options.label.formatter({ dataIndex: 0 } as DefaultLabelFormatterCallbackParams)).toBe(
'Run #1',
);
});

test('should use dark mode when isDarkMode', () => {
const markChartsWithMarkers = {
...mockCharts,
markers: [mockTimestamps[1]],
};
const options = createMarkLine(markChartsWithMarkers, true);

expect(options.lineStyle.color).toBe(CHART_THEME.DARK.axisColor);
});
});

describe('onChartZoom', () => {
Expand Down

0 comments on commit 0b57d3e

Please sign in to comment.