核心图:具有自定义轴标签的轴AlternateBandFills [英] Core plot: axis alternatingBandFills with custom axis labels

查看:83
本文介绍了核心图:具有自定义轴标签的轴AlternateBandFills的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Core Plot库在iOS上绘制图形。我想在yAxis的网格线之间添加颜色。我已经设法通过设置轴的alternatingBandFills属性来做到这一点。但是,我也必须在yAxis上使用自定义标签,并且当我提供自定义标签时,出于某些原因,ternatingBandFills属性不起作用。

I am using the Core Plot library for drawing graphs on the iOS. I want to add colors between the grid lines of the yAxis. I have managed to do it with setting the alternatingBandFills property of the axis. However, I have to use custom labels on the yAxis as well, and when I am providing the custom labels, the alternatingBandFills property does not work for some reason.

任何帮助如何在yAxis上的网格线之间的空间添加颜色以及使用自定义标签的人,将不胜感激。

Any help how to add colors to the spaces between the grid lines on the yAxis as well as using custom labels will be much appreciated.

我现在使用的代码如下:

The code that I am using now looks like:

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostedGraph.axisSet;
    CPTXYAxis *yAxis = axisSet.yAxis; 

    yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(minValueX);        
    yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;

    NSArray *yAxisTickLocations = [NSArray arrayWithObjects:
                                   [NSDecimalNumber numberWithDouble:lowerRedRangeFrom],
                                   [NSDecimalNumber numberWithDouble:lowerOrangeRangeFrom],
                                   [NSDecimalNumber numberWithDouble:greenRangeFrom],
                                   [NSDecimalNumber numberWithDouble:upperOrangeRangeFrom],
                                   [NSDecimalNumber numberWithDouble:upperRedRangeFrom],
                                   [NSDecimalNumber numberWithDouble:upperRedRangeTo],
                                   nil];
    NSArray *yAxisLabels = [NSArray arrayWithObjects:@"Label1",@"Label2", @"Label3",@"Label4",@"Label5",@"Label6", nil];

    NSUInteger labelLocationY = 0;
    NSMutableArray *customLabelsY = [NSMutableArray arrayWithCapacity:[yAxisLabels count]];
    for (NSNumber *tickLocation in yAxisTickLocations) {

        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [yAxisLabels objectAtIndex:labelLocationY++] textStyle:axisSet.xAxis.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
        newLabel.rotation = M_PI/4;
        [customLabelsY addObject:newLabel];
    }

    axisSet.yAxis.axisLabels =  [NSSet setWithArray:customLabelsY];
    yAxis.alternatingBandFills = [NSArray arrayWithObjects:
                                  [CPTColor redColor],
                                  [CPTColor orangeColor],
                                  [CPTColor greenColor],
                                  [CPTColor orangeColor],
                                  [CPTColor redColor], nil];


推荐答案

我知道了:

轴的标记策略应为: CPTAxisLabelingPolicyLocationsProvided ,文档中指出:用户设置刻度位置;轴进行标记。 。

The labeling policy of the axis should be: CPTAxisLabelingPolicyLocationsProvided, for which the documentation states: " User sets tick locations; axis makes labels.".

现在,我们只需要指定刻度线的位置即可。这是通过创建带有位置的 NSSet 对象来完成的。然后我们必须设置轴的 majorTickLocations 属性。

Now we only need to specify the locations of the ticks. This is done by creating a NSSetobject with the locations. Then we have to set the majorTickLocations property of the axis.

所以我的代码现在看起来像:

So my code now looks like:

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostedGraph.axisSet;
    CPTXYAxis *yAxis = axisSet.yAxis;

    yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(minValueX);        
    yAxis.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;

    NSSet *majorTickLocations = [NSSet setWithObjects:
                                 [NSDecimalNumber numberWithDouble:lowerRedRangeFrom],
                                 [NSDecimalNumber numberWithDouble:lowerOrangeRangeFrom],
                                 [NSDecimalNumber numberWithDouble:greenRangeFrom],
                                 [NSDecimalNumber numberWithDouble:upperOrangeRangeFrom],
                                 [NSDecimalNumber numberWithDouble:upperRedRangeFrom],
                                 [NSDecimalNumber numberWithDouble:upperRedRangeTo],
                                 nil];
    yAxis.majorTickLocations = majorTickLocations;

    yAxis.alternatingBandFills = [NSArray arrayWithObjects:
                                  [CPTColor redColor],
                                  [CPTColor orangeColor],
                                  [CPTColor greenColor],
                                  [CPTColor orangeColor],
                                  [CPTColor redColor], nil];

这篇关于核心图:具有自定义轴标签的轴AlternateBandFills的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆