Skip to content

Commit

Permalink
Merge pull request #870 from visualize-admin/feat/sorted-columns-in-d…
Browse files Browse the repository at this point in the history
…ownloaded-files

feat: Sort columns properly in downloaded files
  • Loading branch information
bprusinowski authored Nov 18, 2022
2 parents e7ff69a + 60cd9ef commit 3dcd49a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ You can also check the [release page](https://github.com/visualize-admin/visuali
- Enhancements:
- Cube Checker:
- Introduced a Temporal dimensions scaleType & timeFormat checks
- Data download:
- Order of columns in downloaded files now matches the order visible in the dataset preview (based on shacl:order)
- Bug fixes:
- Correctly retrieve min & max values for Temporal dimensions when scaleType is not equal to Interval

Expand Down
4 changes: 3 additions & 1 deletion app/components/data-download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { OperationResult, useClient } from "urql";

import { QueryFilters } from "@/charts/shared/chart-helpers";
import { DataSource } from "@/configurator";
import { getSortedColumns } from "@/configurator/components/datatable";
import { useLocale } from "@/src";

import { Observation } from "../domain/data";
Expand Down Expand Up @@ -84,6 +85,7 @@ export type FileFormat = typeof FILE_FORMATS[number];
const makeColumnLabel = (dim: DimensionMetadataFragment) => {
return `${dim.label}${dim.unit ? ` (${dim.unit})` : ""}`;
};

const prepareData = ({
dimensions,
measures,
Expand All @@ -93,7 +95,7 @@ const prepareData = ({
measures: DimensionMetadataFragment[];
observations: Observation[];
}) => {
const columns = keyBy([...dimensions, ...measures], (d) => d.iri);
const columns = keyBy(getSortedColumns(dimensions, measures), (d) => d.iri);
const data = observations.map((obs) =>
Object.keys(obs).reduce((acc, key) => {
const col = columns[key];
Expand Down
11 changes: 6 additions & 5 deletions app/configurator/components/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const DataSetPreviewTable = ({
observations: Observation[];
}) => {
const headers = useMemo(() => {
return getSortedHeaders(dimensions, measures);
return getSortedColumns(dimensions, measures);
}, [dimensions, measures]);

if (observations) {
Expand Down Expand Up @@ -231,7 +231,8 @@ export const DataSetTable = ({
if (!data?.dataCubeByIri) {
return [];
}
return getSortedHeaders(

return getSortedColumns(
data.dataCubeByIri.dimensions,
data.dataCubeByIri.measures
);
Expand All @@ -252,13 +253,13 @@ export const DataSetTable = ({
}
};

function getSortedHeaders(
export const getSortedColumns = (
dimensions: DimensionMetadataFragment[],
measures: DimensionMetadataFragment[]
) {
) => {
const allDimensions = [...dimensions, ...measures];
allDimensions.sort((a, b) =>
ascending(a.order ?? Infinity, b.order ?? Infinity)
);
return allDimensions;
}
};

1 comment on commit 3dcd49a

@vercel
Copy link

@vercel vercel bot commented on 3dcd49a Nov 18, 2022

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-ixt1.vercel.app
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-alpha.vercel.app

Please sign in to comment.