我想做这样的事情:http://youtu.be/PeRhxSCx2xM

我尝试实现这个方法:

//viewDidLoad
//plotSpace.delegate = self;

- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space
     willChangePlotRangeTo:(CPTPlotRange *)newRange
             forCoordinate:(CPTCoordinate)coordinate {

    NSLog(@"WillChangePlotRangeTo");

    // only allows scrolling to the right
    // remove this to have scrolling in both directions
    if (newRange.locationDouble < 0.0F) {
        newRange.location = CPTDecimalFromFloat(0.0);
    }

    // Adjust axis to keep them in view at the left and bottom;
    // adjust scale-labels to match the scroll.
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    if (coordinate == CPTCoordinateX) {
        axisSet.yAxis.orthogonalCoordinateDecimal = newRange.location;
        axisSet.xAxis.titleLocation = CPTDecimalFromFloat(newRange.locationDouble +
                                                         (newRange.lengthDouble / 2.0F));
    } else {
        axisSet.xAxis.orthogonalCoordinateDecimal = newRange.location;
        axisSet.yAxis.titleLocation = CPTDecimalFromFloat(newRange.locationDouble +
                                                         (newRange.lengthDouble / 2.0F));
    }

    return newRange;
}

我尝试这样做,这个解决方案在我缩小时有效,但在我放大时不起作用。
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(3.0)];
plotSpace.globalYRange = plotSpace.yRange;

任何人都请解释我,正确的结果是什么,以及它实际上是如何工作的。

谢谢回复

最佳答案

最简单的方法是忽略 newRange 并返回 y 坐标的现有 yRange。您也不需要在这里更新轴属性。有更简单的方法可以做到这一点。

图形设置:

// leave the titleLocation for both axes at the default (NAN) to center the titles
xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

绘图空间委托(delegate):
- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space
      willChangePlotRangeTo:(CPTPlotRange *)newRange
              forCoordinate:(CPTCoordinate)coordinate {

    CPTPlotRange *updatedRange = nil;

    switch ( coordinate ) {
    case CPTCoordinateX:
        if (newRange.locationDouble < 0.0F) {
            CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
            mutableRange.location = CPTDecimalFromFloat(0.0);
            updatedRange = mutableRange;
        }
        else {
            updatedRange = newRange;
        }
        break;
    case CPTCoordinateY:
        updatedRange = ((CPTXYPlotSpace *)space).yRange;
        break;
    }
    return updatedRange;
}

关于iphone - 核心图禁用 y 轴缩放、滚动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9495423/

10-09 02:15