Skip to content

feat(legend): expose sorting function#1644

Merged
markov00 merged 6 commits intoelastic:masterfrom
markov00:2022_04_11-sort_legend
Apr 14, 2022
Merged

feat(legend): expose sorting function#1644
markov00 merged 6 commits intoelastic:masterfrom
markov00:2022_04_11-sort_legend

Conversation

@markov00
Copy link
Copy Markdown
Collaborator

@markov00 markov00 commented Apr 11, 2022

Summary

The ability to sort the legend programmatically is exposed through Settings.legendSort.
The function will be used the same way as the sort compare function in JS where the two values (a, b) passed are the SeriesIdentifiers.

Details

See the storybook story for an usage example

// the sorting value can be part of the dataset or externally defined
  const categoricalIndex: Array<unknown> = ['Apple', 'Orange', 'Banana'];
  const legendSort = (a: SeriesIdentifier, b: SeriesIdentifier) => {
    // extract the value from the accessors to identify the current selected series
    // the SeriesIdentifier can be casted to the chart type specific one
    const categoryA = (a as XYChartSeriesIdentifier)?.splitAccessors?.get(1) ?? '';
    const categoryB = (b as XYChartSeriesIdentifier)?.splitAccessors?.get(1) ?? '';
    // find the index of each series
    const catAIndex = categoricalIndex.indexOf(categoryA);
    const catBIndex = categoricalIndex.indexOf(categoryB);
    // compare the indices and return the order
    return catAIndex - catBIndex;
  };
  return (
    <Chart>
      <Settings showLegend baseTheme={useBaseTheme()} legendSort={legendSort} />

related to elastic/kibana#86184 elastic/kibana#122329

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)
  • New public API exports have been added to packages/charts/src/index.ts
  • 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

@markov00 markov00 added enhancement New feature or request :legend Legend related issue :xy Bar/Line/Area chart related labels Apr 11, 2022
@markov00 markov00 requested review from monfera, nickofthyme and rshen91 and removed request for rshen91 April 12, 2022 08:49
@markov00 markov00 marked this pull request as ready for review April 12, 2022 08:50
Co-authored-by: Nick Partridge <nick.ryan.partridge@gmail.com>
@markov00 markov00 merged commit 128114c into elastic:master Apr 14, 2022
@markov00 markov00 deleted the 2022_04_11-sort_legend branch April 14, 2022 10:23
nickofthyme pushed a commit that referenced this pull request Apr 14, 2022
# [46.0.0](v45.1.1...v46.0.0) (2022-04-14)

### Bug Fixes

* **axis:** ticks generation for linear scale on bar charts ([#1645](#1645)) ([65d0e7d](65d0e7d))
* **axis:** use correct desired tick count based on axis type ([#1646](#1646)) ([512a6cd](512a6cd))
* **deps:** update dependency @elastic/eui to v53 ([#1639](#1639)) ([34bf325](34bf325))
* **deps:** update dependency @elastic/eui to v54 ([#1642](#1642)) ([6eaca0a](6eaca0a))

### Features

* **axes:** option to fit domain to list of annotation `SpecIds` ([#1641](#1641)) ([220350d](220350d))
* **goal:** auto generated linear ticks ([#1637](#1637)) ([5437d8e](5437d8e))
* **legend:** expose sorting function ([#1644](#1644)) ([128114c](128114c))

### BREAKING CHANGES

* **goal:** goal chart now requires domain min and max to be defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request :legend Legend related issue :xy Bar/Line/Area chart related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants