Reputation: 1519
I have a scatter-plot where I plot circles. It draws fine and I scale it depending on aspect ratio from the users device. The circles can vary in size incredibly, so I want to scale the plotspace accordingly. The problem is that when I use
[graph.defaultPlotSpace scaleToFitPlots:graph.allPlots];
the scaling is done with respect to the y-axis. I get perfect circles, since I work with aspect ratio, but since this is a portrait orientation, and the scaling is done with respect to y-axis, the circles get cut off in the x-axis. Since the x-axis is shorter in regard of aspect ratio I'd like to scale with respect to it instead. Is that possible?
What I'm trying to achieve is to "zoom out" or "scale" my plotSpace so all my circles fit in the plotSpace, which I can't seem to figure out without breaking aspect ratio and thus getting stretches circles.
It looks like this:
And I want it to look like this:
EDIT:
Code I currently use to scale:
[self.mGraph layoutIfNeeded];
[self.mGraph.defaultPlotSpace scaleToFitPlots:self.mGraph.allPlots];
float width = self.mGraph.plotAreaFrame.bounds.size.width;
float height = self.mGraph.plotAreaFrame.bounds.size.height;
float aspectRatio = width / height;
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.0f * aspectRatio)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.0f)];
plotSpace.yRange = yRange;
Upvotes: 0
Views: 203
Reputation: 27381
You can compute the aspect ratio of the plot area from the size
of the plotArea
layer. Call -layoutIfNeeded
on the graph first to make sure the size is up-to-date. Use the computed aspect ratio to adjust the plot ranges computed by -scaleToFitPlots:
.
After scaling, get the size of the plot area and the lengths of the plot ranges:
CGSize paSize = self.mGraph.plotAreaFrame.plotArea.bounds.size;
double xLen = plotSpace.xRange.lengthDouble;
double yLen = plotSpace.yRange.lengthDouble;
You want the ratio xLen / yLen
to end up the same as paSize.width / paSize.height
. Expand the xRange
or yRange
by a factor greater than one (1) to make the ratios equal.
Upvotes: 1