【问题标题】:Correct way to set bar width in coreplot?在 coreplot 中设置条形宽度的正确方法?
【发布时间】:2014-01-14 22:26:49
【问题描述】:

随着更多数据添加到我的图表中,我的条形高度似乎会缩小和增长。是否可以修复它以使其每次都保持不变(比如 10 像素高)?

以下是我认为的相关代码:

#define kBarHeight 10

-(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 lightGrayColor];
    barLineStyle.lineWidth = 0.5;

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

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


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

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

}

 //Setting the ranges - possibly the incorrect part?
 CPTXYPlotSpace *countryPlotSpace = (CPTXYPlotSpace *) countryGraph.defaultPlotSpace;
    CSGeoStat *stat = (CSGeoStat *) self.countryData[self.countryData.count-1];
    countryPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(stat.count * 1.1)];
    countryPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-20.0) length:CPTDecimalFromFloat([self.countryData count]*kBarHeight*1.5)];

    CPTXYPlotSpace *timeZonePlotSpace = (CPTXYPlotSpace *) timeZoneGraph.defaultPlotSpace;
    stat = (CSGeoStat *) self.timeZoneData[self.timeZoneData.count-1];
    timeZonePlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(stat.count * 1.1)];
    timeZonePlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-20.0) length:CPTDecimalFromFloat([self.timeZoneData count]*kBarHeight*1.5)];

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSNumber *num = @0;
    //Bars are horizontal, too! 
    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*(kBarHeight+3)) + kBarHeight/2 ];

            break;
    }
    return num;

}

【问题讨论】:

    标签: ios core-plot


    【解决方案1】:

    试试这个:

    countryPlot.barWidthsAreInViewCoordinates = YES;
    countryPlot.barWidths = 10.0;
    

    第一行告诉 Core Plot 使用视图坐标而不是绘图坐标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2012-04-19
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多