Apexcharts.js: Question: Access chart data inside legend

Created on 28 Aug 2018  路  8Comments  路  Source: apexcharts/apexcharts.js

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.

Most helpful comment

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.

All 8 comments

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.

Sweet! Thanks 馃檹

Followup question: Is it possible to pass in custom markup for the labels?

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

Ah I see, thanks!

opts.globals NOT WORKING now working with opts.w.globals

The params have changed a little bit for legend.formatter function in the last update v2.2.1
Please see the docs - https://apexcharts.com/docs/options/legend/#formatter

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

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

Is not working anymore? I need it...

I am trying to return an array from the legend formatter, but I am getting a type error

(method) ApexLegend.formatter?(legendName: string, opts?: any): string
Type '(val: string) => string | string[]' is not assignable to type '(legendName: string, opts?: any) => string'.
  Type 'string | string[]' is not assignable to type 'string'.
    Type 'string[]' is not assignable to type 'string'.ts(2322)
apex-types.d.ts(643, 5): The expected type comes from property 'formatter' which is declared here on type 'ApexLegend'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

frlinw picture frlinw  路  3Comments

rudeayelo picture rudeayelo  路  3Comments

jlil picture jlil  路  3Comments

jeroenpol picture jeroenpol  路  3Comments

felixalguzman picture felixalguzman  路  3Comments