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

feat: Temporal ordinal dimension is taken as second priority when inferring x-axis for columns #1381

Merged
merged 1 commit into from
Feb 29, 2024
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
86 changes: 86 additions & 0 deletions app/charts/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ColumnConfig, TableFields } from "@/configurator";
import { Dimension, Measure } from "@/domain/data";
import { TimeUnit } from "@/graphql/resolver-types";
import { RDFCubeViewQueryMock } from "@/test/cube-view-query-mock";

import bathingWaterData from "../test/__fixtures/data/DataCubeMetadataWithComponentValues-bathingWater.json";
Expand All @@ -13,6 +14,56 @@ import {

RDFCubeViewQueryMock;

const mockDimensions: Record<string, Dimension> = {
geoCoordinates: {
__typename: "GeoCoordinatesDimension",
cubeIri: "https://cube-iri",
isKeyDimension: true,
values: [],
iri: "geo-coordinates-dimension-iri",
isNumerical: false,
label: "Geo coordinates dimension",
},
ordinal: {
__typename: "OrdinalDimension",
cubeIri: "https://cube-iri",
isKeyDimension: true,
values: [],
iri: "ordinal-dimension-iri",
isNumerical: true,
label: "Ordinal dimension",
},
temporal: {
__typename: "TemporalDimension",
cubeIri: "https://cube-iri",
isKeyDimension: true,
values: [],
timeFormat: "%Y",
timeUnit: TimeUnit.Year,
iri: "temporal-dimension-iri",
isNumerical: true,
label: "Temporal dimension",
},
temporalOrdinal: {
__typename: "TemporalOrdinalDimension",
cubeIri: "https://cube-iri",
isKeyDimension: true,
values: [],
iri: "temporal-ordinal-dimension-iri",
isNumerical: true,
label: "Temporal ordinal dimension",
},
ordinal2: {
__typename: "OrdinalDimension",
cubeIri: "https://cube-iri",
isKeyDimension: true,
values: [],
iri: "ordinal-dimension-2-iri",
isNumerical: false,
label: "Ordinal dimension 2",
},
};

describe("initial config", () => {
it("should create an initial table config with column order based on dimension order", () => {
const config = getInitialConfig({
Expand All @@ -39,6 +90,41 @@ describe("initial config", () => {
["https://environment.ld.admin.ch/foen/nfi/evaluationType", 7],
]);
});

it("should create an initial column config having x axis correctly inferred (temporal ordinal)", () => {
const config = getInitialConfig({
chartType: "column",
iris: ["https://environment.ld.admin.ch/foen/nfi"],
dimensions: [
mockDimensions.geoCoordinates,
mockDimensions.ordinal,
mockDimensions.temporalOrdinal,
mockDimensions.ordinal2,
],
measures: forestAreaData.data.dataCubeByIri.measures as any as Measure[],
}) as ColumnConfig;

expect(config.fields.x.componentIri).toEqual(
"temporal-ordinal-dimension-iri"
);
});

it("should create an initial column config having x axis correctly inferred (temporal > temporal ordinal)", () => {
const config = getInitialConfig({
chartType: "column",
iris: ["https://environment.ld.admin.ch/foen/nfi"],
dimensions: [
mockDimensions.geoCoordinates,
mockDimensions.ordinal,
mockDimensions.temporalOrdinal,
mockDimensions.temporal,
mockDimensions.ordinal2,
],
measures: forestAreaData.data.dataCubeByIri.measures as any as Measure[],
}) as ColumnConfig;

expect(config.fields.x.componentIri).toEqual("temporal-dimension-iri");
});
});

describe("possible chart types", () => {
Expand Down
15 changes: 10 additions & 5 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
Measure,
NumericalMeasure,
} from "@/domain/data";
import { truthy } from "@/domain/types";
import {
DEFAULT_CATEGORICAL_PALETTE_NAME,
getDefaultCategoricalPaletteName,
Expand Down Expand Up @@ -139,12 +140,16 @@ export const chartTypesOrder: { [k in ChartType]: number } = {
*/
const findPreferredDimension = (
dimensions: Component[],
preferredType?: DimensionType
preferredTypes?: DimensionType[]
) => {
const dim =
dimensions.find(
(d) => d.__typename === preferredType && d.isKeyDimension
) ??
preferredTypes
?.map((preferredType) =>
dimensions.find(
(d) => d.__typename === preferredType && d.isKeyDimension
)
)
.filter(truthy)[0] ??
dimensions.find((d) => d.isKeyDimension) ??
dimensions[0];

Expand Down Expand Up @@ -379,7 +384,7 @@ export const getInitialConfig = (
case "column":
const columnXComponentIri = findPreferredDimension(
sortBy(dimensions, (d) => (isGeoDimension(d) ? 1 : -1)),
"TemporalDimension"
["TemporalDimension", "TemporalOrdinalDimension"]
).iri;

return {
Expand Down
Loading