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

refactor(xy): axis title and related simplifications #1324

Merged
merged 66 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
568f17d
refactor: axis size dry up
monfera Aug 23, 2021
df378b5
refactor: fewer branching in getSimplePadding
monfera Aug 23, 2021
0482f91
refactor: axis size dry up 2 and minor perf
monfera Aug 23, 2021
e12145c
chore: removed outdated comment discussed with Marco
monfera Aug 23, 2021
7c23762
chore: no need for padding object
monfera Aug 23, 2021
dadd6b7
refactor: removed redundant predicates
monfera Aug 23, 2021
95f05e5
refactor: debug rect arg order and ctx removals and misc changes
monfera Aug 23, 2021
5a3c665
refactor: unified title renderer
monfera Aug 23, 2021
b4ee675
refactor: unified title renderer 2
monfera Aug 23, 2021
78f1c47
refactor: unified title renderer 3 - common ordering
monfera Aug 23, 2021
a9605f2
refactor: unified title renderer 4 - unifying horiz vert 2
monfera Aug 23, 2021
acb19df
refactor: unified title renderer 5 - unifying files
monfera Aug 23, 2021
12d7abb
fix: undid regression in unified title renderer 4
monfera Aug 23, 2021
a38991f
refactor: minor
monfera Aug 23, 2021
adc9aa1
refactor: unified title renderer 6
monfera Aug 23, 2021
f8bcd2b
refactor: unified title renderer 7
monfera Aug 23, 2021
e62302e
refactor: unified title renderer 8
monfera Aug 23, 2021
8b6cbd5
refactor: unified title renderer 9 - single renderer
monfera Aug 23, 2021
0833c7d
refactor: dry rotation
monfera Aug 23, 2021
4992a7e
refactor: textX textY cleanup
monfera Aug 23, 2021
6a49f28
refactor: unneeded let
monfera Aug 23, 2021
042bb5d
refactor: defer calc past bail branches
monfera Aug 23, 2021
583fc59
refactor: rename to textLineOrigin
monfera Aug 23, 2021
da326f1
refactor: textLineOrigin simplified
monfera Aug 23, 2021
6699e29
refactor: textLineOrigin destructuring simplified
monfera Aug 23, 2021
c364311
refactor: dryed up multiplicated switch case structures
monfera Aug 23, 2021
bd77ffd
refactor: expression based isOverflow
monfera Aug 23, 2021
5fd242c
refactor: dried up some overflow
monfera Aug 23, 2021
de674ba
chore: destruct in parameter list
monfera Aug 24, 2021
59a409d
refactor: withCanvasTextBBoxCalculator first cut
monfera Aug 24, 2021
77539ba
refactor: rename ht Marco
monfera Aug 24, 2021
cef746e
refactor: switch a function to withMeasureText and remove svg measurer
monfera Aug 24, 2021
3ec0b5b
test: canvas vs svg measurer difference fixed
monfera Aug 24, 2021
6b84c6f
refactor: two more conversions to withMeasure
monfera Aug 24, 2021
e8b2e54
refactor: bars converted to withMeasure
monfera Aug 24, 2021
570ac6b
refactor: legendSize to withMeasure
monfera Aug 24, 2021
688a8fd
refactor: last CanvasTextBBoxCalculator gone
monfera Aug 24, 2021
b825655
refactor: deleted dom measurer and consolidated files
monfera Aug 24, 2021
0fc5da1
refactor: measurer superficial simplification
monfera Aug 24, 2021
7338d0f
refactor: measurer no longer using a class object
monfera Aug 24, 2021
6293798
refactor: earlier test for no canvas context
monfera Aug 24, 2021
06618b5
chore: simpler styling
monfera Aug 24, 2021
7e864ef
refactor: bar renderer switching to reduce
monfera Aug 24, 2021
c816cc6
chore: less scrolling
monfera Aug 24, 2021
d228c61
refactor: super minor extractions
monfera Aug 24, 2021
5ebf663
chore: removed husky altogether
monfera Aug 24, 2021
671f117
Merge branch 'master' into time-axis-03
monfera Aug 24, 2021
a211683
Merge remote-tracking branch 'origin/master' into time-axis-03
monfera Aug 24, 2021
87d1ed1
refactor: minor code touches to bars
monfera Aug 24, 2021
e676dd4
refactor: bail early
monfera Aug 24, 2021
51c724f
refactor: y0Scaled is now a const
monfera Aug 24, 2021
b28d72e
refactor: split dual-variable if
monfera Aug 24, 2021
8817047
refactor: height variants are now constants
monfera Aug 24, 2021
f317114
refactor: sign of rawHeight is the same
monfera Aug 24, 2021
84c8236
refactor: removed provably inconsequential calculations
monfera Aug 24, 2021
0dc4962
refactor: last let in renderBars is gone
monfera Aug 24, 2021
87c5219
refactor: minor rename
monfera Aug 24, 2021
d214f2c
refactor: phased out legacy triplicated functions in favor of the sta…
monfera Aug 24, 2021
5a37565
fix: array sort requires all three cases
monfera Aug 24, 2021
51d55de
fix: min height pr regression plus optionality simplifications
monfera Aug 24, 2021
afb5529
refactor: moved bail condition upstream
monfera Aug 24, 2021
6cf997a
chore: renovate json prettier fix due to failing master
monfera Aug 24, 2021
9fc7a37
Merge remote-tracking branch 'origin/master' into time-axis-03
monfera Aug 24, 2021
3d5d045
chore: renovate json prettier fix due to failing master
monfera Aug 24, 2021
d7a68f6
chore: fix prettier issues
nickofthyme Aug 24, 2021
24c939f
refactor: linear processing and no box mutation
monfera Aug 24, 2021
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
4 changes: 1 addition & 3 deletions .huskyrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = {
hooks: {
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS && yarn typecheck:all && yarn pq && lint-staged',
},
hooks: {},
markov00 marked this conversation as resolved.
Show resolved Hide resolved
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Box, TextMeasure } from '../../../../common/text_utils';
import { ScaleContinuous } from '../../../../scales';
import { ScaleType } from '../../../../scales/constants';
import { SettingsSpec } from '../../../../specs';
import { CanvasTextBBoxCalculator } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { withTextMeasure } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { snapDateToESInterval } from '../../../../utils/chrono/elasticsearch';
import { clamp, range } from '../../../../utils/common';
import { Dimensions } from '../../../../utils/dimensions';
Expand Down Expand Up @@ -56,21 +56,16 @@ function getValuesInRange(
/**
* Resolves the maximum number of ticks based on the chart width and sample label based on formatter config.
*/
function getTicks(chartWidth: number, xAxisLabelConfig: Config['xAxisLabel']): number {
const bboxCompute = new CanvasTextBBoxCalculator();
const labelSample = xAxisLabelConfig.formatter(Date.now());
const { width } = bboxCompute.compute(
labelSample,
xAxisLabelConfig.padding,
xAxisLabelConfig.fontSize,
xAxisLabelConfig.fontFamily,
);
bboxCompute.destroy();
const maxTicks = Math.floor(chartWidth / width);
// Dividing by 2 is a temp fix to make sure {@link ScaleContinuous} won't produce
// to many ticks creating nice rounded tick values
// TODO add support for limiting the number of tick in {@link ScaleContinuous}
return maxTicks / 2;
function getTicks(chartWidth: number, { formatter, padding, fontSize, fontFamily }: Config['xAxisLabel']): number {
return withTextMeasure((textMeasure) => {
const labelSample = formatter(Date.now());
const { width } = textMeasure(labelSample, padding, fontSize, fontFamily);
const maxTicks = Math.floor(chartWidth / width);
// Dividing by 2 is a temp fix to make sure {@link ScaleContinuous} won't produce
// to many ticks creating nice rounded tick values
// TODO add support for limiting the number of tick in {@link ScaleContinuous}
return maxTicks / 2;
});
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { ScaleContinuous } from '../../../../scales';
import { ScaleType } from '../../../../scales/constants';
import { createCustomCachedSelector } from '../../../../state/create_selector';
import { CanvasTextBBoxCalculator } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { withTextMeasure } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { getHeatmapConfigSelector } from './get_heatmap_config';
import { getHeatmapTableSelector } from './get_heatmap_table';

Expand Down Expand Up @@ -37,13 +37,11 @@ export const getXAxisRightOverflow = createCustomCachedSelector(
timeZone,
},
);
const bboxCompute = new CanvasTextBBoxCalculator();
const maxTextWidth = timeScale.ticks().reduce((acc, d) => {
const text = formatter(d);
const textSize = bboxCompute.compute(text, padding, fontSize, fontFamily, 1);
return Math.max(acc, textSize.width + padding);
}, 0);
bboxCompute.destroy();
return maxTextWidth / 2;
return withTextMeasure(
(textMeasure) =>
timeScale.ticks().reduce((acc, d) => {
return Math.max(acc, textMeasure(formatter(d), padding, fontSize, fontFamily, 1).width + padding);
}, 0) / 2,
);
},
);
78 changes: 27 additions & 51 deletions packages/charts/src/chart_types/xy_chart/axes/axes_sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

import { SmallMultiplesSpec } from '../../../specs';
import { Position } from '../../../utils/common';
import { getSimplePadding, PerSideDistance } from '../../../utils/dimensions';
import { innerPad, outerPad, PerSideDistance } from '../../../utils/dimensions';
import { AxisId } from '../../../utils/ids';
import { AxisStyle, Theme } from '../../../utils/themes/theme';
import { getSpecsById } from '../state/utils/spec';
import { isVerticalAxis } from '../utils/axis_type_utils';
import { AxisViewModel, getTitleDimension, shouldShowTicks } from '../utils/axis_utils';
import { AxisSpec } from '../utils/specs';

const nullPadding = (): PerSideDistance => ({ left: 0, right: 0, top: 0, bottom: 0 });

/** @internal */
export function computeAxesSizes(
{ axes: sharedAxesStyles, chartMargins }: Theme,
Expand All @@ -26,65 +24,43 @@ export function computeAxesSizes(
axisSpecs: AxisSpec[],
smSpec?: SmallMultiplesSpec,
): PerSideDistance & { margin: { left: number } } {
const axisMainSize = nullPadding();
const axisLabelOverflow = nullPadding();
const axisMainSize: PerSideDistance = { left: 0, right: 0, top: 0, bottom: 0 };
const axisLabelOverflow: PerSideDistance = { left: 0, right: 0, top: 0, bottom: 0 };

axisDimensions.forEach(({ maxLabelBboxWidth = 0, maxLabelBboxHeight = 0, isHidden }, id) => {
const axisSpec = getSpecsById<AxisSpec>(axisSpecs, id);
if (!axisSpec || isHidden) {
if (isHidden || !axisSpec) {
return;
}
const { tickLine, axisTitle, axisPanelTitle, tickLabel } = axesStyles.get(id) ?? sharedAxesStyles;
const showTicks = shouldShowTicks(tickLine, axisSpec.hide);
const { position, title } = axisSpec;
const labelPadding = getSimplePadding(tickLabel.padding);
const labelPaddingSum = tickLabel.visible ? labelPadding.inner + labelPadding.outer : 0;

const tickDimension = showTicks ? tickLine.size + tickLine.padding : 0;
const titleDimension = title ? getTitleDimension(axisTitle) : 0;
const { tickLine, axisTitle, axisPanelTitle, tickLabel } = axesStyles.get(id) ?? sharedAxesStyles;
const hasPanelTitle = Boolean(isVerticalAxis(position) ? smSpec?.splitVertically : smSpec?.splitHorizontally);
const panelTitleDimension = hasPanelTitle ? getTitleDimension(axisPanelTitle) : 0;
const labelPaddingSum = tickLabel.visible ? innerPad(tickLabel.padding) + outerPad(tickLabel.padding) : 0;
const titleDimension = title ? getTitleDimension(axisTitle) : 0;
const tickDimension = shouldShowTicks(tickLine, axisSpec.hide) ? tickLine.size + tickLine.padding : 0;
const axisDimension = labelPaddingSum + tickDimension + titleDimension + panelTitleDimension;
const maxAxisHeight = tickLabel.visible ? maxLabelBboxHeight + axisDimension : axisDimension;
const maxAxisWidth = tickLabel.visible ? maxLabelBboxWidth + axisDimension : axisDimension;

switch (position) {
case Position.Top:
axisMainSize.top += maxAxisHeight + chartMargins.top;
// find the max half label size to accommodate the left/right labels
// TODO use first and last labels
axisLabelOverflow.left = Math.max(axisLabelOverflow.left, maxLabelBboxWidth / 2);
axisLabelOverflow.right = Math.max(axisLabelOverflow.right, maxLabelBboxWidth / 2);
break;
case Position.Bottom:
axisMainSize.bottom += maxAxisHeight + chartMargins.bottom;
// find the max half label size to accommodate the left/right labels
// TODO use first and last labels
axisLabelOverflow.left = Math.max(axisLabelOverflow.left, maxLabelBboxWidth / 2);
axisLabelOverflow.right = Math.max(axisLabelOverflow.right, maxLabelBboxWidth / 2);
break;
case Position.Right:
axisMainSize.right += maxAxisWidth + chartMargins.right;
// TODO use first and last labels
axisLabelOverflow.top = Math.max(axisLabelOverflow.top, maxLabelBboxHeight / 2);
axisLabelOverflow.bottom = Math.max(axisLabelOverflow.bottom, maxLabelBboxHeight / 2);
break;
case Position.Left:
default:
axisMainSize.left += maxAxisWidth + chartMargins.left;
// TODO use first and last labels
axisLabelOverflow.top = Math.max(axisLabelOverflow.top, maxLabelBboxHeight / 2);
axisLabelOverflow.bottom = Math.max(axisLabelOverflow.bottom, maxLabelBboxHeight / 2);
// TODO use first and last labels
if (isVerticalAxis(position)) {
axisLabelOverflow.top = Math.max(axisLabelOverflow.top, maxLabelBboxHeight / 2);
axisLabelOverflow.bottom = Math.max(axisLabelOverflow.bottom, maxLabelBboxHeight / 2);
const maxAxisWidth = axisDimension + (tickLabel.visible ? maxLabelBboxWidth : 0);
axisMainSize.left += position === Position.Left ? maxAxisWidth + chartMargins.left : 0;
axisMainSize.right += position === Position.Right ? maxAxisWidth + chartMargins.right : 0;
} else {
// find the max half label size to accommodate the left/right labels
axisLabelOverflow.left = Math.max(axisLabelOverflow.left, maxLabelBboxWidth / 2);
axisLabelOverflow.right = Math.max(axisLabelOverflow.right, maxLabelBboxWidth / 2);
const maxAxisHeight = axisDimension + (tickLabel.visible ? maxLabelBboxHeight : 0);
axisMainSize.top += position === Position.Top ? maxAxisHeight + chartMargins.top : 0;
axisMainSize.bottom += position === Position.Bottom ? maxAxisHeight + chartMargins.bottom : 0;
}
});

const left = Math.max(axisLabelOverflow.left + chartMargins.left, axisMainSize.left);
return {
margin: {
left: left - axisMainSize.left,
},
left,
right: Math.max(axisLabelOverflow.right + chartMargins.right, axisMainSize.right),
top: Math.max(axisLabelOverflow.top + chartMargins.top, axisMainSize.top),
bottom: Math.max(axisLabelOverflow.bottom + chartMargins.bottom, axisMainSize.bottom),
};
const right = Math.max(axisLabelOverflow.right + chartMargins.right, axisMainSize.right);
const top = Math.max(axisLabelOverflow.top + chartMargins.top, axisMainSize.top);
const bottom = Math.max(axisLabelOverflow.bottom + chartMargins.bottom, axisMainSize.bottom);
return { left, right, top, bottom, margin: { left: left - axisMainSize.left } };
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export function renderLineAnnotations(
dash: lineStyle.line.dash,
};

annotations.forEach(({ linePathPoints, panel }) => {
withPanelTransform(ctx, panel, rotation, renderingArea, (ctx) => {
renderMultiLine(ctx, [linePathPoints], stroke);
});
});
annotations.forEach(({ linePathPoints, panel }) =>
withPanelTransform(ctx, panel, rotation, renderingArea, () => renderMultiLine(ctx, [linePathPoints], stroke)),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,12 @@ export function renderRectAnnotations(
) {
const fillColor = stringToRGB(rectStyle.fill);
fillColor.opacity *= rectStyle.opacity;
const fill: Fill = {
color: fillColor,
};
const fill: Fill = { color: fillColor };
const strokeColor = stringToRGB(rectStyle.stroke);
strokeColor.opacity *= rectStyle.opacity;
const stroke: Stroke = {
color: strokeColor,
width: rectStyle.strokeWidth,
};
const stroke: Stroke = { color: strokeColor, width: rectStyle.strokeWidth };

const rectsLength = annotations.length;

for (let i = 0; i < rectsLength; i++) {
const { rect, panel } = annotations[i];
withPanelTransform(ctx, panel, rotation, renderingArea, (ctx) => {
renderRect(ctx, rect, fill, stroke);
});
}
annotations.forEach(({ rect, panel }) =>
withPanelTransform(ctx, panel, rotation, renderingArea, () => renderRect(ctx, rect, fill, stroke)),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ interface GridProps {
}

/** @internal */
export function renderGrids(ctx: CanvasRenderingContext2D, props: GridProps) {
const {
perPanelGridLines,
renderingArea: { left, top },
} = props;
export function renderGrids(
ctx: CanvasRenderingContext2D,
{ perPanelGridLines, renderingArea: { left, top } }: GridProps,
) {
withContext(ctx, () => {
ctx.translate(left, top);

perPanelGridLines.forEach(({ lineGroups, panelAnchor: { x, y } }) => {
withContext(ctx, () => {
ctx.translate(x, y);
Expand Down

This file was deleted.

Loading