Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(partition): linked text maximum length config #665

Merged
merged 3 commits into from
May 7, 2020
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/chart_types/partition_chart/layout/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ export const configMetadata = {
type: 'number',
documentation: 'Limits the total count of linked labels. The first N largest slices are kept.',
},
maxTextLength: {
dflt: 100,
min: 2,
max: 200,
documentation: 'Limits the total number of characters in linked labels.',
},
textColor: { dflt: '#000000', type: 'color' },
textInvertible: { dflt: false, type: 'boolean' },
textOpacity: { dflt: 1, min: 0, max: 1, type: 'number' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface LinkLabelConfig extends LabelConfig {
radiusPadding: Distance;
lineWidth: Pixels;
maxCount: number;
maxTextLength: number;
}

export interface FillFontSizeRange {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { Distance } from '../types/geometry_types';
import { Config } from '../types/config_types';
import { TAU, trueBearingToStandardPositionAngle } from '../utils/math';
import { LinkLabelVM, ShapeTreeNode, ValueGetterFunction } from '../types/viewmodel_types';
import { LinkLabelVM, RawTextGetter, ShapeTreeNode, ValueGetterFunction } from '../types/viewmodel_types';
import { meanAngle } from '../geometry';
import { TextMeasure } from '../types/types';
import { ValueFormatter } from '../../../../utils/commons';
Expand All @@ -31,9 +31,10 @@ export function linkTextLayout(
nodesWithoutRoom: ShapeTreeNode[],
currentY: Distance[],
anchorRadius: Distance,
rawTextGetter: Function,
rawTextGetter: RawTextGetter,
valueGetter: ValueGetterFunction,
valueFormatter: ValueFormatter,
maxTextLength: number,
): LinkLabelVM[] {
const { linkLabel } = config;
const maxDepth = nodesWithoutRoom.reduce((p: number, n: ShapeTreeNode) => Math.max(p, n.depth), 0);
Expand Down Expand Up @@ -69,7 +70,8 @@ export function linkTextLayout(
const stemFromY = y;
const stemToX = x + north * west * cy - west * relativeY;
const stemToY = cy;
const text = rawTextGetter(node);
const rawText = rawTextGetter(node);
const text = rawText.length <= maxTextLength ? rawText : `${rawText.substr(0, maxTextLength - 1)}…`; // ellipsis is one char
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine for now, in the future we should account for RTL languages :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thanks for the nudge, will incorporate in the follow-up.

const valueText = valueFormatter(valueGetter(node));
const labelFontSpec = {
fontStyle: 'normal',
Expand Down
3 changes: 3 additions & 0 deletions src/chart_types/partition_chart/layout/viewmodel/viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ export function shapeViewModel(
return !(foundInFillText && foundInFillText.rows.length !== 0);
});

const maxLinkedLabelTextLength = config.linkLabel.maxTextLength;

const linkLabelViewModels = linkTextLayout(
textMeasure,
config,
Expand All @@ -317,6 +319,7 @@ export function shapeViewModel(
rawTextGetter,
valueGetter,
valueFormatter,
maxLinkedLabelTextLength,
);

const pickQuads: PickFunction = (x, y) => {
Expand Down
10 changes: 9 additions & 1 deletion stories/sunburst/2_value_formatted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { mocks } from '../../src/mocks/hierarchical/index';
import { config } from '../../src/chart_types/partition_chart/layout/config/config';
import React from 'react';
import { indexInterpolatedFillColor, interpolatorTurbo, productLookup } from '../utils/utils';
import { number } from '@storybook/addon-knobs';

export const example = () => (
<Chart className="story-chart">
Expand Down Expand Up @@ -48,7 +49,14 @@ export const example = () => (
},
},
]}
config={{ outerSizeRatio: 0.9, linkLabel: { fontStyle: 'italic', valueFont: { fontWeight: 900 } } }}
config={{
outerSizeRatio: 0.9,
linkLabel: {
fontStyle: 'italic',
valueFont: { fontWeight: 900 },
maxTextLength: number('maxTextLength', 20, { range: true, min: 1, max: 100 }),
},
}}
/>
</Chart>
);