开发者

Core-Plot: Trouble Adding Data Points to a Line Graph

Currently I am using the following code from the scatter plot example code:

// Axes
CPTXYAxisSet *xyAxisSet = (id)graph.axisSet;
CPTXYAxis *xAxis = xyAxisSet.xAxis;
CP开发者_JAVA百科TMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy];
lineStyle.lineCap = kCGLineCapButt;

My problem is though, the graph somewhat resembles a bar graph. X and Y values on the axes both only go from 0 to 1. How to make both axes show the values from [-5,5]?


You need to set the xRange and yRange for the plotspace to set the range of values the plot will show. Something like:

plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(-5.0) length: CPTDecimalFromDouble(10.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(-5.0) length: CPTDecimalFromDouble(10.0)];

To get actual points to show up, you need to set up the graph's dataSource object, and it needs to respond to the datasource methods:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;

If you only have one plot, you can ignore the first argument to numberForPlot:. The second argument will be 0 when it wants the x axis value for the point, and it will be 1 when it wants the Y axis value for the point. Index is the point number for your plot. So assuming you have the data in an NSArray called myDataArray, where each object in that array is another NSArray of x,y values, you'll have something like:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;
{
    return [myDataArray count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
{
    return [[myDataArray objectAtIndex: index] objectAtIndex: fieldEnum];
}

You can set up the data array with fixed data like:

NSArray *myDataArray = [NSArray arrayWithObjects: 
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 2], [NSNumber numberWithInt: 3], nil],
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 3], [NSNumber numberWithInt: 2], nil],
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 5], [NSNumber numberWithInt: 5], nil],
    nil];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜