Search code examples
constraintsaxiscore-plotfloating

Core Plot: Constrain axis only when it gets near the edge of the graph


I am using Core Plot in my iPad application to plot some data points on a scatter plot. I would like the axes to pan around with the data until one of the axes gets near the edge of the graph. Then I would like that axis to be constrained so that it does not go beyond the edge of the graph. I know that I can use CPTConstraints to fix the axes, but I don't want them to always be fixed. I simply want them to be fixed when they come near the edge of the graph so that they remain visible. I've attached some screenshots of my application to show you what I mean.

enter image description here

enter image description here


Solution

  • Use a plot space delegate to monitor changes to the plot space. Implement the -plotSpace:willChangePlotRangeTo:forCoordinate: delegate method and use the new plot range to decide whether to set the orthogonalCoordinate or constraints to position the axis.