Core Plot generating a flat line, X or Y range incorrect?

I am using core plot to plot about 2000 data points. They load fine, but I only see a smooth line:

I am not sure why it is just a flat line rather than showing a graph of all of my values which are in a large range. Have I done something wrong with setting up the plot space?

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromFloat(num_points)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromFloat(num_points)];

Here is my numberForPlot method as well:

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    NSNumber *num = [sortedArray objectAtIndex:index];
    return num;
}

Any ideas what I am doing wrong here?

Answers


There are a couple of different issues here:

  1. The plot space ranges are given in data coordinates. You can either calculate the ranges yourself or use the plot space method -scaleToFitPlots: to calculate them automatically. See my answer to your related question on the Core Plot discussion board for how to calculate the ranges manually.

  2. Your -numberForPlot:field:recordIndex: ignores the field parameter. You're returning the same value for both CPScatterPlotFieldX and CPScatterPlotFieldY, hence the diagonal line.

One possible solution:

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    NSNumber *num = nil;

    switch ( fieldEnum ) {
    case CPScatterPlotFieldX:
        num = [NSNumber numberWithUnsignedInteger:index];
        break;
    case CPScatterPlotFieldY:
        num = [sortedArray objectAtIndex:index];
        break;
    }

    return num;
}

Eric


Need Your Help

Execute competing calculations in parallel and discard all but the first one that finishes

java multithreading scala concurrency java.util.concurrent

I wrote a function that generates a labyrinth based on randomness. Most of the time, this function is very fast. But every once in a while, due to bad luck with the random numbers, it takes a coupl...

How to make a lua app for PSP?

scripting lua playstation-portable

I am trying to use Luaplayer HM v2 on my PSP, but I am unable to make a simple Helo World app.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.