From ab05c3b92429854650047c7443007c1eb4b4c617 Mon Sep 17 00:00:00 2001 From: Riley Windsor Date: Thu, 19 Dec 2024 19:08:12 -0500 Subject: [PATCH] Fix charts --- .../components/flows/ViewExperiment/Chart.tsx | 72 ++++++++++--------- .../ViewExperiment/ExperimentListing.tsx | 4 +- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/apps/frontend/app/components/flows/ViewExperiment/Chart.tsx b/apps/frontend/app/components/flows/ViewExperiment/Chart.tsx index a42acea1..e07419dd 100644 --- a/apps/frontend/app/components/flows/ViewExperiment/Chart.tsx +++ b/apps/frontend/app/components/flows/ViewExperiment/Chart.tsx @@ -60,42 +60,50 @@ const ChartModal: React.FC = ({ onClose, project }) => { }; const parseCSV = (data) => { + console.log(data); const rows = data.trim().split('\n'); - const headers = rows[0].split(','); + const headers = rows[0].split(',') as string[]; + //Create a dictionary to store the data + const dataDict = {} as any; - const xList = [] as any[]; - const yLists = [] as any[]; - - var xIndex = 0; + // Iterate through the rows and put them under the correct header for (let i = 0; i < headers.length; i++) { - yLists.push([]); - if (headers[i] === xAxis) { - xIndex = i; - } + dataDict[headers[i]] = []; } - yLists.splice(xIndex, 1); - + // Iterate through the rows and put them under the correct header for (let i = 1; i < rows.length; i++) { - const cols = rows[i].split(','); - xList.push(cols[xIndex]); - for (let j = 0; j < cols.length; j++) { - if (j < xIndex) { - yLists[j].push(cols[j]); - } - else if (j > xIndex) { - yLists[j - 1].push(cols[j]); + // Split the row by commas when not inside quotes + const row = rows[i].split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/); + for (let j = 0; j < row.length; j++) { + const val = row[j].replace(/"/g, ''); + // If the value is a number, convert it to a number + if (!isNaN(val)) { + row[j] = parseFloat(val); + dataDict[headers[j]].push(row[j]); } } } + //Remove items with empty arrays + for (let i = 0; i < headers.length; i++) { + if (dataDict[headers[i]].length == 0) { + delete dataDict[headers[i]]; + } + } + + console.log(dataDict); + const xIndex = headers.indexOf(xAxis); + const xList = headers.includes(xAxis) ? dataDict[xAxis] : dataDict[headers[0]]; + const yLists = headers.map((header) => dataDict[header]); return { headers, xList, yLists, xIndex }; }; + const generateColors = (numColors) => { const colors = [] as string[]; for (let i = 0; i < numColors; i++) { - const color = `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 0.2)`; + const color = `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 0.4)`; colors.push(color); } return colors; @@ -201,21 +209,19 @@ const ChartModal: React.FC = ({ onClose, project }) => {

X-Axis Column:

-
+ setXAxis(header)} - name="xaxis" - value={header} - - /> - -
+ ))} - + + } - {/* {project.finished ? + {project.finished ? : null - } */} + } { project.creator == session?.user?.id! ?