tremorlabs / tremor

React components to build charts and dashboards
https://tremor.so
Apache License 2.0
15.39k stars 446 forks source link

[Feature]: Way to compose the Legend labels? #978

Open nicostombros opened 2 months ago

nicostombros commented 2 months ago

What problem does this feature solve?

It would be great to be able to pass children into Legend component such that we can create our own labels. At the moment, I use Legend like the following

<Legend
   categories={data.map((d) => d.name)}
   colors={data.map((d) => d.color)}
   className='h-auto flex flex-col'
/>

However, there are cases where I'd want to greatly customise the Legend labels. For instance, I'd like to be able to include icons for each of my labels, or to use a Tailwind gradient as the label color. Example of how this might work from a dev standpoint is:

<Legend
   className='h-auto flex flex-col'
>
  {data.map(d => (
    <LegendLabel color="green" className="...">{d.icon}{d.title}</LegendLabel>
  ))}
</Legend>

If this is actually possible then please let me know, as I couldn't find documentation for that.

What does the proposed API look like?

No response

abdelkd commented 2 months ago
<Legend
   className='h-auto flex flex-col'
>
  {({ data }) => (
    data.map(d => (
      <LegendLabel color="green" className="...">{d.icon}{d.title}</LegendLabel>
    ))}
  )}
</Legend>

I think this might be a good API or a renderItem prop that would give item as an argument. something like this

<Legend
   className='h-auto flex flex-col'
   categories={categories}
   colors={colors}
   renderItem={(item) => <LegendLabel color="green" className="...">{d.icon}{d.title}</LegendLabel>}
/>
ctwillie commented 2 months ago

+1

severinlandolt commented 1 month ago

Interesting idea, will be taking a look at it for one of our next releases.

abdelkd commented 1 month ago

@severinlandolt I would love to work on it once it's decided to be implemented

severinlandolt commented 1 month ago

@abdelkd Sounds cool, I would definitely go down the suggested the route of using an optional render prop. Happy to have working on a PR :)

abdelkd commented 1 month ago

@severinlandolt I've added a renderItem prop which takes a function that has two parameters name and Circle, the latter is a component that renders the circle found in legend, so users can manipulate text without loosing circle if it's wanted. do you have other things to sugges?

severinlandolt commented 1 month ago

Thanks, will be taking a look at it for v3.16 :)