Reputation: 2493
In my app, there are 100 x-points in a scatter plot, and the plot xRange length is 5, I mean only show 5 points in one screen. I want to update the yRange dynamically with an animation based on currently visible points.
For example, the currently visible points are x: {1,2,3,4,5}, y: {15,25,35.45,55}, so the y range is [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(15) length:CPTDecimalFromFloat(5)]
.
After scrolling the plot the x values change to {8,9,10,11,12} and the y values are {100,200,300,400,500}, hence the new y range changes to [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(100) length:CPTDecimalFromFloat(5)]
. Now I want this change to happen with animation. How to add animation to update the plot range?
Upvotes: 0
Views: 1043
Reputation: 53367
Here's code I use to do the vertical range animation. Note that you need to take care for several points (global and local y range, axis labels).
- (void)updateVerticalMainGraphRange
{
CPTXYPlotSpace *plotSpace = (id)mainGraph.defaultPlotSpace;
CPTXYAxisSet *axisSet = (id)mainGraph.axisSet;
float animationDuration = 0.3;
if (([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) != 0) {
animationDuration = 3;
}
// Set the y axis ticks depending on the maximum value.
CPTXYAxis *y = axisSet.yAxis;
...
Compute roundedLocalMinValue and roundedLocalMaxValue here as the smallest and largest
values in the local (visible) y range.
...
// Let the larger area (negative or positive) determine the size of the major tick range.
NSDecimalNumber *minAbsolute = [roundedLocalMinValue abs];
NSDecimalNumber *maxAbsolute = [roundedLocalMaxValue abs];
float interval;
if ([minAbsolute compare: maxAbsolute] == NSOrderedDescending) {
interval = [self intervalFromRange: minAbsolute];
} else {
interval = [self intervalFromRange: maxAbsolute];
}
...
intervalFromRange: is a local function to determine a good amount of ticks for a given range value.
...
// Apply new interval length and minor ticks now only if they lead to equal or less labels.
// Otherwise do it after the animation.
// This is necessary to avoid a potentially large intermittent number of labels during animation.
NSDecimal newInterval = CPTDecimalFromFloat(interval);
NSDecimal oldInterval = y.majorIntervalLength;
if (NSDecimalCompare(&oldInterval, &newInterval) == NSOrderedAscending) {
y.majorIntervalLength = newInterval;
y.minorTicksPerInterval = [self minorTicksFromInterval: interval];
newMainYInterval = -1;
} else {
newMainYInterval = interval; // Keep this temporarily in this ivar. It is applied at the end of the animation.
}
CPTPlotRange *plotRange = [CPTPlotRange plotRangeWithLocation: roundedLocalMinValue.decimalValue
length: [[roundedLocalMaxValue decimalNumberBySubtracting: roundedLocalMinValue] decimalValue]];
[CPTAnimation animate: plotSpace
property: @"globalYRange"
fromPlotRange: plotSpace.globalYRange
toPlotRange: plotRange
duration: animationDuration
withDelay: 0
animationCurve: CPTAnimationCurveCubicInOut
delegate: self];
[CPTAnimation animate: plotSpace
property: @"yRange"
fromPlotRange: plotSpace.yRange
toPlotRange: plotRange
duration: animationDuration
withDelay: 0
animationCurve: CPTAnimationCurveCubicInOut
delegate: self];
}
#pragma mark - Coreplot delegate methods
- (void)animationDidFinish: (CPTAnimationOperation *)operation
{
if (operation.boundObject == mainGraph.defaultPlotSpace) {
// Animation of the main graph vertical plot space.
// We can now set the final interval length and tick count.
if (newMainYInterval > 0) {
CPTXYAxisSet *axisSet = (id)mainGraph.axisSet;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromFloat(newMainYInterval);
y.minorTicksPerInterval = [self minorTicksFromInterval: newMainYInterval];
newMainYInterval = 0;
}
}
}
- (void)animationCancelled: (CPTAnimationOperation *)operation
{
if (operation.boundObject == mainGraph.defaultPlotSpace) {
// Animation of the main graph vertical plot space.
// We can now set the final interval length and tick count.
if (newMainYInterval > 0) {
CPTXYAxisSet *axisSet = (id)mainGraph.axisSet;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromFloat(newMainYInterval);
y.minorTicksPerInterval = [self minorTicksFromInterval: newMainYInterval];
newMainYInterval = 0;
}
}
}
Upvotes: 2