4

I'm using apexcharts v3.19.3 and I would like to know how to limit my zoom out option to the available x-axis range. Currently, I can zoom out indefinitely. To summarize my request I can zoom out till my graph shrinks

The example code in apexcharts docs will be enough to recreate the issue. Keep zooming out to recreate my problem

Ankur Datta
  • 161
  • 2
  • 6

1 Answers1

5

Well, after some digging around I solved the problem. Leaving the solution here. So, Apex gives us access to several events. BeforeZoom fires before zoom and gives us access to the new max and min positions. I just compared to see whether my original range was smaller than the new range. If so I returned my original range, which disables the zoom out.

    charts : { 
    events : {
                beforeZoom : (e, {xaxis}) => {
                    let maindifference = (new Date(props.data[0].date)).valueOf() - new Date(props.data[props.data.length-1].date).valueOf();
                    let zoomdifference =   xaxis.max - xaxis.min ;
                    if( zoomdifference > maindifference )
                    return  {
                        // dont zoom out any further
                        xaxis: {
                            min: props.data[0].date,
                            max: props.data[props.data.length-1].date
                        }
                    }; 
                    else {
                        return {
                            // keep on zooming
                            xaxis: {
                                min: xaxis.min,
                                max: xaxis.max
                            }
                        }
                    }
                }
            }
            }
Ankur Datta
  • 161
  • 2
  • 6
  • 1
    This worked perfectly for me too! Out of curiosity, do you know how it may be possible to disabled the zoom button? Or at least style it so it's obvious to the user that they can no longer zoom out? – Adam Jun 15 '21 at 12:47
  • 1
    You can disable the zoom option, here's a link to the docs. https://apexcharts.com/docs/options/chart/zoom/ – Ankur Datta Jun 17 '21 at 07:45
  • 1
    is it possible to zoom till days only I don't want to zoom till time arrives. Actually in my case I have dates on my x axis – aditya kumar Jun 25 '21 at 06:52
  • @adityakumar that would be pretty simple, get the timestamps from the dates in your x axis and then you'd have to ensure the min max difference from the timestamps are in days. Sorry for the incredibly delayed answer. – Ankur Datta Jul 20 '21 at 06:33