2013-01-07 159 views
0

我是新開發的Mac應用程序開發人員。我正在繪製使用核心繪圖框架工作的條形圖。 實際上我想要的是,當我選擇一個月時,我想繪製過去6個月的圖形。當我選擇'12/2012'時,我能夠在過去6個月內正確繪製圖形。但是當我選擇'02/2013'時,圖表只顯示1月和2月,而不顯示前幾個月。使用核心圖在可可上繪製條形圖

- (int)maxVal:(NSMutableArray *)arr { 
    NSDictionary *dict = [arr objectAtIndex:0]; 
    NSString* dictValue = [dict objectForKey:@"X_VAL"]; 
    int mxm = [dictValue intValue]; 
    for (int i=0; i<[arr count]; i++) { 
     if ([[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue] > mxm) { 
      mxm = [[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue]; 
     } 
    } 
    return mxm; 

} 


- (int)minVal:(NSMutableArray *)arr { 


    int mn = [[[arr objectAtIndex:0] objectForKey:@"X_VAL"] intValue]; 
    for (int i=0; i<[arr count]; i++) { 
     if ([[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue] < mn) { 
      mn = [[[arr objectAtIndex:i] objectForKey:@"X_VAL"] intValue]; 
     } 
    } 
    return mn; 
} 

- (void)drawGraph { 
    if ([graphMutableArray count] > 0) { 
     NSRect viewRect = [aGraphView bounds]; 
     double xAxisStart = 0, yAxisStart = 0, xAxisLength = [graphMutableArray count], yAxisLength = [[graphMutableArray valueForKeyPath:@"@max.Y_VAL"] doubleValue], tenthPartOfYAxis = (yAxisLength + (yAxisLength/10)); 
     NSLog(@"Y Axis Length = %f", yAxisLength); 
     for (int k = 0; k < xAxisLength; k++) { 
      NSDictionary *dict1 = [graphMutableArray objectAtIndex:k]; 
      NSDictionary *dict2 = [dict1 objectForKey:@"X_VAL"]; 
      NSDictionary *dict3 = [dict1 valueForKey:@"Y_VAL"]; 
      NSLog(@"dict 2 = %@ and dict 3 = %@", dict2, dict3); 
     } 

     graph = [[CPTXYGraph alloc] initWithFrame:viewRect]; 
     [graph applyTheme:[CPTTheme themeNamed:kCPTSlateTheme]];//kCPTDarkGradientTheme]];//kCPTStocksTheme]]; 
     aGraphView.hostedGraph = graph; 
     graph.plotAreaFrame.masksToBorder = NO; 
     graph.paddingLeft = 90.0; 
     graph.paddingTop = 20.0; 
     graph.paddingRight = 20.0; 
     graph.paddingBottom = 70.0; 

     // Add plot space for horizontal bar charts 
     CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
     plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xAxisStart) 
                 length:CPTDecimalFromDouble(xAxisLength + 1)]; 
     plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yAxisStart) 
                 length:CPTDecimalFromDouble(tenthPartOfYAxis)]; 

     CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
     CPTXYAxis *x = axisSet.xAxis; 
     NSString *cptDecimalstr = [NSString stringWithFormat:@"%f", xAxisLength /[x.axisLabels count]]; 
     x.majorIntervalLength = CPTDecimalFromString(cptDecimalstr); 
     x.minorTicksPerInterval = 1; 
     x.majorTickLineStyle = nil; 
     x.minorTickLineStyle = nil; 
     x.axisLineStyle = nil; 
     x.titleLocation = CPTDecimalFromFloat(1.0f); 
     x.titleOffset = 55.0f; 

     // Define some custom labels for the data elements 
     x.labelRotation = M_PI/4; 
     x.labelingPolicy = CPTAxisLabelingPolicyNone; 


     NSLog(@"Max = %d and Min = %d", [self maxVal:graphMutableArray], [self minVal:graphMutableArray]); 

     NSMutableArray *tempCustomTickLocations = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:1.7],[NSNumber numberWithFloat:3.0],[NSNumber numberWithFloat:4.1],[NSNumber numberWithFloat:5.3],[NSNumber numberWithFloat:6.5],[NSNumber numberWithFloat:6.9],[NSNumber numberWithFloat:7.1],[NSNumber numberWithFloat:7.5],[NSNumber numberWithFloat:8.1],[NSNumber numberWithFloat:8.5],[NSNumber numberWithFloat:8.9],nil]; 

     NSMutableArray *tempMonthsArr = [NSMutableArray arrayWithObjects:@"",@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil]; 


     NSMutableArray *tempMonthsMutArr 
     = [[[NSMutableArray alloc] initWithCapacity:[graphMutableArray count]] autorelease], *tempMutCustomTickLocations 
     = [[[NSMutableArray alloc] initWithCapacity:[graphMutableArray count]] autorelease]; 

     int k = 0; 

     for (int l = [self minVal:graphMutableArray]; l <=[self maxVal:graphMutableArray]; l++) { 
      NSString *tempMonth = [tempMonthsArr objectAtIndex:l]; 
      [tempMonthsMutArr addObject:tempMonth]; 
      [tempMutCustomTickLocations addObject:[tempCustomTickLocations objectAtIndex:k]]; 
      k = k + 1; 
      NSLog(@"tempMutCustomTickLocations= %@",tempMutCustomTickLocations); 
     } 

     if ([self minVal:graphMutableArray] == [self maxVal:graphMutableArray]) { 
      NSString *tempMonth = [tempMonthsArr objectAtIndex:[self minVal:graphMutableArray]]; 
      [tempMonthsMutArr addObject:tempMonth]; 
      [tempMutCustomTickLocations addObject:[tempCustomTickLocations objectAtIndex:0]]; 
     } 


     NSMutableArray *customTickLocations = [NSMutableArray arrayWithArray:tempMutCustomTickLocations]; 


     NSArray *xAxisLabels = [NSArray arrayWithArray:tempMonthsMutArr]; 
     NSUInteger labelLocation = 0; 
     NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[graphMutableArray count]]; 

     for (NSNumber *tickLocation in customTickLocations) { 
      //NSLog(@"tickLocation==%d",tickLocation.intValue); 
      CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
      newLabel.tickLocation = [tickLocation decimalValue];//[tickLocation decimalValue]; 
      newLabel.offset = x.labelOffset + x.majorTickLength; 
      // NSLog(@"[tickLocation floatValue] = %f", [tickLocation floatValue]); 
      newLabel.offset = x.labelOffset + x.majorTickLength; 
      //NSLog(@"x.labelOffset = %f, x.majorTickLength = %f and newLabel.offset = %f", x.labelOffset, x.majorTickLength, newLabel.offset); 
      newLabel.rotation = M_PI/4; 
      //NSLog(@"-=-=-=--=-=-=- %f",x.labelOffset); 
      //NSLog(@"%f", x.majorTickLength); 

      [customLabels addObject:newLabel]; 
      //NSLog(@"%@",customLabels); 

      [newLabel release]; 
     } 

     x.axisLabels = [NSSet setWithArray:customLabels]; 
     NSLog(@"%@",x.axisLabels); 


     CPTXYAxis *y = axisSet.yAxis; 
     y.axisLineStyle = nil; 
     y.majorTickLineStyle = nil; 
     y.minorTickLineStyle = nil; 
     NSString *cptDecimalStr = [NSString stringWithFormat:@"%f", yAxisLength/10]; 
     y.majorIntervalLength = CPTDecimalFromString(cptDecimalStr); 
     //y.constantCoordinateValue = CPTDecimalFromString(@"0"); 
     // y.title = @"Y Axis"; 
     y.titleOffset = 45.0f; 
     y.titleLocation = CPTDecimalFromFloat(150.0f); 
     //CPTPieChart 

     CPTBarPlot *plot = [[CPTBarPlot alloc] initWithFrame:CGRectZero]; 
     plot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) 
                 length:CPTDecimalFromDouble(xAxisLength)]; 
     plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"0.50"] 
          decimalValue]; //CPTDecimalFromDouble(0.50f); 

     plot.fill = [CPTFill fillWithColor:[CPTColor lightGrayColor]]; 
     plot.dataSource = self; 

     [graph addPlot:plot ];//toPlotSpace:plotSpace]; 
    } 
    else { 

     aGraphView.hostedGraph = nil; 
     [[NSAlert alertWithMessageText:@"No Data Found" defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal]; 

    } 
} 

任何人都可以幫我解決問題!!!

+0

當選擇發生變化時,您會發生什麼變化?你可以顯示你的數據源方法嗎? –

+0

@EricSkroch當我改變日期範圍(即,如果我選擇二月2013,那麼我想要前6個月,即直到2012年9月) – user1838670

回答

0

您需要更新繪圖空間的xRange。這會將軸標籤移動到正確的位置並在新範圍內繪製數據點。您還需要將新的數據點添加到圖中。