-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add graph visualization for selected numbers #2884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: alpha
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,128 @@ | ||||||||||||
import React, { useState, useEffect } from 'react'; | ||||||||||||
import Chart from 'components/Chart/Chart.react'; | ||||||||||||
import { ChartColorSchemes } from 'lib/Constants'; | ||||||||||||
import styles from './GraphPanel.scss'; | ||||||||||||
|
||||||||||||
function parseDate(val) { | ||||||||||||
if (!val) { | ||||||||||||
return null; | ||||||||||||
} | ||||||||||||
if (val instanceof Date) { | ||||||||||||
return val.getTime(); | ||||||||||||
} | ||||||||||||
if (typeof val === 'string') { | ||||||||||||
const d = new Date(val); | ||||||||||||
return isNaN(d) ? null : d.getTime(); | ||||||||||||
} | ||||||||||||
if (val.iso) { | ||||||||||||
const d = new Date(val.iso); | ||||||||||||
return isNaN(d) ? null : d.getTime(); | ||||||||||||
} | ||||||||||||
return null; | ||||||||||||
} | ||||||||||||
|
||||||||||||
export default function GraphPanel({ selectedCells, order, data, columns, width }) { | ||||||||||||
if (!selectedCells || selectedCells.rowStart < 0) { | ||||||||||||
return null; | ||||||||||||
} | ||||||||||||
const { rowStart, rowEnd, colStart, colEnd } = selectedCells; | ||||||||||||
const columnNames = order.slice(colStart, colEnd + 1).map(o => o.name); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add bounds checking for array slicing. The code slices the Add bounds checking: + if (colStart < 0 || colEnd >= order.length) {
+ return <div className={styles.empty}>Invalid column selection.</div>;
+ }
const columnNames = order.slice(colStart, colEnd + 1).map(o => o.name); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||
const columnTypes = columnNames.map(name => columns[name]?.type); | ||||||||||||
|
||||||||||||
const isSingleColumn = columnNames.length === 1; | ||||||||||||
|
||||||||||||
const initialUseXAxis = | ||||||||||||
!isSingleColumn && | ||||||||||||
(columnTypes[0] === 'Date' || columnTypes[0] === 'Number') && | ||||||||||||
columnTypes.slice(1).some(t => t === 'Number'); | ||||||||||||
|
||||||||||||
const [useXAxis, setUseXAxis] = useState(initialUseXAxis); | ||||||||||||
|
||||||||||||
useEffect(() => { | ||||||||||||
setUseXAxis(initialUseXAxis); | ||||||||||||
}, [selectedCells?.rowStart, selectedCells?.rowEnd, selectedCells?.colStart, selectedCells?.colEnd]); | ||||||||||||
|
||||||||||||
const chartData = {}; | ||||||||||||
|
||||||||||||
if (useXAxis) { | ||||||||||||
let seriesIndex = 0; | ||||||||||||
for (let j = 1; j < columnNames.length; j++) { | ||||||||||||
if (columnTypes[j] === 'Number') { | ||||||||||||
chartData[columnNames[j]] = { | ||||||||||||
color: ChartColorSchemes[seriesIndex], | ||||||||||||
points: [], | ||||||||||||
}; | ||||||||||||
seriesIndex++; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
for (let i = rowStart; i <= rowEnd; i++) { | ||||||||||||
const row = data[i]; | ||||||||||||
if (!row) continue; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix linting issues: Add braces after if conditions. The static analysis tool correctly identifies missing braces after if conditions, which violates the project's style guide. Apply this diff to fix the linting issues: - if (!row) continue;
+ if (!row) {
+ continue;
+ }
const ts = parseDate(row.attributes[columnNames[0]]);
- if (ts === null) continue;
+ if (ts === null) {
+ continue;
+ }
for (let j = 1; j < columnNames.length; j++) {
const val = row.attributes[columnNames[j]];
if (typeof val === 'number' && !isNaN(val)) {
chartData[columnNames[j]].points.push([ts, val]);
}
}
}
} else {
let seriesIndex = 0;
columnNames.forEach((col, idx) => {
if (columnTypes[idx] === 'Number') {
chartData[col] = { color: ChartColorSchemes[seriesIndex], points: [] };
seriesIndex++;
}
});
let x = 0;
for (let i = rowStart; i <= rowEnd; i++, x++) {
const row = data[i];
- if (!row) continue;
+ if (!row) {
+ continue;
+ } Also applies to: 45-45, 64-64 🧰 Tools🪛 GitHub Check: Lint[failure] 43-43: 🤖 Prompt for AI Agents
|
||||||||||||
let x = row.attributes[columnNames[0]]; | ||||||||||||
if (columnTypes[0] === 'Date') { | ||||||||||||
x = parseDate(x); | ||||||||||||
} else if (typeof x === 'string') { | ||||||||||||
x = parseFloat(x); | ||||||||||||
} | ||||||||||||
if (typeof x !== 'number' || isNaN(x)) { | ||||||||||||
continue; | ||||||||||||
} | ||||||||||||
for (let j = 1; j < columnNames.length; j++) { | ||||||||||||
if (columnTypes[j] !== 'Number') continue; | ||||||||||||
const val = row.attributes[columnNames[j]]; | ||||||||||||
if (typeof val === 'number' && !isNaN(val)) { | ||||||||||||
chartData[columnNames[j]].points.push([x, val]); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} else { | ||||||||||||
let seriesIndex = 0; | ||||||||||||
columnNames.forEach((col, idx) => { | ||||||||||||
if (columnTypes[idx] === 'Number') { | ||||||||||||
chartData[col] = { color: ChartColorSchemes[seriesIndex], points: [] }; | ||||||||||||
seriesIndex++; | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
let x = 0; | ||||||||||||
for (let i = rowStart; i <= rowEnd; i++, x++) { | ||||||||||||
const row = data[i]; | ||||||||||||
if (!row) continue; | ||||||||||||
columnNames.forEach((col, idx) => { | ||||||||||||
if (columnTypes[idx] !== 'Number') return; | ||||||||||||
const val = row.attributes[col]; | ||||||||||||
if (typeof val === 'number' && !isNaN(val)) { | ||||||||||||
chartData[col].points.push([x, val]); | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (Object.keys(chartData).length === 0) { | ||||||||||||
return <div className={styles.empty}>No numeric data selected.</div>; | ||||||||||||
} | ||||||||||||
|
||||||||||||
const chartWidth = width - 20; | ||||||||||||
const xAxisType = useXAxis ? 'time' : 'index'; | ||||||||||||
return ( | ||||||||||||
<div className={styles.graphPanel}> | ||||||||||||
<div className={styles.options}> | ||||||||||||
<label> | ||||||||||||
<input | ||||||||||||
type="checkbox" | ||||||||||||
checked={useXAxis} | ||||||||||||
onChange={() => setUseXAxis(!useXAxis)} | ||||||||||||
disabled={isSingleColumn} | ||||||||||||
/>{' '} | ||||||||||||
Use first column as X-axis | ||||||||||||
</label> | ||||||||||||
</div> | ||||||||||||
<Chart | ||||||||||||
width={chartWidth} | ||||||||||||
height={400} | ||||||||||||
data={chartData} | ||||||||||||
xAxisType={xAxisType} | ||||||||||||
hideXAxisLabels={isSingleColumn} | ||||||||||||
/> | ||||||||||||
</div> | ||||||||||||
); | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.graphPanel { | ||
height: 100%; | ||
overflow: auto; | ||
background-color: #fefafb; | ||
padding: 10px; | ||
} | ||
|
||
.options { | ||
margin-bottom: 10px; | ||
} | ||
|
||
.empty { | ||
padding: 10px; | ||
color: #555; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add PropTypes validation.
The component is missing PropTypes validation, which would help catch type-related issues during development.
Add PropTypes validation:
📝 Committable suggestion
🤖 Prompt for AI Agents