Skip to content

Commit 7166e42

Browse files
authored
feat(partition): linked text maximum length config (#665)
Adds a config option to limit the length of linked label text in character count
1 parent 4421748 commit 7166e42

6 files changed

Lines changed: 24 additions & 4 deletions

File tree

Loading

src/chart_types/partition_chart/layout/config/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ export const configMetadata = {
210210
type: 'number',
211211
documentation: 'Limits the total count of linked labels. The first N largest slices are kept.',
212212
},
213+
maxTextLength: {
214+
dflt: 100,
215+
min: 2,
216+
max: 200,
217+
documentation: 'Limits the total number of characters in linked labels.',
218+
},
213219
textColor: { dflt: '#000000', type: 'color' },
214220
textInvertible: { dflt: false, type: 'boolean' },
215221
textOpacity: { dflt: 1, min: 0, max: 1, type: 'number' },

src/chart_types/partition_chart/layout/types/config_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface LinkLabelConfig extends LabelConfig {
5555
radiusPadding: Distance;
5656
lineWidth: Pixels;
5757
maxCount: number;
58+
maxTextLength: number;
5859
}
5960

6061
export interface FillFontSizeRange {

src/chart_types/partition_chart/layout/viewmodel/link_text_layout.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import { Distance } from '../types/geometry_types';
2020
import { Config } from '../types/config_types';
2121
import { TAU, trueBearingToStandardPositionAngle } from '../utils/math';
22-
import { LinkLabelVM, ShapeTreeNode, ValueGetterFunction } from '../types/viewmodel_types';
22+
import { LinkLabelVM, RawTextGetter, ShapeTreeNode, ValueGetterFunction } from '../types/viewmodel_types';
2323
import { meanAngle } from '../geometry';
2424
import { TextMeasure } from '../types/types';
2525
import { ValueFormatter } from '../../../../utils/commons';
@@ -31,9 +31,10 @@ export function linkTextLayout(
3131
nodesWithoutRoom: ShapeTreeNode[],
3232
currentY: Distance[],
3333
anchorRadius: Distance,
34-
rawTextGetter: Function,
34+
rawTextGetter: RawTextGetter,
3535
valueGetter: ValueGetterFunction,
3636
valueFormatter: ValueFormatter,
37+
maxTextLength: number,
3738
): LinkLabelVM[] {
3839
const { linkLabel } = config;
3940
const maxDepth = nodesWithoutRoom.reduce((p: number, n: ShapeTreeNode) => Math.max(p, n.depth), 0);
@@ -69,7 +70,8 @@ export function linkTextLayout(
6970
const stemFromY = y;
7071
const stemToX = x + north * west * cy - west * relativeY;
7172
const stemToY = cy;
72-
const text = rawTextGetter(node);
73+
const rawText = rawTextGetter(node);
74+
const text = rawText.length <= maxTextLength ? rawText : `${rawText.substr(0, maxTextLength - 1)}…`; // ellipsis is one char
7375
const valueText = valueFormatter(valueGetter(node));
7476
const labelFontSpec = {
7577
fontStyle: 'normal',

src/chart_types/partition_chart/layout/viewmodel/viewmodel.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ export function shapeViewModel(
308308
return !(foundInFillText && foundInFillText.rows.length !== 0);
309309
});
310310

311+
const maxLinkedLabelTextLength = config.linkLabel.maxTextLength;
312+
311313
const linkLabelViewModels = linkTextLayout(
312314
textMeasure,
313315
config,
@@ -317,6 +319,7 @@ export function shapeViewModel(
317319
rawTextGetter,
318320
valueGetter,
319321
valueFormatter,
322+
maxLinkedLabelTextLength,
320323
);
321324

322325
const pickQuads: PickFunction = (x, y) => {

stories/sunburst/2_value_formatted.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { mocks } from '../../src/mocks/hierarchical/index';
2121
import { config } from '../../src/chart_types/partition_chart/layout/config/config';
2222
import React from 'react';
2323
import { indexInterpolatedFillColor, interpolatorTurbo, productLookup } from '../utils/utils';
24+
import { number } from '@storybook/addon-knobs';
2425

2526
export const example = () => (
2627
<Chart className="story-chart">
@@ -48,7 +49,14 @@ export const example = () => (
4849
},
4950
},
5051
]}
51-
config={{ outerSizeRatio: 0.9, linkLabel: { fontStyle: 'italic', valueFont: { fontWeight: 900 } } }}
52+
config={{
53+
outerSizeRatio: 0.9,
54+
linkLabel: {
55+
fontStyle: 'italic',
56+
valueFont: { fontWeight: 900 },
57+
maxTextLength: number('maxTextLength', 20, { range: true, min: 1, max: 100 }),
58+
},
59+
}}
5260
/>
5361
</Chart>
5462
);

0 commit comments

Comments
 (0)