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): width with duplicate nested pie slice labels #1585

Merged
merged 14 commits into from
Feb 23, 2022

Conversation

nickofthyme
Copy link
Collaborator

@nickofthyme nickofthyme commented Feb 9, 2022

Summary

fixes a bug returning a zero-width legend for identical legend labels using legendMaxDepth option.

Before

image

After

image

Details

The legend label logic in partition charts was understandably simplified to assume the formatted labels across all depths are unique. However, this simplification runs into issues whenever two or more labels across any layer or any depth are identical.

if (formattedValue && formattedValue !== HIERARCHY_ROOT_KEY) {
// save only the max depth, so we can compute the the max extension of the legend
keys.set(formattedValue, Math.max(depth, keys.get(formattedValue) ?? 0));
}

The logic above creates a keys Map to track the depths across layers and nodes using the formatted label as the key. So in the case where the label is the same for two separate layers (as pictured above), the depth count is incorrectly combined to 2.

// current
const legenLabels: LegendItemLabel[] = [
  {
    'label': 'Testing a super duper really long legend',
    'depth': 2
  }
]

For the current case, this becomes a problem when limiting the depth using legendMaxDepth in which the depth count is now incorrectly 2 and thus returns [] causing the legend width to be 0.

export function getLegendLabels(layers: Layer[], tree: HierarchyOfArrays, legendMaxDepth: number) {
return flatSlicesNames(layers, 0, tree).filter(({ depth }) => depth <= legendMaxDepth);
}

The solution -- prevent label collisions using a key derived from the arrayNode.path returning all legend labels as a unique value in the labels array. So the example able would be separated into two where the first is at a depth of 1 and the second is at a depth of 2

// expected
const legenLabels: LegendItemLabel[] = [
  {
    'label': 'Testing a super duper really long legend',
    'depth': 1
  },
  {
    'label': 'Testing a super duper really long legend',
    'depth': 2
  }
]

I confirmed the functionality of the legend highlight strategy is working as expected with this unique label change.

Issues

fix #1584

Checklist

  • The proper chart type label has been added (e.g. :xy, :partition)
  • The proper feature labels have been added (e.g. :interactions, :axis)
  • All related issues have been linked (i.e. closes #123, fixes #123)
  • Unit tests have been added or updated to match the most common scenarios
  • The proper documentation and/or storybook story has been added or updated
  • The code has been checked for cross-browser compatibility (Chrome, Firefox, Safari, Edge)

@nickofthyme nickofthyme added :legend Legend related issue :partition Partition/PieChart/Donut/Sunburst/Treemap chart related labels Feb 9, 2022
@nickofthyme
Copy link
Collaborator Author

@markov00 can you review and approve this?

Copy link
Member

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

LGTM

@nickofthyme
Copy link
Collaborator Author

2 Playwright tests are failing only in the CI. Merging for now and will troubleshoot in a separate PR.

@nickofthyme nickofthyme changed the title fix(legend): width with duplicate pie slice labels fix(legend): width with duplicate nested pie slice labels Feb 23, 2022
@nickofthyme nickofthyme merged commit 869c572 into elastic:master Feb 23, 2022
@nickofthyme nickofthyme deleted the fix-pie-legend-width branch February 23, 2022 17:46
nickofthyme pushed a commit that referenced this pull request Mar 1, 2022
# [44.0.0](v43.1.1...v44.0.0) (2022-03-01)

### Bug Fixes

* **axis:** correct tick alignment in ordinal scale with numeric values ([#1609](#1609)) ([915349d](915349d))
* **legend:** width with duplicate nested pie slice labels ([#1585](#1585)) ([1073231](1073231))
* **partition:** consider legend extras when computing the legend size ([#1611](#1611)) ([2078f3d](2078f3d))
* **xy:** dataIndex keeps original data order on small multiples ([#1597](#1597)) ([9e2566c](9e2566c))

### Features

* **api:** expose Predicate enum ([#1574](#1574)) ([1f73eec](1f73eec))
* **heatmap:** allow rotation of x axis labels ([#1514](#1514)) ([b655156](b655156))

### BREAKING CHANGES

* **heatmap:** `width`, `align`, and `baseline` style properties are removed from the `xAxisLabels` and `yAxisLabels` style of the Heatmap theme.

Co-authored-by: Marco Vettorello <vettorello.marco@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:legend Legend related issue :partition Partition/PieChart/Donut/Sunburst/Treemap chart related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad legend sizing when using 2 identical labels
2 participants