在条形图中动态设置条形的宽度

时间:2012-07-11 16:24:14

标签: objective-c xcode4.3 core-plot

大家好 有人可以帮助我解决我遇到的这个问题吗

我可以在coreplote中绘制条形图,条宽不等,意思是说我的X轴间隔是1个单位,我将条形图宽度设置为相同的1个单位。但是有一些点,其中x轴值在0.5单位处变化,并且我希望条宽度等于发生变化的宽度。数据正在动态变化。有没有其他我可以使用的解决方案,看起来仍像条形图。使用条形的唯一原因是我需要填充绘图空间。

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)barChart.defaultPlotSpace;
        plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromInt(0) 
                                                       length:CPDecimalFromInt(rangeY)];
        plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.5f) 
                                                       length:CPDecimalFromFloat(rangeX)];

        CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
        CPXYAxis *x = axisSet.xAxis;
        x.axisLineStyle = nil;
        x.majorTickLineStyle = nil;
        x.minorTickLineStyle = nil;
        x.majorIntervalLength = CPDecimalFromString(@"1");
        x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");

        x.title = [[[tempCol colName] componentsSeparatedByString:@"*@"] objectAtIndex:0];
        x.titleLocation = CPDecimalFromFloat((rangeX-1)/2);
        x.titleOffset = 25.0f;

        // Define some custom labels for the data elements
        x.labelRotation = 0;
        x.labelingPolicy = CPAxisLabelingPolicyNone;
        NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];
        NSMutableArray *xAxisLabels = [[NSMutableArray alloc] init];
        for (int i = 0; i < [tempGraph.d2pArray count]; i++) {
            tempD2P = [tempGraph.d2pArray objectAtIndex:i];
            [customTickLocations addObject:[NSNumber numberWithInt:i]];
            [xAxisLabels addObject:[tempD2P d2pName]];
        }
        NSUInteger labelLocation = 0;
        NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
        for (NSNumber *tickLocation in customTickLocations) {
            CPAxisLabel *newLabel = [[CPAxisLabel alloc] 
                                     initWithText:[xAxisLabels objectAtIndex:labelLocation++] 
                                     textStyle:x.labelTextStyle];
            newLabel.tickLocation = [tickLocation decimalValue];
            newLabel.offset = 0;
            newLabel.rotation = 0;
            newLabel.alignment = CPAlignmentCenter;     
            [customLabels addObject:newLabel];
            [newLabel release];
            newLabel = nil;
        }
        [xAxisLabels release];
        xAxisLabels = nil;
        [customTickLocations release];
        customTickLocations = nil;
        x.axisLabels =  [NSSet setWithArray:customLabels];

        CPXYAxis *y = axisSet.yAxis;
        CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle];
        majorGridLineStyle.lineWidth = 0.75;
        majorGridLineStyle.lineColor = [CPColor colorWithCGColor:[[UIColor grayColor] CGColor]];
        y.axisLineStyle = nil;
        y.majorTickLineStyle = nil;
        y.majorGridLineStyle = majorGridLineStyle;
        y.minorTickLineStyle = nil;
        y.majorIntervalLength = CPDecimalFromInt(rangeY/8);
        y.orthogonalCoordinateDecimal = CPDecimalFromString(@"-0.5");
        y.title = [[[tempCol colName] componentsSeparatedByString:@"*@"] objectAtIndex:1];
        y.titleOffset = 30 + 5*log10(mult);
        y.titleLocation = CPDecimalFromInt(rangeY/2);
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setMaximumFractionDigits:0];
        y.labelFormatter = formatter;
        [formatter release];
        formatter = nil;
        char firstChar = 'F';
        CPBarPlot *barPlot;
        // First bar plot
        for (int i = 0; i < noOfBarCharts; i++) {
            static CPTextStyle *whiteText = nil;
            if ( !whiteText ) {
                whiteText = [[CPTextStyle alloc] init];
                whiteText.color = [CPColor whiteColor];
                whiteText.fontSize = 14;
            }
            barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor colorWithComponentRed:[[[rgbArray objectAtIndex:0] objectAtIndex:i] floatValue]/255.0 
                                                                                  green:[[[rgbArray objectAtIndex:1] objectAtIndex:i] floatValue]/255.0 
                                                                                   blue:[[[rgbArray objectAtIndex:2] objectAtIndex:i] floatValue]/255.0 
                                                                                  alpha:1.0] 
                                          horizontalBars:NO];
            barPlot.baseValue = CPDecimalFromString(@"0");
            barPlot.dataSource = self;
            barPlot.barOffset = ([tempGraph.graType rangeOfString:@"GROUPED"].location != NSNotFound)?(i+0.5)-(noOfBarCharts/2):0;
            barPlot.labelOffset = 0;
            barPlot.barWidth = 20;
            barPlot.identifier = [NSString stringWithFormat:@"%d%c", [tempGraph.graID intValue], (firstChar - noOfBarCharts +1 + i)];
            barPlot.delegate = self;




            [barChart addPlot:barPlot 
                  toPlotSpace:plotSpace];

2 个答案:

答案 0 :(得分:1)

我自己想出来而不是使用条形图我使用带有步骤图的散点图实现了这个诀窍:

Plot.interpolation = CPTScatterPlotInterpolationStepped;

并用以下内容填充情节:

Plot.areaFill = [CPTFill fillWithColor:[CPTColor blueColor]];
Plot.areaBaseValue = CPTDecimalFromInteger(0);

答案 1 :(得分:0)

如果您没有在条形图上设置边框线,则可以将条形宽度设置为条形图之间的距离的一半,然后将条形图宽度加倍。