-
Notifications
You must be signed in to change notification settings - Fork 212
feat: support area enlargement (linear axis custom distribution) #4441
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
Open
xuefei1313
wants to merge
7
commits into
develop
Choose a base branch
from
003-area-enlargement
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
231fe75
feat: support area enlargement (linear axis custom distribution)
xuefei1313 401ced3
feat: add documents of customDistribution
xuefei1313 b8361fc
feat: optimiz with comment
xuefei1313 3c7c04d
feat: support area enlargement (linear axis custom distribution)
xuefei1313 27dd0ac
feat: support area enlargement (linear axis custom distribution)
xuefei1313 910e270
feat: merge the breaks capability into piecewise
xuefei1313 a27dd0d
feat: optimiz breaks in domain
xuefei1313 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
common/changes/@visactor/vchart/feat-area-enlargement.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@visactor/vchart", | ||
| "comment": "feat: support area enlargement (linear axis custom distribution)", | ||
| "type": "minor" | ||
| } | ||
| ], | ||
| "packageName": "@visactor/vchart", | ||
| "email": "lixuef1313@163.com" | ||
| } |
39 changes: 39 additions & 0 deletions
39
docs/assets/demos/builtin-theme/charts/area-enlargement.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { IChartInfo } from './interface'; | ||
|
|
||
| const spec = { | ||
| type: 'line', | ||
| data: [ | ||
| { | ||
| id: 'line', | ||
| values: [ | ||
| { x: '1', y: 1 }, | ||
| { x: '2', y: 5 }, | ||
| { x: '3', y: 7 }, | ||
| { x: '4', y: 8 }, | ||
| { x: '5', y: 8.5 }, | ||
| { x: '6', y: 9 }, | ||
| { x: '7', y: 9.5 }, | ||
| { x: '8', y: 10 } | ||
| ] | ||
| } | ||
| ], | ||
| xField: 'x', | ||
| yField: 'y', | ||
| axes: [ | ||
| { | ||
| orient: 'left', | ||
| type: 'linear', | ||
| customDistribution: { | ||
| domain: [0, 7, 9, 10], | ||
| ratio: [0.2, 0.6, 0.2] | ||
| } | ||
| } | ||
| ] | ||
| }; | ||
|
|
||
| const areaEnlargement: IChartInfo = { | ||
| name: 'Area Enlargement', | ||
| spec | ||
| }; | ||
|
|
||
| export default areaEnlargement; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/vchart/__tests__/unit/component/cartesian/axis/linear-axis-distribution.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { GlobalScale } from '../../../../../src/scale/global-scale'; | ||
| import { DataSet, csvParser } from '@visactor/vdataset'; | ||
| import { dimensionStatistics } from '../../../../../src/data/transforms/dimension-statistics'; | ||
| import type { CartesianLinearAxis } from '../../../../../src/index'; | ||
| // eslint-disable-next-line no-duplicate-imports | ||
| import { CartesianAxis } from '../../../../../src/index'; | ||
| import { ComponentTypeEnum, type IComponent, type IComponentOption } from '../../../../../src/component/interface'; | ||
| import { EventDispatcher } from '../../../../../src/event/event-dispatcher'; | ||
| import { getTestCompiler } from '../../../../util/factory/compiler'; | ||
| import { getTheme, initChartDataSet } from '../../../../util/context'; | ||
| import { getCartesianAxisInfo } from '../../../../../src/component/axis/cartesian/util'; | ||
|
|
||
| const dataSet = new DataSet(); | ||
| initChartDataSet(dataSet); | ||
| dataSet.registerParser('csv', csvParser); | ||
| dataSet.registerTransform('dimensionStatistics', dimensionStatistics); | ||
|
|
||
| const ctx: IComponentOption = { | ||
| type: ComponentTypeEnum.cartesianLinearAxis, | ||
| eventDispatcher: new EventDispatcher({} as any, { addEventListener: () => {} } as any) as any, | ||
| dataSet, | ||
| map: new Map(), | ||
| mode: 'desktop-browser', | ||
| globalInstance: { | ||
| isAnimationEnable: () => true, | ||
| getContainer: () => ({}), | ||
| getTooltipHandlerByUser: (() => undefined) as () => undefined | ||
| } as any, | ||
| getCompiler: getTestCompiler, | ||
| getAllRegions: () => [], | ||
| getRegionsInIndex: () => [], | ||
| getChart: () => ({ getSpec: () => ({}) } as any), | ||
| getRegionsInIds: () => [], | ||
| getRegionsInUserIdOrIndex: () => [], | ||
| getAllSeries: () => [], | ||
| getSeriesInIndex: () => [], | ||
| getSeriesInIds: () => [], | ||
| getSeriesInUserIdOrIndex: () => [], | ||
| getAllComponents: () => [], | ||
| getComponentByIndex: () => undefined, | ||
| getComponentsByKey: () => [], | ||
| getComponentsByType: () => [], | ||
| getChartLayoutRect: () => ({ width: 0, height: 0, x: 0, y: 0 }), | ||
| getChartViewRect: () => ({ width: 500, height: 500 } as any), | ||
| globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), | ||
| getTheme: getTheme, | ||
| getComponentByUserId: () => undefined, | ||
| animation: false, | ||
| onError: () => {}, | ||
| getSeriesData: () => undefined | ||
| }; | ||
|
|
||
| const getAxisSpec = (spec: any) => ({ | ||
| sampling: 'simple', | ||
| ...spec | ||
| }); | ||
|
|
||
| describe('LinearAxis customDistribution', () => { | ||
| beforeAll(() => { | ||
| // @ts-ignore | ||
| jest.spyOn(CartesianAxis.prototype, 'collectData').mockImplementation(() => { | ||
| return [{ min: 0, max: 10 }]; | ||
| }); | ||
| }); | ||
|
|
||
| test('should create piecewise domain and range from customDistribution', () => { | ||
| // Mock getNewScaleRange to return [0, 100] | ||
| // @ts-ignore | ||
| jest.spyOn(CartesianAxis.prototype, 'getNewScaleRange').mockReturnValue([0, 100]); | ||
|
|
||
| let spec = getAxisSpec({ | ||
| orient: 'left', | ||
| customDistribution: { | ||
| domain: [0, 5, 10], | ||
| ratio: [0.8, 0.2] | ||
| } | ||
| }); | ||
|
|
||
| const transformer = new CartesianAxis.transformerConstructor({ | ||
| type: 'cartesianAxis-linear', | ||
| getTheme: getTheme, | ||
| mode: 'desktop-browser' | ||
| }); | ||
| spec = transformer.transformSpec(spec, {}).spec; | ||
| const linearAxis = CartesianAxis.createComponent( | ||
| { | ||
| type: getCartesianAxisInfo(spec).componentName, | ||
| spec | ||
| }, | ||
| ctx | ||
| ) as CartesianLinearAxis; | ||
|
|
||
| linearAxis.created(); | ||
| linearAxis.init({}); | ||
|
|
||
| // Test Domain | ||
| // @ts-ignore | ||
| linearAxis.updateScaleDomain(); | ||
| const scale = linearAxis.getScale(); | ||
| expect(scale.domain()).toEqual([0, 5, 10]); | ||
|
|
||
| // Test Range | ||
| // @ts-ignore | ||
| const newRange = linearAxis.getNewScaleRange(); | ||
| // 0 -> 0 | ||
| // 5 -> 0 + 0.8 * 100 = 80 | ||
| // 10 -> 80 + 0.2 * 100 = 100 | ||
| expect(newRange).toEqual([0, 80, 100]); | ||
| }); | ||
|
|
||
| test('should handle gaps in customDistribution', () => { | ||
| let spec = getAxisSpec({ | ||
| orient: 'left', | ||
| customDistribution: { | ||
| domain: [0, 5, 8, 10], | ||
| ratio: [0.4, 0.4] | ||
| } | ||
| }); | ||
|
|
||
| const transformer = new CartesianAxis.transformerConstructor({ | ||
| type: 'cartesianAxis-linear', | ||
| getTheme: getTheme, | ||
| mode: 'desktop-browser' | ||
| }); | ||
| spec = transformer.transformSpec(spec, {}).spec; | ||
| const linearAxis = CartesianAxis.createComponent( | ||
| { | ||
| type: getCartesianAxisInfo(spec).componentName, | ||
| spec | ||
| }, | ||
| ctx | ||
| ) as CartesianLinearAxis; | ||
|
|
||
| linearAxis.created(); | ||
| linearAxis.init({}); | ||
|
|
||
| // @ts-ignore | ||
| linearAxis.updateScaleDomain(); | ||
| const scale = linearAxis.getScale(); | ||
| expect(scale.domain()).toEqual([0, 5, 8, 10]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the gap-handling path for
customDistribution, when the configured intervals exactly cover the domain but the sum of theirratiovalues is less than 1,totalGapDomainstays 0, so all segments get only their configured share andcurrentPosends beforeend; the subsequent correction that forces the last point toendimplicitly inflates only the final segment. This makes the effective visual ratio of the last interval larger than its configuredratio, which can be surprising. Consider either normalizing ratios to sum to 1 when there are no gaps, or distributing the leftover proportion across all segments instead of only the last one, so that the effective behavior matches user expectations more closely.