Skip to content
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
29 changes: 12 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@statisticsfinland/pxvisualizer",
"version": "1.0.4",
"version": "1.0.5",
"description": "Component library for visualizing PxGraf data",
"main": "./dist/pxv.cjs",
"jestSonar": {
Expand Down
8 changes: 8 additions & 0 deletions src/core/conversion/TestFixtures/groupHorizontalBarChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ export const GROUP_HORIZONTAL_BAR_CHART_WITH_SUM_SORTING: IQueryVisualizationRes
}
};

export const GROUP_HORIZONTAL_BAR_CHART_WITH_REVERSED_SORTING: IQueryVisualizationResponse = {
...GROUP_HORIZONTAL_BAR_CHART_WITH_SUM_SORTING,
visualizationSettings: {
...GROUP_HORIZONTAL_BAR_CHART_WITH_SUM_SORTING.visualizationSettings,
sorting: "reversed"
}
};

export const GROUP_HORIZONTAL_BAR_CHART_WITH_PRELIMINARY_DATA_SET: {pxGraphData: IQueryVisualizationResponse} = {
pxGraphData: {
"tableReference": { name: "table.px", hierarchy: ["foo", "bar"] },
Expand Down
15 changes: 15 additions & 0 deletions src/core/conversion/viewSorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const ASCENDING = 'ascending';
export const DESCENDING = 'descending';
export const SUM = 'sum';
export const NO_SORTING = 'no_sorting';
export const REVERSED = 'reversed';

export const ASCENDING_SORTING_FUNC = (a : IDataCell, b: IDataCell) => (a.value ?? 0) - (b.value ?? 0);
export const DESCENDING_SORTING_FUNC = (a: IDataCell, b: IDataCell) => (b.value ?? 0) - (a.value ?? 0);
Expand Down Expand Up @@ -33,6 +34,20 @@ export function sortViewBasedOnSum(view: View, sortingFunc: (a: IDataCell, b: ID
return reorderView(view, sumArray, sortingFunc);
}

export function reverseViewOrder(view: View) {
const reversedSeries = [...view.series].reverse().map(s => ({
...s,
series: [...s.series].reverse(),
rowNameGroup: [...s.rowNameGroup].reverse()
}));

return {
...view,
series: reversedSeries,
columnNameGroups: [...view.columnNameGroups].reverse()
};
}

function get1DSortingIndexBuffer(series: IDataCell[], sortingFunc: (a: IDataCell, b: IDataCell) => number): number[] {
const indices = [...Array(series.length).keys()];
indices.sort((a, b) => sortingFunc(series[a], series[b]));
Expand Down
151 changes: 146 additions & 5 deletions src/core/conversion/viewUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ETimeVariableInterval, EVariableType, EVisualizationType, IContentComponent, IQueryVisualizationResponse, IVariableMeta, IVariableValueMeta, TMultiLanguageString } from '../types/queryVisualizationResponse';
import { ESeriesType, TSingleSelections, View } from '../types/view';
import { GROUP_HORIZONTAL_BAR_CHART_WITH_PRELIMINARY_DATA_SET, GROUP_HORIZONTAL_BAR_CHART_WITH_SUM_SORTING } from './TestFixtures/groupHorizontalBarChart';
import { GROUP_HORIZONTAL_BAR_CHART_WITH_PRELIMINARY_DATA_SET, GROUP_HORIZONTAL_BAR_CHART_WITH_SUM_SORTING,GROUP_HORIZONTAL_BAR_CHART_WITH_REVERSED_SORTING } from './TestFixtures/groupHorizontalBarChart';
import { HORIZONTAL_BAR_CHART_ASCENDING, HORIZONTAL_BAR_CHART_WITH_SELECTABLES } from './TestFixtures/horizontalBarChart';
import { LINE_CHART_WITH_COMBINATION_SERIES, LINE_CHART_WITH_MULTISELECTABLE_VARIABLE, LINE_CHART_WITH_QUARTER_SERIES } from './TestFixtures/lineChart';
import { PERCENT_HORIZONTAL_BAR_CHART, PERCENT_HORIZONTAL_BAR_CHART_VIEW, PERCENT_HORIZONTAL_BAR_CHART_WITH_SELECTABLES, PERCENT_HORIZONTAL_BAR_CHART_WITH_SELECTABLES_VIEW } from './TestFixtures/percentHorizontalBarChart';
Expand All @@ -13,7 +13,7 @@ import {
PERCENT_VERTICAL_BAR_CHART_WITH_SELECTABLES_VIEW
} from './TestFixtures/percentVerticalBarChart';
import { SELECTABLE_TABLE_WITH_MISSING_DATA } from './TestFixtures/tableChart';
import { ASCENDING, DESCENDING, SUM } from './viewSorting';
import { ASCENDING, DESCENDING, SUM, REVERSED, NO_SORTING } from './viewSorting';
import { buildSeries, convertPxGrafResponseToView, convertToRelative } from './viewUtils';
import { v4 as uuidV4 } from 'uuid'

Expand Down Expand Up @@ -85,19 +85,36 @@ describe('series metadata', () => {
expectedSeries: [
{ value: 1.1, precision: 1, preliminary: false },
{ value: 2.11, precision: 2, preliminary: false }
]},
]
},
{
sorting: DESCENDING,
expectedSeries: [
{ value: 2.11, precision: 2, preliminary: false },
{ value: 1.1, precision: 1, preliminary: false }
]},
]
},
{
sorting: SUM,
expectedSeries: [
{ value: 2.11, precision: 2, preliminary: false },
{ value: 1.1, precision: 1, preliminary: false }
]
},
{
sorting: NO_SORTING,
expectedSeries: [
{ value: 1.1, precision: 1, preliminary: false },
]}
{ value: 2.11, precision: 2, preliminary: false }
]
},
{
sorting: REVERSED,
expectedSeries: [
{ value: 2.11, precision: 2, preliminary: false },
{ value: 1.1, precision: 1, preliminary: false }
]
}
])('returns sorted metadata with view', ({
sorting, expectedSeries
}) => {
Expand Down Expand Up @@ -610,6 +627,130 @@ describe('horizontal bar chart view conversion', () => {
expect(resultView).toEqual(expectedView);
});

it('returns a view with reversed sorting', () => {
const resultView: View = convertPxGrafResponseToView(GROUP_HORIZONTAL_BAR_CHART_WITH_REVERSED_SORTING, {});
const expectedView: View = {
tableReferenceName: "table.px",
seriesType: ESeriesType.Nominal,
visualizationSettings: {
defaultSelectableVariableCodes: null,
sorting: "reversed",
timeVariableIntervals: ETimeVariableInterval.Year,
visualizationType: EVisualizationType.GroupHorizontalBarChart,
},
selectableVarNames: [],
colVarNames: [
{
"en": "Greenhouse gas",
"fi": "Kasvihuonekaasu",
"sv": "Växthusgas",
},
],
columnNameGroups: [
[
{
"en": "Nitrous oxide (N2O)",
"fi": "Dityppioksidi (N2O)",
"sv": "Dikväveoksid (N2O)",
},
],
[
{
"en": "Methane (CH4)",
"fi": "Metaani (CH4)",
"sv": "Metan (CH4)",
},
],
[
{
"en": "Carbon dioxide (CO2)",
"fi": "Hiilidioksidi (CO2)",
"sv": "Koldioksid (CO2)",
},
],
],
header: {
"en": "Emission, thousand tonnes of CO2 eq. 2020 by Emission category, Greenhouse gas",
"fi": "Päästö, tuhatta tonnia CO2-ekv. 2020 muuttujina Päästöluokka, Kasvihuonekaasu",
"sv": "Utsläpp, tusen ton CO2-ekv. 2020 efter Utsläppsklass, Växthusgas",
},
rowVarNames: [
{
"en": "Emission category",
"fi": "Päästöluokka",
"sv": "Utsläppsklass",
}
],
series: [
{
rowNameGroup: [
{
"en": "1A4bi Residential, stationary",
"fi": "1A4bi Kotitalouksien lämmitys",
"sv": "1A4bi Hushållen, egen uppvärmning av bostäder och lokaler",
},
],
series: [
{ value: 35, precision: 0, preliminary: false },
{ value: 155, precision: 0, preliminary: false },
{ value: 748, precision: 0, preliminary: false }
]
},
{
rowNameGroup: [
{
"en": "1A3b Road transportation",
"fi": "1A3b Tieliikenne",
"sv": "1A3b Vägtrafik",
},
],
series: [
{ value: 82, precision: 0, preliminary: false },
{ value: 8, precision: 0, preliminary: false },
{ value: 9845, precision: 0, preliminary: false }
]
},
{
rowNameGroup: [
{
"en": "1A3a Domestic aviation",
"fi": "1A3a Kotimaan lentoliikenne",
"sv": "1A3a Inrikes flygtrafik",
},
],
series: [
{ value: 1, precision: 0, preliminary: false },
{ value: 0, precision: 0, preliminary: false },
{ value: 86, precision: 0, preliminary: false }
]
},
],
sources: [
{
"en": "PxVisualizer-en",
"fi": "PxVisualizer-fi",
"sv": "PxVisualizer-sv",
},
],
subheaderValues: [],
units: [
{
name: {
"en": "Emission, thousand tonnes of CO2 eq.",
"fi": "Päästö, tuhatta tonnia CO2-ekv.",
"sv": "Utsläpp, tusen ton CO2-ekv.",
},
unit: {
"en": "thousand tonnes of CO2 eq.",
"fi": "tuhatta tonnia CO2-ekv.",
"sv": "tusen ton CO2-ekv.",
},
}
],
};
expect(resultView).toEqual(expectedView);
});

it('Every cell in series should preliminary true if series rowNameGroup contains preliminary data series', () => {
const view = convertPxGrafResponseToView(GROUP_HORIZONTAL_BAR_CHART_WITH_PRELIMINARY_DATA_SET.pxGraphData, {});
expect(view.series[0].series.every(d => d.preliminary)).toBeFalsy();
Expand Down
3 changes: 2 additions & 1 deletion src/core/conversion/viewUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IQueryVisualizationResponse } from "../types";
import { EVariableType, EVisualizationType, IContentComponent, IVariableMeta, IVariableValueMeta, TMultiLanguageString } from "../types/queryVisualizationResponse";
import { ESeriesType, IDataCell, IDataSeries, IUnitInfo, TSingleSelections, TValueSelectionAmounts, View } from "../types/view";
import { ASCENDING, ASCENDING_SORTING_FUNC, DESCENDING, DESCENDING_SORTING_FUNC, NO_SORTING, SUM, sortViewBasedOnSeries, sortViewBasedOnSeriesRelative, sortViewBasedOnSum } from "./viewSorting";
import { ASCENDING, ASCENDING_SORTING_FUNC, DESCENDING, DESCENDING_SORTING_FUNC, NO_SORTING, SUM, REVERSED, sortViewBasedOnSeries, sortViewBasedOnSeriesRelative, sortViewBasedOnSum, reverseViewOrder } from "./viewSorting";
import { cartesianProduct, onlyUnique } from "./utilityFunctions";
import { TVariableSelections } from "../types/variableSelections";
import Translations from "./translations";
Expand All @@ -21,6 +21,7 @@ export function convertPxGrafResponseToView(
case null: return view;
case undefined: return view;
case NO_SORTING: return view;
case REVERSED: return reverseViewOrder(view);
case SUM: return sortViewBasedOnSum(view, DESCENDING_SORTING_FUNC);
case ASCENDING: return sortViewBasedOnSeries(view, 0, ASCENDING_SORTING_FUNC);
case DESCENDING: return sortViewBasedOnSeries(view, 0, DESCENDING_SORTING_FUNC);
Expand Down
Loading