Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:58:50+00:00 2026-06-03T13:58:50+00:00

The delegate numberForPlot is being called correctly and the values returned are NSNumbers e.g.

  • 0

The delegate numberForPlot is being called correctly and the values returned are NSNumbers e.g. 40. (The Y axis range is going from 0-100 -> percentages.)

The x-axis is working ok as you can see in the screenshot below.

enter image description here

The code

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"Schedule: %@", self.schedule.name);

    self.plotData = [NSMutableArray array];
    NSSet *logs = self.schedule.logs;

    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateTimeOriginal" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    NSArray *sortedLogsByDate = [logs sortedArrayUsingDescriptors:sortDescriptors];

    NSDate *oldestDate = ((LogEntry *)[sortedLogsByDate objectAtIndex:0]).dateTimeOriginal;
    NSDate *newestDate = ((LogEntry *)[sortedLogsByDate lastObject]).dateTimeOriginal;

    NSLog(@"First: %@", oldestDate);
    NSLog(@"Last: %@", newestDate);

    NSInteger intervalInSeconds = 60*60*24*7; //One Week

    NSInteger oldestDateInSeconds = [oldestDate timeIntervalSince1970];
    NSInteger newestDateInSeconds = [newestDate timeIntervalSince1970];

    NSInteger numberOfWeeks = (newestDateInSeconds - oldestDateInSeconds + intervalInSeconds - 1)/intervalInSeconds; //Integer division and round up (faster then converting to floats and round()

    NSLog(@"Number of weeks: %d", numberOfWeeks);

    NSDate *previousDate = oldestDate;
    for (int i = 1; i < numberOfWeeks+1; i++) {
        NSDate *nextDate = [previousDate dateByAddingTimeInterval:i*intervalInSeconds]; //Add one week

        NSPredicate *datePredicate = [NSPredicate predicateWithFormat:@"(dateTimeOriginal >= %@) AND (dateTimeOriginal <= %@)", previousDate, nextDate];
        NSSet *logsInThisPeriod = [logs filteredSetUsingPredicate:datePredicate];

        if (logsInThisPeriod.count > 0) {
            //Get logs between previous and next date
            NSPredicate *onTimePredicate = [NSPredicate predicateWithFormat:@"status == %@", @"onTime"];
            NSSet *onTime = [logs filteredSetUsingPredicate:onTimePredicate];

            NSPredicate *postponedPredicate = [NSPredicate predicateWithFormat:@"status == %@", @"postPoned"];
            NSSet *postponed = [logs filteredSetUsingPredicate:postponedPredicate];

            NSPredicate *missedPredicate = [NSPredicate predicateWithFormat:@"status == %@", @"missed"];
            NSSet *missed = [logs filteredSetUsingPredicate:missedPredicate];

            NSInteger onTimeCount = onTime.count;
            NSInteger postponedCount = postponed.count;
            NSInteger missedCount = missed.count;
            NSInteger total = onTimeCount + postponedCount + missedCount;

            NSInteger onTimePercentage = onTimeCount*100/total;
            NSInteger postponedPercentage = postponedCount*100/total;
            NSInteger missedPercentage = missedCount*100/total;

            NSDictionary *dataPoint = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInteger:onTimePercentage], @"onTime", [NSNumber numberWithInteger:postponedPercentage], @"postponed", [NSNumber numberWithInteger:missedPercentage], @"missed", nextDate, @"date", nil];
            [self.plotData addObject:dataPoint];
        }
        else {
            NSMutableDictionary *previousDictionary = [[self.plotData lastObject] mutableCopy];
            [previousDictionary setObject:nextDate forKey:@"date"];
            [self.plotData addObject:previousDictionary];
        }

        previousDate = nextDate;
    }

    //Create host view
    self.hostingView = [[CPTGraphHostingView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.hostingView];

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

    // Setup scatter plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    NSTimeInterval xLow       = 0.0f;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat((newestDateInSeconds - oldestDateInSeconds))];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(100.0)];
    plotSpace.allowsUserInteraction = YES;

    // Axes
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x          = axisSet.xAxis;
    x.majorIntervalLength         = CPTDecimalFromFloat((float)intervalInSeconds);
    x.minorTicksPerInterval       = 0;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = kCFDateFormatterShortStyle;
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
    timeFormatter.referenceDate = oldestDate;
    x.labelFormatter            = timeFormatter;

    CPTXYAxis *y = axisSet.yAxis;
    y.majorIntervalLength         = CPTDecimalFromString(@"10.0");
    y.minorTicksPerInterval       = 1;

    //Ontime plot
    CPTScatterPlot *onTimePlot = [[CPTScatterPlot alloc] init];
    onTimePlot.identifier = @"onTimePlot";

    CPTMutableLineStyle *onTimeLineStyle = [onTimePlot.dataLineStyle mutableCopy];
    onTimeLineStyle.lineWidth                = 3.f;
    onTimeLineStyle.lineColor                = [CPTColor greenColor];
    onTimePlot.dataLineStyle = onTimeLineStyle;

    onTimePlot.dataSource = self;
    [graph addPlot:onTimePlot];

    // Put an area gradient under the plot above
    CPTColor *areaColor = [CPTColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.3];
    CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]];
    areaGradient.angle = -90.0f;
    CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient];
    onTimePlot.areaFill = areaGradientFill;
    onTimePlot.areaBaseValue = CPTDecimalFromString(@"1.75");

    //Postponed plot
    CPTScatterPlot *postponedPlot = [[CPTScatterPlot alloc] init];
    postponedPlot.identifier = @"postponedPlot";

    CPTMutableLineStyle *postponedLineStyle = [postponedPlot.dataLineStyle mutableCopy];
    postponedLineStyle.lineWidth                 = 3.f;
    postponedLineStyle.lineColor                 = [CPTColor orangeColor];
    postponedPlot.dataLineStyle = postponedLineStyle;

    postponedPlot.dataSource = self;
    [graph addPlot:postponedPlot];

    //Missed plot
    CPTScatterPlot *missedPlot = [[CPTScatterPlot alloc] init];
    missedPlot.identifier = @"missedPlot";

    CPTMutableLineStyle *missedLineStyle = [missedPlot.dataLineStyle mutableCopy];
    missedLineStyle.lineWidth                = 3.f;
    missedLineStyle.lineColor                = [CPTColor redColor];
    missedPlot.dataLineStyle = missedLineStyle;

    missedPlot.dataSource = self;
    [graph addPlot:missedPlot];
}

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

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

    if (plot.identifier == @"onTimePlot") {
        NSNumber *onTime = [plotPoint valueForKey:@"onTime"];
        return onTime;
    }
    else if (plot.identifier == @"postponedPlot") {
        NSNumber *postponed = [plotPoint valueForKey:@"postponed"];
        return postponed;
    }
    else if (plot.identifier == @"missedPlot") {
        NSNumber *missed =  [plotPoint valueForKey:@"missed"];
        return missed;
    }
    else {
        return nil;
    }
}

Update
This didn’t change a thing:

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

    NSNumber *num = nil;

    // FieldEnum determines if we return an X or Y value.
    if ( fieldEnum == CPTScatterPlotFieldX )
    {
        return [NSNumber numberWithFloat:[[plotPoint valueForKey:@"date"] timeIntervalSince1970]];
    }
    else    // Y-Axis
    {
        if (plot.identifier == @"onTimePlot") {
            num = [plotPoint valueForKey:@"onTime"];
        }
        else if (plot.identifier == @"postponedPlot") {
            num = [plotPoint valueForKey:@"postponed"];
        }
        else if (plot.identifier == @"missedPlot") {
            num =  [plotPoint valueForKey:@"missed"];
        }

        return [NSNumber numberWithFloat:[num floatValue]];
    }
}

Bonusquestion:
How can I make the axis labels static and let the plot area be scrollable?
How can I change the ‘viewport’ of the y-axis labels? (I want 0-100 visible on launch, now it’s 20-80 or something like that…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:58:52+00:00Added an answer on June 3, 2026 at 1:58 pm

    I think you need something like this in numberForPlot:field:recordIndex:

    if (plot.identifier == @"onTimePlot")
    {
        // FieldEnum determines if we return an X or Y value.
        if ( fieldEnum == CPTScatterPlotFieldX )
        {
            return [NSNumber numberWithFloat:point.x];
        }
        else    // Y-Axis
        {
            return [NSNumber numberWithFloat:point.y];
        }
    }
    

    You have to return the X value one time through the method and the Y value the next.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: delegate not being called I declared a delegate in my appdelegate class,
I'm designing a delegate method that is called when the remote server needs input
Using a delegate I can call any function asynchronously. From the documentation I understand
I have a UITextview delegate - (BOOL)textView:(UITextView *)textView that is never getting called and
Is there any delegate that gets called when we tap on the - button
I have a delegate method with the following tasks: get something from the internet
I have a custom delegate derived from QStyleOptionViewItem which is trying to draw multiline
NSURLConnection delegate method is not called (didReceiveResponse,connectionDidFinishLoading etc) .when i am using a same
I want to delegate some function calls from one object to another: objA.feature =
the regionDidChangeAnimated is a delegate method which gets called instantaneously. I want to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.