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

时间:2014-01-14 01:39:52

标签: ios core-plot

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

我正在努力寻找文件,但我似乎无法找到适合这个领域的任何东西。如果我将横条设为水平,我还需要改变什么?它只是一个显示器,还是我还需要更改数据?

这些将是

  • xRange,yRange

  • -(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

而且我更喜欢将这些酒吧叠加在一起,只有很小的分离......所以我错过了什么?

1 个答案:

答案 0 :(得分:1)

在条形图上将barsAreHorizontal设置为YES以制作水平条。其他一切都是相同的,除了条形位置现在在y轴上,条形尖端和基础值在x轴上。