开发者

Move core plot x axis to last point

My core plot graph in my iPhone app is drawing nicely. However, one thing I can't seem to figure out. How can I get the graph to scroll to the end of its x axis after the view loads, rather than starting at the origin? I would like the last data point upon the x axis to be visible when the graph appears. How?

I saw the plotSpace:willDisplaceBy: delegate method, which is "close" to what I need, but I need to be able to tell the graph to displace itself to the end of its x ax开发者_运维百科is when its view loads. I haven't found any relevant example code for core plot to do this.


You need to set the xRange of the plot space so that the last point is visible. If you know the ending value and the length of the range you want to be visible, you can do something like this:

CPTXYPlotSpace *plotSpace = graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange rangeWithLocation:CPTDecimalFromDouble(end - length)
                                            length:CPTDecimalFromDouble(length)];

The example assumes that the desired length is positive and your values are doubles. You can of course do the calculations using NSDecimal values if needed.


use this method :

-(CPPlotRange *)plotSpace:(CPPlotSpace *)space willChangePlotRangeTo:(CPPlotRange *)newRange forCoordinate:(CPCoordinate)coordinate
{
    if (coordinate == CPCoordinateY) {
        CPPlotRange *maxRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(6.0f)];
        CPPlotRange *changedRange = [[newRange copy] autorelease];
        [changedRange shiftEndToFitInRange:maxRange];
        [changedRange shiftLocationToFitInRange:maxRange];
        newRange =  changedRange;
    }
    return newRange;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜