Skip to content

Commit c6a4e95

Browse files
authored
Closes #243 line graph with datetime crashing (#245)
* BugFix * Docs on Point Mode
1 parent dbe1f18 commit c6a4e95

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

docs/modules/ROOT/pages/user-guide/reports/line-chart.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Colors are assigned automatically to the different fields selected in
5353
the report footer.
5454

5555
|X Scale |List |linear |How to scale the values on the x-axis. Can be
56-
either linear or logarithmic.
56+
either linear, logarithmic or point. Use point for categorical data.
5757

5858
|Y Scale |List |linear |How to scale the values on the y-axis. Can be
5959
either linear or logarithmic.

src/chart/line/LineChart.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const NeoLineChart = (props: ChartProps) => {
3030
}
3131

3232
const [isTimeChart, setIsTimeChart] = React.useState(false);
33+
const [parseFormat, setParseFormat] = React.useState("%Y-%m-%dT%H:%M:%SZ");
34+
3335
const settings = (props.settings) ? props.settings : {};
3436

3537
const colorScheme = (settings["colors"]) ? settings["colors"] : 'set2';
@@ -88,23 +90,31 @@ const NeoLineChart = (props: ChartProps) => {
8890
data: []
8991
}))
9092

93+
const isDate = (x) => {
94+
return x.__isDate__;
95+
}
96+
9197
const isDateTime = (x) => {
92-
return x !== undefined && x.day !== undefined && x.hour !== undefined && x.minute !== undefined &&
93-
x.month !== undefined && x.nanosecond !== undefined && x.second !== undefined && x.year !== undefined;
98+
return x.__isDateTime__;
99+
}
100+
101+
const isDateTimeOrDate = (x) => {
102+
return isDate(x) || isDateTime(x) || x instanceof Date;
94103
}
95104

96105
records.forEach((row) => {
97106
selection['value'].forEach(key => {
98107
const index = data.findIndex(item => (item as Record<string, any>).id === key)
99-
const x: any = row.get(selection['x']) || 0
108+
let x: any = row.get(selection['x']) || 0
100109
const y: any = recordToNative(row.get(key)) || 0
101110
if (data[index] && !isNaN(y)) {
102-
if (isDateTime(x)) {
111+
if (isDate(x)) {
103112
data[index].data.push({ x, y })
104-
}
105-
if (!isNaN(x)) {
113+
} else if (isDateTime(x)) {
114+
x = new Date(x.toString());
106115
data[index].data.push({ x, y })
107116
}
117+
else data[index].data.push({ x, y })
108118
}
109119
})
110120
})
@@ -124,13 +134,20 @@ const NeoLineChart = (props: ChartProps) => {
124134

125135
// TODO - Nivo has a bug that, when we switch from a time-axis to a number axis, the visualization breaks.
126136
// Therefore, we now require a manual refresh.
127-
const chartIsTimeChart = (data[0] !== undefined && data[0].data[0] !== undefined && data[0].data[0]['x'] !== undefined && isNaN(data[0].data[0]['x']));
137+
138+
139+
const chartIsTimeChart = (data[0] !== undefined && data[0].data[0] !== undefined && data[0].data[0]['x'] !== undefined && isDateTimeOrDate(data[0].data[0]['x']));
140+
128141
if (isTimeChart !== chartIsTimeChart) {
129142
if (!chartIsTimeChart) {
130143
return <div style={{ margin: "15px" }}>
131144
Line chart switched from time-axis to number-axis.
132145
Please re-run the report to see your changes. </div>;
133146
}
147+
148+
let p = chartIsTimeChart ? (isDateTime(data[0].data[0]['x']) ? "%Y-%m-%dT%H:%M:%SZ": "%Y-%m-%d" ) : "";
149+
150+
setParseFormat(p);
134151
setIsTimeChart(chartIsTimeChart);
135152
}
136153

@@ -151,12 +168,13 @@ const NeoLineChart = (props: ChartProps) => {
151168
return <code style={{ margin: "10px" }}>Invalid tick size specification for time chart. Parameter value must be set to "every [number] ['years', 'months', 'weeks', 'days', 'hours', 'seconds', 'milliseconds']".</code>;
152169
}
153170

154-
171+
//T18:40:32.142+0100
172+
//%Y-%m-%dT%H:%M:%SZ
155173
const lineViz = <div className="h-full w-full overflow-hidden" style={{ height: "100%" }}>
156174
<ResponsiveLine
157175
data={data}
158176
xScale={isTimeChart ?
159-
{ format: "%Y-%m-%dT%H:%M:%SZ", type: "time" } :
177+
{ format: parseFormat, type: "time" } :
160178
xScale == 'linear' ?
161179
{ type: xScale, min: minXValue, max: maxXValue, stacked: false, reverse: false } :
162180
{ type: xScale, min: minXValue, max: maxXValue, constant: xScaleLogBase, base: xScaleLogBase }

src/config/ReportConfig.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export const REPORT_TYPES = {
534534
"xScale": {
535535
label: "X Scale",
536536
type: SELECTION_TYPES.LIST,
537-
values: ["linear", "log"],
537+
values: ["linear", "log", "point"],
538538
default: "linear"
539539
},
540540
"yScale": {

0 commit comments

Comments
 (0)