Skip to content

feat: allow use of functions for y, y0, split and stack accessors#943

Merged
nickofthyme merged 8 commits intoelastic:masterfrom
nickofthyme:feat/fn-accessors
Dec 9, 2020
Merged

feat: allow use of functions for y, y0, split and stack accessors#943
nickofthyme merged 8 commits intoelastic:masterfrom
nickofthyme:feat/fn-accessors

Conversation

@nickofthyme
Copy link
Copy Markdown
Collaborator

@nickofthyme nickofthyme commented Dec 8, 2020

Summary

closes #808, closes #838, related to #837

Allows for functional accessors (AccessorFn) on yAccessors, y0Accessors, splitSeriesAccessors and stackAccessors.

Note: The stackAccessors values are not yet used, currently treated as boolean value (i.e. length > 0)

<BarSeries
  id="bars1"
  xAccessor={(d) => d.x}
  yAccessors={[(d) => d.y]}
  splitSeriesAccessors={[(d) => d.g]}
  data={data}
/>

Complex example with seriesName

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).

Checklist

  • Any consumer-facing exports were added to src/index.ts (and stories only import from ../src except for test data & storybook)
  • Proper documentation or storybook story was added for features that require explanation or tutorials
  • Unit tests were updated or added to match the most common scenarios

@nickofthyme nickofthyme added enhancement New feature or request :data Data/series/scales related issue :xy Bar/Line/Area chart related labels Dec 8, 2020
@codecov-io
Copy link
Copy Markdown

codecov-io commented Dec 8, 2020

Codecov Report

Merging #943 (81b798f) into master (3c823fd) will increase coverage by 0.53%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            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     
Flag Coverage Δ
unittests 70.14% <77.77%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/chart_types/xy_chart/utils/specs.ts 100.00% <ø> (ø)
src/utils/accessor.ts 81.81% <ø> (ø)
src/chart_types/xy_chart/utils/series.ts 83.39% <77.77%> (-0.25%) ⬇️
src/mocks/scale/index.ts 100.00% <0.00%> (ø)
src/mocks/utils.ts 100.00% <0.00%> (ø)
src/mocks/index.ts 100.00% <0.00%> (ø)
src/mocks/store/store.ts 92.59% <0.00%> (ø)
src/mocks/annotations/annotations.ts 73.33% <0.00%> (ø)
src/mocks/series/utils.ts 100.00% <0.00%> (ø)
src/mocks/theme.ts 100.00% <0.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3c823fd...b75b8ca. Read the comment docs.

Copy link
Copy Markdown
Collaborator

@markov00 markov00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Nick for taking the ownership of this feature.
It looks great, I've written few comments I'd like to discuss before merging

*/
export type UnaryAccessorFn<Return = any> = (datum: Datum) => Return;
export interface UnaryAccessorFn<Return = any> {
seriesName?: string;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the comment relate to the accessor seriesName, this can be renamed to something like:
getAccessorName or getAccessorFieldName

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +67 to +76
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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you change the seriesName field, remember to change also this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +382 to +383
const accessorStr = getAccessorString(accessor, index);
splitAccessors.set(accessorStr, value);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@nickofthyme nickofthyme merged commit 22425d3 into elastic:master Dec 9, 2020
@nickofthyme nickofthyme deleted the feat/fn-accessors branch December 9, 2020 17:08
markov00 pushed a commit that referenced this pull request Dec 9, 2020
# [24.4.0](v24.3.0...v24.4.0) (2020-12-09)

### Bug Fixes

* empty labels on debug state ([#940](#940)) ([3c823fd](3c823fd))

### Features

* allow use of functions for y, y0, split and stack accessors ([#943](#943)) ([22425d3](22425d3))
@markov00
Copy link
Copy Markdown
Collaborator

markov00 commented Dec 9, 2020

🎉 This PR is included in version 24.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markov00 markov00 added the released Issue released publicly label Dec 9, 2020
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
# [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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:data Data/series/scales related issue enhancement New feature or request released Issue released publicly :xy Bar/Line/Area chart related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow functional accessors on y, y0, mark and split Allow non-primitive data values

3 participants