Skip to content

Commit

Permalink
Merge pull request #1281 from visualize-admin/feat/common-joinby-dime…
Browse files Browse the repository at this point in the history
…nsion

refactor: Common joinBy dimension
  • Loading branch information
bprusinowski authored Dec 4, 2023
2 parents 7a4bec9 + df5db9d commit 9f88517
Show file tree
Hide file tree
Showing 41 changed files with 518 additions and 158 deletions.
1 change: 1 addition & 0 deletions app/browse/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export const DataSetTable = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
6 changes: 3 additions & 3 deletions app/browser/dataset-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const DataSetPreview = ({
}) => {
const footnotesClasses = useFootnotesStyles({ useMarginTop: false });
const locale = useLocale();
const filters = [{ iri: dataSetIri }];
const cubeFilters = [{ iri: dataSetIri }];
const [
{ data: previewData, fetching: fetchingPreview, error: previewError },
] = useDataCubePreviewQuery({
Expand All @@ -114,7 +114,7 @@ export const DataSetPreview = ({
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
cubeFilters: filters,
cubeFilters,
},
});
const classes = useStyles({
Expand Down Expand Up @@ -197,7 +197,7 @@ export const DataSetPreview = ({
<DataDownloadMenu
dataSource={dataSource}
title={dataCubeByIri.title}
filters={filters}
filters={cubeFilters}
/>
{dataCubeByIri.observations.sparqlEditorUrl && (
<RunSparqlQuery
Expand Down
1 change: 1 addition & 0 deletions app/charts/area/chart-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const ChartAreasVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
1 change: 1 addition & 0 deletions app/charts/column/chart-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const ChartColumnsVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
4 changes: 3 additions & 1 deletion app/charts/column/columns-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ const useColumnsState = (
return `${getYError(d)}${yErrorMeasure?.unit ?? ""}`;
};

const y = getY(d);

return {
xAnchor,
yAnchor,
Expand All @@ -246,7 +248,7 @@ const useColumnsState = (
xValue: xTimeUnit ? timeFormatUnit(xLabel, xTimeUnit) : xLabel,
datum: {
label: undefined,
value: `${yValueFormatter(getY(d))}`,
value: y !== null && isNaN(y) ? "-" : `${yValueFormatter(getY(d))}`,
error: getError(d),
color: "",
},
Expand Down
3 changes: 2 additions & 1 deletion app/charts/column/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export const Columns = () => {
return chartData.map((d) => {
const key = getRenderingKey(d);
const xScaled = xScale(getX(d)) as number;
const y = getY(d) ?? NaN;
const yRaw = getY(d);
const y = yRaw === null || isNaN(yRaw) ? 0 : yRaw;
const yScaled = yScale(y);
const yRender = yScale(Math.max(y, 0));
const height = Math.abs(yScaled - y0);
Expand Down
1 change: 1 addition & 0 deletions app/charts/combo/chart-combo-line-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const ChartComboLineColumnVisualization = (
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
1 change: 1 addition & 0 deletions app/charts/combo/chart-combo-line-dual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const ChartComboLineDualVisualization = (
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
1 change: 1 addition & 0 deletions app/charts/combo/chart-combo-line-single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const ChartComboLineSingleVisualization = (
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
10 changes: 8 additions & 2 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const chartTypesOrder: { [k in ChartType]: number } = {
* @param dimensions
* @param preferredType
*/
export const findPreferredDimension = (
const findPreferredDimension = (
dimensions: Component[],
preferredType?: DimensionType
) => {
Expand Down Expand Up @@ -1885,9 +1885,11 @@ const adjustSegmentSorting = ({
export const getPossibleChartTypes = ({
dimensions,
measures,
allowedChartTypes,
}: {
dimensions: Dimension[];
measures: Measure[];
allowedChartTypes?: ChartType[];
}): ChartType[] => {
const numericalMeasures = measures.filter(isNumericalMeasure);
const ordinalMeasures = measures.filter(isOrdinalMeasure);
Expand Down Expand Up @@ -1945,7 +1947,11 @@ export const getPossibleChartTypes = ({
}

return chartTypes
.filter((d) => possibles.includes(d))
.filter(
(d) =>
possibles.includes(d) &&
(!allowedChartTypes || allowedChartTypes.includes(d))
)
.sort((a, b) => chartTypesOrder[a] - chartTypesOrder[b]);
};

Expand Down
1 change: 1 addition & 0 deletions app/charts/line/chart-lines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const ChartLinesVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
1 change: 1 addition & 0 deletions app/charts/map/chart-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const ChartMapVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
1 change: 1 addition & 0 deletions app/charts/pie/chart-pie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const ChartPieVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
1 change: 1 addition & 0 deletions app/charts/scatterplot/chart-scatterplot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const ChartScatterplotVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
10 changes: 4 additions & 6 deletions app/charts/shared/chart-data-filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
DataSource,
Filters,
getFiltersByMappingStatus,
QueryFilters,
useChartConfigFilters,
useConfiguratorState,
} from "@/configurator";
Expand Down Expand Up @@ -97,10 +96,9 @@ export const ChartDataFilters = (props: ChartDataFiltersProps) => {
const cubeQueryFilters = queryFilters.find(
(d) => d.iri === cube.iri
) as DataCubeObservationFilter;
const filtersByMappingStatus = getFiltersByMappingStatus(
chartConfig,
cube.iri
);
const filtersByMappingStatus = getFiltersByMappingStatus(chartConfig, {
cubeIri: cube.iri,
});
const { unmappedFilters, mappedFilters } = filtersByMappingStatus;
const unmappedKeys = Object.keys(unmappedFilters);
const unmappedQueryFiltersArray = Object.entries(
Expand Down Expand Up @@ -214,7 +212,7 @@ type DataFilterProps = {
dataSource: DataSource;
chartConfig: ChartConfig;
dataFilters: DataFilters;
interactiveFilters: QueryFilters;
interactiveFilters: Filters;
disabled: boolean;
};

Expand Down
65 changes: 53 additions & 12 deletions app/charts/shared/chart-helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { parseDate } from "@/configurator/components/ui-helpers";
import { FIELD_VALUE_NONE } from "@/configurator/constants";
import { Component, Dimension, Measure, Observation } from "@/domain/data";
import { truthy } from "@/domain/types";
import { JOIN_BY_DIMENSION_IRI } from "@/graphql/hook-utils";
import { DataCubeObservationFilter } from "@/graphql/resolver-types";
import {
InteractiveFiltersState,
Expand Down Expand Up @@ -93,12 +94,10 @@ export const useQueryFilters = ({

return {
iri: cube.iri,
componentIris:
dimensions.length > 0 && measures.length > 0
? [...dimensions, ...measures]
.filter((d) => d.cubeIri === cube.iri)
.map((d) => d.iri)
: undefined,
// componentIris: getComponentIris(cube.iri, {
// dimensions,
// measures,
// }),
filters: prepareQueryFilters(
chartConfig.chartType,
filters,
Expand All @@ -120,10 +119,48 @@ export const useQueryFilters = ({
]);
};

// Handle correctly when improving performance of data fetching!
// const getComponentIris = (
// cubeIri: string,
// options: {
// dimensions: Dimension[];
// measures: Measure[];
// }
// ) => {
// const { dimensions, measures } = options;

// if (dimensions.length === 0 && measures.length === 0) {
// return;
// }

// const filteredDimensionIris: string[] = [];

// for (const dimension of dimensions) {
// if (dimension.isJoinByDimension) {
// if (dimension.originalIris.some((d) => d.cubeIri === cubeIri)) {
// filteredDimensionIris.push(
// ...dimension.originalIris.map((d) => d.dimensionIri)
// );
// }
// } else {
// if (dimension.cubeIri === cubeIri) {
// filteredDimensionIris.push(dimension.iri);
// }
// }
// }

// return [
// ...filteredDimensionIris,
// ...measures.filter((d) => d.cubeIri === cubeIri).map((d) => d.iri),
// ];
// };

type IFKey = keyof NonNullable<InteractiveFiltersConfig>;

export const getChartConfigFilterComponentIris = ({ cubes }: ChartConfig) => {
return Object.keys(getChartConfigFilters(cubes));
return Object.keys(getChartConfigFilters(cubes)).filter(
(d) => d !== JOIN_BY_DIMENSION_IRI
);
};

const getMapChartConfigAdditionalFields = ({ fields }: MapConfig) => {
Expand Down Expand Up @@ -212,12 +249,16 @@ export const extractChartConfigComponentIris = (chartConfig: ChartConfig) => {
});
}

return uniq(
[...fieldIris, ...additionalFieldIris, ...filterIris, ...IFIris].filter(
Boolean
return (
uniq(
[...fieldIris, ...additionalFieldIris, ...filterIris, ...IFIris].filter(
Boolean
)
)
// Important so the order is consistent when querying.
).sort();
.filter((d) => d !== JOIN_BY_DIMENSION_IRI)
// Important so the order is consistent when querying.
.sort()
);
};

/** Use to remove missing values from chart data. */
Expand Down
1 change: 1 addition & 0 deletions app/charts/table/chart-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ChartTableVisualization = ({
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
14 changes: 7 additions & 7 deletions app/components/chart-filters-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export const ChartFiltersList = (props: ChartFiltersListProps) => {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
cubeFilters: filters
? filters.map((filter) => ({
iri: filter.iri,
componentIris: filter.componentIris,
filters: filter.filters,
}))
: [],
cubeFilters:
filters?.map((filter) => ({
iri: filter.iri,
componentIris: filter.componentIris,
filters: filter.filters,
joinBy: filter.joinBy,
})) ?? [],
},
pause: !filters,
});
Expand Down
6 changes: 4 additions & 2 deletions app/components/chart-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const ChartPreviewInner = (props: ChartPreviewProps) => {
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
filters: cube.filters,
joinBy: cube.joinBy,
})),
},
});
Expand All @@ -93,8 +95,8 @@ export const ChartPreviewInner = (props: ChartPreviewProps) => {
} = useChartTablePreview();

const handleToggleTableView = useEvent(() => setIsTablePreview((c) => !c));
const dimensions = components?.dataCubesComponents.dimensions ?? [];
const measures = components?.dataCubesComponents.measures ?? [];
const dimensions = components?.dataCubesComponents.dimensions;
const measures = components?.dataCubesComponents.measures;
const allComponents = useMemo(() => {
if (!components?.dataCubesComponents) {
return [];
Expand Down
1 change: 1 addition & 0 deletions app/components/chart-published.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ const ChartPublishedInner = (props: ChartPublishInnerProps) => {
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
Expand Down
2 changes: 2 additions & 0 deletions app/components/chart-selection-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ const PublishChartButton = () => {
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
filters: cube.filters,
joinBy: cube.joinBy,
})),
},
});
Expand Down
4 changes: 2 additions & 2 deletions app/components/chart-with-filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ const GenericChart = (props: GenericChartProps) => {
props;
const queryFilters = useQueryFilters({
chartConfig,
dimensions: dimensions ?? [],
measures: measures ?? [],
dimensions,
measures,
});
const commonProps = {
dataSource,
Expand Down
1 change: 0 additions & 1 deletion app/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export type FilterValueMultiValues = FilterValueMulti["values"];
const Filters = t.record(t.string, FilterValue, "Filters");

export type Filters = t.TypeOf<typeof Filters>;
export type QueryFilters = Filters | FilterValueSingle;

// Meta
const Title = t.type({
Expand Down
Loading

1 comment on commit 9f88517

@vercel
Copy link

@vercel vercel bot commented on 9f88517 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app
visualization-tool-alpha.vercel.app

Please sign in to comment.