Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Scatterplot issues with optional segment #749

Merged
merged 2 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 14 additions & 8 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export const getInitialConfig = ({
},
};
case "scatterplot":
const segmentComponent =
getCategoricalDimensions(dimensions)[0] ||
getGeoDimensions(dimensions)[0];

return {
version: CHART_CONFIG_VERSION,
chartType: "scatterplot",
Expand All @@ -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":
Expand Down
13 changes: 8 additions & 5 deletions app/charts/scatterplot/scatterplot-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -128,19 +128,22 @@ const useScatterplotState = ({
const colors = scaleOrdinal<string, string>();
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 {
Expand Down