tingo5
Posts: 8
Joined: Fri Nov 05, 2021 9:53 pm

How to scale Y Axis for only one Series?

This may not be specific to Stock...

I have a series and an SMA 200:

Code: Select all

          series: [{
                type: 'candlestick',
                name: chart.symbol,
                id: chart.symbol,
                data: data.futures_data,
                lineWidth: 1,
                // pointWidth: 2.0,
                // pointPadding: 0.2
            },
            {
                type: 'sma',
                color: 'rgba(0, 160, 0, .7)',
                lineWidth: 1,
                linkedTo: chart.symbol,
                params: {
                    period: 200
                },
                enableMouseTracking: false
            }
The Y axis is automatically scaled:

Code: Select all

        yAxis: [
            { // Primary Y Axis
                labels: {
                    x: 70,
                    format: numberFormat,
                }
            }]
Sometimes the 200 SMA is far away from the candlesticks, causing the Y Axis to expand and squeeze the candlesticks.

I do not want to scale the Y Axis with the SMA 200. If it falls outside of the chart that is fine.

So, how can I set the Y Axis so that it only uses the candlesticks to automatically scale?

I tried manually setting the Y Axis within the load event using

Code: Select all

this.yAxis[0].setExtremes
, but that seems like a bad way of doing it. In addition, it does not update when I scroll the chart.
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: How to scale Y Axis for only one Series?

Hello,

Could you reproduce the issue in an online editor that I could work on? Now it's hard to tell what the problem is without taking a look at your code.

Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
tingo5
Posts: 8
Joined: Fri Nov 05, 2021 9:53 pm

Re: How to scale Y Axis for only one Series?

Image

Please click on 1m to zoom in and see the issue.

When the SMA is far from the price series, the Y axis scales to include the SMA.

I want to show only the Price series.

https://jsfiddle.net/geazqvtn/1/

I want it to look like this. The SMA is outside the Y Min and Max:

Image
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: How to scale Y Axis for only one Series?

Hello,

In that case you can use xAxis.events.afterSetExtremes() callback function to update SMA series visibility after zooming.

Demo: https://jsfiddle.net/BlackLabel/nzwo9ej7/
API: https://api.highcharts.com/highmaps/xAx ... etExtremes

Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
tingo5
Posts: 8
Joined: Fri Nov 05, 2021 9:53 pm

Re: How to scale Y Axis for only one Series?

> In that case you can use xAxis.events.afterSetExtremes() callback function to update SMA series visibility after zooming.

I don't want to hide the SMA.
tingo5
Posts: 8
Joined: Fri Nov 05, 2021 9:53 pm

Re: How to scale Y Axis for only one Series?

I gather you are going to say use setExtremes() to set the Y Axis upon redraw.

This doesn't work. I gather setExtremes() calls redraw().
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: How to scale Y Axis for only one Series?

Hi,

Well, if you don't want to hide that series and only zoom to the candlestick series, I suggest using yAxis.setExtremes() but not in the renderer, but in the afterSetExtremes() callback function only with a flag so as not to fall into an infinite loop.

Demo: https://jsfiddle.net/BlackLabel/3hba9emd/
API: https://api.highcharts.com/class-refere ... etExtremes

Let me know if that was what you were looking for!
Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
tingo5
Posts: 8
Joined: Fri Nov 05, 2021 9:53 pm

Re: How to scale Y Axis for only one Series?

Why do you need to use that hacky code when using afterSetExtremes callback?
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: How to scale Y Axis for only one Series?

Hi

Because the setExtremes() method calls the afterSetExtremes() callback function, and without such a way, the first time this function is called, it would create an infinite loop.

Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/

Return to “Highcharts Stock”