Skip to content

Commit

Permalink
feat: Temporal ordinal dimension is taken as second priority when inf…
Browse files Browse the repository at this point in the history
…erring x-axis for columns

fix #1373
  • Loading branch information
ptbrowne committed Feb 29, 2024
1 parent 90c7a75 commit 4051889
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
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

0 comments on commit 4051889

Please sign in to comment.