question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customize legend labels in Stream component

See original GitHub issue

Hello, I’ve read the docs for Stream and Legend but did not find anything regarding how to customize legend labels.

I’ve a situation like this:

const data = [
  {
    JD: 10500,
    PC: 6700,
	date: '2018-09'
  },
  {
    JD: 7860,
    PC: 5430,
    date: '2018-08'
  }
];

const sellers = [
  {
    id: 'JD',
    name: 'John Dorian'
  },
  {
    id: 'PC',
    name: 'Percival Cox'
  }
];

const keys = sellers.map(s => s.id);

There is a way to display the name of each seller inside the legend instead of their id apart from mapping the data array and change it to use seller’s name as keys instead of their id?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
plouccommented, Mar 20, 2019

I decided to use keys for legends for the sake of simplicity, I don’t see why it’s ridiculous as it’s totally fine to have a key named 'With Something'. And if your data storage doesn’t allow to use such keys, you can easily map the data prior to passing it to the chart:

const mapped = useMemo(() => data.map(d => ({
    ...d,
    'With Something': d.withSomething,
    'Without Something': d.withoutSomething,
})), [data])

useMemo is optional, but it should be used to prevent unnecessary renderings.

That being said, I admit it could be nice to override the labels/legends… but using a simple mapping can be limiting, for example you won’t be able to compute those depending on the values, maybe the lib could support a get(Label|Legend) property which receive the whole datum.

5reactions
WeHateNickcommented, Aug 28, 2019

@plouc I think a pattern like get(label|legend) is necessary.

For instance, I have am using a very large dataset with two letter states for keys like so:

const data = [
  nv: '300',
  al: '200',
  az: '700',
...
]

const keys = ['nv', 'al', 'az',]

Now if I want my labels to show as “Nevada, Alabama, Arizona” it’s not as simple as just mapping over my keys that I pass into the keys prop of BarChart. I would now have to change the keys of my whole dataset in data so that they match keys. This is untenable for very large datasets and seems like a really bad pattern to be manipulating my actual dataset for something that is purely presentational.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GGPlot Legend Title, Position and Labels - Datanovia
This R graphics tutorial shows how to customize a ggplot legend. ... Rename legend labels and change the order of items in a...
Read more >
How to Customize Legend With GenerateLabels in Chart JS
How to Customize Legend With GenerateLabels in Chart JSIn this video we will explore how to customize legend with generatelabels in chart js ......
Read more >
Work with a legend item—ArcGIS Pro | Documentation
If a legend item does not include labels, the count appears adjacent to the patch. Use the text boxes to customize the text...
Read more >
Solved: Custom name legend - Microsoft Power BI Community
Custom name legend · Click your chart, and click the small triange next to the measure name on fields tab. · Click the...
Read more >
Legend in React Chart component - Syncfusion
You can also customize the fontStyle , size , fontWeight , color , textAlignment , fontFamily , opacity and textOverflow of legend title....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found