-
Notifications
You must be signed in to change notification settings - Fork 221
fix: render boxPlot outliers after updateDataSync from empty data #4695
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
dyk1454683243-sudo
wants to merge
2
commits into
VisActor:develop
Choose a base branch
from
dyk1454683243-sudo:cursor/fix-boxplot-outliers-updatedatasync-4270-b8e8
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 all commits
Commits
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
10 changes: 10 additions & 0 deletions
10
common/changes/@visactor/vchart/fix-boxplot-outliers-updateDataSync-4270_2026-09-20.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,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@visactor/vchart", | ||
| "comment": "fix: register boxPlot outliersField statistics so updateDataSync from empty data still renders outlier points", | ||
| "type": "patch" | ||
| } | ||
| ], | ||
| "packageName": "@visactor/vchart" | ||
| } |
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
273 changes: 273 additions & 0 deletions
273
packages/vchart/__tests__/unit/series/box-plot-outliers.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,273 @@ | ||
| import { DataSet } from '@visactor/vdataset'; | ||
| import { EventDispatcher } from '../../../src/event/event-dispatcher'; | ||
| import { GlobalScale } from '../../../src/scale/global-scale'; | ||
| import VChart from '../../../src'; | ||
| import { BoxPlotChart, registerBoxplotChart } from '../../../src/chart/box-plot'; | ||
| import { BoxPlotSeries } from '../../../src/series/box-plot/box-plot'; | ||
| import { BOX_PLOT_OUTLIER_VALUE_FIELD } from '../../../src/constant/box-plot'; | ||
| import { SeriesMarkNameEnum } from '../../../src/series/interface/type'; | ||
| import { getTestCompiler } from '../../util/factory/compiler'; | ||
| import { getTheme, initChartDataSet, seriesOption } from '../../util/context'; | ||
| import { createDiv, removeDom } from '../../util/dom'; | ||
|
|
||
| registerBoxplotChart(); | ||
|
|
||
| const dataSet = new DataSet(); | ||
| initChartDataSet(dataSet); | ||
|
|
||
| const filledValues = [ | ||
| { | ||
| x: 'Sub-Saharan Africa', | ||
| y1: 8.72, | ||
| y2: 9.73, | ||
| y3: 10.17, | ||
| y4: 10.51, | ||
| y5: 11.64, | ||
| y6: [12.01, 12.02, 14.03] | ||
| }, | ||
| { | ||
| x: 'South Asia', | ||
| y1: 9.4, | ||
| y2: 10.06, | ||
| y3: 10.75, | ||
| y4: 11.56, | ||
| y5: 12.5 | ||
| } | ||
| ]; | ||
|
|
||
| const createBoxPlotSeries = () => { | ||
| const series = new BoxPlotSeries<any>( | ||
| { | ||
| type: 'boxPlot', | ||
| xField: 'x', | ||
| minField: 'y1', | ||
| q1Field: 'y2', | ||
| medianField: 'y3', | ||
| q3Field: 'y4', | ||
| maxField: 'y5', | ||
| outliersField: 'y6' | ||
| }, | ||
| seriesOption({ dataSet }) | ||
| ); | ||
| (series as any)._outliersField = 'y6'; | ||
| (series as any)._fieldX = ['x']; | ||
| (series as any)._fieldY = ['y5', 'y3', 'y2', 'y4', 'y1']; | ||
| (series as any)._xAxisHelper = { | ||
| getScale: () => ({ type: 'band' }) | ||
| }; | ||
| (series as any)._yAxisHelper = { | ||
| getScale: () => ({ type: 'linear' }) | ||
| }; | ||
| return series; | ||
| }; | ||
|
|
||
| const createBoxPlotChart = (values?: Record<string, unknown>[]) => { | ||
| const spec = { | ||
| type: 'boxPlot', | ||
| data: [ | ||
| { | ||
| id: 'boxPlot', | ||
| values: values ?? [] | ||
| } | ||
| ], | ||
| xField: 'x', | ||
| minField: 'y1', | ||
| q1Field: 'y2', | ||
| medianField: 'y3', | ||
| q3Field: 'y4', | ||
| maxField: 'y5', | ||
| outliersField: 'y6', | ||
| direction: 'vertical', | ||
| animation: false | ||
| } as any; | ||
| const transformer = new BoxPlotChart.transformerConstructor({ | ||
| type: 'boxPlot', | ||
| seriesType: 'boxPlot', | ||
| getTheme, | ||
| mode: 'desktop-browser' | ||
| }); | ||
| const info = transformer.initChartSpec(spec); | ||
| const chartDataSet = new DataSet(); | ||
| initChartDataSet(chartDataSet); | ||
| const chart = new BoxPlotChart(spec, { | ||
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
| // @ts-ignore | ||
| eventDispatcher: new EventDispatcher({} as any, { addEventListener: () => {} } as any), | ||
| globalInstance: { | ||
| isAnimationEnable: () => false, | ||
| getContainer: () => ({}), | ||
| getTooltipHandlerByUser: (() => undefined) as () => undefined | ||
| }, | ||
| render: {} as any, | ||
| dataSet: chartDataSet, | ||
| map: new Map(), | ||
| container: null, | ||
| mode: 'desktop-browser', | ||
| getCompiler: getTestCompiler, | ||
| globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), | ||
| getTheme, | ||
| onError: () => {}, | ||
| getSpecInfo: () => info | ||
| } as any); | ||
| chart.created(transformer); | ||
| chart.init(); | ||
| return chart; | ||
| }; | ||
|
|
||
| const getOutlierStatistic = (series: { getStatisticFields: () => { key: string; operations: string[] }[] }) => | ||
| series.getStatisticFields().find(field => field.key === 'y6'); | ||
|
|
||
| const getFoldedOutlierValues = (series: any): number[] => { | ||
| const rows = series._outlierData?.getLatestData?.() ?? series._outlierData?.getDataView?.()?.latestData ?? []; | ||
| return rows | ||
| .map((row: Record<string, unknown>) => row[BOX_PLOT_OUTLIER_VALUE_FIELD]) | ||
| .filter((value: unknown): value is number => typeof value === 'number'); | ||
| }; | ||
|
|
||
| describe('BoxPlotSeries getStatisticFields outliersField', () => { | ||
| test('adds array-min/array-max when outliersField is omitted from super fields', () => { | ||
| const series = createBoxPlotSeries(); | ||
|
|
||
| expect(getOutlierStatistic(series)).toEqual({ | ||
| key: 'y6', | ||
| operations: ['array-min', 'array-max'] | ||
| }); | ||
| }); | ||
|
|
||
| test('replaces min/max with array operations when outliersField is already present', () => { | ||
| const series = createBoxPlotSeries(); | ||
| (series as any)._fieldY = ['y5', 'y3', 'y2', 'y4', 'y1', 'y6']; | ||
|
|
||
| expect(getOutlierStatistic(series)).toEqual({ | ||
| key: 'y6', | ||
| operations: ['array-min', 'array-max'] | ||
| }); | ||
| expect(series.getStatisticFields().filter(field => field.key === 'y6')).toHaveLength(1); | ||
| }); | ||
|
|
||
| test('still registers outliersField when axis helpers are not ready', () => { | ||
| const series = createBoxPlotSeries(); | ||
| (series as any)._xAxisHelper = undefined; | ||
| (series as any)._yAxisHelper = undefined; | ||
|
|
||
| expect(getOutlierStatistic(series)).toEqual({ | ||
| key: 'y6', | ||
| operations: ['array-min', 'array-max'] | ||
| }); | ||
| }); | ||
|
|
||
| test('does not invent a statistic field when outliersField is unset', () => { | ||
| const series = createBoxPlotSeries(); | ||
| (series as any)._outliersField = undefined; | ||
|
|
||
| expect(getOutlierStatistic(series)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('boxPlot outliersField after empty-init updateData', () => { | ||
| test('folds y6 array rows after updateData from empty values', () => { | ||
| const chart = createBoxPlotChart([]); | ||
| const series = chart.getAllSeries()[0] as any; | ||
|
|
||
| expect(getOutlierStatistic(series)).toEqual({ | ||
| key: 'y6', | ||
| operations: ['array-min', 'array-max'] | ||
| }); | ||
| expect(getFoldedOutlierValues(series)).toEqual([]); | ||
|
|
||
| chart.updateData('boxPlot', filledValues); | ||
|
|
||
| expect(series.getViewData()?.latestData).toHaveLength(2); | ||
| expect(getFoldedOutlierValues(series)).toEqual([12.01, 12.02, 14.03]); | ||
| expect(series.getViewDataStatistics()?.latestData?.y6).toMatchObject({ | ||
| min: 12.01, | ||
| max: 14.03 | ||
| }); | ||
| }); | ||
|
|
||
| test('non-empty init with outliersField still folds outlier rows', () => { | ||
| const chart = createBoxPlotChart(filledValues); | ||
| const series = chart.getAllSeries()[0] as any; | ||
|
|
||
| expect(getOutlierStatistic(series)).toEqual({ | ||
| key: 'y6', | ||
| operations: ['array-min', 'array-max'] | ||
| }); | ||
| expect(getFoldedOutlierValues(series)).toEqual([12.01, 12.02, 14.03]); | ||
| expect(series.getViewDataStatistics()?.latestData?.y6).toMatchObject({ | ||
| min: 12.01, | ||
| max: 14.03 | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| const describeRender = typeof document === 'undefined' ? describe.skip : describe; | ||
|
|
||
| const createIssueSpec = (values?: Record<string, unknown>[]) => | ||
| ({ | ||
| type: 'boxPlot', | ||
| width: 500, | ||
| height: 400, | ||
| data: values | ||
| ? [{ id: 'boxPlot', values }] | ||
| : [ | ||
| { | ||
| id: 'boxPlot' | ||
| } | ||
| ], | ||
| xField: 'x', | ||
| minField: 'y1', | ||
| q1Field: 'y2', | ||
| medianField: 'y3', | ||
| q3Field: 'y4', | ||
| maxField: 'y5', | ||
| outliersField: 'y6', | ||
| direction: 'vertical', | ||
| animation: false | ||
| } as any); | ||
|
|
||
| const getOutlierGraphics = (chart: VChart) => { | ||
| const series = chart.getChart()?.getAllSeries()[0] as any; | ||
| const outlierMark = series?.getMarks()?.find((mark: { name?: string }) => mark.name === SeriesMarkNameEnum.outlier); | ||
| return { | ||
| series, | ||
| graphics: outlierMark?.getGraphics?.() ?? [] | ||
| }; | ||
| }; | ||
|
|
||
| describeRender('VChart boxPlot outliersField updateDataSync', () => { | ||
| let dom: HTMLElement; | ||
| let chart: VChart; | ||
|
|
||
| beforeEach(() => { | ||
| dom = createDiv(); | ||
| dom.style.width = '500px'; | ||
| dom.style.height = '400px'; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| chart?.release(); | ||
| removeDom(dom); | ||
| }); | ||
|
|
||
| test('renders outlier points after updateDataSync from empty data', () => { | ||
| chart = new VChart(createIssueSpec(), { dom, animation: false }); | ||
| chart.renderSync(); | ||
| expect(getFoldedOutlierValues(getOutlierGraphics(chart).series)).toEqual([]); | ||
|
|
||
| chart.updateDataSync('boxPlot', filledValues); | ||
|
|
||
| const { series, graphics } = getOutlierGraphics(chart); | ||
| expect(getFoldedOutlierValues(series)).toEqual([12.01, 12.02, 14.03]); | ||
| expect(graphics.length).toBeGreaterThanOrEqual(3); | ||
| }); | ||
|
|
||
| test('non-empty init with outliersField still renders outlier points', () => { | ||
| chart = new VChart(createIssueSpec(filledValues), { dom, animation: false }); | ||
| chart.renderSync(); | ||
|
|
||
| const { series, graphics } = getOutlierGraphics(chart); | ||
| expect(getFoldedOutlierValues(series)).toEqual([12.01, 12.02, 14.03]); | ||
| expect(graphics.length).toBeGreaterThanOrEqual(3); | ||
| }); | ||
| }); | ||
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.
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.
[P2] 请用有效 spec 复现字段缺失,避免手动构造内部状态作为修复依据
这里手动将
_fieldY设为不含y6,但真实BoxPlotChartSpecTransformer._getDefaultSeriesSpec()会把outliersField放入对应的数据轴字段,且这一过程不依赖初始 data 是否为空;CartesianSeries.getStatisticFields()也根据配置字段和轴 helper 生成统计项,而不是根据初始数据的键生成。因此这个用例绕开了 #4270 的实际初始化路径。撤回本 PR 的生产代码改动后,原 issue 的浏览器复现以及纵向、横向、清空再填充的真实 VChart 用例仍然通过,只有这里和手动清空 axis helpers 的用例失败。请改为通过有效 spec / 公共 API 触发、基线失败而 PR 通过的回归测试,再据此决定是否需要新增统计字段兜底。