From 985eb4d10f8687fc2b6e389ed16965d5446626e4 Mon Sep 17 00:00:00 2001 From: Bartosz Prusinowski Date: Fri, 30 Sep 2022 10:59:50 +0200 Subject: [PATCH 1/2] fix: Scatterplot issues with optional segment --- app/charts/index.ts | 22 +++++++++++++------- app/charts/scatterplot/scatterplot-state.tsx | 13 +++++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/charts/index.ts b/app/charts/index.ts index b32b1c566..907b466bd 100644 --- a/app/charts/index.ts +++ b/app/charts/index.ts @@ -202,6 +202,10 @@ export const getInitialConfig = ({ }, }; case "scatterplot": + const segmentComponent = + getCategoricalDimensions(dimensions)[0] || + getGeoDimensions(dimensions)[0]; + return { version: CHART_CONFIG_VERSION, chartType: "scatterplot", @@ -213,14 +217,16 @@ export const getInitialConfig = ({ componentIri: measures.length > 1 ? measures[1].iri : measures[0].iri, }, - segment: { - componentIri: getCategoricalDimensions(dimensions)[0].iri, - palette: DEFAULT_PALETTE, - colorMapping: mapValueIrisToColor({ - palette: DEFAULT_PALETTE, - dimensionValues: getCategoricalDimensions(dimensions)[0]?.values, - }), - }, + ...(segmentComponent + ? { + componentIri: segmentComponent.iri, + palette: DEFAULT_PALETTE, + colorMapping: mapValueIrisToColor({ + palette: DEFAULT_PALETTE, + dimensionValues: segmentComponent.values, + }), + } + : {}), }, }; case "pie": diff --git a/app/charts/scatterplot/scatterplot-state.tsx b/app/charts/scatterplot/scatterplot-state.tsx index bbd289989..4d441cef6 100644 --- a/app/charts/scatterplot/scatterplot-state.tsx +++ b/app/charts/scatterplot/scatterplot-state.tsx @@ -30,7 +30,7 @@ import { mkNumber, useFormatNumber, } from "@/configurator/components/ui-helpers"; -import { Observation } from "@/domain/data"; +import { DimensionValue, Observation } from "@/domain/data"; import { estimateTextWidth } from "@/utils/estimate-text-width"; export interface ScatterplotState { @@ -128,19 +128,22 @@ const useScatterplotState = ({ const colors = scaleOrdinal(); const segmentDimension = dimensions.find( (d) => d.iri === fields.segment?.componentIri - ) as $FixMe; + ); const getSegmentLabel = useMemo(() => { - const segmentValuesByValue = keyBy(segmentDimension.values, (x) => x.value); + const segmentValuesByValue = keyBy( + segmentDimension?.values, + (x) => x.value + ); return (segment: string): string => { return segmentValuesByValue[segment]?.label || segment; }; - }, [segmentDimension.values]); + }, [segmentDimension?.values]); if (fields.segment && segmentDimension && fields.segment.colorMapping) { const orderedSegmentLabelsAndColors = segments.map((segment) => { const dvIri = segmentDimension.values.find( - (s: $FixMe) => s.label === segment + (d: DimensionValue) => d.label === segment )?.value; return { From 12863140f1be77454150a47be26f9d267e0c04ce Mon Sep 17 00:00:00 2001 From: Bartosz Prusinowski Date: Fri, 30 Sep 2022 16:02:31 +0200 Subject: [PATCH 2/2] docs: Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45d41b79c..70d11a248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ You can also check the [release page](https://github.com/visualize-admin/visuali ## Unreleased -Nothing yet. +- Charts: fix bugs that caused scatterplot and pie charts to crash in case no categorical dimensions were present in a dataset ## [3.9.4] - 2022-09-29