开发者

Custom Labels over the bar graph (CorePlot Framework)

开发者 https://www.devze.com 2023-04-02 23:40 出处:网络
i have used CorePlot for the first time and want now customize the Bar Graph. You see it in this picture.

i have used CorePlot for the first time and want now customize the Bar Graph. You see it in this picture.

Custom Labels over the bar graph (CorePlot Framework)

I want now add the Number over every Bar on top of the Bar

hope you know what i mean.

How can i realize it?

This is my Code

-(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 = [CPPlotRa开发者_JAVA技巧nge 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];
}


If you just want simple numeric labels, set the labelTextStyle and labelFormatter properties on the plot. If you need more complex labels, you can add the -dataLabelForPlot:recordIndex: method to your datasource and create your own custom labels.

See the example apps included with Core Plot for sample code. The labeling mechanism is the same for all plot types. The "Vertical Bar Chart" in the Plot Gallery app demonstrates the first technique. Several of the other plots in the Plot Gallery use the datasource technique.

0

精彩评论

暂无评论...
验证码 换一张
取 消