2012-05-08 87 views
1

代表numberForPlot被正確調用並且返回的值是NSNumbers例如40.(Y軸範圍從0-100 - >百分比)核心散點圖不可見,我做錯了什麼?

x軸正常工作正如您可以在下面的截圖中看到的。

enter image description here

代碼

- (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; 
    } 
} 

更新 這並沒有改變任何事情:

-(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: 我怎樣才能使軸標籤靜態並讓情節區域可滾動嗎? 如何更改y軸標籤的「視口」? (我想0-100上推出可見,現在是20-80或者類似的東西...)

回答

0

我認爲你需要在numberForPlot是這樣的:領域: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]; 
    } 
} 

你有通過方法一次返回X值,下一次返回Y值。

+0

我更新了我的文章,我如何嘗試您的建議。不幸的是它沒有奏效。我很困惑,爲什麼在我做這個之前我的x軸已經工作了? – Pieter

+0

我注意到另外一件事,我將工作散點圖與你的比較,我使用[plot.identifier isEqual:@「mainPlot」]而不是==。這可能是比較問題嗎? – Thompsonian

+0

謝謝你檢查了這一點,但沒有看起來沒有問題。我使用斷點檢查了該部分,並且該部分工作良好。而且返回的數字也很好。 – Pieter