在我的应用程序中,散点图中有100个x点,而xRange长度为5,我的意思是在一个屏幕上只显示5个点。我想用基于当前可见点的动画动态更新yRange。
例如,当前可见点为x:{1,2,3,4,5},y:{15,25,35.45,55},因此y范围为[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(15) length:CPTDecimalFromFloat(5)]
。
滚动绘图后,x值变为{8,9,10,11,12},y值为{100,200,300,400,500},因此新y范围变为[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(100) length:CPTDecimalFromFloat(5)]
。现在我希望这个改变发生在动画上。如何添加动画以更新绘图范围?
答案 0 :(得分:2)
这是我用来做垂直范围动画的代码。请注意,您需要注意几个点(全局和局部y范围,轴标签)。
- (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;
}
}
}