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(heatmap): allow rotation of x axis labels #1514

Merged
merged 41 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7c08e5c
feat: allow rotation of x axis labels
rshen91 Dec 1, 2021
52c7a98
feat: add rotation to y axis label
rshen91 Dec 1, 2021
587c9db
Merge remote-tracking branch 'upstream/master' into heatmap-rotation-…
rshen91 Dec 6, 2021
6bd128a
feat: add overflow to config for truncation
rshen91 Dec 7, 2021
a156574
feat: implement overflow through config for user to set
rshen91 Dec 7, 2021
8eba89b
test: update story to show truncation
rshen91 Dec 7, 2021
985e299
feat: add alternating ability for x axis labels
rshen91 Dec 7, 2021
6a5671f
test: add vrts
rshen91 Dec 8, 2021
8287a2a
fix: code review
rshen91 Dec 9, 2021
a98d1c4
Merge remote-tracking branch 'upstream/master' into heatmap-rotation-…
rshen91 Dec 16, 2021
1f51475
test: update vrts
rshen91 Dec 16, 2021
d126289
Merge branch 'master' into pr/1514
markov00 Dec 21, 2021
63e40b2
Limit the rotation to [-90,+90] range and fix the alignment
markov00 Dec 27, 2021
4e7bf9b
Merge branch 'master' into pr/1514
markov00 Dec 27, 2021
1270693
Extend screenspace scale compressor to consider different left/right …
markov00 Jan 26, 2022
3061bda
Add 2dim vectors base operations
markov00 Jan 26, 2022
3a96395
Add optimal rect rotation fn
markov00 Jan 26, 2022
c88f9a2
Add transform for Vec2 array
markov00 Jan 26, 2022
8806f97
Compute axis space based on label rotations
markov00 Feb 4, 2022
a3a9d36
Remove alternate and rotation on y
markov00 Feb 8, 2022
628d6f3
Cleanup time axis
markov00 Feb 9, 2022
b227f04
Merge remote-tracking branch 'upstream/master' into pr/1514
markov00 Feb 9, 2022
4085b23
Render truncated text on x
markov00 Feb 10, 2022
6684c02
Reduce ticks on time axis
markov00 Feb 10, 2022
9710525
Reduce ticks on time scale
markov00 Feb 14, 2022
726485a
Merge branch 'master' into pr/1514
markov00 Feb 14, 2022
ea3fc48
Cleanu up APIs
markov00 Feb 14, 2022
3d9f2b9
Fix up commits
markov00 Feb 14, 2022
976b8d4
Move the theme constraints to chart theme selector
markov00 Feb 15, 2022
e5cbede
Fix test files
markov00 Feb 15, 2022
dcf27f6
Simplify x axis size code
markov00 Feb 15, 2022
401c3d8
Cleanup scale compressor code
markov00 Feb 15, 2022
b45f82a
Update VRTs and tests
markov00 Feb 15, 2022
6373d19
Remove unused fn
markov00 Feb 15, 2022
cccab73
Remove a test case in favour of showing the titles in an existing scr…
markov00 Feb 15, 2022
535a58c
Render x axis ticks as an extension of the gridline
markov00 Feb 22, 2022
57795ae
Consider the grid stroke width in the grid width/height calculation
markov00 Feb 22, 2022
ceab0e6
Force the rotation to a minimum optimized angle to avoid overlaps
markov00 Feb 23, 2022
a2d71ab
Move VR test and update screenshots
markov00 Feb 23, 2022
a5a92e5
Fix missing prop
markov00 Feb 23, 2022
9b8cd96
Merge branch 'master' into heatmap-rotation-labels
markov00 Feb 23, 2022
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.
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.
12 changes: 12 additions & 0 deletions integration/tests/heatmap_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ describe('Heatmap stories', () => {
`http://localhost:9001/?path=/story/heatmap-alpha--time-snap&globals=theme:light&knob-dataset=${dataset}`,
);
});

it('should allow rotation of labels', async () => {
await common.expectChartAtUrlToMatchScreenshot(
'http://localhost:9001/?path=/story/test-cases--overlapping-heatmap-labels&&knob-set%20rotation%20of%20x%20axis%20label_labels=45',
);
});

it('should allow customizable length for x axis labels and allow alternating labels', async () => {
await common.expectChartAtUrlToMatchScreenshot(
'http://localhost:9001/?path=/story/test-cases--overlapping-heatmap-labels&&knob-set%20rotation%20of%20x%20axis%20label_labels=45&knob-show_labels=true&knob-set the max text length for the x axis labels_labels=5&knob-set overflow property for x axis labels_labels=true&knob-set x axis labels to alternate_labels=true',
);
});
});
17 changes: 17 additions & 0 deletions packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,15 @@ export interface HeatmapConfig {
visible: boolean;
padding: number;
formatter: (value: string | number) => string;
rotation: number;
overflow: 'ellipsis' | false;
maxTextLength: {
dflt: number;
min: number;
markov00 marked this conversation as resolved.
Show resolved Hide resolved
max: number;
documentation: string;
};
markov00 marked this conversation as resolved.
Show resolved Hide resolved
alternate: boolean;
};
// (undocumented)
yAxisLabel: Font & {
Expand All @@ -1184,6 +1193,14 @@ export interface HeatmapConfig {
bottom?: number;
};
formatter: (value: string | number) => string;
rotation: number;
overflow: 'ellipsis' | false;
maxTextLength: {
dflt: number;
min: number;
max: number;
documentation: string;
};
};
}

Expand Down
17 changes: 17 additions & 0 deletions packages/charts/src/chart_types/heatmap/layout/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export const config: Config = {
baseline: 'verticalAlign' as CanvasTextBaseline,
padding: 6,
formatter: String,
rotation: 0,
overflow: 'ellipsis',
maxTextLength: {
dflt: 100,
min: 2,
max: 200,
documentation: 'Limits the total number of characters in the x axis label',
},
alternate: false,
},
yAxisLabel: {
name: 'Y Value',
Expand All @@ -63,6 +72,14 @@ export const config: Config = {
baseline: 'verticalAlign' as CanvasTextBaseline,
padding: 5,
formatter: String,
rotation: 0,
overflow: 'ellipsis',
maxTextLength: {
dflt: 100,
min: 2,
max: 200,
documentation: 'Limits the total number of characters in linked labels.',
},
},
grid: {
cellWidth: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export interface Config {
visible: boolean;
padding: number;
formatter: (value: string | number) => string;
rotation: number;
overflow: 'ellipsis' | false;
maxTextLength: {
dflt: number;
min: number;
max: number;
documentation: string;
};
alternate: boolean;
};
yAxisLabel: Font & {
name: string;
Expand All @@ -58,6 +67,14 @@ export interface Config {
visible: boolean;
padding: number | { left?: number; right?: number; top?: number; bottom?: number };
formatter: (value: string | number) => string;
rotation: number;
overflow: 'ellipsis' | false;
maxTextLength: {
dflt: number;
min: number;
max: number;
documentation: string;
};
};
grid: {
cellWidth: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ScaleBand, scaleBand, scaleQuantize } from 'd3-scale';
import { colorToRgba } from '../../../../common/color_library_wrappers';
import { fillTextColor } from '../../../../common/fill_text_color';
import { Pixels } from '../../../../common/geometry';
import { Box, maximiseFontSize, TextMeasure } from '../../../../common/text_utils';
import { Box, cutToLength, maximiseFontSize, TextMeasure } from '../../../../common/text_utils';
import { ScaleContinuous } from '../../../../scales';
import { ScaleType } from '../../../../scales/constants';
import { LinearScale, OrdinalScale, RasterTimeScale, SettingsSpec } from '../../../../specs';
Expand Down Expand Up @@ -392,12 +392,14 @@ function getXTicks(
{ xValues, xNumericExtent }: HeatmapTable,
gridHeight: number,
): Array<TextBox> {
const maxTextLength = config.xAxisLabel.maxTextLength.max;
const getTextValue = (
formatter: Config['xAxisLabel']['formatter'],
scaleCallback: (x: string | number) => number | undefined | null,
overflow: Config['xAxisLabel']['overflow'],
) => (value: string | number): TextBox => {
return {
text: formatter(value),
text: overflow ? cutToLength(formatter(value), maxTextLength) : formatter(value),
value,
isValue: false,
...config.xAxisLabel,
Expand All @@ -418,13 +420,20 @@ function getXTicks(
timeZone: config.timeZone,
},
);
return timeScale.ticks().map<TextBox>(getTextValue(config.xAxisLabel.formatter, (x) => timeScale.scale(x)));
return timeScale
.ticks()
.map<TextBox>(getTextValue(config.xAxisLabel.formatter, (x) => timeScale.scale(x), config.xAxisLabel.overflow));
}

return xValues.map<TextBox>((textBox: string | number) => {
return {
...getTextValue(config.xAxisLabel.formatter, xScale)(textBox),
x: chartDimensions.left + (xScale(textBox) || 0) + xScale.bandwidth() / 2,
};
return config.xAxisLabel.overflow
? {
...getTextValue(config.xAxisLabel.formatter, xScale, config.xAxisLabel.overflow)(textBox),
x: chartDimensions.left + (xScale(textBox) || 0) + xScale.bandwidth() / 2,
}
: {
...getTextValue(config.xAxisLabel.formatter, xScale, config.xAxisLabel.overflow)(textBox),
x: chartDimensions.left + (xScale(textBox) || 0) + xScale.bandwidth() / 2,
};
markov00 marked this conversation as resolved.
Show resolved Hide resolved
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,47 @@ export function renderCanvas2d(
resultText,
// the alignment for y axis labels is fixed to the right
{ ...config.yAxisLabel, align: 'right' },
config.yAxisLabel.rotation,
);
}),
),

() =>
// render text on X axis
config.xAxisLabel.visible &&
withContext(ctx, () =>
heatmapViewModel.xValues.forEach((xValue) =>
renderText(ctx, { x: xValue.x, y: xValue.y }, xValue.text, config.xAxisLabel),
),
),
withContext(ctx, () => {
// alternate rendering xAxisLabels
return config.xAxisLabel.alternate
? heatmapViewModel.xValues.forEach((xValue, index) => {
if (index % 2 === 0) {
// xAxis label rotations
return config.xAxisLabel.rotation !== 0
? renderText(
ctx,
{ x: xValue.x, y: xValue.y },
xValue.text,
{ ...config.xAxisLabel, align: 'left' },
config.xAxisLabel.rotation,
)
: // no rotations specified
renderText(ctx, { x: xValue.x, y: xValue.y }, xValue.text, config.xAxisLabel);
}
})
: // render all xAxisLabels
heatmapViewModel.xValues.forEach((xValue) =>
// xAxisLabel rotations
config.xAxisLabel.rotation !== 0
? renderText(
ctx,
{ x: xValue.x, y: xValue.y },
xValue.text,
{ ...config.xAxisLabel, align: 'left' },
config.xAxisLabel.rotation,
)
: // no rotations specified
renderText(ctx, { x: xValue.x, y: xValue.y }, xValue.text, config.xAxisLabel),
);
}),
markov00 marked this conversation as resolved.
Show resolved Hide resolved
]);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function renderTickLabel(
);

const center = { x: tickLabelProps.x + tickLabelProps.offsetX, y: tickLabelProps.y + tickLabelProps.offsetY };

if (debug) {
const { maxLabelBboxWidth, maxLabelBboxHeight, maxLabelTextWidth: width, maxLabelTextHeight: height } = dimension;
// full text container
Expand Down
111 changes: 111 additions & 0 deletions storybook/stories/test_cases/9_overlapping_heatmap_labels.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { action } from '@storybook/addon-actions';
import { boolean, number, text } from '@storybook/addon-knobs';
import React from 'react';

import { Chart, Heatmap, Settings } from '@elastic/charts';

import { useBaseTheme } from '../../use_base_theme';

export const Example = () => {
const showLabels = boolean('show', true, 'labels');
const useGlobalMinFontSize = boolean('use global min fontSize', true, 'labels');

const minFontSize = number('min fontSize', 6, { step: 1, min: 4, max: 10, range: true }, 'labels');
const maxFontSize = number('max fontSize', 12, { step: 1, min: 10, max: 64, range: true }, 'labels');

const minCellHeight = number('min cell height', 10, { step: 1, min: 3, max: 8, range: true }, 'grid');
const maxCellHeight = number('max cell height', 30, { step: 1, min: 8, max: 45, range: true }, 'grid');

const setRotation = number('set rotation of x axis label', 45, { step: 1, min: 0, max: 359, range: true }, 'labels');
const allowOverflow = boolean('set overflow property for x axis labels', true, 'labels');
const maxTextLength = text('set the max text length for the x axis labels', '20', 'labels');
const shouldAlternate = boolean('set x axis labels to alternate', false, 'labels');
return (
<Chart>
<Settings
onElementClick={action('onElementClick')}
showLegend
legendPosition="right"
brushAxis="both"
baseTheme={useBaseTheme()}
onBrushEnd={action('onBrushEnd')}
/>
<Heatmap
id="heatmap2"
colorScale={{
type: 'bands',
bands: [
{ start: -Infinity, end: 1000, color: '#AADC32' },
{ start: 1000, end: 5000, color: '#35B779' },
{ start: 5000, end: 10000, color: '#24868E' },
{ start: 10000, end: 50000, color: '#3B528B' },
{ start: 50000, end: Infinity, color: '#471164' },
],
}}
data={[
[1880, 'F', 'Helen Helen Helen Helen Helen', 636, 0.00651612638826278],
[1880, 'F', 'Amanda Amanda Amanda Amanda Amanda', 241, 0.00246916109995492],
[1880, 'F', 'Dorothy Dorothy Dorothy Dorothy Dorothy', 112, 0.00114749395516577],
[1880, 'F', 'Linda Linda Linda Linda Linda Linda', 270, 0.000276628007048891],
[1880, 'F', 'Deborah Deborah Deborah Deborah Deborah Deborah', 12, 0.000122945780910618],
[1880, 'F', 'Jessica', 7, 7.17183721978607e-5],
[1881, 'F', 'Helen', 612, 0.00619088564058469],
[1881, 'F', 'Amanda Amanda Amanda Amanda Amanda', 263, 0.0026604622932578],
[1881, 'F', 'Betty', 112, 0.00113297253553184],
[1881, 'F', 'Dorothy Dorothy Dorothy Dorothy Dorothy', 109, 0.00110262505690152],
[1881, 'F', 'Linda Linda Linda Linda Linda', 38, 0.000384401395984017],
[1881, 'F', 'Deborah Deborah Deborah Deborah', 14, 0.00014162156694148],
]}
xAccessor={(d) => d[2]}
yAccessor={(d) => d[0]}
valueAccessor={(d) => d[3]}
valueFormatter={(value) => value.toFixed(0.2)}
xSortPredicate="alphaAsc"
config={{
grid: {
stroke: {
width: 0,
},
cellHeight: {
min: minCellHeight,
max: maxCellHeight,
},
},
cell: {
maxWidth: 'fill',
label: {
minFontSize,
maxFontSize,
visible: showLabels,
useGlobalMinFontSize,
},
border: {
stroke: 'transparent',
strokeWidth: 1,
},
},
yAxisLabel: {
visible: true,
},
xAxisLabel: {
visible: true,
rotation: setRotation,
overflow: allowOverflow ? 'ellipsis' : false,
maxTextLength: {
max: parseFloat(maxTextLength),
},
alternate: shouldAlternate,
},
}}
/>
</Chart>
);
};
1 change: 1 addition & 0 deletions storybook/stories/test_cases/test_cases.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export { Example as legendScrollBarSizing } from './5_legend_scroll_bar_sizing.s
export { Example as accessibilityCustomizations } from './6_a11y_custom_description.story';
export { Example as rtlText } from './7_rtl_text.story';
export { Example as testPointsOutsideOfDomain } from './8_test_points_outside_of_domain.story';
export { Example as overlappingHeatmapLabels } from './9_overlapping_heatmap_labels.story';