Jean
Jean

Reputation: 2625

Core plot majorIntervalLength : unable to make it work

am trying to plot a simple graph (cannot be so tough! I have used core plot successfully before!). I have some dates to be plotted in the x-axis. And some small float values in the y-axis, like 0.139503, 0.139481, etc. I have to plot 5 such values (which I fetch from the backend). For the y-axis range, am finding the lowest of these values and setting it as the start of the y-axis range. And am setting the majorIntervalLength to (highestValue - LowestValue)/5, which is (for example) 0.007946. But the y-axis interval seems to be fixed at interval of 1. So, since my y-axis values are really small (difference between them is in the order of 0.01), all plots are being drawn at y=0.0. Here's the code snippet :

-(void) initializeGraph {


    if ((self.graph == nil) || (self.graph == NULL)) {

        self.graphHostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];

        self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
    graph.paddingLeft   = 2.0;
    graph.paddingRight  = 2.0;
    graph.paddingTop    = 2.0;
    graph.paddingBottom = 2.0;
    graph.title = @"Graph Plot";

    // plotting style is set to line plots
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineColor = [CPTColor blackColor];
    lineStyle.lineWidth = 2.0f;

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.graph.axisSet;
    axisSet.xAxis.majorTickLineStyle = lineStyle;
    axisSet.xAxis.majorTickLength = 7.0f;
    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
    axisSet.xAxis.labelRotation = 90.0;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
    // Determine the lowest and highest values to be plotted in Y Axis)
    CGFloat yLow = 0.1;
    CGFloat yHigh = yLow;
    for (int i=0 ; i<self.dict.count ; i++) {
        MyCustomObject *myData = (MyCustomObject *) [dict objectForKey:
                                                           [self.arrayKeys objectAtIndex:i]];

        if (myData.mem < yLow) {
            yLow = myData.mem;
        }
        if (yHigh < myData.mem) {
            yHigh = myData.mem;
        }
    }
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
                                                  length:CPTDecimalFromFloat(5)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yLow)
                                                    length:CPTDecimalFromFloat(5)];

    // To determine the point where your graph starts
    //axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(yLow);
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromCGFloat((yHigh-yLow)/5.0);
    NSLog(@"Y axis interval : %f", (yHigh-yLow)/5.0);
    ...
}

-(NSNumber *) numberForPlot:(CPTPlot *)plot
                  field:(NSUInteger)fieldEnum
            recordIndex:(NSUInteger)index {
    if ( fieldEnum == CPTScatterPlotFieldY ) {
        return [NSNumber numberWithFloat:myData.mem];
    } else {
        return [NSNumber numberWithInt:index];
    }
}

Why is that the majorIntervalLength for y axis that am setting not working?

Upvotes: 1

Views: 944

Answers (2)

Jean
Jean

Reputation: 2625

Setting length of yRange (as Erik Skroch suggested) to (yHigh-yLow) helped

Upvotes: 0

andrew lattis
andrew lattis

Reputation: 1549

are you setting the axisSet.yAxis.majorTickLength anywhere? i don't see it in your code above.

if the majorTickLength is nil nothing will show up for the tick's.

Upvotes: 0

Related Questions