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(axis): scale tick labels to fix text truncation on chrome #38

Merged
merged 4 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions src/lib/axes/canvas_text_bbox_calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ export class CanvasTextBBoxCalculator implements BBoxCalculator {
private attachedRoot: HTMLElement;
private offscreenCanvas: HTMLCanvasElement;
private context: CanvasRenderingContext2D | null;
// TODO specify styles for text
// TODO specify how to hide the svg from the current dom view
// like moving it a -9999999px
private scaledFontSize: number;

constructor(rootElement?: HTMLElement) {
this.offscreenCanvas = document.createElement('canvas');
this.context = this.offscreenCanvas.getContext('2d');
this.attachedRoot = rootElement || document.documentElement;
this.attachedRoot.appendChild(this.offscreenCanvas);
this.scaledFontSize = 100;
}
compute(text: string, fontSize = 16, fontFamily = 'Arial'): Option<BBox> {
if (!this.context) {
return none;
}
this.context.font = `${fontSize}px ${fontFamily}`;

// We scale the text up to get a more accurate computation of the width of the text
// because `measureText` can vary a lot between browsers.
const scalingFactor = this.scaledFontSize / fontSize;
this.context.font = `${this.scaledFontSize}px ${fontFamily}`;
const measure = this.context.measureText(text);

return some({
width: measure.width,
width: measure.width / scalingFactor,
height: fontSize,
});
}
Expand Down
25 changes: 25 additions & 0 deletions src/stories/axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '..';
import { PartialTheme } from '../lib/themes/theme';
import { LineSeries } from '../specs';
import { DataGenerator } from '../utils/data_generators/data_generator';

function createThemeAction(title: string, min: number, max: number, value: number) {
return number(title, value, {
Expand Down Expand Up @@ -266,4 +267,28 @@ storiesOf('Axis', module)
/>
</Chart>
);
})
.add('w many tick labels', () => {
const dg = new DataGenerator();
const data = dg.generateSimpleSeries(31);
return (
<Chart renderer="canvas" className={'story-chart'}>
<Settings debug={true} />
<Axis
id={getAxisId('bottom')}
position={Position.Bottom}
title={'Bottom axis'}
showOverlappingTicks={true}
/>
<AreaSeries
id={getSpecId('lines')}
xScaleType={ScaleType.Linear}
yScaleType={ScaleType.Linear}
xAccessor="x"
yAccessors={['y']}
data={data}
yScaleToDataExtent={false}
/>
</Chart>
);
});