feat: allow use of functions for y, y0, split and stack accessors#943
feat: allow use of functions for y, y0, split and stack accessors#943nickofthyme merged 8 commits intoelastic:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #943 +/- ##
==========================================
+ Coverage 70.13% 70.66% +0.53%
==========================================
Files 342 359 +17
Lines 11026 11342 +316
Branches 2391 2416 +25
==========================================
+ Hits 7733 8015 +282
- Misses 3279 3307 +28
- Partials 14 20 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
markov00
left a comment
There was a problem hiding this comment.
Thanks Nick for taking the ownership of this feature.
It looks great, I've written few comments I'd like to discuss before merging
src/utils/accessor.ts
Outdated
| */ | ||
| export type UnaryAccessorFn<Return = any> = (datum: Datum) => Return; | ||
| export interface UnaryAccessorFn<Return = any> { | ||
| seriesName?: string; |
There was a problem hiding this comment.
seriesName doesn't sound related to the accessor function. A series describe one or multiple objects made up of one or multiple fields and values. Here instead you are requesting a name for an accessor.
We can also barely specify it as a name or fieldName
There was a problem hiding this comment.
Yeah good point. I don't wanna use name because they could unknowingly set the name based on the variable name of the function itself. See 4b6876c
| * @param index | ||
| * @internal | ||
| */ | ||
| export function getAccessorString(accessor: Accessor | AccessorFn, index: number) { |
There was a problem hiding this comment.
Similar to the comment relate to the accessor seriesName, this can be renamed to something like:
getAccessorName or getAccessorFieldName
| yAccessorFn.seriesName = 'simple y value'; | ||
|
|
||
| // complex example | ||
| const yAccessorFn: AccessorFn = ({ range }) => \`\${range.to} - \${range.from}\`; | ||
| yAccessorFn.seriesName = 'complex y value'; | ||
| \`\`\` | ||
|
|
||
| If no \`seriesName\` is provided, the default value will be set using the index \`(index:0)\`. | ||
|
|
||
| Try changing the \`seriesName\` for the y and split accessor functions in the storybook knobs. |
There was a problem hiding this comment.
In case you change the seriesName field, remember to change also this
| const accessorStr = getAccessorString(accessor, index); | ||
| splitAccessors.set(accessorStr, value); |
There was a problem hiding this comment.
Here there can be an issue when the seriesName of an accessor function is the same as the key of another accessor.
On the provided story, if you use splitAccessorFn.seriesName = 'g1 the splitSeriesAccessors={['g1', splitAccessorFn]} will wrongly split the data.
I'm not sure if we need to prevent this or just discourage this practice.
There was a problem hiding this comment.
Yeah, that's tough to avoid. I think it's the same with duplicate Accessor strings in that we cannot really avoid it. I added a note to the story but maybe we can figure out a better way in the future (81b798f)
|
🎉 This PR is included in version 24.4.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
# [24.4.0](elastic/elastic-charts@v24.3.0...v24.4.0) (2020-12-09) ### Bug Fixes * empty labels on debug state ([opensearch-project#940](elastic/elastic-charts#940)) ([3d1281b](elastic/elastic-charts@3d1281b)) ### Features * allow use of functions for y, y0, split and stack accessors ([opensearch-project#943](elastic/elastic-charts#943)) ([e881723](elastic/elastic-charts@e881723))
Summary
closes #808, closes #838, related to #837
Allows for functional accessors (
AccessorFn) onyAccessors,y0Accessors,splitSeriesAccessorsandstackAccessors.Complex example with
seriesNameIf no
seriesNameis provided, the default value will be set using the index(index:0).Checklist
src/index.ts(and stories only import from../srcexcept for test data & storybook)