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

CartesianAxis: Improve interval option 'equidistantPreserveStart' #3768

Merged
merged 19 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cartesian/CartesianAxis.tsx
Expand Up @@ -44,7 +44,7 @@
hide?: boolean;
label?: any;

minTickGap?: number;
minTickGap?: TickGap;

Check failure on line 47 in src/cartesian/CartesianAxis.tsx

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Cannot find name 'TickGap'.

Check failure on line 47 in src/cartesian/CartesianAxis.tsx

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Property 'minTickGap' of exported interface has or is using private name 'TickGap'.

Check failure on line 47 in src/cartesian/CartesianAxis.tsx

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Cannot find name 'TickGap'.

Check failure on line 47 in src/cartesian/CartesianAxis.tsx

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Property 'minTickGap' of exported interface has or is using private name 'TickGap'.
nikolasrieble marked this conversation as resolved.
Show resolved Hide resolved
ticks?: CartesianTickItem[];
tickSize?: number;
tickFormatter?: TickFormatter;
Expand Down
62 changes: 62 additions & 0 deletions src/cartesian/getEquidistantTicks.ts
@@ -0,0 +1,62 @@
import { CartesianTickItem, CartesianViewBox, Size } from '../util/types';
import { mathSign } from '../util/DataUtils';
import { TickFormatter, TickGap } from './CartesianAxis';

Check failure on line 3 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"./CartesianAxis"' has no exported member 'TickGap'.

Check failure on line 3 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"./CartesianAxis"' has no exported member 'TickGap'.
import { getEveryNthWithCondition } from '../util/getEveryNthWithCondition';
import { doesTickFitInBetweenStartAndEnd, getInitialStartAndEnd, getSizeOfTick } from '../util/TickUtils';

Check failure on line 5 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"../util/TickUtils"' has no exported member 'doesTickFitInBetweenStartAndEnd'.

Check failure on line 5 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"../util/TickUtils"' has no exported member 'getInitialStartAndEnd'.

Check failure on line 5 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"../util/TickUtils"' has no exported member 'getSizeOfTick'.

Check failure on line 5 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"../util/TickUtils"' has no exported member 'doesTickFitInBetweenStartAndEnd'.

Check failure on line 5 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"../util/TickUtils"' has no exported member 'getInitialStartAndEnd'.

Check failure on line 5 in src/cartesian/getEquidistantTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Module '"../util/TickUtils"' has no exported member 'getSizeOfTick'.

export function getEquidistantTicks(
sizeKey: 'width' | 'height',
unitSize: Size,
angle?: number,
ticks?: CartesianTickItem[],
tickFormatter?: TickFormatter,
viewBox?: CartesianViewBox,
minTickGap?: TickGap,
fontSize?: string,
letterSpacing?: string,
): CartesianTickItem[] {
const result = (ticks || []).slice();
const { length } = result;
const sign = length >= 2 ? mathSign(result[1].coordinate - result[0].coordinate) : 1;

const { start: initialStart, end } = getInitialStartAndEnd(viewBox, sign, sizeKey);
let index = 0;
// Premature optimisation idea 1: Estimate a lower bound, and start from there.
// For now, start from every tick
let stepsize = 1;
let start = initialStart;

while (stepsize < result.length) {
// Given stepsize, evaluate whether every stepsize-th tick can be shown.
// If it can not, then increase the stepsize by 1, and try again.

const entry = ticks?.[index];

// Break condition - If we have evaluate all the ticks, then we are done.
if (entry === undefined) {
return getEveryNthWithCondition(ticks, stepsize);
}

// Check if the element collides with the next element
const size = getSizeOfTick(tickFormatter, entry, index, sizeKey, fontSize, letterSpacing, unitSize, angle);

const tickCoord = entry.coordinate;
// We will always show the first tick.
const isShow = index === 0 || doesTickFitInBetweenStartAndEnd(sign, tickCoord, size, start, end);

if (!isShow) {
// Start all over with a largerÍ stepsize
index = 0;
start = initialStart;
stepsize += 1;
}

if (isShow) {
// If it can be shown, update the start
start = tickCoord + sign * (size / 2 + minTickGap);
index += stepsize;
}
}

return [];
}
18 changes: 16 additions & 2 deletions src/cartesian/getTicks.ts
@@ -1,9 +1,9 @@
import _ from 'lodash';
import { CartesianTickItem, Size } from '../util/types';
import { mathSign, isNumber } from '../util/DataUtils';
import { getStringSize } from '../util/DOMUtils';
import { Props as CartesianAxisProps } from './CartesianAxis';
import { Global } from '../util/Global';

import {
isVisible,
getEveryNThTick,
Expand Down Expand Up @@ -126,7 +126,7 @@
unit && sizeKey === 'width' ? getStringSize(unit, { fontSize, letterSpacing }) : { width: 0, height: 0 };

const getTickSize = (content: CartesianTickItem, index: number) => {
const value = _.isFunction(tickFormatter) ? tickFormatter(content.value, index) : content.value;

Check failure on line 129 in src/cartesian/getTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

'_' refers to a UMD global, but the current file is a module. Consider adding an import instead.

Check failure on line 129 in src/cartesian/getTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

'_' refers to a UMD global, but the current file is a module. Consider adding an import instead.
// Recharts only supports angles when sizeKey === 'width'
return sizeKey === 'width'
? getAngledTickWidth(getStringSize(value, { fontSize, letterSpacing }), unitSize, angle)
Expand All @@ -135,7 +135,21 @@

const sign = ticks.length >= 2 ? mathSign(ticks[1].coordinate - ticks[0].coordinate) : 1;
const boundaries = getTickBoundaries(viewBox, sign, sizeKey);


if (interval === 'equidistant') {
return getEquidistantTicks(

Check failure on line 140 in src/cartesian/getTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Cannot find name 'getEquidistantTicks'.

Check failure on line 140 in src/cartesian/getTicks.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Cannot find name 'getEquidistantTicks'.
sizeKey,
unitSize,
angle,
ticks,
tickFormatter,
viewBox,
minTickGap,
fontSize,
letterSpacing,
);
}

if (interval === 'equidistantPreserveStart') {
candidates = getTicksStart(sign, boundaries, getTickSize, ticks, minTickGap);

Expand Down
5 changes: 5 additions & 0 deletions src/util/TickUtils.ts
@@ -1,4 +1,9 @@
import _ from 'lodash';

import { TickFormatter } from '../cartesian/CartesianAxis';
import { getAngledRectangleWidth } from './CartesianUtils';

Check failure on line 4 in src/util/TickUtils.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Duplicate identifier 'getAngledRectangleWidth'.

Check failure on line 4 in src/util/TickUtils.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Duplicate identifier 'getAngledRectangleWidth'.
import { getStringSize } from './DOMUtils';
import { getAngledRectangleWidth } from './CartesianUtils';

Check failure on line 6 in src/util/TickUtils.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Duplicate identifier 'getAngledRectangleWidth'.

Check failure on line 6 in src/util/TickUtils.ts

View workflow job for this annotation

GitHub Actions / build-node-version-14 (14.x)

Duplicate identifier 'getAngledRectangleWidth'.
import { getEveryNthWithCondition } from './getEveryNthWithCondition';
import { Size, CartesianViewBox, CartesianTickItem } from './types';

Expand Down
9 changes: 8 additions & 1 deletion src/util/types.ts
Expand Up @@ -1120,8 +1120,15 @@ export interface BaseAxisProps {
* 'preserveStartEnd' keeps the left tick on collision and ensures that the first and last ticks are always shown.
* 'equidistantPreserveStart' computes which ticks are shown according to the 'preserveStart' strategy and
* then selects a number N such that every nTh tick will be shown.
* 'equidistant' selects a number N such that every nTh tick will be shown without collision.
*/
export type AxisInterval = number | 'preserveStart' | 'preserveEnd' | 'preserveStartEnd' | 'equidistantPreserveStart';
export type AxisInterval =
| number
| 'preserveStart'
| 'preserveEnd'
| 'preserveStartEnd'
| 'equidistantPreserveStart'
| 'equidistant';

export interface TickItem {
value?: any;
Expand Down
Expand Up @@ -8,7 +8,14 @@ export default {

export const TickPositioning = {
render: () => {
const intervalOptions = ['preserveStart', 'preserveEnd', 'preserveStartEnd', 'equidistantPreserveStart', 0];
const intervalOptions = [
'preserveStart',
'preserveEnd',
'preserveStartEnd',
'equidistantPreserveStart',
'equidistant',
0,
];

return (
<ResponsiveContainer>
Expand Down