feat: implement new Data interface + review PlotlyBarChart API and docs + hide BucketViewer and OpenLayers

This commit is contained in:
Demenech
2024-04-09 15:21:08 -03:00
parent 3aac4dabf9
commit 88f6199d18
6 changed files with 109 additions and 67 deletions

View File

@@ -1,7 +1,8 @@
import { QueryClient, QueryClientProvider, useQuery } from "react-query";
import { Plotly } from "./Plotly";
import Papa, { ParseConfig } from "papaparse";
import LoadingSpinner from "./LoadingSpinner";
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
import { Plotly } from './Plotly';
import Papa, { ParseConfig } from 'papaparse';
import LoadingSpinner from './LoadingSpinner';
import { Data } from '../types/properties';
const queryClient = new QueryClient();
@@ -17,7 +18,7 @@ async function getCsv(url: string, bytes: number) {
async function parseCsv(
file: string,
parsingConfig: ParseConfig,
parsingConfig: ParseConfig
): Promise<any> {
return new Promise((resolve, reject) => {
Papa.parse(file, {
@@ -39,43 +40,40 @@ async function parseCsv(
}
export interface PlotlyBarChartProps {
url?: string;
data?: { [key: string]: number | string }[];
rawCsv?: string;
randomId?: number;
data: Data;
uniqueId?: number;
bytes?: number;
parsingConfig?: ParseConfig;
xAxis: string;
yAxis: string;
lineLabel?: string;
// TODO: commented out because this doesn't work. I believe
// this would only make any difference on charts with multiple
// traces.
// lineLabel?: string;
title?: string;
}
export const PlotlyBarChart: React.FC<PlotlyBarChartProps> = ({
url,
data,
rawCsv,
bytes = 5132288,
parsingConfig = {},
xAxis,
yAxis,
lineLabel,
title = "",
// lineLabel,
title = '',
}) => {
const randomId = Math.random();
const uniqueId = Math.random();
return (
// Provide the client to your App
<QueryClientProvider client={queryClient}>
<PlotlyBarChartInner
url={url}
data={data}
rawCsv={rawCsv}
randomId={randomId}
uniqueId={uniqueId}
bytes={bytes}
parsingConfig={parsingConfig}
xAxis={xAxis}
yAxis={yAxis}
lineLabel={lineLabel ?? yAxis}
// lineLabel={lineLabel ?? yAxis}
title={title}
/>
</QueryClientProvider>
@@ -83,30 +81,28 @@ export const PlotlyBarChart: React.FC<PlotlyBarChartProps> = ({
};
const PlotlyBarChartInner: React.FC<PlotlyBarChartProps> = ({
url,
data,
rawCsv,
randomId,
uniqueId,
bytes,
parsingConfig,
xAxis,
yAxis,
lineLabel,
// lineLabel,
title,
}) => {
if (data) {
if (data.values) {
return (
<div className="w-full" style={{ height: "500px" }}>
<div className="w-full" style={{ height: '500px' }}>
<Plotly
layout={{
title,
}}
data={[
{
x: data.map((d) => d[xAxis]),
y: data.map((d) => d[yAxis]),
type: "bar",
name: lineLabel,
x: data.values.map((d) => d[xAxis]),
y: data.values.map((d) => d[yAxis]),
type: 'bar',
// name: lineLabel,
},
]}
/>
@@ -114,18 +110,18 @@ const PlotlyBarChartInner: React.FC<PlotlyBarChartProps> = ({
);
}
const { data: csvString, isLoading: isDownloadingCSV } = useQuery(
["dataCsv", url, randomId],
() => getCsv(url as string, bytes ?? 5132288),
{ enabled: !!url },
['dataCsv', data.url, uniqueId],
() => getCsv(data.url as string, bytes ?? 5132288),
{ enabled: !!data.url }
);
const { data: parsedData, isLoading: isParsing } = useQuery(
["dataPreview", csvString, randomId],
['dataPreview', csvString, uniqueId],
() =>
parseCsv(
rawCsv ? (rawCsv as string) : (csvString as string),
parsingConfig ?? {},
data.csv ? (data.csv as string) : (csvString as string),
parsingConfig ?? {}
),
{ enabled: rawCsv ? true : !!csvString },
{ enabled: data.csv ? true : !!csvString }
);
if (isParsing || isDownloadingCSV)
<div className="w-full flex justify-center items-center h-[500px]">
@@ -133,7 +129,7 @@ const PlotlyBarChartInner: React.FC<PlotlyBarChartProps> = ({
</div>;
if (parsedData)
return (
<div className="w-full" style={{ height: "500px" }}>
<div className="w-full" style={{ height: '500px' }}>
<Plotly
layout={{
title,
@@ -142,8 +138,8 @@ const PlotlyBarChartInner: React.FC<PlotlyBarChartProps> = ({
{
x: parsedData.data.map((d: any) => d[xAxis]),
y: parsedData.data.map((d: any) => d[yAxis]),
type: "bar",
name: lineLabel,
type: 'bar',
// name: lineLabel, TODO: commented out because this doesn't work
},
]}
/>

View File

@@ -8,7 +8,8 @@ export * from './components/OpenLayers/OpenLayers';
export * from './components/Map';
export * from './components/PdfViewer';
export * from "./components/Excel";
export * from "./components/BucketViewer";
// NOTE: hidden for now
// export * from "./components/BucketViewer";
export * from "./components/Iframe";
export * from "./components/Plotly";
export * from "./components/PlotlyLineChart";

View File

@@ -0,0 +1,11 @@
/*
* All components should use this interface for
* its data property.
* Based on vega.
*
*/
export interface Data {
url?: string;
values?: { [key: string]: number | string }[];
csv?: string;
}