Skip to content
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

fix(legend): legend sizes with ordinal data #867

Merged
merged 5 commits into from
Oct 20, 2020

Conversation

rshen91
Copy link
Contributor

@rshen91 rshen91 commented Oct 13, 2020

Summary

Fixes #811
Before this PR, if the legend has LegendExtra and no spacingBuffer specified in the theme, the labels in the legend will be truncated when hovering over the series.

Oct-15-2020 07-53-03

After the fix, the legend is sized to correctly show the labels and values for the legend:
Oct-16-2020 15-40-10

Checklist

Delete any items that are not applicable to this PR.

  • 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

@rshen91 rshen91 changed the title fix: remove early return for ordinal lastValues fix(legend): legend sizes with ordinal data Oct 13, 2020
@codecov-io
Copy link

codecov-io commented Oct 13, 2020

Codecov Report

Merging #867 into master will increase coverage by 0.19%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #867      +/-   ##
==========================================
+ Coverage   69.56%   69.76%   +0.19%     
==========================================
  Files         321      336      +15     
  Lines       10630    10269     -361     
  Branches     2195     1964     -231     
==========================================
- Hits         7395     7164     -231     
+ Misses       3226     3038     -188     
- Partials        9       67      +58     
Flag Coverage Δ
#unittests ?

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

Impacted Files Coverage Δ
src/chart_types/xy_chart/state/utils/utils.ts 92.27% <100.00%> (-0.01%) ⬇️
src/utils/dimensions.ts 71.42% <0.00%> (-28.58%) ⬇️
src/utils/data_generators/data_generator.ts 45.45% <0.00%> (-9.10%) ⬇️
...rc/chart_types/xy_chart/renderer/canvas/bubbles.ts 64.70% <0.00%> (-7.52%) ⬇️
src/components/portal/utils.ts 21.42% <0.00%> (-7.15%) ⬇️
.../partition_chart/state/selectors/compute_legend.ts 79.06% <0.00%> (-5.72%) ⬇️
...c/chart_types/xy_chart/annotations/rect/tooltip.ts 89.47% <0.00%> (-5.53%) ⬇️
...state/selectors/get_internal_is_tooltip_visible.ts 75.00% <0.00%> (-5.00%) ⬇️
src/components/tooltip/tooltip.tsx 59.37% <0.00%> (-4.63%) ⬇️
src/components/chart_status.tsx 87.50% <0.00%> (-4.17%) ⬇️
... and 181 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 3bdd1ee...77a11ba. Read the comment docs.

1 similar comment
@codecov-commenter
Copy link

Codecov Report

Merging #867 into master will increase coverage by 0.19%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #867      +/-   ##
==========================================
+ Coverage   69.56%   69.76%   +0.19%     
==========================================
  Files         321      336      +15     
  Lines       10630    10269     -361     
  Branches     2195     1964     -231     
==========================================
- Hits         7395     7164     -231     
+ Misses       3226     3038     -188     
- Partials        9       67      +58     
Flag Coverage Δ
#unittests ?

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

Impacted Files Coverage Δ
src/chart_types/xy_chart/state/utils/utils.ts 92.27% <100.00%> (-0.01%) ⬇️
src/utils/dimensions.ts 71.42% <0.00%> (-28.58%) ⬇️
src/utils/data_generators/data_generator.ts 45.45% <0.00%> (-9.10%) ⬇️
...rc/chart_types/xy_chart/renderer/canvas/bubbles.ts 64.70% <0.00%> (-7.52%) ⬇️
src/components/portal/utils.ts 21.42% <0.00%> (-7.15%) ⬇️
.../partition_chart/state/selectors/compute_legend.ts 79.06% <0.00%> (-5.72%) ⬇️
...c/chart_types/xy_chart/annotations/rect/tooltip.ts 89.47% <0.00%> (-5.53%) ⬇️
...state/selectors/get_internal_is_tooltip_visible.ts 75.00% <0.00%> (-5.00%) ⬇️
src/components/tooltip/tooltip.tsx 59.37% <0.00%> (-4.63%) ⬇️
src/components/chart_status.tsx 87.50% <0.00%> (-4.17%) ⬇️
... and 181 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 3bdd1ee...77a11ba. Read the comment docs.

Comment on lines 167 to 169
if (xDomain.scaleType === ScaleType.Ordinal) {
lastValues.set(seriesKey, { y0: initialY0, y1: initialY1 });
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this is needed. Wouldn't it just set the correct values without this?

Copy link
Contributor Author

@rshen91 rshen91 Oct 19, 2020

Choose a reason for hiding this comment

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

Agreed - removed in a054fdc

lastValue: LastValues,
formatter: (value: any, options?: TickFormatterOptions | undefined) => string,
) {
return showLegendExtra || xScaleType === 'ordinal'
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can't check the type here because the legendSizeSelector needs to know these values, to fix the issue. You could check the xScaleType before rendering in the LegendItem component.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another way to fix this would be to add a sizingLabel prop to this and use that value in the legendSizeSelector instead of formatted.

Copy link
Contributor Author

@rshen91 rshen91 Oct 19, 2020

Choose a reason for hiding this comment

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

Commit a054fdc and discussed in zoom 🙇‍♀️

Comment on lines -113 to -116
defaultExtra: {
raw: lastValue && lastValue.y0 !== null ? lastValue.y0 : null,
formatted: lastValue && lastValue.y0 !== null ? formatter(lastValue.y0) : null,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Careful with extracting this, the value here is y0 not y1 like the function returns

Copy link
Contributor Author

@rshen91 rshen91 Oct 19, 2020

Choose a reason for hiding this comment

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

Thanks! Commit a054fdc for changes

@rshen91 rshen91 added the :legend Legend related issue label Oct 16, 2020
@codecov-io
Copy link

codecov-io commented Oct 16, 2020

Codecov Report

Merging #867 into master will increase coverage by 0.53%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #867      +/-   ##
==========================================
+ Coverage   69.55%   70.09%   +0.53%     
==========================================
  Files         321      336      +15     
  Lines       10632    10920     +288     
  Branches     2196     2238      +42     
==========================================
+ Hits         7395     7654     +259     
- Misses       3228     3251      +23     
- Partials        9       15       +6     
Flag Coverage Δ
#unittests 69.58% <100.00%> (+0.03%) ⬆️

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

Impacted Files Coverage Δ
src/chart_types/xy_chart/state/utils/utils.ts 92.18% <ø> (-0.10%) ⬇️
src/chart_types/xy_chart/legend/legend.ts 87.50% <100.00%> (+6.54%) ⬆️
...t_types/xy_chart/state/selectors/compute_legend.ts 100.00% <100.00%> (ø)
...y_chart/state/selectors/get_legend_items_labels.ts 100.00% <100.00%> (ø)
src/mocks/store/store.ts 92.59% <0.00%> (ø)
src/mocks/specs/index.ts 100.00% <0.00%> (ø)
src/mocks/scale/index.ts 100.00% <0.00%> (ø)
src/mocks/series/index.ts 100.00% <0.00%> (ø)
src/mocks/index.ts 100.00% <0.00%> (ø)
src/mocks/store/index.ts 100.00% <0.00%> (ø)
... and 9 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 eda6889...6b57387. Read the comment docs.

@rshen91 rshen91 marked this pull request as ready for review October 19, 2020 13:51
Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

Awesome! LGTM, I made one comment on the tests to fix but other than that I approve.

Comment on lines 356 to 382
it('should return correct legendSizingLabel with linear scale and showExtraLegend set to true', () => {
const formatter = (d: string | number) => `${Number(d).toFixed(2)} dogs`;
const lastValues = { y0: null, y1: 14 };
const showExtraLegend = true;
const xScaleIsLinear = ScaleType.Linear;

expect(getLegendExtra(showExtraLegend, xScaleIsLinear, formatter, 'y1', lastValues)).toMatchObject({
legendSizingLabel: '14.00 dogs',
});
});
it('should return formatted to null with ordinal scale and showExtraLegend set to true', () => {
const formatter = (d: string | number) => `${Number(d).toFixed(2)} dogs`;
const lastValues = { y0: null, y1: 14 };

expect(getLegendExtra(true, ScaleType.Ordinal, formatter, 'y1', lastValues)).toMatchObject({
formatted: null,
});
});
it('should return legendSizingLabel null with showLegendExtra set to false', () => {
const formatter = (d: string | number) => `${Number(d).toFixed(2)} dogs`;
const lastValues = { y0: null, y1: 14 };
const showLegendExtra = false;

expect(getLegendExtra(showLegendExtra, ScaleType.Ordinal, formatter, 'y1', lastValues)).toMatchObject({
legendSizingLabel: null,
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should assert all three parts of the extraValue for all of these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! Addressed in commit 6b57387

@rshen91 rshen91 merged commit 7559e0d into elastic:master Oct 20, 2020
@rshen91 rshen91 deleted the legend-sizing branch October 20, 2020 17:33
@rshen91 rshen91 added the bug Something isn't working label Oct 20, 2020
markov00 pushed a commit that referenced this pull request Nov 24, 2020
# [24.1.0](v24.0.0...v24.1.0) (2020-11-24)

### Bug Fixes

* **area_charts:** correctly represent baseline with negative data points ([#896](#896)) ([d1243f1](d1243f1))
* **legend:** legend sizes with ordinal data ([#867](#867)) ([7559e0d](7559e0d)), closes [#811](#811)
* render orphan data points on lines and areas ([#900](#900)) ([0be282b](0be282b)), closes [#783](#783)
* specs swaps correctly reflected in state ([#901](#901)) ([7fba882](7fba882))

### Features

* **legend:** allow legend text to be copyable ([#877](#877)) ([9cd3459](9cd3459)), closes [#710](#710)
* allow clearing series colors from memory ([#899](#899)) ([ab1af38](ab1af38))
* merge series domain with the domain of another group ([#912](#912)) ([325b013](325b013))
* small multiples for XY charts (alpha) ([#793](#793)) ([d288208](d288208)), closes [#500](#500) [#500](#500)
@markov00
Copy link
Member

🎉 This PR is included in version 24.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markov00 markov00 added the released Issue released publicly label Nov 24, 2020
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
# [24.1.0](elastic/elastic-charts@v24.0.0...v24.1.0) (2020-11-24)

### Bug Fixes

* **area_charts:** correctly represent baseline with negative data points ([opensearch-project#896](elastic/elastic-charts#896)) ([b622fda](elastic/elastic-charts@b622fda))
* **legend:** legend sizes with ordinal data ([opensearch-project#867](elastic/elastic-charts#867)) ([74bcbad](elastic/elastic-charts@74bcbad)), closes [opensearch-project#811](elastic/elastic-charts#811)
* render orphan data points on lines and areas ([opensearch-project#900](elastic/elastic-charts#900)) ([3e2c739](elastic/elastic-charts@3e2c739)), closes [opensearch-project#783](elastic/elastic-charts#783)
* specs swaps correctly reflected in state ([opensearch-project#901](elastic/elastic-charts#901)) ([a94347f](elastic/elastic-charts@a94347f))

### Features

* **legend:** allow legend text to be copyable ([opensearch-project#877](elastic/elastic-charts#877)) ([21a96d3](elastic/elastic-charts@21a96d3)), closes [opensearch-project#710](elastic/elastic-charts#710)
* allow clearing series colors from memory ([opensearch-project#899](elastic/elastic-charts#899)) ([e97f4ab](elastic/elastic-charts@e97f4ab))
* merge series domain with the domain of another group ([opensearch-project#912](elastic/elastic-charts#912)) ([716ad5a](elastic/elastic-charts@716ad5a))
* small multiples for XY charts (alpha) ([opensearch-project#793](elastic/elastic-charts#793)) ([3b88e1c](elastic/elastic-charts@3b88e1c)), closes [opensearch-project#500](elastic/elastic-charts#500) [opensearch-project#500](elastic/elastic-charts#500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working :legend Legend related issue released Issue released publicly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad legend sizing with long showLegendExtra values - Ordinal
5 participants