gpt4 book ai didi

ios - 如何用动画更新 CorePlot Y 范围?

转载 作者:行者123 更新时间:2023-11-28 22:27:53 26 4
gpt4 key购买 nike

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

比如当前可见的点是x:{1,2,3,4,5},y:{15,25,35.45,55},所以y范围是[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat (15) 长度:CPTDecimalFromFloat(5)]。 滚动绘图后,x 值变为 {8,9,10,11,12},y 值为 {100,200,300,400,500},因此新的 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;
}
}
}

关于ios - 如何用动画更新 CorePlot Y 范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18414875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com