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

fix(heatmap): x-axis labels overlapping for time series data #850

Merged
merged 1 commit into from
Oct 6, 2020
Merged
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
38 changes: 20 additions & 18 deletions src/chart_types/heatmap/layout/viewmodel/viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { scaleBand, scaleQuantize } from 'd3-scale';
import { ScaleContinuous } from '../../../../scales';
import { ScaleType } from '../../../../scales/constants';
import { SettingsSpec } from '../../../../specs';
import { CanvasTextBBoxCalculator } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { Dimensions } from '../../../../utils/dimensions';
import { Pixels } from '../../../partition_chart/layout/types/geometry_types';
import { Box, TextMeasure } from '../../../partition_chart/layout/types/types';
Expand Down Expand Up @@ -53,23 +54,24 @@ export interface TextBox extends Box {
y: number;
}

function getTicksPerWidth(width: number) {
if (width <= 100) {
return 0;
}
if (width > 100 && width <= 200) {
return 1;
}
if (width > 200 && width <= 300) {
return 2;
}
if (width > 300 && width <= 500) {
return 3;
}
if (width > 500 && width < 700) {
return 5;
}
return 10;
/**
* 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;
}

/** @internal */
Expand Down Expand Up @@ -130,7 +132,7 @@ export function shapeViewModel(
range: [0, maxGridAreaWidth],
},
{
ticks: getTicksPerWidth(chartDimensions.width),
ticks: getTicks(chartDimensions.width, config.xAxisLabel),
timeZone: config.timeZone,
},
)
Expand Down