2011-09-07 44 views
1

我第一次使用CorePlot,現在想自定義條形圖。你在這張照片中看到它。條形圖上的自定義標籤(CorePlot框架)

enter image description here

我想現在在每欄上的酒吧

頂部添加號碼希望你知道我的意思。

我該如何認識它?

這是我的代碼

-(void) generateDataSamples 
{ 
    int rawSamples [] = {1,3,6,2,1,1,3,15,2,1}; 
    int numSamples = sizeof(rawSamples)/sizeof(int); 

    samples = [[NSMutableArray alloc] initWithCapacity:numSamples]; 

    for (int i = 0; i < numSamples; i++){ 
     double x = i; 
     double y = rawSamples[i]; 
     NSDictionary *sample = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithDouble:x],X_VAL, 
           [NSNumber numberWithDouble:y],Y_VAL, 
           nil]; 
     [samples addObject:sample]; 
    } 
} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    [self generateDataSamples]; 

    double xAxisStart = 0; 
    double xAxisLength = [samples count]; 

    double yAxisStart = 0; 
    double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue]; 


    CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:self.view.bounds]; 
    [self.view addSubview:hostingView]; 

    CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame:self.view.bounds]; 
    hostingView.hostedGraph = graph; 



    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0) 
                length:CPDecimalFromDouble(xAxisLength +1)]; 

    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0) 
                length:CPDecimalFromDouble(yAxisLength + 0.5)]; 

    CPBarPlot *plot = [[CPBarPlot alloc] initWithFrame:CGRectZero]; 
    plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0) 
               length:CPDecimalFromDouble(xAxisLength)]; 

    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
    CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
    lineStyle.lineColor = [CPColor whiteColor]; 
    lineStyle.lineWidth = 1.0f; 
    CPTextStyle *cyanStyle = [CPTextStyle textStyle]; 
    cyanStyle.color = [CPColor cyanColor]; 
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    [formatter setMaximumFractionDigits:0]; 

    plot.barOffset = .5; 
    plot.dataSource = self; 


    [graph addPlot:plot]; 

    [plot release]; 
    [graph release]; 
    [hostingView release]; 
} 

回答

1

如果你只是想簡單的數字標籤,設置的情節labelTextStylelabelFormatter性能。如果您需要更復雜的標籤,您可以將-dataLabelForPlot:recordIndex:方法添加到您的數據源並創建您自己的自定義標籤。

查看示例代碼的Core Plot附帶的示例應用程序。所有繪圖類型的標記機制都是相同的。 Plot Gallery應用程序中的「垂直條形圖」演示了第一種技術。繪圖庫中的一些其他繪圖使用數據源技術。

+0

很棒的知識展示和幫助@TeamNoX解決他們問題的出色答案。對我來說有用的部分是'dataLabelForPlot'。感謝和+1。 – Jacksonkr