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

AG-13345: Last label on axis gets clipped #3436

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
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
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 31 additions & 37 deletions packages/ag-charts-community/src/chart/axis/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ export abstract class Axis<
false
);

get labelNodes() {
return this.tickLabelGroupSelection.nodes();
}

private _crossLines: CrossLine[] = [];
set crossLines(value: CrossLine[]) {
const { CrossLineConstructor } = this.constructor as typeof Axis;
Expand Down Expand Up @@ -221,8 +225,8 @@ export abstract class Axis<

protected axisContext: AxisContext | undefined = undefined;

private labelFormatter: ((datum: any) => string) | undefined = undefined;
private datumFormatter: ((datum: any) => string) | undefined = undefined;
private labelFormatter: ((datum: unknown) => string) | undefined = undefined;
private datumFormatter: ((datum: unknown) => string) | undefined = undefined;
private scaleFormatterParams: CrosslineFormatterParams<D> | undefined = undefined;

protected readonly destroyFns: Array<() => void> = [];
Expand Down Expand Up @@ -256,7 +260,7 @@ export abstract class Axis<
this.destroyFns.forEach((f) => f());
}

protected updateRange() {
protected updateScale() {
const { range: rr, visibleRange: vr, scale } = this;
const span = (rr[1] - rr[0]) / (vr[1] - vr[0]);
const shift = span * vr[0];
Expand Down Expand Up @@ -309,11 +313,11 @@ export abstract class Axis<
return value >= min - tolerance && value <= max + tolerance;
}

protected defaultDatumFormatter(datum: any, fractionDigits: number): string {
protected defaultDatumFormatter(datum: unknown, fractionDigits: number): string {
return formatValue(datum, fractionDigits + 1);
}

protected defaultLabelFormatter(datum: any, fractionDigits: number): string {
protected defaultLabelFormatter(datum: unknown, fractionDigits: number): string {
return formatValue(datum, fractionDigits);
}

Expand Down Expand Up @@ -403,7 +407,7 @@ export abstract class Axis<
fontWeight,
spacing,
} = mergeDefaults(stylerOutput, additionalStyles, defaultStyle);
return { fill, fontFamily, fontSize, fontStyle, fontWeight, spacing: spacing };
return { fill, fontFamily, fontSize, fontStyle, fontWeight, spacing };
}

protected getTickSize() {
Expand All @@ -419,12 +423,12 @@ export abstract class Axis<
return;
}

caption.enabled = true;
caption.color = title.color;
caption.fontFamily = title.fontFamily;
caption.fontSize = title.fontSize;
caption.fontStyle = title.fontStyle;
caption.fontWeight = title.fontWeight;
caption.enabled = title.enabled;
caption.wrapping = title.wrapping;

const titleNode = caption.node;
Expand Down Expand Up @@ -456,30 +460,23 @@ export abstract class Axis<
this.setDomains(...domains);
}

private _lastDomain: D[] | undefined = undefined;
protected animatable = true;
setDomains(...domains: D[][]) {
const { domain, animatable } = this.scale.normalizeDomains(...domains);

if (this._lastDomain !== domain) {
this.dataDomain = this.normaliseDataDomain(domain);
this.dataDomain = this.normaliseDataDomain(domain);

if (this.reverse) {
this.dataDomain.domain = this.dataDomain.domain.slice().reverse();
}
if (this.reverse) {
this.dataDomain.domain.reverse();
}

this._lastDomain = domain;
this.animatable = animatable;
}

_scaleNiceDomainRangeExtent: number = NaN;
calculateLayout(initialPrimaryTickCount?: number) {
const { scale, label, visibleRange, nice } = this;

const { rotation, parallelFlipRotation, regularFlipRotation } = this.calculateRotations();
const sideFlag = this.label.getSideFlag();

this.updateScale();

const rangeExtent = findRangeExtent(this.range);
Expand Down Expand Up @@ -518,10 +515,10 @@ export abstract class Axis<
const specifier = label.format;
this.labelFormatter =
scale.tickFormatter({ domain: tickDomain, specifier, ticks, fractionDigits }) ??
((x: any) => this.defaultLabelFormatter(x, fractionDigits));
((value: unknown) => this.defaultLabelFormatter(value, fractionDigits));
this.datumFormatter =
scale.datumFormatter({ domain: tickDomain, specifier, ticks, fractionDigits }) ??
((x: any) => this.defaultDatumFormatter(x, fractionDigits));
((value: unknown) => this.defaultDatumFormatter(value, fractionDigits));
this.scaleFormatterParams = { domain: tickDomain, ticks, fractionDigits };

this.layout.label = {
Expand All @@ -530,13 +527,15 @@ export abstract class Axis<
format: this.label.format,
};

const sideFlag = label.getSideFlag();
const anySeriesActive = this.isAnySeriesActive();
const { rotation, parallelFlipRotation, regularFlipRotation } = this.calculateRotations();

this.crossLines.forEach((crossLine) => {
crossLine.sideFlag = -sideFlag as ChartAxisLabelFlipFlag;
crossLine.direction = rotation === -Math.PI / 2 ? ChartAxisDirection.X : ChartAxisDirection.Y;
if (crossLine instanceof CartesianCrossLine) {
crossLine.label.parallel ??= this.label.parallel;
crossLine.label.parallel ??= label.parallel;
}
crossLine.parallelFlipRotation = parallelFlipRotation;
crossLine.regularFlipRotation = regularFlipRotation;
Expand Down Expand Up @@ -567,10 +566,6 @@ export abstract class Axis<
return matrix.transformBBox(bbox);
}

updateScale() {
this.updateRange();
}

protected calculateRotations() {
const rotation = toRadians(this.rotation);
// When labels are parallel to the axis line, the `parallelFlipFlag` is used to
Expand Down Expand Up @@ -653,7 +648,12 @@ export abstract class Axis<
protected abstract updateLabels(): void;

// For formatting (nice rounded) tick values.
formatTick(value: any, index: number, fractionDigits?: number, defaultFormatter?: (datum: any) => string): string {
formatTick(
value: unknown,
index: number,
fractionDigits?: number,
defaultFormatter?: (datum: unknown) => string
): string {
const {
labelFormatter,
label: { formatter },
Expand All @@ -672,7 +672,7 @@ export abstract class Axis<
}

// For formatting arbitrary values between the ticks.
formatDatum(value: any): string {
formatDatum(value: unknown): string {
const {
label: { formatter },
moduleCtx: { callbackCache },
Expand All @@ -691,10 +691,10 @@ export abstract class Axis<
return String(result ?? value);
}

private getScaleValueFormatter(format?: string): (value: any) => string {
private getScaleValueFormatter(format?: string): (value: unknown) => string {
const { scaleFormatterParams } = this;

let formatter: ((value: any) => string) | undefined;
let formatter: ((value: unknown) => string) | undefined;
try {
if (format != null && scaleFormatterParams != null) {
formatter = this.scale.tickFormatter({ ...scaleFormatterParams, specifier: format });
Expand All @@ -703,7 +703,7 @@ export abstract class Axis<
Logger.warnOnce(`the format string ${format} is invalid, ignoring.`);
}

formatter ??= (value: any) => this.formatDatum(value);
formatter ??= (value: unknown) => this.formatDatum(value);

return formatter;
}
Expand Down Expand Up @@ -784,15 +784,9 @@ export abstract class Axis<
seriesKeyProperties: () =>
this.boundSeries.reduce((keys, series) => {
const seriesKeys = series.getKeyProperties(this.direction);

seriesKeys.forEach((key) => {
if (keys.indexOf(key) < 0) {
keys.push(key);
}
});

seriesKeys.forEach((key) => keys.add(key));
return keys;
}, [] as string[]),
}, new Set<string>()),
scaleValueFormatter: (specifier?: string) => this.getScaleValueFormatter(specifier),
scaleInvert: (val) => scale.invert(val, true),
scaleInvertNearest: (val) => scale.invert(val, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CategoryAxis<
return { domain, clipped: false };
}

override updateScale() {
protected override updateScale() {
super.updateScale();

let { paddingInner, paddingOuter } = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ export class GroupedCategoryAxis extends CategoryAxis {
private computeLayout() {
this.updateDirection();
this.updateScale();
this.updateRange();
this.resizeTickTree();

if (!this.tickTreeLayout?.depth) {
Expand Down Expand Up @@ -438,9 +437,11 @@ export class GroupedCategoryAxis extends CategoryAxis {
this.tickLabelGroupSelection.clear();
}

protected override updateRange() {
super.updateRange();
protected override updateScale() {
super.updateScale();
this.tickScale.range = this.scale.range;
// Outer padding must equal half inner padding to keep groups center point aligned.
this.scale.paddingOuter = this.scale.paddingInner / 2;
}

override processData() {
Expand Down Expand Up @@ -490,12 +491,6 @@ export class GroupedCategoryAxis extends CategoryAxis {
});
}

override updateScale(): void {
super.updateScale();
// Outer padding must equal half inner padding to keep groups center point aligned.
this.scale.paddingOuter = this.scale.paddingInner / 2;
}

filterDuplicateArrays(array: string[][]): string[][] {
const seen = new Set<string>();
return array.filter((item) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/ag-charts-community/src/chart/cartesianChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ export class CartesianChart extends Chart {
seriesRect.width + gridLinePadding * 2,
layoutBBox.height + gridLinePadding
);

const lastLabel = axis.label.enabled ? axis.labelNodes.at(-1) : null;
if (lastLabel) {
const labelBBox = lastLabel.getBBox();
lastLabel.visible =
seriesRect.x + labelBBox.y + labelBBox.height <=
layoutBBox.x + layoutBBox.width + this.padding.right;
}
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/ag-charts-community/src/chart/chartAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { ModuleMap } from '../module/moduleMap';
import type { Scale } from '../scale/scale';
import type { BBox } from '../scene/bbox';
import type { Node } from '../scene/node';
import type { TransformableText } from '../scene/shape/text';
import type { AxisGridLine } from './axis/axisGridLine';
import type { AxisLine } from './axis/axisLine';
import type { AxisTick, TickInterval } from './axis/axisTick';
Expand Down Expand Up @@ -93,6 +94,7 @@ export interface ChartAxis {
translation: { x: number; y: number };
type: string;
visibleRange: [number, number];
labelNodes: TransformableText[];
}

export interface ChartAxisLabel extends FontOptions {
Expand Down
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.
2 changes: 1 addition & 1 deletion packages/ag-charts-community/src/module/axisContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface AxisContext {
position?: AgCartesianAxisPosition;
scale: Scale<any, any, any>;
getCanvasBounds(): BBoxValues | undefined;
seriesKeyProperties(): string[];
seriesKeyProperties(): Set<string>;
scaleInvert(position: number): any;
scaleInvertNearest(position: number): any;
scaleValueFormatter(specifier?: string): (x: any) => string;
Expand Down
21 changes: 9 additions & 12 deletions packages/ag-charts-community/src/scene/matrix.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { isNumberEqual } from '../util/number';
import { BBox } from './bbox';

export const IDENTITY_MATRIX_ELEMENTS: readonly number[] = [1, 0, 0, 1, 0, 0];

function closeValue(val: number, ref: number, errorMargin = 1e-8) {
return val === ref || Math.abs(ref - val) < errorMargin;
}

/**
* As of Jan 8, 2019, Firefox still doesn't implement
* `getTransform(): DOMMatrix;`
Expand All @@ -31,8 +28,8 @@ export class Matrix {
return [...this.elements];
}

constructor(elements: number[] = [...IDENTITY_MATRIX_ELEMENTS]) {
this.elements = elements;
constructor(elements: readonly number[] = IDENTITY_MATRIX_ELEMENTS) {
this.elements = [...elements];
}

setElements(elements: readonly number[]): this {
Expand Down Expand Up @@ -63,12 +60,12 @@ export class Matrix {
get identity(): boolean {
const e = this.elements;
return (
closeValue(e[0], 1) &&
closeValue(e[1], 0) &&
closeValue(e[2], 0) &&
closeValue(e[3], 1) &&
closeValue(e[4], 0) &&
closeValue(e[5], 0)
isNumberEqual(e[0], 1) &&
isNumberEqual(e[1], 0) &&
isNumberEqual(e[2], 0) &&
isNumberEqual(e[3], 1) &&
isNumberEqual(e[4], 0) &&
isNumberEqual(e[5], 0)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ag-charts-community/src/util/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function inRange(value: number, range: [number, number], epsilon: number
}

export function isNumberEqual(a: number, b: number, epsilon: number = 1e-10) {
return Math.abs(a - b) < epsilon;
return a === b || Math.abs(a - b) < epsilon;
}

export function isNegative(value: number) {
Expand Down