0

在我的应用程序中,散点图中有 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)]。现在我希望这种变化发生在动画中。如何添加动画以更新绘图范围?

4

1 回答 1

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;
        }
    }
}
于 2013-08-24T09:47:51.333 回答