mbpro
mbpro

Reputation: 2490

Bars not aligned with X-Axis ticks in CPTBarPlot of Core Plot

I have a problem with CPTBarPlot in Core Plot on Cocoa. Namely, I cannot achieve to control step width of bars. X-Axis ticks are set normally as integer from 1 to infinite and drawing Scatter Plot on it works.

However, when bar plot is drawn, only first columns starts (with proper offset, of course) on first tick. Second is a bit further form its tick, so is third and all others as shown on screenshot:

enter image description here

I haven't found any property to control this, however, I found that data source method

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

Behaves differently than when I draw Scatter Plot. namely, it enumerates only integer 3, which is CPTBarPlotBindingBarTips and doesn't enumerate any other constant, I am especially bothered with absence of CPTBarPlotBindingBarLocations.

If I start VerticalBarChart from Plot Gallery mac demo project I've checked and seen that same method enumerates 3,4 and 2 constants in same order.

I am using Core Plot 0.9 namely, I believe the same as in this demo. Still...

How to solve this?

Here is the complete code for labels and ticks on X axis;

    x.labelRotation = M_PI/4;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelOffset = 0.0;

    CPTMutableTextStyle *labelStyle = [CPTMutableTextStyle textStyle];
    labelStyle.fontSize = 9.0;
    x.labelTextStyle = labelStyle;

//    NSMutableArray *dateArray = [dataSeries objectAtIndex:0];
    NSMutableArray *customTickLocations = [NSMutableArray array];
    NSMutableArray *xAxisLabels = [NSMutableArray array];

    for (int i=0;i<[dataSeries count];i++) {

        [customTickLocations addObject:[NSDecimalNumber numberWithInt:i+1]];
        [xAxisLabels addObject:[dataSeries objectAtIndex:i]];

    }

    //    [customTickLocations addObject:[NSDecimalNumber numberWithInt:[dateArray count]]];

    NSUInteger labelLocation = 0;
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
    for (NSNumber *tickLocation in customTickLocations) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = x.labelOffset + x.majorTickLength;
        newLabel.rotation = M_PI/4;
        [customLabels addObject:newLabel];
        [newLabel release];
    }

    [x setMajorTickLocations:[NSSet setWithArray:customTickLocations]];

    x.axisLabels =  [NSSet setWithArray:customLabels];

I have to add that same ticks are being used when scatter plot is drawn on same plot space and it matches perfectly....

Upvotes: 2

Views: 1358

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

The field parameter will be one of the members of this enum:

typedef enum _CPTBarPlotField {
    CPTBarPlotFieldBarLocation = 2, ///< Bar location on independent coordinate axis.
    CPTBarPlotFieldBarTip      = 3, ///< Bar tip value.
    CPTBarPlotFieldBarBase     = 4  ///< Bar base (used only if barBasesVary is YES).
} CPTBarPlotField;

If the plot's plotRange property is nil (the default), the plot will ask the datasource for locations and tips for each bar. If barBasesVary == YES, it will also ask for base values for each bar.

Upvotes: 2

Related Questions