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.

Question: Access chart data inside legend

See original GitHub issue

Hi,

is it possible to access the chart data inside the legend? For example I want to show the individual value next to the legend text in a doughnut chart.

labels: ['A', 'B', 'C']
series: [100, 20, 55]

And the legend should display:

🔴  A - 100
🔵  B - 20
⚫️  C - 55

However inside the legend.formatter I have only access to the label itself.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
junedchhipacommented, Nov 12, 2018

Very good point and I have already taken that into consideration. You can do it like this

legend: {
    formatter: function(label, opts) {
        return label + " - " + opts.w.globals.series[opts.seriesIndex]
    }
}

screen shot 2018-08-28 at 5 31 14 pm

Will add in the docs.

2reactions
junedchhipacommented, Nov 12, 2018

Not directly, but there is another way you can style your legend text elements.

If you return an array in the legend.formatter function, your texts are split into different tspan elements inside the parent <text> element. So, you can then target them with CSS and apply styles.

Here is how you would format the legend.formatter function

legend: {
    formatter: function(val, opts) {
        return [val, " - ", opts.w.globals.series[opts.seriesIndex]]
    }
}

and then you can apply CSS like

.apexcharts-legend-text tspan:nth-child(1) {
    font-weight: lighter;
    fill: #999;
}

.apexcharts-legend-text tspan:nth-child(3) {
    font-weight: bold;
}

which will result in legend like these screen shot 2018-08-28 at 6 36 50 pm

Read more comments on GitHub >

github_iconTop Results From Across the Web

Legend labels not displaying in chart with dynamic row source
The chart has a legend that shows the name of each agency with a color ... I get an error "The Microsoft Access...
Read more >
AmCharts: How to access values showing in legend
To access the legend values, you have to create a valueFunction inside the legend object: "legend": { // .
Read more >
How To Add and Remove Legends In Excel Chart? - eduCBA
Guide to Legends in Chart. Tutorials on how to add, remove and change the position of legends in excel chart with examples and...
Read more >
Chart Elements
A chart's legend shows what kind of data is represented in the chart. By default, the text that appears in the legend is...
Read more >
How To Add a Legend to a Chart in Excel (2 Methods, FAQs)
You can use a legend to show the same data differently, or it can clarify what the various axes, colors or labels on...
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