-
Notifications
You must be signed in to change notification settings - Fork 4
refactor(color): compute continuous color domain via series plugins #642
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Populate the series registry so getSeriesPlugin works (see plugin registration gotcha). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to write this for all tests? Can you put it in a general setup? |
||
| import '../../../plugins'; | ||
| import type {ChartData} from '../../../types'; | ||
| import {getDomainForContinuousColorScale} from '../color'; | ||
|
|
||
| type Series = ChartData['series']['data']; | ||
|
|
||
| describe('utils/color/getDomainForContinuousColorScale', () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to avoid tying test names to file paths. When directories change, keeping test names in sync can be easily overlooked, which might lead to outdated or confusing test names in the future. |
||
| // One case per supported type guards each plugin's field mapping individually, | ||
| // so a regression in a single type (e.g. a swapped field) is caught. | ||
| test.each<{type: string; data: unknown[]; expected: number[]}>([ | ||
| { | ||
| type: 'line', | ||
| data: [ | ||
| {x: 0, y: 1}, | ||
| {x: 1, y: 5}, | ||
| ], | ||
| expected: [1, 5], | ||
| }, | ||
| { | ||
| type: 'area', | ||
| data: [ | ||
| {x: 0, y: 1}, | ||
| {x: 1, y: 5}, | ||
| ], | ||
| expected: [1, 5], | ||
| }, | ||
| { | ||
| type: 'bar-x', | ||
| data: [ | ||
| {x: 0, y: 1}, | ||
| {x: 1, y: 5}, | ||
| ], | ||
| expected: [1, 5], | ||
| }, | ||
| { | ||
| type: 'waterfall', | ||
| data: [ | ||
| {x: 0, y: 1}, | ||
| {x: 1, y: 5}, | ||
| ], | ||
| expected: [1, 5], | ||
| }, | ||
| { | ||
| type: 'scatter', | ||
| data: [ | ||
| {x: 0, y: 1}, | ||
| {x: 1, y: 5}, | ||
| ], | ||
| expected: [1, 5], | ||
| }, | ||
| { | ||
| type: 'bar-y', | ||
| data: [ | ||
| {x: 2, y: 'a'}, | ||
| {x: 8, y: 'b'}, | ||
| ], | ||
| expected: [2, 8], | ||
| }, | ||
| { | ||
| type: 'pie', | ||
| data: [ | ||
| {name: 'a', value: 10}, | ||
| {name: 'b', value: 40}, | ||
| ], | ||
| expected: [10, 40], | ||
| }, | ||
| { | ||
| type: 'heatmap', | ||
| data: [ | ||
| {x: 0, y: 0, value: 10}, | ||
| {x: 1, y: 1, value: 40}, | ||
| ], | ||
| expected: [10, 40], | ||
| }, | ||
| { | ||
| type: 'funnel', | ||
| data: [ | ||
| {name: 'a', value: 10}, | ||
| {name: 'b', value: 40}, | ||
| ], | ||
| expected: [10, 40], | ||
| }, | ||
| { | ||
| type: 'x-range', | ||
| data: [ | ||
| {x0: 0, x1: 5, y: 'a'}, | ||
| {x0: 10, x1: 12, y: 'b'}, | ||
| ], | ||
| expected: [2, 5], | ||
| }, | ||
| ])('computes [min, max] for "$type"', ({type, data, expected}) => { | ||
| const series = [{type, data}] as Series; | ||
|
|
||
| expect(getDomainForContinuousColorScale({series})).toEqual(expected); | ||
| }); | ||
|
|
||
| test('flattens values across multiple series', () => { | ||
| const series = [ | ||
| { | ||
| type: 'scatter', | ||
| data: [ | ||
| {x: 0, y: 1}, | ||
| {x: 1, y: 5}, | ||
| ], | ||
| }, | ||
| { | ||
| type: 'line', | ||
| data: [ | ||
| {x: 0, y: 0}, | ||
| {x: 1, y: 9}, | ||
| ], | ||
| }, | ||
| ] as Series; | ||
|
|
||
| expect(getDomainForContinuousColorScale({series})).toEqual([0, 9]); | ||
| }); | ||
|
|
||
| test.each(['treemap', 'sankey', 'radar'])( | ||
| 'throws for the "%s" series which has no continuous color scale', | ||
| (type) => { | ||
| const series = [{type, data: [{name: 'a', value: 1}]}] as Series; | ||
|
|
||
| expect(() => getDomainForContinuousColorScale({series})).toThrow( | ||
| `The method for calculation a domain for a continuous color scale for the "${type}" series is not defined`, | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ export const barYPlugin: SeriesPlugin<BarYSeries> = { | |
| validateXYSeries({series, xAxis, yAxis}); | ||
| validateStacking({series}); | ||
| }, | ||
| getColorValue: (d) => Number(d.x), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If colorValue is strictly meant to be a number, it might be better practice to handle the type casting outside the plugin closure rather than inside it. |
||
| prepareShapeData, | ||
| renderShapes, | ||
| tooltip: { | ||
|
|
||
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.
It might be beneficial to group these methods into logical blocks, similar to how it's done with the tooltip. Since the list of functions is growing, adding a bit of structure would prevent the flat list from becoming difficult to read.