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

[WIP]Fix/axis break by length #3563

Open
wants to merge 3 commits into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -1507,12 +1507,12 @@ describe('VChart', () => {
const range = la.getScale().range();
expect(range[0]).toBeCloseTo(426);
expect(range[1]).toBeCloseTo(423.8873352657334);
expect(range[2]).toBeCloseTo(423.58867525684406);
expect(range[2]).toBeCloseTo(423.88585307996357);
expect(range[3]).toBeCloseTo(0);

const rightDomain = ra.getScale().domain();

expect(rightDomain[0]).toBeCloseTo(-393970724.0726612);
expect(rightDomain[0]).toBeCloseTo(-394248484.9018328);
expect(rightDomain[1]).toBeCloseTo(80000000000);
expect(ra.getScale().range()).toEqual([426, 0]);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* break-data 的测试用例
*/
import { breakData } from '../../../../../../src/component/axis/mixin/util/break-data';
import { breakData, breakScopeByLength } from '../../../../../../src/component/axis/mixin/util/break-data';

describe('break data ', () => {
it('break data by break points when scopeType is "length"', () => {
Expand All @@ -10,20 +10,21 @@ describe('break data ', () => {
400, 80, 100
];
const breakPoints = [500, 3000];
const { domain, scope } = breakData(data, breakPoints, 'length');
const { domain } = breakData(data, breakPoints, 'length');

expect(domain).toStrictEqual([
[44, 500],
[500, 3000],
[3000, 7715]
]);
const scope = breakScopeByLength(domain, [breakPoints as [number, number]], [44, 7715]);

expect(scope.length).toEqual(3);
expect(scope[0][0]).toBeCloseTo(0);
expect(scope[0][1]).toBeCloseTo(0.06136455389584174);
expect(scope[1][0]).toBeCloseTo(0.06136455389584174);
expect(scope[1][1]).toBeCloseTo(0.36549589557260126);
expect(scope[2][0]).toBeCloseTo(0.36549589557260126);
expect(scope[0][1]).toBeCloseTo(0.08818410365499903);
expect(scope[1][0]).toBeCloseTo(0.08818410365499903);
expect(scope[1][1]).toBeCloseTo(0.08818410365499903);
expect(scope[2][0]).toBeCloseTo(0.08818410365499903);
expect(scope[2][1]).toBeCloseTo(1);
});

Expand Down
8 changes: 6 additions & 2 deletions packages/vchart/src/component/axis/mixin/linear-axis-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isXAxis } from '../cartesian/util/common';
import type { IOrientType } from '../../../typings/space';
import type { IComponentOption } from '../../interface/common';
import type { StringOrNumber } from '../../../typings';
import { breakData } from './util/break-data';
import { breakData, breakScopeByLength } from './util/break-data';

export const e10 = Math.sqrt(50);
export const e5 = Math.sqrt(10);
Expand Down Expand Up @@ -43,9 +43,9 @@ export interface LinearAxisMixin {
*/
_break: {
domain: [number, number][];
scope: [number, number][];
breakDomains: [number, number][];
breaks: ILinearAxisBreakSpec[];
scope: [number, number][];
};
event: IEvent;
_orient: IOrientType;
Expand Down Expand Up @@ -205,6 +205,10 @@ export class LinearAxisMixin {
this.expandDomain(domain);
this.includeZero(domain);
this.setDomainMinMax(domain);

if (this._break && this._spec.breaks[0].scopeType === 'length') {
this._break.scope = breakScopeByLength(this._break.domain, this._break.breakDomains, domain);
}
return domain;
}

Expand Down
43 changes: 41 additions & 2 deletions packages/vchart/src/component/axis/mixin/util/break-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from '@visactor/vutils';
import { last } from '@visactor/vutils';

const setDomain = (min: number, max: number, breaks: number[]): [number, number][] =>
breaks.reduce(
Expand Down Expand Up @@ -82,13 +82,52 @@ function breakScope(data: number[], points: number[], scopeType: 'count' | 'leng
return res;
}

export function breakScopeByLength(
domain: [number, number][],
breakDomains: [number, number][],
finalDomain: number[]
): [number, number][] {
const d0 = finalDomain[0];
const d1 = last(finalDomain);
const newDomain: { isBreak?: boolean; domain: [number, number] }[] = [];
let sum = 0;

domain.forEach((d, index) => {
if (breakDomains.some(b => b[0] === d[0] && b[1] === d[1])) {
newDomain.push({
isBreak: true,
domain: d
});
} else {
const newD0 = index === 0 ? d0 : d[0];
const newD1 = index === domain.length - 1 ? d1 : d[1];
newDomain.push({ domain: [newD0, newD1] });
sum += newD1 - newD0;
}
});

return sum > 0
? newDomain.reduce((res, d, index) => {
const r = d.isBreak ? 0 : (d.domain[1] - d.domain[0]) / sum;

if (index === 0) {
res.push([0, r]);
} else {
res.push([res[index - 1][1], res[index - 1][1] + r]);
}

return res;
}, [])
: [[0, 1]];
}

export function breakData(data: number[], points: number[], scopeType?: 'count' | 'length') {
// 现将数据和断点排序
data.sort(sorter);
points.sort(sorter);

return {
domain: breakDomain(data, points),
scope: breakScope(data, points, scopeType)
scope: scopeType === 'count' ? breakScope(data, points, scopeType) : []
};
}
Loading