Skip to content

Commit

Permalink
feat: alphabetical sorting of formatted labels for flat legend
Browse files Browse the repository at this point in the history
  • Loading branch information
monfera committed Mar 30, 2021
1 parent 448b601 commit d899405
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 26 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 14 additions & 8 deletions src/chart_types/partition_chart/layout/utils/legend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { CategoryKey } from '../../../../common/category';
import { map } from '../../../../common/iterables';
import { LegendItem } from '../../../../common/legend';
import { LegendPositionConfig } from '../../../../specs/settings';
import { identity } from '../../../../utils/common';
import { isHierarchicalLegend } from '../../../../utils/legend';
import { Layer } from '../../specs';
import { QuadViewModel } from '../types/viewmodel_types';
Expand All @@ -45,10 +44,6 @@ function compareTreePaths(
return a.length - b.length; // if one path is fully contained in the other, then parent (shorter) goes first
}

function compareNames({ dataName: a }: QuadViewModel, { dataName: b }: QuadViewModel): number {
return a < b ? -1 : a > b ? 1 : 0;
}

/** @internal */
export function getLegendItems(
id: string,
Expand All @@ -61,6 +56,17 @@ export function getLegendItems(
const uniqueNames = new Set(map(({ dataName, fillColor }) => makeKey(dataName, fillColor), quadViewModel));
const useHierarchicalLegend = isHierarchicalLegend(flatLegend, legendPosition);

const formattedLabel = ({ dataName, depth }: QuadViewModel) => {
const formatter = layers[depth - 1]?.nodeLabel;
return formatter ? formatter(dataName) : dataName;
};

function compareNames(aItem: QuadViewModel, bItem: QuadViewModel): number {
const a = formattedLabel(aItem);
const b = formattedLabel(bItem);
return a < b ? -1 : a > b ? 1 : 0;
}

const excluded: Set<string> = new Set();
const items = quadViewModel.filter(({ depth, dataName, fillColor }) => {
if (legendMaxDepth !== null && depth > legendMaxDepth) {
Expand All @@ -78,11 +84,11 @@ export function getLegendItems(

items.sort(flatLegend ? compareNames : compareTreePaths);

return items.map<LegendItem>(({ dataName, fillColor, depth, path }) => {
const formatter = layers[depth - 1]?.nodeLabel ?? identity;
return items.map<LegendItem>((item) => {
const { dataName, fillColor, depth, path } = item;
return {
color: fillColor,
label: formatter(dataName),
label: formattedLabel(item),
childId: dataName,
depth: useHierarchicalLegend ? depth - 1 : 0,
path,
Expand Down
34 changes: 16 additions & 18 deletions stories/small_multiples/7_sunbursts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,24 @@ export const Example = () => {
valueAccessor={(d: Datum) => d.exportVal as number}
valueFormatter={(d: number) => `$${config.fillLabel.valueFormatter(Math.round(d / 1000000000))}\u00A0Bn`}
smallMultiples="sm"
layers={
[
{
groupByRollup: (d: Datum) => d.sitc1,
nodeLabel: (d: any) => productLookup[d].name,
fillLabel: { maximizeFontSize: true },
shape: {
fillColor: (d: ShapeTreeNode) => productToColor.get(d.dataName)!,
},
layers={[
{
groupByRollup: (d: Datum) => d.sitc1,
nodeLabel: (d: any) => productLookup[d].name,
fillLabel: { maximizeFontSize: true },
shape: {
fillColor: (d: ShapeTreeNode) => productToColor.get(d.dataName)!,
},
{
groupByRollup: (d: Datum) => d.dest,
nodeLabel: (d: any) => countryLookup[d].name,
fillLabel: { maximizeFontSize: true },
shape: {
fillColor: (d: ShapeTreeNode) => countryToColor.get(d.dataName)!,
},
},
{
groupByRollup: (d: Datum) => d.dest,
nodeLabel: (d: any) => countryLookup[d].name,
fillLabel: { maximizeFontSize: true },
shape: {
fillColor: (d: ShapeTreeNode) => countryToColor.get(d.dataName)!,
},
] /* .slice(layerFrom, layerTo) */
}
},
]}
config={{
partitionLayout: PartitionLayout.sunburst,
linkLabel: {
Expand Down

0 comments on commit d899405

Please sign in to comment.