Skip to content

Commit

Permalink
Add generic to LineChart
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbaldwin44 committed Jul 25, 2024
1 parent 6817858 commit 56df7c4
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions locust/webui/src/components/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ import {
TooltipComponentOption,
} from 'echarts';

import { IUiState } from 'redux/slice/ui.slice';
import { ICharts } from 'types/ui.types';
import { formatLocaleString, formatLocaleTime } from 'utils/date';

interface ILine {
interface ILine<ChartType> {
name: string;
key: keyof ICharts;
key: keyof ChartType;
}

interface ICreateOptions {
interface ICreateOptions<ChartType> {
title: string;
seriesData: EChartsOption['Series'][];
charts: ICharts;
charts: ChartType;
colors?: string[];
}

export interface ILineChartProps {
export interface ILineChartProps<ChartType = ICharts> {
title: string;
lines: ILine[];
lines: ILine<ChartType>[];
colors?: string[];
}

interface ILineChart extends ILineChartProps {
charts: ICharts;
interface ILineChart<ChartType> extends ILineChartProps<ChartType> {
charts: ChartType;
}

interface ILineChartZoomEvent {
Expand All @@ -51,7 +50,12 @@ registerTheme('locust', {
},
});

const createOptions = ({ charts, title, seriesData, colors }: ICreateOptions) => ({
const createOptions = <ChartType extends Pick<ICharts, 'time'>>({
charts,
title,
seriesData,
colors,
}: ICreateOptions<ChartType>) => ({
legend: {
icon: 'circle',
inactiveColor: CHART_TEXT_COLOR,
Expand Down Expand Up @@ -155,15 +159,21 @@ const createOptions = ({ charts, title, seriesData, colors }: ICreateOptions) =>
},
});

const getSeriesData = ({ charts, lines }: { charts: IUiState['charts']; lines: ILine[] }) =>
const getSeriesData = <ChartType,>({
charts,
lines,
}: {
charts: ChartType;
lines: ILine<ChartType>[];
}) =>
lines.map(({ key, name }) => ({
name,
type: 'line',
showSymbol: true,
data: charts[key],
}));

const createMarkLine = (charts: ICharts) => ({
const createMarkLine = <ChartType extends Pick<ICharts, 'markers'>>(charts: ChartType) => ({
symbol: 'none',
label: {
formatter: (params: DefaultLabelFormatterCallbackParams) => `Run #${params.dataIndex + 1}`,
Expand All @@ -172,7 +182,12 @@ const createMarkLine = (charts: ICharts) => ({
data: (charts.markers || []).map((timeMarker: string) => ({ xAxis: timeMarker })),
});

export default function LineChart({ charts, title, lines, colors }: ILineChart) {
export default function LineChart<ChartType extends Pick<ICharts, 'time' | 'markers'> = ICharts>({
charts,
title,
lines,
colors,
}: ILineChart<ChartType>) {
const [chart, setChart] = useState<ECharts | null>(null);

const chartContainer = useRef<HTMLDivElement | null>(null);
Expand All @@ -184,7 +199,12 @@ export default function LineChart({ charts, title, lines, colors }: ILineChart)

const initChart = init(chartContainer.current, 'locust');
initChart.setOption(
createOptions({ charts, title, seriesData: getSeriesData({ charts, lines }), colors }),
createOptions<ChartType>({
charts,
title,
seriesData: getSeriesData<ChartType>({ charts, lines }),
colors,
}),
);
initChart.on('datazoom', datazoom => {
const { batch } = datazoom as ILineChartZoomEvent;
Expand Down Expand Up @@ -264,7 +284,7 @@ export default function LineChart({ charts, title, lines, colors }: ILineChart)
xAxis: { data: charts.time },
series: lines.map(({ key }, index) => ({
data: charts[key],
...(index === 0 ? { markLine: createMarkLine(charts) } : {}),
...(index === 0 ? { markLine: createMarkLine<ChartType>(charts) } : {}),
})),
});
}
Expand Down

0 comments on commit 56df7c4

Please sign in to comment.