Skip to content

Commit

Permalink
Add same axis feature for scatter plot
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafasaifee42 committed Mar 25, 2024
1 parent 704d14e commit b276f89
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Components/AggregatedDataExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function AggregatedDataExplorer(props: Props) {
selectedCountryOrRegion: regionId,
signatureSolution: undefined,
signatureSolutionForDataList: 'All',
keepAxisSame: false,
};

const [state, dispatch] = useReducer(Reducer, initialState);
Expand Down Expand Up @@ -207,6 +208,12 @@ function AggregatedDataExplorer(props: Props) {
payload: verticalBarLayout,
});
};
const updateKeepAxisSame = (d: boolean) => {
dispatch({
type: 'UPDATE_KEEP_AXIS_SAME',
payload: d,
});
};
return (
<div>
<div
Expand Down Expand Up @@ -279,6 +286,7 @@ function AggregatedDataExplorer(props: Props) {
updateReverseOrder,
updateBarLayout,
updateSignatureSolutionForDataList,
updateKeepAxisSame,
}}
>
<div
Expand Down
8 changes: 8 additions & 0 deletions src/Components/CountryVisualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function CountryVisualization(props: Props) {
disaggregationMetaData.length > 0 ? disaggregationMetaData[0] : undefined,
disaggregationGraphType: 'country',
disaggregationOrder: 'first',
keepAxisSame: false,
};

const [state, dispatch] = useReducer(Reducer, initialState);
Expand Down Expand Up @@ -271,6 +272,12 @@ function CountryVisualization(props: Props) {
payload: disaggregationOrder,
});
};
const updateKeepAxisSame = (d: boolean) => {
dispatch({
type: 'UPDATE_KEEP_AXIS_SAME',
payload: d,
});
};
return (
<Context.Provider
// eslint-disable-next-line react/jsx-no-constructed-context-values
Expand Down Expand Up @@ -299,6 +306,7 @@ function CountryVisualization(props: Props) {
updateDisaggregationIndicator,
updateDisaggregationGraphType,
updateDisaggregationOrder,
updateKeepAxisSame,
}}
>
<div className='undp-container'>
Expand Down
2 changes: 2 additions & 0 deletions src/Context/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Context = createContext<CtxDataType>({
signatureSolution: undefined,
signatureSolutionForDataList: 'All',
showReference: false,
keepAxisSame: false,
updateGraphType: (
_d:
| 'scatterPlot'
Expand Down Expand Up @@ -74,6 +75,7 @@ const Context = createContext<CtxDataType>({
| 'Poverty and Inequality'
| 'Resilience',
) => {},
updateKeepAxisSame: (_d: boolean) => {},
});

export default Context;
2 changes: 2 additions & 0 deletions src/Context/Reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export default (state: any, action: any) => {
return { ...state, disaggregationGraphType: action.payload };
case 'UPDATE_DISAGGREGATION_ORDER':
return { ...state, disaggregationOrder: action.payload };
case 'UPDATE_KEEP_AXIS_SAME':
return { ...state, keepAxisSame: action.payload };
default:
return { ...state };
}
Expand Down
39 changes: 33 additions & 6 deletions src/GrapherComponent/ScatterPlot/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Delaunay } from 'd3-delaunay';
import { scaleOrdinal, scaleLinear, scaleThreshold, scaleSqrt } from 'd3-scale';
import minBy from 'lodash.minby';
import UNDPColorModule from 'undp-viz-colors';
import flattenDeep from 'lodash.flattendeep';
import min from 'lodash.min';
import { Tooltip } from '../../Components/Tooltip';
import {
CountryGroupDataType,
Expand Down Expand Up @@ -47,6 +49,7 @@ export function Graph(props: Props) {
selectedRegions,
selectedIncomeGroups,
selectedCountryGroup,
keepAxisSame,
} = useContext(Context) as CtxDataType;
const [selectedColor, setSelectedColor] = useState<string | undefined>(
undefined,
Expand Down Expand Up @@ -103,7 +106,24 @@ export function Graph(props: Props) {
.range([0.25, 30])
.nice()
: undefined;

const xFullArray = flattenDeep(
data.map(d => {
const xIndicatorIndex = d.indicators.findIndex(
el => xIndicatorMetaData.DataKey === el.indicator,
);
const arr = d.indicators[xIndicatorIndex].yearlyData.map(el => el.value);
return arr;
}),
);
const yFullArray = flattenDeep(
data.map(d => {
const yIndicatorIndex = d.indicators.findIndex(
el => yIndicatorMetaData.DataKey === el.indicator,
);
const arr = d.indicators[yIndicatorIndex].yearlyData.map(el => el.value);
return arr;
}),
);
const dataFormatted = orderBy(
data
.map(d => {
Expand Down Expand Up @@ -245,7 +265,6 @@ export function Graph(props: Props) {
'radiusValue',
'desc',
);

const refXIndicatorIndex = regionData
? regionData.indicators.findIndex(
el => xIndicatorMetaData.DataKey === el.indicator,
Expand All @@ -266,14 +285,18 @@ export function Graph(props: Props) {
: regionData.indicators[refXIndicatorIndex].yearlyData[
regionData.indicators[refXIndicatorIndex].yearlyData.length - 1
]?.value;
const xMaxValue = maxBy(dataFormatted, d => d.xVal)
const xMaxValue = keepAxisSame
? (max(xFullArray) as number)
: maxBy(dataFormatted, d => d.xVal)
? refXVal && showReference
? (maxBy(dataFormatted, d => d.xVal)?.xVal as number) > refXVal
? (maxBy(dataFormatted, d => d.xVal)?.xVal as number)
: refXVal
: (maxBy(dataFormatted, d => d.xVal)?.xVal as number)
: 0;
const xMinValue = minBy(dataFormatted, d => d.xVal)
const xMinValue = keepAxisSame
? (min(xFullArray) as number)
: minBy(dataFormatted, d => d.xVal)
? refXVal && showReference
? (minBy(dataFormatted, d => d.xVal)?.xVal as number) < refXVal
? (minBy(dataFormatted, d => d.xVal)?.xVal as number)
Expand All @@ -300,14 +323,18 @@ export function Graph(props: Props) {
: regionData.indicators[refYIndicatorIndex].yearlyData[
regionData.indicators[refYIndicatorIndex].yearlyData.length - 1
]?.value;
const yMaxValue = maxBy(dataFormatted, d => d.yVal)
const yMaxValue = keepAxisSame
? (max(yFullArray) as number)
: maxBy(dataFormatted, d => d.yVal)
? refYVal && showReference
? (maxBy(dataFormatted, d => d.yVal)?.yVal as number) > refYVal
? (maxBy(dataFormatted, d => d.yVal)?.yVal as number)
: refYVal
: (maxBy(dataFormatted, d => d.yVal)?.yVal as number)
: 0;
const yMinValue = minBy(dataFormatted, d => d.yVal)
const yMinValue = keepAxisSame
? (min(yFullArray) as number)
: minBy(dataFormatted, d => d.yVal)
? refYVal && showReference
? (minBy(dataFormatted, d => d.yVal)?.yVal as number) < refYVal
? (minBy(dataFormatted, d => d.yVal)?.yVal as number)
Expand Down
12 changes: 12 additions & 0 deletions src/GrapherComponent/Settings/ScatterPlotSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function ScatterPlotSettings(props: Props) {
yAxisIndicator,
sizeIndicator,
colorIndicator,
keepAxisSame,
showLabel,
showMostRecentData,
selectedCountryOrRegion,
Expand All @@ -47,6 +48,7 @@ export function ScatterPlotSettings(props: Props) {
updateShowLabel,
updateShowMostRecentData,
updateShowReference,
updateKeepAxisSame,
} = useContext(Context) as CtxDataType;
const scatterPlotIndicators = indicators.filter(d => !d.IsCategorical);
const sizeIndicators = indicators.filter(d => d.Sizing);
Expand Down Expand Up @@ -240,6 +242,16 @@ export function ScatterPlotSettings(props: Props) {
>
Show World/Regional Reference
</Checkbox>
<Checkbox
style={{ margin: 0 }}
className='undp-checkbox'
checked={keepAxisSame}
onChange={e => {
updateKeepAxisSame(e.target.checked);
}}
>
Use same axes to compare between years
</Checkbox>
</div>
</div>
{!selectedCountryOrRegion && countries.length > 1 ? (
Expand Down
8 changes: 8 additions & 0 deletions src/RegionVisualization/Visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function VisualizationEl(props: Props) {
disaggregationMetaData.length > 0 ? disaggregationMetaData[0] : undefined,
disaggregationGraphType: 'global',
disaggregationOrder: 'first',
keepAxisSame: false,
};

const [state, dispatch] = useReducer(Reducer, initialState);
Expand Down Expand Up @@ -270,6 +271,12 @@ function VisualizationEl(props: Props) {
payload: disaggregationOrder,
});
};
const updateKeepAxisSame = (d: boolean) => {
dispatch({
type: 'UPDATE_KEEP_AXIS_SAME',
payload: d,
});
};
return (
<Context.Provider
// eslint-disable-next-line react/jsx-no-constructed-context-values
Expand Down Expand Up @@ -298,6 +305,7 @@ function VisualizationEl(props: Props) {
updateDisaggregationIndicator,
updateDisaggregationGraphType,
updateDisaggregationOrder,
updateKeepAxisSame,
}}
>
<div>
Expand Down
2 changes: 2 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export interface CtxDataType {
| 'Governance'
| 'Poverty and Inequality'
| 'Resilience';
keepAxisSame: boolean;
updateGraphType: (
_d:
| 'scatterPlot'
Expand Down Expand Up @@ -209,6 +210,7 @@ export interface CtxDataType {
| 'Poverty and Inequality'
| 'Resilience',
) => void;
updateKeepAxisSame: (_d: boolean) => void;
}

export interface CountryListType {
Expand Down

0 comments on commit b276f89

Please sign in to comment.