Boosted_d16
Boosted_d16

Reputation: 14082

if statement in Highcharts

I want to reverse the legend order dynamically based on the legend's 'align' option but I'm a beginner in javascript, I have no idea how it would look like.

If the legend's align is set to 'left' or 'right' then I want to set the legend's 'reverse' option to 'true' else 'false'

Here's my jsfillde: https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/legend/reversed/

In python I would do something like this:

if legend align == “left” or “right”:
     legend reverse = true
else:
     legend reverse = false

Upvotes: 0

Views: 1454

Answers (1)

ppotaczek
ppotaczek

Reputation: 39099

You need to use the if - else statement outside the chart configuration object:

var options = {
    legend: {
        align: 'right',
        ...
    },

    ...
}

if (
    options.legend.align === 'left' ||
    options.legend.align === 'right'
) {
    options.legend.reversed = true;
} else {
    options.legend.reversed = false;
}

Highcharts.chart('container', options);

Live demo: https://jsfiddle.net/BlackLabel/c1jhtoys/

Related question: "this" inside object

Upvotes: 1

Related Questions