Nilesh Kumar
Nilesh Kumar

Reputation: 2160

Core Plot 1.4 - Reduce the gap between Y-axis and X-axis labels

I am trying to implement Core plot in my iOS application. There are many issues which I am facing.I am going to list one by one.

1)When my app runs it should start with graph showing with origin (0,0). Because at present its displaying somewhere which is not visible. Then I changed below Boolean value to YES and then I scrolled and saw the the line which was way above the Origin.

"plotSpace.allowsUserInteraction = YES;

2)I can manually adjust the space between 2 points on X-axis and Y-Axis lines.

Here is the code which I tried by following Rey Wenderlich tutorial to plot graph, but as you see in image I can see only one line. For others lines I need to drag up and down to see others lines. And a special thanks to Eric Skroch and his team for their wonderful work. Thanks

enter image description here

////////   configure x-Axes

    CPTXYAxis *xAxis = [axisSet xAxis];

    CPTAxis *x = axisSet.xAxis;
    x.title = @"";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 5.0f;
    x.labelOffset = 5.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.labelRotation=45.0f;

    CGFloat dateCount = [dayArray count];
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
    NSInteger i = 0;
    for (NSString *date in dayArray)
    {

        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date  textStyle:x.labelTextStyle];
        CGFloat location = i++;
        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = x.majorTickLength;
        if (label)
        {
            [xLabels addObject:label];
            [xLocations addObject:[NSNumber numberWithFloat:location]];
        }
    }
    x.axisLabels = xLabels;
    x.majorTickLocations = xLocations;

    //////// Configure Y-Axes

    CPTAxis *y = axisSet.yAxis;
    y.title = @"Hb";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -50.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = -0.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.majorTickLength = 1.0f;
    y.minorTickLength = 1.0f;

    NSInteger majorIncrement = 2;
    NSInteger minorIncrement = 2;
    CGFloat yMax = 30.0f;  
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = 0; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location =CPTDecimalFromInteger(j);
            label.tickLocation =location;
            label.offset = -y.majorTickLength- y.labelOffset;
            if (label) {
                [yLabels addObject:label];
            }
            [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;

Upvotes: 0

Views: 215

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

If you want to provide custom axis labels, change the labelingPolicy:

x.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelingPolicy = CPTAxisLabelingPolicyNone;

The automatic labeling policy sets the tick locations and labels automatically to "nice" values based on the length of the plot range. The locations provided policy automatically creates labels at the tick locations you provide.

Upvotes: 1

Related Questions