Skip to content

Commit 654b948

Browse files
committed
feat(graph): add x-axis option
1 parent 6f5d7fe commit 654b948

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/components/GraphPanel/GraphPanel.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import Chart from 'components/Chart/Chart.react';
33
import { ChartColorSchemes } from 'lib/Constants';
44
import styles from './GraphPanel.scss';
@@ -28,25 +28,48 @@ export default function GraphPanel({ selectedCells, order, data, columns, width
2828
const { rowStart, rowEnd, colStart, colEnd } = selectedCells;
2929
const columnNames = order.slice(colStart, colEnd + 1).map(o => o.name);
3030
const columnTypes = columnNames.map(name => columns[name]?.type);
31-
const timeSeries =
32-
columnTypes.length > 1 &&
33-
columnTypes[0] === 'Date' &&
34-
columnTypes.slice(1).every(t => t === 'Number');
31+
32+
const initialUseXAxis =
33+
columnNames.length > 1 &&
34+
(columnTypes[0] === 'Date' || columnTypes[0] === 'Number') &&
35+
columnTypes.slice(1).some(t => t === 'Number');
36+
37+
const [useXAxis, setUseXAxis] = useState(initialUseXAxis);
38+
39+
useEffect(() => {
40+
setUseXAxis(initialUseXAxis);
41+
}, [selectedCells?.rowStart, selectedCells?.rowEnd, selectedCells?.colStart, selectedCells?.colEnd]);
3542

3643
const chartData = {};
37-
if (timeSeries) {
44+
45+
if (useXAxis) {
46+
let seriesIndex = 0;
3847
for (let j = 1; j < columnNames.length; j++) {
39-
chartData[columnNames[j]] = { color: ChartColorSchemes[j - 1], points: [] };
48+
if (columnTypes[j] === 'Number') {
49+
chartData[columnNames[j]] = {
50+
color: ChartColorSchemes[seriesIndex],
51+
points: [],
52+
};
53+
seriesIndex++;
54+
}
4055
}
4156
for (let i = rowStart; i <= rowEnd; i++) {
4257
const row = data[i];
4358
if (!row) continue;
44-
const ts = parseDate(row.attributes[columnNames[0]]);
45-
if (ts === null) continue;
59+
let x = row.attributes[columnNames[0]];
60+
if (columnTypes[0] === 'Date') {
61+
x = parseDate(x);
62+
} else if (typeof x === 'string') {
63+
x = parseFloat(x);
64+
}
65+
if (typeof x !== 'number' || isNaN(x)) {
66+
continue;
67+
}
4668
for (let j = 1; j < columnNames.length; j++) {
69+
if (columnTypes[j] !== 'Number') continue;
4770
const val = row.attributes[columnNames[j]];
4871
if (typeof val === 'number' && !isNaN(val)) {
49-
chartData[columnNames[j]].points.push([ts, val]);
72+
chartData[columnNames[j]].points.push([x, val]);
5073
}
5174
}
5275
}
@@ -62,7 +85,8 @@ export default function GraphPanel({ selectedCells, order, data, columns, width
6285
for (let i = rowStart; i <= rowEnd; i++, x++) {
6386
const row = data[i];
6487
if (!row) continue;
65-
columnNames.forEach(col => {
88+
columnNames.forEach((col, idx) => {
89+
if (columnTypes[idx] !== 'Number') return;
6690
const val = row.attributes[col];
6791
if (typeof val === 'number' && !isNaN(val)) {
6892
chartData[col].points.push([x, val]);
@@ -78,6 +102,16 @@ export default function GraphPanel({ selectedCells, order, data, columns, width
78102
const chartWidth = width - 20;
79103
return (
80104
<div className={styles.graphPanel}>
105+
<div className={styles.options}>
106+
<label>
107+
<input
108+
type="checkbox"
109+
checked={useXAxis}
110+
onChange={() => setUseXAxis(!useXAxis)}
111+
/>{' '}
112+
Use first column as X-axis
113+
</label>
114+
</div>
81115
<Chart width={chartWidth} height={400} data={chartData} />
82116
</div>
83117
);

src/components/GraphPanel/GraphPanel.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
padding: 10px;
66
}
77

8+
.options {
9+
margin-bottom: 10px;
10+
}
11+
812
.empty {
913
padding: 10px;
1014
color: #555;

0 commit comments

Comments
 (0)