Search code examples
iosscalecore-plotzoomingscatter-plot

How do i control the zooming / scaling of the scatter plot graph to show both axes


I am trying to draw a line graph using the scatter plot in the core-plot library for ios.

Being a noob at this library, I was using an example from here as a reference

While I am able to get to a point where I have my line graphs working, I find that in case of datasets where the y axis data is grouped together and much greater than 0, the graph zooms / scales to a point where the x-axis itself is no longer visible.

Refer this graph:

enter image description here

I would prefer my graph to always look like this:

enter image description here

I am using scaleToFitPlots to scale the graph and my understanding is that using that function has this effect as per this question.

If I do not use scaleToFitPlots function, does that mean I will need to manually calculate my axis scale / intervals etc manually based on the set of values being plotted?

Is there any better alternate? The above answered question also mentions using -scaleToFitPlots. Does anyone know what is meant by that?


Solution

  • If you always want the axes to cross at the same place, use the orthogonalCoordinateDecimal property. It defaults to zero for both axes, but you can change that if you want. If the crossing point is outside the visible range after calling -scaleToFitPlots:, you can adjust the location and length of the yRange afterwards to extend the range to cover the crossing point.

    If you don't care where they cross but always want the axes visible, use axisConstraints. Several of the Core Plot example apps demonstrate how to do this. For example, this code will lock the x-axis to the bottom of the plot area:

    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
    

    You can also use a relative offset to do things like keep an axis in the middle of the plot area.