ShurupuS
ShurupuS

Reputation: 2923

The half of the first bar is hidden in Core Plot

I have a Core Plot BarChart like this:

enter image description here

As you can see - the half of the first bar is hidden by the y axis. How can I make the bounds of the visible area of the graph a little bit wider?

EDIT:

- (void) drawBarPlot
{
    [self configureBarGraph];
    [self configureBarPlot];
    [self configureAxes];
}

- (void) configureBarGraph {

    // 1 - Create the graph

    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.barChartView.bounds];
    graph.plotAreaFrame.masksToBorder = NO;
    self.barChartView.hostedGraph = graph;

    // 2 - Configure the graph

    [graph applyTheme:nil];

    graph.paddingBottom = 0.0f;
    graph.paddingLeft = 0.0f;
    graph.paddingTop = 0.0f;
    graph.paddingRight = 0.0f;

    graph.plotAreaFrame.paddingLeft = 30.0f;
    graph.plotAreaFrame.paddingTop = 20.0f;
    graph.plotAreaFrame.paddingRight = 30.0f;
    graph.plotAreaFrame.paddingBottom = 20.0f;

    // 3 - Set up plot space

    CGFloat xMin = 0.0f;
    CGFloat xMax = (CGFloat)[[[[PlotDataStore sharedInstance] docsCount] valueForKey:@"docs"] count];
    CGFloat yMin = 0.0f;
    CGFloat yMax = 10.0f;  // should determine dynamically based on max price
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];
}

- (void) configureBarPlot {

    // 1 - Set up the plot

    self.barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];

    // 2 - Set up line style

    CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
    barLineStyle.lineColor = [CPTColor lightGrayColor];
    barLineStyle.lineWidth = 0.5;

    // 3 - Add plots to graph

    CPTGraph *graph = self.barChartView.hostedGraph;

    self.barPlot.dataSource = self;
    self.barPlot.delegate = self;

    self.barPlot.barCornerRadius = 0.0f;
    self.barPlot.lineStyle = barLineStyle;
    self.barPlot.baseValue = CPTDecimalFromDouble(0.0f);
    self.barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:100.0f / 255.0f green:192.0f / 255.0f blue:245.0f / 255.0f alpha:1.0f]];

    [graph addPlot:self.barPlot toPlotSpace:(CPTPlotSpace *) graph.defaultPlotSpace];

}

- (void) configureAxes {

    // 1 - Configure styles

    CPTMutableTextStyle *axisLabelsStyle = [CPTMutableTextStyle textStyle];
    axisLabelsStyle.color = [CPTColor grayColor];
    axisLabelsStyle.fontName = @"Helvetica";

    // 2 - Get the graph's axis set

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.barChartView.hostedGraph.axisSet;

    axisSet.xAxis.axisLineStyle = nil;
    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    axisSet.xAxis.labelTextStyle = axisLabelsStyle;

    NSArray *customTickLocations = [NSArray arrayWithObjects: [NSDecimalNumber numberWithInt:0], [NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:2], [NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:4], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:6], [NSDecimalNumber numberWithInt:7], [NSDecimalNumber numberWithInt:8], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:11], [NSDecimalNumber numberWithInt:11], nil];
    NSArray *xAxisLabels = [[[PlotDataStore sharedInstance] docsCount] valueForKey:@"dates"];
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];

    for (NSNumber *tickLocation in customTickLocations) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:[tickLocation intValue]] textStyle:axisSet.xAxis.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.minorTickLength;
        [customLabels addObject:newLabel];
    }

    axisSet.xAxis.axisLabels =  [NSSet setWithArray:customLabels];

    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    axisSet.yAxis.axisLineStyle = nil;
    axisSet.yAxis.axisLabels = nil;
}

- (NSUInteger) numberOfRecordsForPlot:(CPTPlot *)plot {

    if ([plot class] == [CPTBarPlot class]) {
        return (NSUInteger)[[[[PlotDataStore sharedInstance] docsCount] valueForKey:@"docs"] count];
    }

    return 0;
}

- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {

    if ([plot class] == [CPTBarPlot class]) {

        switch ( fieldEnum ) {

            case CPTBarPlotFieldBarLocation:
                return [NSNumber numberWithUnsignedInteger:index];
                break;

            case CPTBarPlotFieldBarTip:
                return (NSNumber *)[[[[PlotDataStore sharedInstance] docsCount] valueForKey:@"docs"] objectAtIndex:index];
                break;

        }
    }
}

Upvotes: 0

Views: 296

Answers (2)

Eric Skroch
Eric Skroch

Reputation: 27381

The bar locations are integers. Therefore, with the xRange starting at zero (0), the first bar is centered at the left edge of the plot. To fix it, make the starting location -0.5 and the length (number of bars + 1).

Upvotes: 1

Volker
Volker

Reputation: 4658

You can set various properties of the graph or plot space influencing the display width. For example you can set the xRange of the plotSpace

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(calculatedLength)];

or the orthogonalCoordinateDecimal for y and x axis

y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);

Upvotes: 0

Related Questions