Skip to content

Commit

Permalink
add useGlobalMinFontSize option
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Sep 6, 2021
1 parent 9017878 commit 3c2dc77
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 7 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.
8 changes: 7 additions & 1 deletion integration/tests/heatmap_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ describe('Heatmap stories', () => {
{ left: 300, top: 300 },
);
});
it('should maximize the label fontSize', async () => {
it('should maximize the label with an unique fontSize', async () => {
await page.setViewport({ width: 420, height: 600 });
await common.expectChartAtUrlToMatchScreenshot('http://localhost:9001/?path=/story/heatmap-alpha--categorical');
});
it('should maximize the label fontSize', async () => {
await page.setViewport({ width: 420, height: 600 });
await common.expectChartAtUrlToMatchScreenshot(
'http://localhost:9001/?path=/story/heatmap-alpha--categorical&knob-use global min fontSize_labels=false',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const config: Config = {
fontVariant: 'normal',
fontWeight: 'normal',
textOpacity: 1,
useGlobalMinFontSize: true,
},
border: {
strokeWidth: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface Config {
label: Font & {
minFontSize: Pixels;
maxFontSize: Pixels;
useGlobalMinFontSize: boolean;
maxWidth: Pixels | 'fill';
visible: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface HeatmapViewModel {
stroke: Stroke;
};
cells: Cell[];
cellFontSize: (c: Cell) => Pixels;
xValues: Array<TextBox>;
yValues: Array<TextBox>;
pageSize: number;
Expand Down Expand Up @@ -117,6 +118,7 @@ export const nullHeatmapViewModel: HeatmapViewModel = {
xValues: [],
yValues: [],
pageSize: 0,
cellFontSize: () => 0,
};

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ export function shapeViewModel(
config.cell.label,
config.cell.label.minFontSize,
config.cell.label.maxFontSize,
// adding 4px padding per side to avoid that text touches the edges
cellWidthInner - 8,
cellHeightInner - 8,
// adding 3px padding per side to avoid that text touches the edges
cellWidthInner - 6,
cellHeightInner - 6,
);

acc[cellKey] = {
Expand Down Expand Up @@ -373,6 +373,9 @@ export function shapeViewModel(
yLines.push({ x1: chartDimensions.left, y1: y, x2: chartDimensions.width + chartDimensions.left, y2: y });
}

const cells = Object.values(cellMap);
const tableMinFontSize = cells.reduce((acc, { fontSize }) => Math.min(acc, fontSize), Infinity);

return {
config,
heatmapViewModel: {
Expand All @@ -389,7 +392,8 @@ export function shapeViewModel(
},
},
pageSize,
cells: Object.values(cellMap),
cells,
cellFontSize: (cell: Cell) => (config.cell.label.useGlobalMinFontSize ? tableMinFontSize : cell.fontSize),
xValues: textXValues,
yValues: textYValues,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ export function renderCanvas2d(
const { x, y } = heatmapViewModel.gridOrigin;
ctx.translate(x, y);
filteredCells.forEach((cell) => {
if (cell.visible && Number.isFinite(cell.fontSize))
const fontSize = heatmapViewModel.cellFontSize(cell);
if (cell.visible && Number.isFinite(fontSize))
renderText(ctx, { x: cell.x + cell.width / 2, y: cell.y + cell.height / 2 }, cell.formatted, {
...config.cell.label,
fontSize: cell.fontSize,
fontSize,
align: 'center',
baseline: 'middle',
});
Expand Down
2 changes: 2 additions & 0 deletions storybook/stories/heatmap/2_categorical.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useBaseTheme } from '../../use_base_theme';
export const Example = () => {
const data = BABYNAME_DATA.filter(([year]) => year > 1950 && year < 1960);
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');
Expand Down Expand Up @@ -68,6 +69,7 @@ export const Example = () => {
minFontSize,
maxFontSize,
visible: showLabels,
useGlobalMinFontSize,
},
border: {
stroke: 'transparent',
Expand Down

0 comments on commit 3c2dc77

Please sign in to comment.