ios - 使用水平条形图条的任何特殊注意事项

标签 ios core-plot

我一直在尝试使用 iOS 中的 CorePlot 制作水平条形图。

我正在尝试查找文档,但我似乎无法找到适合该领域的任何内容。如果我将条形图设置为水平,还需要更改什么?它只是一个显示的东西,还是我也需要更改数据?

这些是

  • x范围,y范围

  • -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 实现

这是我目前所拥有的...

#pragma mark -
#pragma mark - Chart behavior
-(void)initPlot {
    [self configureGraph];
    [self configurePlots];
    [self configureAxes];
}

-(void)configureGraph {

    CPTGraph *countryGraph = [[CPTXYGraph alloc] initWithFrame:self.countryGraph.bounds];
    CPTGraph *timeZoneGraph = [[CPTXYGraph alloc] initWithFrame:self.timeZoneGraph.bounds];

    countryGraph.plotAreaFrame.masksToBorder = NO;
    timeZoneGraph.plotAreaFrame.masksToBorder = NO;


    self.countryGraph.hostedGraph = countryGraph;
    self.timeZoneGraph.hostedGraph = timeZoneGraph;


    CPTXYPlotSpace *countryPlotSpace = (CPTXYPlotSpace *) countryGraph.defaultPlotSpace;
    countryPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.countryData[0] count])];
    countryPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.countryData count]*50)];

    CPTXYPlotSpace *timeZonePlotSpace = (CPTXYPlotSpace *) timeZoneGraph.defaultPlotSpace;
    timeZonePlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.timeZoneData[0] count])];
    timeZonePlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.timeZoneData count]*50)];




}

-(void)configurePlots {




    CPTBarPlot *countryPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:19/255.0 green:221/255.0 blue:187/255.0 alpha:1] horizontalBars:YES];

    countryPlot.identifier = @"CountryPlot";

    CPTBarPlot *timeZonePlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:30/255.0 green:33/255.0 blue:36/255.0 alpha:1] horizontalBars:YES];
    timeZonePlot.identifier = @"TimeZonePlot";


    CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
    barLineStyle.lineColor = [CPTColor whiteColor];
    barLineStyle.lineWidth = 1.0;

    CPTGraph *countryGraph = self.countryGraph.hostedGraph;
    CPTGraph *timeZoneGraph = self.timeZoneGraph.hostedGraph;

    countryPlot.dataSource = self;
    countryPlot.delegate = self;
    countryPlot.barWidth = CPTDecimalFromDouble(20);
    countryPlot.baseValue = CPTDecimalFromString(@"0");
    countryPlot.lineStyle = barLineStyle;
    countryPlot.barOffset = CPTDecimalFromFloat(0.25f);
    [countryGraph addPlot:countryPlot toPlotSpace:countryGraph.defaultPlotSpace];


    timeZonePlot.dataSource = self;
    timeZonePlot.delegate = self;
    timeZonePlot.baseValue = CPTDecimalFromString(@"0");
    timeZonePlot.barWidth = CPTDecimalFromDouble(20);
    timeZonePlot.lineStyle = barLineStyle;
    timeZonePlot.barOffset = CPTDecimalFromFloat(0.25f);

    [timeZoneGraph addPlot:timeZonePlot toPlotSpace:timeZoneGraph.defaultPlotSpace];

}



-(void)configureAxes {

    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];


    CPTXYAxisSet *countryAxisSet = (CPTXYAxisSet *) self.countryGraph.hostedGraph.axisSet;

    CPTXYAxis *x = countryAxisSet.xAxis;

    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.axisLineStyle = axisLineStyle;
    x.majorIntervalLength = CPTDecimalFromString(@"20");

    CPTXYAxis *y = countryAxisSet.yAxis;

    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.axisLineStyle = axisLineStyle;
    y.majorIntervalLength = CPTDecimalFromString(@"50");


    CPTXYAxisSet *timeZoneAxisSet = (CPTXYAxisSet *) self.timeZoneGraph.hostedGraph.axisSet;

    CPTXYAxis *x2 = timeZoneAxisSet.xAxis;

    x2.labelingPolicy = CPTAxisLabelingPolicyNone;
    x2.axisLineStyle = axisLineStyle;
    x2.majorIntervalLength = CPTDecimalFromString(@"20");

    CPTXYAxis *y2 = timeZoneAxisSet.yAxis;

    y2.labelingPolicy = CPTAxisLabelingPolicyNone;
    y2.axisLineStyle = axisLineStyle;
    y2.majorIntervalLength = CPTDecimalFromString(@"50");




}

#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    NSUInteger num = 0;
    if ([plot.identifier isEqual:@"CountryPlot"]) {
       num =  self.countryData.count;
    } else {
       num = self.timeZoneData.count;
    }
    return num;


}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSNumber *num = @0;

    switch (fieldEnum) {
        case CPTBarPlotFieldBarTip:
            if ([plot.identifier isEqual:@"CountryPlot"]) {
                num = [NSNumber numberWithInteger:[self.countryData[index] count]];
            } else {
                num = [NSNumber numberWithInteger:[self.timeZoneData[index] count]];

            }
            break;

        default:
            num = [NSNumber numberWithInteger:index];
            break;
    }
    return num;

}

这是它的作用 enter image description here

而且我更希望条形图以较小的间隔堆叠在一起……所以我错过了什么?

最佳答案

在条形图上将 barsAreHorizo​​ntal 设置为 YES 以生成水平条。其他一切都是一样的,除了条的位置现在在 y 轴上,条的尖端和基值在 x 轴上。

关于ios - 使用水平条形图条的任何特殊注意事项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104663/

相关文章:

cocoa - 在 Core Plot 的 Debug模式下使用 xcode 时出现问题

iphone - 如何在 Core Plot 中使用自定义标签添加刻度线?

ios - 核心剧情: Two color axis label

ios - 我的 IBAction 中的 NSDictionary 变为零

ios - Apple App 链接打不开,而是要求 itunes

ios - 小 View 在多个 View 上实时显示数据

ios - 为什么文字是颠倒的?

ios - 代码签名资源规则路径在 Jenkins 上失败,CODE_SIGN_RESOURCE_RULES_PATH

objective-c - 按下按钮上传下一个 View 或 webview 时如何显示事件指示器?

swift - 使用未声明的类型 'CPTMutableAxisLabelSet'