Skip to content

Commit

Permalink
add line series for totals
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Nov 20, 2024
1 parent 3c8f077 commit 637a025
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions x-pack/plugins/data_usage/public/app/components/chart_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
niceTimeFormatter,
DARK_THEME,
LIGHT_THEME,
LineSeries,
} from '@elastic/charts';
import { i18n } from '@kbn/i18n';
import { LegendAction } from './legend_action';
Expand Down Expand Up @@ -59,6 +60,18 @@ export const ChartPanel: React.FC<ChartPanelProps> = ({
[minTimestamp, maxTimestamp]
);

// Calculate the total for each time bucket
const totalSeries = useMemo(() => {
const totalsMap = new Map<number, number>();

series.forEach((stream) => {
stream.data.forEach((point) => {
totalsMap.set(point.x, (totalsMap.get(point.x) || 0) + point.y);
});
});

return Array.from(totalsMap.entries()).map(([x, y]) => ({ x, y }));
}, [series]);
const renderLegendAction = useCallback(
({ label }: { label: string }) => {
return (
Expand Down Expand Up @@ -87,6 +100,19 @@ export const ChartPanel: React.FC<ChartPanelProps> = ({
xDomain={{ min: minTimestamp, max: maxTimestamp }}
legendAction={renderLegendAction}
/>
<LineSeries
id="Total"
name="Total"
data={totalSeries}
xScaleType={ScaleType.Time}
yScaleType={ScaleType.Linear}
xAccessor="x"
yAccessors={['y']}
lineSeriesStyle={{
line: { strokeWidth: 2 },
point: { visible: 'always', radius: 5 },
}}
/>
{series.map((stream, streamIdx) => (
<BarSeries
key={streamIdx}
Expand Down

0 comments on commit 637a025

Please sign in to comment.