I am creating an app that will display several plots in several screens. Therefore, it is chosen to create a separate class which creates a graph from the Coreplot framework. All the functions / methods are called in the correct way, except for the numberForPlot:plotfield:recordIndex: method. The axis and hostingview are set up correctly and but no data is plotted.

The class is called WiGraph

WiGraph.h

#import <Foundation/Foundation.h>
#import "CorePlot-CocoaTouch.h"
#import "data.h"


@interface WiGraph : NSObject <CPTPlotDataSource>

@property (nonatomic, strong) NSString *gradient;
@property (nonatomic, strong) CPTGraphHostingView *hostingView;
@property (nonatomic, strong) NSArray *plotData;

@property (nonatomic, strong) CPTScatterPlot *plot;
@property (nonatomic, strong) CPTXYGraph *graph;


- (id)initWithData:(NSArray *)data;

- (void)createGraphInView:(UIView *)content;

- (void)setPlotSpaceWithTimeDifference:(int)diff xStart:(int)starttime xLenght:(int)xlenght yStart:(int)ystart yLength:(int)ylenght;

- (void)addGradientWithRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue andAlpha:(CGFloat)alpha;

- (void)changeDotColor:(CPTColor *)col;

- (void)setAxisFromReferenceTime:(int)time andToXHeight:(float)xheight andYHeight:(float)yheight andXInterval:(float)xint andYInterval:(float)yint andLineColor:(CPTColor*)color;

- (void)addplotToGraph;

@end

WiGraph.m

#import "WiGraph.h"
#import "XMLReader.h"
#import <QuartzCore/QuartzCore.h>

@implementation WiGraph
@synthesize gradient, hostingView, plotData, plot, graph;

-(id)initWithData:(NSArray *)data
{
    self = [super init];
    if (self) {

        self.plotData = data;
        self = [self init];        
    }

    return self;
}

#pragma mark create graph

//This function will create a empty graph
- (void)createGraphInView:(UIView *)content
{
    hostingView = [[CPTGraphHostingView alloc] initWithFrame:content.bounds];

    // Create graph from theme
    graph = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:CGRectZero];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
    [graph applyTheme:theme];
    hostingView.hostedGraph = graph;


    plot = [[CPTScatterPlot alloc] init];
}

- (void)setAxisFromReferenceTime:(int)time andToXHeight:(float)xheight andYHeight:(float)yheight andXInterval:(float)xint andYInterval:(float)yint andLineColor:(CPTColor*)color;
{
    NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:time];
// plotting style is set to line plots
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];

    plot.identifier = @"Date Plot";

    lineStyle.lineColor = color;
    lineStyle.lineWidth = 2.0f;
    lineStyle.miterLimit= 1.0f;
    plot.dataLineStyle = lineStyle;
    plot.dataSource = self;


    // X-axis parameters setting
    CPTXYAxisSet *axisSet = (id)graph.axisSet;
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(xint);
    axisSet.xAxis.minorTicksPerInterval = 3;
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecim    alFromFloat(yheight); //added for date, adjust x line
    axisSet.xAxis.majorTickLineStyle = lineStyle;
    axisSet.xAxis.minorTickLineStyle = lineStyle;
    axisSet.xAxis.axisLineStyle = lineStyle;
    axisSet.xAxis.minorTickLength = 2.0f;
    axisSet.xAxis.majorTickLength = 4.0f;
    axisSet.xAxis.labelOffset = 1.0f;

    // added for date
    [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
    NSDateFormatter *dateFormattersec = [[NSDateFormatter alloc]init];
    [dateFormattersec setDateFormat:@"HH:mm"];
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormattersec];
    timeFormatter.referenceDate = refDate;
    axisSet.xAxis.labelFormatter = timeFormatter;


    // Y-axis parameters setting    
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(yint);
    axisSet.yAxis.minorTicksPerInterval = 0.5;
    axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(xheight); // added for date, adjusts y line
    axisSet.yAxis.majorTickLineStyle = lineStyle;
    axisSet.yAxis.minorTickLineStyle = lineStyle;
    axisSet.yAxis.axisLineStyle = lineStyle;
    axisSet.yAxis.minorTickLength = 2.0f;
    axisSet.yAxis.majorTickLength = 4.0f;
    axisSet.yAxis.labelOffset = 1.0f;
}

-(void)setPlotSpaceWithTimeDifference:(int)diff xStart:(int)starttime xLenght:(int)xlenght yStart:(int)ystart yLength:(int)ylenght
{
graph.paddingLeft = 5.0;
graph.paddingTop = 5.0;
graph.paddingRight = 5.0;
graph.paddingBottom = 5.0;

// setup a plot space for the plot to live in
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;

plotSpace.xRange = [CPTPlotRange    plotRangeWithLocation:CPTDecimalFromInt(starttime)
                                                length:CPTDecimalFromFloat(xlenght)];
// sets the range of y values
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(ystart) 
                                            length:CPTDecimalFromFloat(ylenght)];

}

- (void)addplotToGraph
{
    [graph addPlot:plot];
}


#pragma mark Change Colors
- (void)addGradientWithRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue andAlpha:(CGFloat)alpha
{
    CPTColor *areaColor1       = [CPTColor colorWithComponentRed:red green:green blue:blue alpha:alpha];
    CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
areaGradient1.angle = -90.0f;
    CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
    plot.areaFill       = areaGradientFill;
    plot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];

    [self addplotToGraph];
}

- (void)changeDotColor:(CPTColor *)col
{
    CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    greenCirclePlotSymbol.fill = [CPTFill fillWithColor:col];
    greenCirclePlotSymbol.size = CGSizeMake(3.0, 3.0);
    plot.plotSymbol = greenCirclePlotSymbol; 

    [self addplotToGraph];
}

#pragma mark -
#pragma mark Plot Data Source Methods

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    return plotData.count;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSDictionary *tempDict = [plotData objectAtIndex:index];

    int tmp = [[tempDict retrieveForPath:[NSString stringWithFormat:@"%d",fieldEnum]]intValue];


    NSDecimalNumber *rr = [[NSDecimalNumber alloc]initWithInt:tmp];
    return rr;
}

@end

The number for plot does not get called, does this have to do with a delegate?

I am currently creating the object as:

//Create new Graph
grafiek = [[WiGraph alloc]initWithData:self.measuredData.temperature];

//Create empty hostingView with size of viewgraphA
[grafiek createGraphInView:self.viewGraphA];

[grafiek setPlotSpaceWithTimeDifference:(self.measuredData.maxDate-self.measuredData.refDate) 
                                             xStart:(-300) 
                                            xLenght:((self.measuredData.maxDate-self.measuredData.refDate)+300) 
                                             yStart:self.measuredData.minTemp 
                                            yLength:(self.measuredData.maxTemp - self.measuredData.minTemp)];

            //Set graph color en scale
            [grafiek setAxisFromReferenceTime:self.measuredData.refDate 
                                 andToXHeight:0 
                                   andYHeight:(self.measuredData.minTemp-1) 
                                 andXInterval:(self.measuredData.maxDate-self.measuredData.refDate)/4 
                                 andYInterval:1
                                 andLineColor:[CPTColor blueColor]];

            //Add Gradient
            [grafiek addGradientWithRed:1.0 
                               andGreen:0.3 
                                andBlue:0.3 
                               andAlpha:0.8];


            //Change dot color
            [grafiek changeDotColor:[CPTColor redColor]];

            //Adding axis and labels to the graph
            [grafiek addplotToGraph];

            [self.viewGraphA addSubview:grafiek.hostingView];

Any help would be appreciated

link|improve this question

50% accept rate
Is -numberOfRecordsForPlot: being called? What does it return? If this method returns zero (0), the plot won't call the other datasource methods. – Eric Skroch 20 hours ago
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.