条形图上的自定义标签(CorePlot 框架)

发布于 2024-12-03 12:19:51 字数 2574 浏览 0 评论 0原文

我第一次使用 CorePlot,现在想要自定义条形图。你在这张照片中看到它。

在此处输入图像描述

我现在想在栏顶部的每个栏上添加数字

希望您知道我的意思。

我怎样才能意识到它?

这是我的代码

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

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

enter image description here

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 = [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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2024-12-10 12:19:51

如果您只需要简单的数字标签,请在绘图上设置 labelTextStylelabelFormatter 属性。如果您需要更复杂的标签,可以将 -dataLabelForPlot:recordIndex: 方法添加到数据源并创建您自己的自定义标签。

有关示例代码,请参阅 Core Plot 附带的示例应用程序。所有绘图类型的标记机制都是相同的。 Plot Gallery 应用程序中的“垂直条形图”演示了第一种技术。图库中的其他几个图都使用数据源技术。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文