Skip to content

Commit

Permalink
Merge pull request #1066 from visualize-admin/fix/interactive-filters…
Browse files Browse the repository at this point in the history
…-issues

fix: Interactive filters issues
  • Loading branch information
bprusinowski authored Jun 14, 2023
2 parents 65a0528 + 9429c32 commit 3e5d8db
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 30 deletions.
65 changes: 63 additions & 2 deletions app/charts/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { TableFields } from "@/configurator";
import { ColumnConfig, TableFields } from "@/configurator";
import { ComponentsQuery } from "@/graphql/query-hooks";
import { DataCubeMetadata } from "@/graphql/types";

import bathingWaterData from "../test/__fixtures/data/DataCubeMetadataWithComponentValues-bathingWater.json";
import forestAreaData from "../test/__fixtures/data/forest-area-by-production-region.json";

import { getInitialConfig, getPossibleChartType } from "./index";
import {
getChartConfigAdjustedToChartType,
getInitialConfig,
getPossibleChartType,
} from "./index";

describe("initial config", () => {
it("should create an initial table config with column order based on dimension order", () => {
Expand All @@ -17,6 +22,7 @@ describe("initial config", () => {
ComponentsQuery["dataCubeByIri"]
>["measures"],
});

expect(
Object.values(config.fields as TableFields).map((x) => [
x["componentIri"],
Expand Down Expand Up @@ -68,3 +74,58 @@ describe("possible chart types", () => {
expect(possibleChartTypes).toEqual(["column", "map", "pie", "table"]);
});
});

describe("chart type switch", () => {
it("should correctly remove non-allowed interactive data filters", () => {
const chartConfig: ColumnConfig = {
version: "1.4.0",
chartType: "column",
filters: {},
fields: {
x: {
componentIri:
"https://environment.ld.admin.ch/foen/ubd0104/parametertype",
},
y: {
componentIri: "https://environment.ld.admin.ch/foen/ubd0104/value",
},
},
interactiveFiltersConfig: {
legend: {
active: false,
componentIri: "",
},
timeRange: {
active: false,
componentIri: "",
presets: {
type: "range",
from: "",
to: "",
},
},
dataFilters: {
active: true,
componentIris: [
"https://environment.ld.admin.ch/foen/ubd0104/dateofprobing",
],
},
},
};
const newConfig = getChartConfigAdjustedToChartType({
chartConfig,
newChartType: "line",
dimensions: bathingWaterData.data.dataCubeByIri
.dimensions as DataCubeMetadata["dimensions"],
measures: bathingWaterData.data.dataCubeByIri
.measures as DataCubeMetadata["measures"],
});

expect(newConfig.interactiveFiltersConfig?.dataFilters.active).toEqual(
false
);
expect(
newConfig.interactiveFiltersConfig?.dataFilters.componentIris
).toEqual([]);
});
});
39 changes: 24 additions & 15 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ const getAdjustedChartConfig = ({
case "filters":
case "fields.segment":
case "fields.animation":
case "interactiveFiltersConfig.dataFilters":
case "interactiveFiltersConfig.legend":
return true;
default:
Expand Down Expand Up @@ -525,7 +526,7 @@ const interactiveFiltersAdjusters: InteractiveFiltersAdjusters = {

return newChartConfig;
},
time: {
timeRange: {
active: ({ oldValue, newChartConfig }) => {
return produce(newChartConfig, (draft) => {
if (draft.interactiveFiltersConfig) {
Expand Down Expand Up @@ -564,21 +565,29 @@ const interactiveFiltersAdjusters: InteractiveFiltersAdjusters = {
},
},
},
dataFilters: {
active: ({ oldValue, newChartConfig }) => {
return produce(newChartConfig, (draft) => {
if (draft.interactiveFiltersConfig) {
draft.interactiveFiltersConfig.dataFilters.active = oldValue;
}
});
},
componentIris: ({ oldValue, newChartConfig }) => {
return produce(newChartConfig, (draft) => {
if (draft.interactiveFiltersConfig) {
draft.interactiveFiltersConfig.dataFilters.componentIris = oldValue;
dataFilters: ({ oldValue, newChartConfig }) => {
return produce(newChartConfig, (draft) => {
if (draft.interactiveFiltersConfig) {
const oldComponentIris = oldValue.componentIris ?? [];

if (oldComponentIris.length > 0) {
const fieldComponentIris = Object.values(draft.fields).map(
(d) => d.componentIri
);
// Remove componentIris that are not in the new chart config, as they
// can't be used as interactive data filters then.
const validComponentIris = oldComponentIris.filter(
(d) => !fieldComponentIris.includes(d)
);
draft.interactiveFiltersConfig.dataFilters.active =
validComponentIris.length > 0;
draft.interactiveFiltersConfig.dataFilters.componentIris =
validComponentIris;
} else {
draft.interactiveFiltersConfig.dataFilters = oldValue;
}
});
},
}
});
},
};

Expand Down
6 changes: 3 additions & 3 deletions app/charts/line/lines-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ const useLinesState = (
const xScale = scaleTime().domain(xDomain);

const xEntireDomain = useMemo(
() => extent(scalesData, (d) => getX(d)) as [Date, Date],
[scalesData, getX]
() => extent(plottableSortedData, (d) => getX(d)) as [Date, Date],
[plottableSortedData, getX]
);
const xEntireScale = scaleTime().domain(xEntireDomain);

const xAxisLabel =
measures.find((d) => d.iri === fields.x.componentIri)?.label ??
fields.x.componentIri;

// y
const minValue = Math.min(min(scalesData, getY) ?? 0, 0);
const maxValue = max(scalesData, getY) ?? 0;
Expand Down
7 changes: 3 additions & 4 deletions app/charts/shared/brush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const BrushTime = () => {
if (from && to) {
return getClosestObservationFromRangeDates([from, to]);
} else {
return [brushWidthScale.domain()[0], brushWidthScale.domain()[1]];
return brushWidthScale.domain();
}
}, [from, getClosestObservationFromRangeDates, to, brushWidthScale]);

Expand All @@ -123,7 +123,6 @@ export const BrushTime = () => {
const [xStart, xEnd] = selection.map((s) => brushWidthScale.invert(s));
const newDates = getClosestObservationFromRangeDates([xStart, xEnd]);

// Update interactive filters state
dispatch({
type: "SET_TIME_RANGE_FILTER",
value: newDates,
Expand All @@ -145,14 +144,14 @@ export const BrushTime = () => {
if (!event.sourceEvent) {
updateBrushEndedStatus(false);
} else {
if (!event.selection) {
if (!event.selection && ref.current) {
// End event fires twice on touchend (MouseEvent and TouchEvent),
// we want to compute mx basing on MouseEvent.
if (event.sourceEvent instanceof MouseEvent) {
const g = select(ref.current);
const [mx] = pointer(event, this);
const x = mx < 0 ? 0 : mx > brushWidth ? brushWidth : mx;
g.call(brush.move as any, [x, x]);
g.call(brush.move, [x, x]);
}
}

Expand Down
18 changes: 12 additions & 6 deletions app/configurator/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,18 @@ export type FieldAdjuster<
measures: DataCubeMetadata["measures"];
}) => NewChartConfigType;

export type InteractiveFiltersAdjusters = {
type AssureKeys<T, U extends { [K in keyof T]: unknown }> = {
[K in keyof T]: U[K];
};

export type InteractiveFiltersAdjusters = AssureKeys<
InteractiveFiltersConfig,
_InteractiveFiltersAdjusters
>;

type _InteractiveFiltersAdjusters = {
legend: FieldAdjuster<ChartConfig, InteractiveFiltersLegend>;
time: {
timeRange: {
active: FieldAdjuster<ChartConfig, boolean>;
componentIri: FieldAdjuster<ChartConfig, string>;
presets: {
Expand All @@ -691,10 +700,7 @@ export type InteractiveFiltersAdjusters = {
to: FieldAdjuster<ChartConfig, string>;
};
};
dataFilters: {
active: FieldAdjuster<ChartConfig, boolean>;
componentIris: FieldAdjuster<ChartConfig, string[]>;
};
dataFilters: FieldAdjuster<ChartConfig, InteractiveFiltersDataConfig>;
};

type BaseAdjusters<NewChartConfigType extends ChartConfig> = {
Expand Down

1 comment on commit 3e5d8db

@vercel
Copy link

@vercel vercel bot commented on 3e5d8db Jun 14, 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-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app

Please sign in to comment.