Skip to content

Commit

Permalink
refactor: Add syncId prop to LineChart component for syncing multiple…
Browse files Browse the repository at this point in the history
… charts
  • Loading branch information
simlarsen committed Aug 21, 2024
1 parent 9db1c59 commit b202b07
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Common/UI/Components/Charts/ChartGroup/ChartGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Text from "../../../../Types/Text";
import LineChart, { ComponentProps as LineChartProps } from "../Line/LineChart";
import React, { FunctionComponent, ReactElement } from "react";

Expand All @@ -22,6 +23,8 @@ export interface ComponentProps {
const ChartGroup: FunctionComponent<ComponentProps> = (
props: ComponentProps,
): ReactElement => {
const syncId: string = Text.generateRandomText(10);

return (
<div className="lg:grid grid-cols-1 gap-5">
{props.charts.map((chart: Chart, index: number) => {
Expand All @@ -44,7 +47,7 @@ const ChartGroup: FunctionComponent<ComponentProps> = (
{chart.description}
</p>
)}
<LineChart key={index} {...chart.props} />
<LineChart key={index} {...chart.props} syncId={syncId} />
</div>
);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ interface LineChartProps extends React.HTMLAttributes<HTMLDivElement> {
legendPosition?: "left" | "center" | "right";
tooltipCallback?: (tooltipCallbackContent: TooltipProps) => void;
customTooltip?: React.ComponentType<TooltipProps>;
syncId?: string | undefined;
}

const LineChart: React.ForwardRefExoticComponent<
Expand Down Expand Up @@ -691,6 +692,7 @@ const LineChart: React.ForwardRefExoticComponent<
<ResponsiveContainer>
<RechartsLineChart
data={data}
syncId={props.syncId}
onClick={
hasOnValueChange && (activeLegend || activeDot)
? () => {
Expand Down
13 changes: 7 additions & 6 deletions Common/UI/Components/Charts/Line/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export interface ComponentProps {
sync: boolean;
}

const LineChartElement: FunctionComponent<ComponentProps> = (
props: ComponentProps,
): ReactElement => {
if (!props.sync) {
return <></>;
}
export interface LineInternalProps extends ComponentProps {
syncId: string;
}

const LineChartElement: FunctionComponent<LineInternalProps> = (
props: LineInternalProps,
): ReactElement => {
const [records, setRecords] = React.useState<Array<ChartDataPoint>>([]);

const categories: Array<string> = props.data.map((item: SeriesPoint) => {
Expand Down Expand Up @@ -64,6 +64,7 @@ const LineChartElement: FunctionComponent<ComponentProps> = (
showTooltip={true}
connectNulls={true}
curve={props.curve}
syncId={props.sync ? props.syncId : undefined}
yAxisWidth={60}
/>
);
Expand Down
13 changes: 8 additions & 5 deletions Dashboard/src/Components/Monitor/MonitorCharts/MonitorChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ export class MonitorCharts {
}): XAxis {
let startTime: Date | undefined =
data.monitorMetricsByMinute[0]?.createdAt || undefined;
let endTime : Date | undefined =
data.monitorMetricsByMinute[data.monitorMetricsByMinute.length - 1]?.createdAt || undefined;
let endTime: Date | undefined =
data.monitorMetricsByMinute[data.monitorMetricsByMinute.length - 1]
?.createdAt || undefined;

let xAxisAggregationType: XAxisAggregateType = XAxisAggregateType.Average;

Expand All @@ -292,9 +293,11 @@ export class MonitorCharts {
xAxisAggregationType = XAxisAggregateType.Average;
}


if(!startTime || !endTime) {
startTime = OneUptimeDate.addRemoveHours(OneUptimeDate.getCurrentDate(), -1);
if (!startTime || !endTime) {
startTime = OneUptimeDate.addRemoveHours(
OneUptimeDate.getCurrentDate(),
-1,
);
endTime = OneUptimeDate.getCurrentDate();
}

Expand Down

0 comments on commit b202b07

Please sign in to comment.