Skip to content

Commit

Permalink
Move axisTitle style to theme
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Dec 20, 2021
1 parent 139b719 commit 8cd19eb
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 25 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.
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.
18 changes: 9 additions & 9 deletions packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,11 @@ export interface HeatmapSpec extends Spec {

// @public (undocumented)
export interface HeatmapStyle {
// (undocumented)
axisTitle: Visible & Font & {
fontSize: Pixels;
padding: Pixels | SimplePadding;
};
brushArea: {
visible: boolean;
fill?: Color;
Expand Down Expand Up @@ -1196,7 +1201,7 @@ export interface HeatmapStyle {
align: TextAlign;
baseline: TextBaseline;
visible: boolean;
padding: number;
padding: Pixels | Padding;
};
// (undocumented)
yAxisLabel: Font & {
Expand All @@ -1206,12 +1211,7 @@ export interface HeatmapStyle {
};
baseline: TextBaseline;
visible: boolean;
padding: number | {
left?: number;
right?: number;
top?: number;
bottom?: number;
};
padding: Pixels | Padding;
};
}

Expand Down Expand Up @@ -2132,9 +2132,9 @@ export type ShowAccessor = (value: PrimitiveValue) => boolean;
// @public
export interface SimplePadding {
// (undocumented)
inner: number;
inner: Pixels;
// (undocumented)
outer: number;
outer: Pixels;
}

// @alpha (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { LinearScale, OrdinalScale, RasterTimeScale } from '../../../../specs';
import { withTextMeasure } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { addIntervalToTime } from '../../../../utils/chrono/elasticsearch';
import { clamp } from '../../../../utils/common';
import { Dimensions, innerPad } from '../../../../utils/dimensions';
import { Dimensions, innerPad, pad } from '../../../../utils/dimensions';
import { Logger } from '../../../../utils/logger';
import { HeatmapStyle, Theme } from '../../../../utils/themes/theme';
import { PrimitiveValue } from '../../../partition_chart/layout/utils/group_by_rollup';
Expand Down Expand Up @@ -56,11 +56,11 @@ function getValuesInRange(
function estimatedNonOverlappingTickCount(
chartWidth: number,
formatter: HeatmapSpec['xAxisLabelFormatter'],
{ padding, fontSize, fontFamily }: HeatmapStyle['xAxisLabel'],
{ fontSize, fontFamily }: HeatmapStyle['xAxisLabel'],
): number {
return withTextMeasure((textMeasure) => {
const labelSample = formatter(Date.now());
const { width } = textMeasure(labelSample, padding, fontSize, fontFamily);
const { width } = textMeasure(labelSample, 0, fontSize, fontFamily);
const maxTicks = 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
Expand Down Expand Up @@ -123,7 +123,7 @@ export function shapeViewModel(
return {
...d,
// position of the Y labels
x: -(typeof padding === 'number' ? padding : padding.right ?? 0),
x: -pad(padding, 'right'),
y: cellHeight / 2 + (yScale(d.value) || 0),
};
});
Expand Down Expand Up @@ -417,13 +417,15 @@ function getXTicks(
formatter: HeatmapSpec['xAxisLabelFormatter'],
scaleCallback: (x: string | number) => number | undefined | null,
) => (value: string | number): TextBox => {
const ip = 6; //innerPad(style.xAxisLabel.padding);
console.log(ip);
return {
text: formatter(value),
value,
isValue: false,
...style.xAxisLabel,
x: scaleCallback(value) ?? 0,
y: style.xAxisLabel.fontSize / 2 + style.xAxisLabel.padding,
y: style.xAxisLabel.fontSize / 2 + ip,
};
};
if (isRasterTimeScale(spec.xScale)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GlobalChartState } from '../../../../state/chart_state';
import { createCustomCachedSelector } from '../../../../state/create_selector';
import { getChartThemeSelector } from '../../../../state/selectors/get_chart_theme';
import { getLegendSizeSelector } from '../../../../state/selectors/get_legend_size';
import { Dimensions, innerPad, outerPad } from '../../../../utils/dimensions';
import { Dimensions, innerPad, outerPad, verticalPad } from '../../../../utils/dimensions';
import { isHorizontalLegend } from '../../../../utils/legend';
import { HeatmapStyle } from '../../../../utils/themes/theme';
import { HeatmapCellDatum } from '../../layout/viewmodel/viewmodel';
Expand Down Expand Up @@ -85,7 +85,9 @@ export const computeChartDimensionsSelector = createCustomCachedSelector(
const yAxisWidth = getYAxisHorizontalUsedSpace(yValues, heatmap.yAxisLabel, textMeasure);

const xAxisTitleVerticalSize = getTextSizeDimension(xAxisLabelName, heatmap.axisTitle, textMeasure, 'height');
const xAxisHeight = heatmap.xAxisLabel.visible ? heatmap.xAxisLabel.fontSize + heatmap.xAxisLabel.padding * 2 : 0;
const xAxisHeight = heatmap.xAxisLabel.visible
? heatmap.xAxisLabel.fontSize + verticalPad(heatmap.xAxisLabel.padding)
: 0;

const availableHeightForGrid = container.height - xAxisTitleVerticalSize - xAxisHeight - legendHeight;

Expand Down Expand Up @@ -118,6 +120,8 @@ export const computeChartDimensionsSelector = createCustomCachedSelector(
left: grid.left,
};

console.log({ grid, container, xAxis, rowHeight });

return { grid, yAxis, xAxis, visibleNumberOfRows, fullHeatmapHeight, rowHeight };
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ScaleType } from '../../../../scales/constants';
import { createCustomCachedSelector } from '../../../../state/create_selector';
import { getChartThemeSelector } from '../../../../state/selectors/get_chart_theme';
import { withTextMeasure } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { horizontalPad } from '../../../../utils/dimensions';
import { getHeatmapSpecSelector } from './get_heatmap_spec';
import { getHeatmapTableSelector } from './get_heatmap_table';

Expand Down Expand Up @@ -40,7 +41,8 @@ export const getXAxisRightOverflow = createCustomCachedSelector(
)
.ticks()
.reduce(
(max, n) => Math.max(max, measure(xAxisLabelFormatter(n), padding, fontSize, fontFamily).width + padding),
(max, n) =>
Math.max(max, measure(xAxisLabelFormatter(n), horizontalPad(padding), fontSize, fontFamily).width),
0,
);
}) / 2;
Expand Down
14 changes: 13 additions & 1 deletion packages/charts/src/utils/dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface PerSideDistance {
export type Margins = PerSideDistance;

/**
* todo seperate type with parition padding type that allows number
* todo separate type with partition padding type that allows number
* @public
*/
export type Padding = PerSideDistance;
Expand All @@ -62,3 +62,15 @@ export const innerPad = (padding: number | Partial<SimplePadding>, minPadding =
/** @internal */
export const outerPad = (padding: number | Partial<SimplePadding>, minPadding = 0) =>
Math.max(minPadding, typeof padding === 'number' ? padding : padding?.outer ?? 0);

/** @internal */
export const verticalPad = (padding: number | Padding, minPadding = 0) =>
Math.max(minPadding, typeof padding === 'number' ? padding * 2 : padding.top + padding.bottom);

/** @internal */
export const horizontalPad = (padding: number | Padding, minPadding = 0) =>
Math.max(minPadding, typeof padding === 'number' ? padding * 2 : padding.left + padding.right);

/** @internal */
export const pad = (padding: number | Padding, direction: keyof Padding, minPadding = 0) =>
Math.max(minPadding, typeof padding === 'number' ? padding : padding[direction]);
4 changes: 2 additions & 2 deletions packages/charts/src/utils/themes/dark_theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export const DARK_THEME: Theme = {
fontWeight: 'normal',
align: 'center',
baseline: 'middle',
padding: 6,
padding: { top: 6, bottom: 6, left: 6, right: 6 },
},
yAxisLabel: {
visible: true,
Expand All @@ -349,7 +349,7 @@ export const DARK_THEME: Theme = {
fontVariant: 'normal',
fontWeight: 'normal',
baseline: 'middle',
padding: 5,
padding: { top: 5, bottom: 5, left: 5, right: 5 },
},
axisTitle: {
visible: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/charts/src/utils/themes/light_theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export const LIGHT_THEME: Theme = {
fontWeight: 'normal',
align: 'center',
baseline: 'middle',
padding: 6,
padding: { top: 6, bottom: 6, left: 6, right: 6 },
},
yAxisLabel: {
visible: true,
Expand All @@ -348,7 +348,7 @@ export const LIGHT_THEME: Theme = {
fontVariant: 'normal',
fontWeight: 'normal',
baseline: 'middle',
padding: 5,
padding: { top: 5, bottom: 5, left: 5, right: 5 },
},
axisTitle: {
visible: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/charts/src/utils/themes/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Color } from '../../common/colors';
import { Pixels, Ratio } from '../../common/geometry';
import { Font, FontStyle, TextAlign, TextBaseline } from '../../common/text_utils';
import { ColorVariant, HorizontalAlignment, RecursivePartial, VerticalAlignment } from '../common';
import { Margins, SimplePadding } from '../dimensions';
import { Margins, Padding, SimplePadding } from '../dimensions';
import { Point } from '../point';
import { PartitionStyle } from './partition';

Expand Down Expand Up @@ -217,14 +217,14 @@ export interface HeatmapStyle {
align: TextAlign;
baseline: TextBaseline;
visible: boolean;
padding: number;
padding: Pixels | Padding;
};
yAxisLabel: Font & {
fontSize: Pixels;
width: Pixels | 'auto' | { max: Pixels };
baseline: TextBaseline;
visible: boolean;
padding: number | { left?: number; right?: number; top?: number; bottom?: number };
padding: Pixels | Padding;
};
axisTitle: Visible & Font & { fontSize: Pixels; padding: Pixels | SimplePadding };
grid: {
Expand Down

0 comments on commit 8cd19eb

Please sign in to comment.