2015-10-29 291 views
1

我已經使用core-plot在我的應用程序中顯示堆疊條形圖,我遵循這個漂亮的教程來實現堆積條形圖,現在該圖形如下所示。 「http://www.gilthonwe.com/2012/06/09/stacked-bar-chart-coreplot-ios/如何在CorePlot上顯示條形值-Stacked條形圖

我用下面的代碼在用戶在屏幕上觸摸它們時顯示條形值。

-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index 
    { 
     if (plot.isHidden == YES) { 
      return; 
     } 
     static CPTMutableTextStyle *style = nil; 
     if (!style) { 
      style = [CPTMutableTextStyle textStyle]; 
      style.color= [CPTColor yellowColor]; 
      style.fontSize = 16.0f; 
      style.fontName = @"Helvetica-Bold"; 
     } 

     NSNumber *price = [NSNumber numberWithDouble:[self doubleForPlot:plot field:CPTBarPlotFieldBarTip recordIndex:index]]; 
     if (!self.priceAnnotation) { 
      NSNumber *x = [NSNumber numberWithInt:0]; 
      NSNumber *y = [NSNumber numberWithInt:0]; 
      NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil]; 
      self.priceAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint]; 
     } 
     static NSNumberFormatter *formatter = nil; 
     if (!formatter) { 
      formatter = [[NSNumberFormatter alloc] init]; 
      [formatter setMaximumFractionDigits:2]; 
     } 
     // 5 - Create text layer for annotation 
     NSString *priceValue = [formatter stringFromNumber:price]; 
     CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:priceValue style:style]; 
     self.priceAnnotation.contentLayer = textLayer; 
     NSLog(@"barWasSelectedAtRecordIndex %lu", (unsigned long)index); 
     NSInteger plotIndex = 0; 
     if ([plot.identifier isEqual:[sets objectForKey:@"Due"]] == YES) { 
      plotIndex = 0; 
     } else if ([plot.identifier isEqual:[sets objectForKey:@"Overdue"]] == YES) { 
      plotIndex = 1; 
     } else if ([plot.identifier isEqual:[sets objectForKey:@"Paid"]] == YES) { 
      plotIndex = 2; 
     } 

     CGFloat x =10.00; 
     NSNumber *anchorX = [NSNumber numberWithFloat:x]; 
     CGFloat y = 10.00; 
     NSNumber *anchorY = [NSNumber numberWithFloat:y]; 
     self.priceAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil]; 
     // 8 - Add the annotation 
     [plot.graph.plotAreaFrame.plotArea addAnnotation:self.priceAnnotation]; 
    } 

請參閱圖中上面的代碼的響應

enter image description here

在這裏,我面臨幾個問題

1.值不高於所選擇的確切的位置處顯示酒吧。

  • 考慮藍條值= 8,綠色條值= 6,紅色條值= 7
  • 如果單擊藍條顯示欄的精確值。 Display Value=8

    如果我點擊綠色條,我會得到藍色條值和綠色條值的累計總和值。 Display Value=14

    如果我點擊紅色條,我會得到藍色,綠色,紅色條的累計和值。 Display Value=21

    如何在用戶觸摸屏幕上的小節時在精確位置上顯示精確的小節點值?

    回答

    1

    問題中的代碼顯示所選欄的「tip」值。在這種情況下,這是條形的頂部。如果您想要所選段的長度,請減去條的「基」值。

    +0

    偉大的埃裏克,請任何代碼示例。我獲得了y位置值,請如何獲得x位置。 – cloudVision

    +0

    從'CPTBarPlotFieldBarBase'字段獲取繪圖數據,並採取差異。 –

    +0

    我已經設法得到x,y的位置來顯示值, 如何從代碼中獲取CPTBarPlotFieldBarBase? – cloudVision