Ios 如何使用动画更新CorePlot Y范围?

Ios 如何使用动画更新CorePlot Y范围?,ios,core-plot,Ios,Core Plot,在我的应用程序中,散点图中有100个x点,而图的x范围长度是5,我的意思是在一个屏幕上只显示5个点。我想使用基于当前可见点的动画动态更新Y范围 例如,当前可见点是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值为{100200300

在我的应用程序中,散点图中有100个x点,而图的x范围长度是5,我的意思是在一个屏幕上只显示5个点。我想使用基于当前可见点的动画动态更新Y范围

例如,当前可见点是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值为{100200300400500},因此新的y范围更改为
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(100)length:CPTDecimalFromFloat(5)]
。现在我希望这种变化发生在动画中。如何添加动画以更新绘图范围?

以下是我用来制作垂直范围动画的代码。请注意,您需要注意几个点(全局和局部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;
        }
    }
}