Skip to content

Commit

Permalink
feat(comparison): add comparison box plot
Browse files Browse the repository at this point in the history
  • Loading branch information
canewton committed Dec 20, 2024
1 parent c376c4f commit 48ac24a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 20 deletions.
56 changes: 49 additions & 7 deletions src/app/model/compare/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
'use client';

import { Tabs } from 'antd';
import { useSearchParams } from 'next/navigation';
import { useMemo, useState } from 'react';
import { Helmet, HelmetProvider } from 'react-helmet-async';

import AppLayout from '@/components/custom/AppLayout/AppLayout';
import { InfoContainer } from '@/components/custom/InfoContainer/InfoContainer';
import { StandardText } from '@/components/custom/StandardText/StandardText';
import { VBox } from '@/components/custom/VBox/VBox';
import ComparisonBoxPlot from '@/components/model/ComparisonBoxPlot';
import { useModelDetails } from '@/hooks/useModelDetails';
import type { PercentileResultMap } from '@/hooks/useModelResults';
import { useAllModelResults } from '@/hooks/useModelResults';
import type { ComparisonChartModel } from '@/types/charts/ComparisonChart';

import CompareModelsTable from '../../../components/model/CompareModelsTable';
import type { ComparisonChartModel } from '../../../components/model/ComparisonChart';
import ComparisonChart from '../../../components/model/ComparisonChart';
import { CropLoadingDetailsTable } from '../../../components/model/CropLoadingDetailsTable';

Expand All @@ -25,6 +28,9 @@ const CompareModelPage = () => {
allModelDetailResults,
);

const [selectedComparisonTab, setSelectedComparisonTab] =
useState<string>('1');

const getComparisonChartModels = (
plotData: PercentileResultMap[],
plotLabels: string[],
Expand All @@ -39,6 +45,31 @@ const CompareModelPage = () => {
return chartInfo;
};

const comparisonChart = useMemo(
() => (
<ComparisonChart
comparisonChartModels={getComparisonChartModels(
allModelResults,
allModelNames,
)}
percentiles={customPercentilesData}
/>
),
[allModelResults, allModelNames, customPercentilesData],
);

const comparisonBoxPlot = useMemo(
() => (
<ComparisonBoxPlot
comparisonChartModels={getComparisonChartModels(
allModelResults,
allModelNames,
)}
/>
),
[allModelResults, allModelNames, customPercentilesData],
);

return (
<AppLayout>
<HelmetProvider>
Expand All @@ -55,12 +86,23 @@ const CompareModelPage = () => {
<CropLoadingDetailsTable modelDetails={allModelDetails} />
</InfoContainer>
<InfoContainer title="Comparison Line Chart">
<ComparisonChart
comparisonChartModels={getComparisonChartModels(
allModelResults,
allModelNames,
)}
percentiles={customPercentilesData}
<Tabs
tabPosition="top"
centered
activeKey={selectedComparisonTab}
onChange={setSelectedComparisonTab}
items={[
{
label: 'Line Chart',
key: '1',
children: comparisonChart,
},
{
label: 'Box Plot',
key: '2',
children: comparisonBoxPlot,
},
]}
/>
</InfoContainer>
</VBox>
Expand Down
2 changes: 1 addition & 1 deletion src/app/model/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import {
useGetModelandBaseModelDetailQuery,
} from '@/store';
import { createNewModel } from '@/store/slices/modelSlice';
import type { ComparisonChartModel } from '@/types/charts/ComparisonChart';
import type { ModelRun } from '@/types/model/ModelRun';
import type { Region } from '@/types/region/Region';

import type { ComparisonChartModel } from '../../components/model/ComparisonChart';
import ComparisonChart from '../../components/model/ComparisonChart';
import { CropLoadingDetailsBaseComparisonTable } from '../../components/model/CropLoadingDetailsTable';
import ModelChart from '../../components/model/ModelChart';
Expand Down
19 changes: 19 additions & 0 deletions src/components/charts/BoxPlot/BoxPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ const BoxPlot = ({
},
},
},
colors: [
'#54478C',
'#2C699A',
'#048BA8',
'#0DB39E',
'#16DB93',
'#83E377',
'#B9E769',
'#EFEA5A',
'#F1C453',
'#F29E4C',
'#FF4D6D',
'#E63946',
],
plotOptions: {
boxPlot: {
colors: undefined,
},
},
};

return (
Expand Down
81 changes: 81 additions & 0 deletions src/components/model/ComparisonBoxPlot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useEffect, useState } from 'react';

import type { ModelDisplay } from '@/hooks/useModelResults';
import type { ComparisonChartModel } from '@/types/charts/ComparisonChart';

import BoxPlot from '../charts/BoxPlot/BoxPlot';

interface ComparisonBoxPlotProps {
comparisonChartModels: ComparisonChartModel[];
reductionStartYear?: number;
reductionCompleteYear?: number;
}

const ComparisonBoxPlot = ({
comparisonChartModels,
reductionStartYear,
reductionCompleteYear,
}: ComparisonBoxPlotProps) => {
function configureDisplayData(
data: ComparisonChartModel[],
): ApexAxisChartSeries {
const res: ApexAxisChartSeries = [];

data.forEach((model) => {
const boxPlotData: {
type: 'boxPlot';
name: string;
data: any[];
} = {
type: 'boxPlot',
name: model.name,
data: [],
};
if ((model.plotData as any)[5] as ModelDisplay[]) {
const dataLen = ((model.plotData as any)[5] as ModelDisplay[])!.length;

const fifthPercentile = ((model.plotData as any)[5] as ModelDisplay[])!;
const lowerQuartile = ((model.plotData as any)[25] as ModelDisplay[])!;
const median = ((model.plotData as any)[50] as ModelDisplay[])!;
const upperQuartile = ((model.plotData as any)[75] as ModelDisplay[])!;
const ninetyFifthPercentile = ((
model.plotData as any
)[95] as ModelDisplay[])!;

for (let i = 0; i < dataLen; i += 10) {
const dataPoint: any = {
x: fifthPercentile[i]!.year,
y: [
fifthPercentile[i]!.value,
lowerQuartile[i]!.value,
median[i]!.value,
upperQuartile[i]!.value,
ninetyFifthPercentile[i]!.value,
],
};
boxPlotData.data.push(dataPoint);
}
}
res.push(boxPlotData);
});
return res;
}

const [displayData, setDisplayData] = useState<ApexAxisChartSeries>(
configureDisplayData(comparisonChartModels),
);

useEffect(() => {
setDisplayData(configureDisplayData(comparisonChartModels));
}, [comparisonChartModels]);

return (
<BoxPlot
data={displayData}
reductionEndYear={reductionCompleteYear}
reductionStartYear={reductionStartYear}
/>
);
};

export default ComparisonBoxPlot;
11 changes: 2 additions & 9 deletions src/components/model/ComparisonChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ import { Select } from 'antd';
import React, { useEffect, useState } from 'react';

import LineChart from '@/components/charts/LineChart/LineChart';
import type {
ModelDisplay,
PercentileResultMap,
} from '@/hooks/useModelResults';

export interface ComparisonChartModel {
plotData: PercentileResultMap;
name: string;
}
import type { ModelDisplay } from '@/hooks/useModelResults';
import type { ComparisonChartModel } from '@/types/charts/ComparisonChart';

interface ComparisonChartProps {
comparisonChartModels: ComparisonChartModel[];
Expand Down
3 changes: 0 additions & 3 deletions src/components/model/ModelBoxPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const ModelBoxPlot = ({
function configureDisplayData(
data: PercentileResultMap,
): ApexAxisChartSeries {
console.log('plotData', data);
const res: ApexAxisChartSeries = [
{
type: 'boxPlot',
Expand All @@ -42,7 +41,6 @@ const ModelBoxPlot = ({
const upperQuartile = ((data as any)[75] as ModelDisplay[])!;
const ninetyFifthPercentile = ((data as any)[95] as ModelDisplay[])!;

console.log('5th', fifthPercentile);
for (let i = 0; i < dataLen; i += 10) {
const dataPoint: any = {
x: fifthPercentile[i]!.year,
Expand All @@ -57,7 +55,6 @@ const ModelBoxPlot = ({
res[0]?.data.push(dataPoint);
}
}
console.log(res);
return res;
}

Expand Down
6 changes: 6 additions & 0 deletions src/types/charts/ComparisonChart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { PercentileResultMap } from '@/hooks/useModelResults';

export interface ComparisonChartModel {
plotData: PercentileResultMap;
name: string;
}

0 comments on commit 48ac24a

Please sign in to comment.