Kram
Kram

Reputation: 4225

Highcharts adding additional series and axis breaks existing yAxis min/max

In the following fiddle (based on a Highcharts example), the first series shows correctly, taking up the entire height of the chart.

http://jsfiddle.net/markalroberts/h9j53yk4/4/

However, when you click the button to add a series, the existing series' scale is recalculated (not what I want).

The code I use to add the axis/series/data:

chart.addSeries({
        type: 'line',
        yAxis: 1,
        data: [1016, 1016, 1015.9, 1015.5, 1012.3, 1009.5, 1009.6, 1010.2, 1013.1, 1016.9, 1018.2, 1016.7],
    })

How can the existing series/yAxis combo be set to remain as it was originally and just have the new series plotted ontop (also scaled to use the entire height of the chart)

Update

As per Mike's answer, what I had to do was simply:

alignTicks: false

Upvotes: 1

Views: 1250

Answers (1)

Mike Zavarello
Mike Zavarello

Reputation: 3554

Working with multiple axes can be tricky. I found the answer by kjfagan in this Stack Overflow question helpful for your situation: Highcharts y-axis Ceiling not being respected.

What I did in your fiddle was first set only the initial y-axis and give it the minPadding: 0 and maxPadding: 0 attributes as per kjfagan's answer:

yAxis: [{ 
    visible: true, 
    startOnTick: false, 
    endOnTick: false, 
    maxPadding: 0, 
    minPadding: 0
}],

Next, in your buttion, event, I set the full set of attributes for the new axis here, including the minPadding and maxPadding attributes:

$('#button').click(function() {
    c.addAxis({ 
        visible: true, 
        startOnTick: false, 
        endOnTick: false, 
        maxPadding: 0, 
        minPadding: 0, 
        opposite: true });
    c.addSeries({yAxis:1})
    c.xAxis[0].series[1].setData(j.Lines[1].Data);
});

Lastly, as Grzegorz Blachliński thoughtfully suggested, I increased your marginTop for the chart itself.

In addition, I added alignTicks: false to prevent the chart from automatically adjusting the axes to one another's values, which can lead to some wonky or unexpected behavior, depending on what you're plotting.

chart: {
    marginTop: 20,
    alignTicks: false
},

Here's the modified version of your fiddle: http://jsfiddle.net/brightmatrix/h9j53yk4/9/

Please note that clicking the button more than once will add multiple instances of the new axis, which will appear as duplicate axis labels. You also had yAxis declared more than once in the chart options of your original fiddle; I removed the extra option.

One last thing: you'll notice the gridlines from both axes don't quite line up. The answer from Sualkcin in the question I cited above has information on how to handle that (see https://stackoverflow.com/a/23549591/2596103).

I hope this information is helpful for you.

Upvotes: 1

Related Questions