Skip to content

Commit

Permalink
Tick generation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobp100 committed Jan 6, 2025
1 parent 9e6f9cb commit ea4ce79
Show file tree
Hide file tree
Showing 19 changed files with 25 additions and 24 deletions.
27 changes: 15 additions & 12 deletions packages/ag-charts-community/src/chart/axis/axisTickGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
this.createTickData(
domain,
niceMode,
visibleRange,
primaryTickCount,
tickGenerationType,
index,
tickData,
terminate,
primaryTickCount,
visibleRange
terminate
);

strategies.push(tickGenerationStrategy);
Expand All @@ -260,12 +260,12 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
this.createTickData(
domain,
niceMode,
visibleRange,
primaryTickCount,
TickGenerationType.FILTER,
index,
tickData,
terminate,
primaryTickCount,
visibleRange
terminate
);
strategies.push(tickFilterStrategy);
}
Expand All @@ -286,12 +286,12 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
private createTickData(
domain: D[],
niceMode: NiceMode,
visibleRange: [number, number],
primaryTickCount: number | undefined,
tickGenerationType: TickGenerationType,
index: number,
tickData: TickData,
terminate: boolean,
primaryTickCount: number | undefined,
visibleRange: [number, number]
terminate: boolean
): TickStrategyResult {
const { scale, interval } = this.axis;
const { step, values, minSpacing, maxSpacing } = interval;
Expand All @@ -314,6 +314,7 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
tickData = this.getTicks({
domain,
niceMode,
visibleRange,
tickGenerationType,
previousTicks,
minTickCount,
Expand All @@ -335,6 +336,7 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
private getTicks({
domain,
niceMode,
visibleRange,
tickGenerationType,
previousTicks,
tickCount,
Expand All @@ -344,6 +346,7 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
}: {
domain: D[];
niceMode: NiceMode;
visibleRange: [number, number];
tickGenerationType: TickGenerationType;
previousTicks: TickDatum[];
tickCount: number;
Expand All @@ -352,7 +355,7 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
primaryTickCount?: number;
}): TickData {
const { axis } = this;
const { range, scale, visibleRange, interval } = axis;
const { range, scale, interval } = axis;
const idGenerator = createIdsGenerator();

const domainParams: ScaleTickParams<any> = {
Expand Down Expand Up @@ -397,14 +400,14 @@ export class AxisTickGenerator<S extends Scale<D, number, TickInterval<S>>, D> {
niceDomain = secondaryAxisTicks.domain.map((d) => scaleStopTsComplaining.toDomain(d));
} else {
// AG-10654 Just use normal ticks for categorical axes.
rawTicks = scaleStopTsComplaining.ticks(tickParams, niceDomain, visibleRange) ?? [];
rawTicks = scaleStopTsComplaining.ticks(tickParams, niceDomain) ?? [];
}
break;
case TickGenerationType.FILTER:
rawTicks = this.filterTicks(previousTicks, tickCount);
break;
default:
rawTicks = scale.ticks(tickParams, niceDomain, visibleRange) ?? [];
rawTicks = scale.ticks(tickParams, niceDomain) ?? [];
break;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/ag-charts-community/src/scale/ordinalTimeScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export class OrdinalTimeScale extends BandScale<Date, TimeInterval | number> {

override ticks(
{ interval, maxTickCount }: ScaleTickParams<TimeInterval | number>,
domain: Date[] = this.domain,
visibleRange: [number, number] = [0, 1]
domain: Date[] = this.domain
): Date[] {
if (!domain.length) {
return [];
Expand All @@ -79,7 +78,7 @@ export class OrdinalTimeScale extends BandScale<Date, TimeInterval | number> {
const stop = Math.max(t0, t1);

if (interval == null) {
return this.getDefaultTicks(maxTickCount, isReversed, visibleRange);
return this.getDefaultTicks(maxTickCount, isReversed);
}

const [r0, r1] = this.range;
Expand All @@ -96,10 +95,10 @@ export class OrdinalTimeScale extends BandScale<Date, TimeInterval | number> {
});
}

private getDefaultTicks(maxTickCount: number, isReversed: boolean, visibleRange: [number, number]) {
private getDefaultTicks(maxTickCount: number, isReversed: boolean) {
const { domain } = this;
const ticks: Date[] = [];
const tickEvery = Math.ceil((domain.length * (visibleRange[1] - visibleRange[0])) / maxTickCount);
const tickEvery = Math.ceil(domain.length / maxTickCount);
const tickOffset = Math.floor(tickEvery / 2);

for (let index = 0; index < domain.length; index += 1) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ag-charts-community/src/scale/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Scale<D, R, I = number> {
toDomain(value: number): D | undefined;
convert(value: D, clamp?: boolean): R;
invert(value: R, exact?: boolean): D | undefined;
ticks(ticks: ScaleTickParams<I>, domain?: D[], visibleRange?: [number, number]): D[] | undefined;
ticks(ticks: ScaleTickParams<I>, domain?: D[]): D[] | undefined;
niceDomain(ticks: ScaleTickParams<I>, domain?: D[]): D[];
tickFormatter(params: ScaleFormatParams<D>): ((x: any) => string) | undefined;
datumFormatter(params: ScaleFormatParams<D>): ((x: any) => string) | undefined;
Expand Down
11 changes: 5 additions & 6 deletions packages/ag-charts-community/src/util/ticks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from './logger';
import { clamp, countFractionDigits, round } from './number';
import { clamp, countFractionDigits } from './number';
import { numberFormat, parseFormat } from './numberFormat';
import { day } from './time/day';
import {
Expand Down Expand Up @@ -233,14 +233,13 @@ export function estimateTickCount(
defaultTickCount: number,
defaultMinSpacing: number
) {
const zoomedRangeExtent = round(rangeExtent / zoomExtent, 2);
defaultMinSpacing = Math.max(defaultMinSpacing, rangeExtent / (defaultTickCount + 1));

if (isNaN(minSpacing)) {
minSpacing = defaultMinSpacing;
}
if (isNaN(maxSpacing)) {
maxSpacing = 2 * minSpacing;
maxSpacing = rangeExtent;
}
if (minSpacing > maxSpacing) {
if (minSpacing === defaultMinSpacing) {
Expand All @@ -250,9 +249,9 @@ export function estimateTickCount(
}
}

const maxTickCount = Math.max(1, Math.floor(zoomedRangeExtent / minSpacing));
const minTickCount = Math.min(maxTickCount, Math.ceil(zoomedRangeExtent / maxSpacing));
const tickCount = clamp(minTickCount, defaultTickCount, maxTickCount);
const maxTickCount = Math.max(1, Math.floor(rangeExtent / (zoomExtent * minSpacing)));
const minTickCount = Math.min(maxTickCount, Math.ceil(rangeExtent / (zoomExtent * maxSpacing)));
const tickCount = clamp(minTickCount, Math.floor(defaultTickCount / zoomExtent), maxTickCount);

return { minTickCount, maxTickCount, tickCount };
}
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.

0 comments on commit ea4ce79

Please sign in to comment.