Skip to content

Commit 310ca57

Browse files
committed
Update XAxisOptions interface to include a formatter function
1 parent 3d13620 commit 310ca57

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

Common/Types/Date.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,8 @@ export default class OneUptimeDate {
11381138
);
11391139
}
11401140

1141+
1142+
11411143
public static getDateWithCustomTime(data: {
11421144
hours: number;
11431145
minutes: number;

Common/UI/Components/Charts/Line/LineChart.tsx

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { LineChart } from "../ChartLibrary/LineChart/LineChart";
2-
import React, { FunctionComponent, ReactElement } from "react";
2+
import React, { FunctionComponent, ReactElement, useEffect } from "react";
33
import SeriesPoint from "../Types/SeriesPoints";
44
import { XAxis } from "../Types/XAxis/XAxis";
55
import YAxis from "../Types/YAxis/YAxis";
66
import ChartCurve from "../Types/ChartCurve";
7+
import XAxisPrecision from "../Types/XAxis/XAxisPrecision";
78

89
const chartdata = [
910
{
@@ -81,16 +82,47 @@ export interface ComponentProps {
8182
}
8283

8384
const LineChartElement: FunctionComponent<ComponentProps> = (
84-
_props: ComponentProps,
85+
props: ComponentProps,
8586
): ReactElement => {
87+
88+
const [records, setRecords] = React.useState<Record<string, any>[]>([]);
89+
90+
const categories: Array<string> = props.data.map((item: SeriesPoint) => {
91+
return item.seriesName;
92+
});
93+
94+
useEffect(() => {
95+
96+
if (!props.data || props.data.length === 0) {
97+
setRecords([]);
98+
}
99+
100+
const maxXValue: number | Date = props.xAxis.options.max;
101+
const minXValue: number | Date = props.xAxis.options.min;
102+
const precision: XAxisPrecision = props.xAxis.options.precision;
103+
104+
for (const seriesData of props.data) {
105+
const yAxisLegend: string = seriesData.seriesName;
106+
for (const data of seriesData.data) {
107+
108+
const xAxisValue: string = props.xAxis.options.formatter(data.x);
109+
const yValue: number = data.y;
110+
}
111+
}
112+
113+
}, [props.data]);
114+
115+
116+
86117
return (
87118
<LineChart
88119
className="h-80"
89-
data={chartdata}
90-
index="date"
91-
categories={["SolarPanels", "Inverters"]}
92-
colors={["indigo", "rose"]}
120+
data={records}
121+
index={props.xAxis.legend}
122+
categories={categories}
123+
colors={["indigo", "rose", "amber"]}
93124
valueFormatter={dataFormatter}
125+
94126
showTooltip={true}
95127
yAxisWidth={60}
96128
onValueChange={(v) => {

Common/UI/Components/Charts/Types/XAxis/XAxis.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import XValue from "../XValue";
12
import XAxisMaxMin from "./XAxisMaxMin";
23
import XAxisPrecision from "./XAxisPrecision";
34
import XAxisType from "./XAxisType";
@@ -7,6 +8,7 @@ export interface XAxisOptions {
78
min: XAxisMaxMin;
89
max: XAxisMaxMin;
910
precision: XAxisPrecision;
11+
formatter: (value: XValue) => string;
1012
}
1113

1214
export interface XAxis {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
type XAxisMaxMin = "auto" | number | Date;
1+
type XAxisMaxMin = number | Date;
22

33
export default XAxisMaxMin;

Dashboard/src/Components/Monitor/MonitorCharts/MonitorChart.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,18 @@ export class MonitorCharts {
258258
public static getXAxisFor(data: {
259259
monitorMetricsByMinute: Array<MonitorMetricsByMinute>;
260260
}): XAxis {
261-
const startTime: Date | undefined =
262-
data.monitorMetricsByMinute[0]?.createdAt || undefined;
263-
const endTime: Date | undefined =
261+
const startTime: Date =
262+
data.monitorMetricsByMinute[0]?.createdAt! || undefined;
263+
const endTime: Date =
264264
data.monitorMetricsByMinute[data.monitorMetricsByMinute.length - 1]
265-
?.createdAt || undefined;
265+
?.createdAt! || undefined;
266266

267267
return {
268268
legend: "Time",
269269
options: {
270270
type: XAxisType.Time,
271-
min: startTime || "auto",
272-
max: endTime || "auto",
271+
min: startTime,
272+
max: endTime,
273273
precision: XAxisPrecision.MINUTE,
274274
},
275275
};

0 commit comments

Comments
 (0)