sujith1406
sujith1406

Reputation: 2822

how to change dates as axis labels in scatterplot

How to put dates as axis labels in a scatterplot when using coreplot?

I found in examples how to change the axislabels in barchart..but when i used this code in scatterplot...

this is not working.

Upvotes: 0

Views: 522

Answers (1)

Rahul Vyas
Rahul Vyas

Reputation: 28720

It works You need to follow the same code used in barChart plot example. I have done this in scatter plot. Here is a method how I'm doing It.

- (void) setRangeForGraph {

        //Customize x and y axis
        CPLineStyle *lineStyle = [CPLineStyle lineStyle];
        lineStyle.lineColor = [CPColor colorWithCGColor:((UIColor*)kProtienColor).CGColor];
        lineStyle.lineWidth = 1.0f;

        CPTextStyle *whiteText = [CPTextStyle textStyle];
        whiteText.color = [CPColor grayColor];              


        CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;

        //Customize x axis
        CPXYAxis *x = axisSet.xAxis;

        //x.title = @"Date";
        x.majorIntervalLength = CPDecimalFromInteger(1);

        x.axisLineStyle = lineStyle;
        x.minorTickLineStyle = lineStyle;

        x.titleOffset = 0;
        x.labelOffset = 5;
        x.titleLocation = CPDecimalFromFloat(0);

        x.titleTextStyle=whiteText;
        x.labelTextStyle=whiteText;
        x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
        x.titleOffset = 0.0f;


        // Define some custom labels for the data elements
        x.labelRotation = M_PI/4;
        x.labelingPolicy = CPAxisLabelingPolicyNone;

        NSMutableArray *ticks = [[[NSMutableArray alloc] initWithCapacity:[self.weightEntries count]] autorelease];

        for(unsigned int counter = 0; counter < [self.weightEntries count];counter++){
            [ticks addObject:[NSNumber numberWithInt:counter]];
        }

        NSUInteger labelLocation = 0;

        NSArray *xAxisLabels = [self createDateAxisLabels];

        NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];

        @try {

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

        }
        @catch (NSException * e) {
            NSLog(@"An exception occured while creating date labels for x-axis");
        }
        @finally {
            x.axisLabels =  [NSSet setWithArray:customLabels];  
        }


        //Customize y axis

        CPXYAxis *y = axisSet.yAxis;

        //y.title = @"Weight";
        y.majorIntervalLength   = CPDecimalFromFloat([high floatValue]/7);

        y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");

        y.titleOffset           = 35.0f;
        y.titleLocation         = CPDecimalFromFloat([high floatValue]/2);
        y.paddingLeft           = 0;
        //y.majorIntervalLength = CPDecimalFromString(@"150");
        y.minorTicksPerInterval = 5;
        y.axisLineStyle         = lineStyle;
        y.titleTextStyle        = whiteText;
        y.minorTickLineStyle    = lineStyle;
        y.labelTextStyle        = whiteText;
        y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");

    }
}

Upvotes: 1

Related Questions