Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

Draw multiple graphs using CorePlot Library

I have to draw multiple graphs .I have to consider one Y Values to red, blue graphs and Y2 Axis values for green graph.I am using core plat library ......I done something like below and getting graph like below image .But i need set different values for y and y2.enter image description here I am not getting how to draw...please help me...

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(self.xRangeMinVal) length:CPTDecimalFromInt(self.xRangeMaxVal)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(self.yRangeMinVal) length:CPTDecimalFromInt(self.yRangeMaxVal)];
    //plotSpace.allowsUserInteraction = YES;
    //plotSpace.delegate = self;

    // Grid line styles
    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorGridLineStyle.lineWidth = 0.75;
    majorGridLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:2.0f], nil];
    majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.4] colorWithAlphaComponent:0.4];
    CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
    minorGridLineStyle.lineWidth = 0.25;
    minorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.4] colorWithAlphaComponent:0.1];    

    CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
    textStyle.color                   = [CPTColor blackColor];
    textStyle.fontSize                = 16.0f;
    textStyle.textAlignment           = CPTTextAlignmentCenter;


    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 3.0;
    axisLineStyle.lineCap   = kCGLineCapRound;
    // Axes
    // Label x axis with a fixed interval policy
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    x.majorIntervalLength = CPTDecimalFromString(@"2.0");
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
    x.minorTicksPerInterval = 1;
    x.majorGridLineStyle = majorGridLineStyle;
    x.minorGridLineStyle = minorGridLineStyle;
    //x.preferredNumberOfMajorTicks=;

    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    //x.title = [NSString stringWithFormat:@"goat"];
    //x.titleOffset = 10.0;
    //x.titleLocation = CPTDecimalFromString(@"0.0");
    x.title = self.xLineTitle;
    x.axisLineStyle            = axisLineStyle;
    x.titleTextStyle = textStyle;

    CPTMutableLineStyle *dottedStyle=[CPTMutableLineStyle lineStyle];


    x.minorGridLineStyle=dottedStyle;

    // Label y with an automatic label policy. 
    CPTXYAxis *y = axisSet.yAxis;
    y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
   // y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"-10.0");
    y.minorTicksPerInterval = 2;
    y.preferredNumberOfMajorTicks = 8;
    y.majorGridLineStyle = majorGridLineStyle;
    y.minorGridLineStyle = minorGridLineStyle;
    //y.labelOffset = 1.0;
    y.title = self.yLineTitle;
    y.titleTextStyle = textStyle;
    y.axisLineStyle            = axisLineStyle;
    y.titleRotation = M_PI * 0.5;
    y.minorGridLineStyle=dottedStyle;


    CPTXYPlotSpace *plotSpace1 = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
   // CPTXYPlotSpace *plotSpace1 = [[[CPTXYPlotSpace alloc] init] autorelease];
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(self.xRangeMinVal) length:CPTDecimalFromInt(self.xRangeMaxVal)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(60)];
    CPTXYAxis *y2 = [[[CPTXYAxis alloc] init] autorelease];
    y2.coordinate                  = CPTCoordinateY;
    y2.plotSpace                   = plotSpace1;
    y2.majorGridLineStyle          = majorGridLineStyle;
    y2.minorGridLineStyle          = minorGridLineStyle;
    y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(self.xRangeMaxVal);
    y2.labelingPolicy              = CPTAxisLabelingPolicyAutomatic;
    //y2.separateLayers            = NO;
    y2.preferredNumberOfMajorTicks = 7;
    y2.minorTicksPerInterval       = 2;//
    y2.tickDirection               = CPTSignPositive;
    y2.axisLineStyle               = axisLineStyle;
    //y2.majorTickLength               = 6.0;
    y2.majorTickLineStyle          = axisLineStyle;
    //y2.minorTickLength               = 4.0;
    y2.title                       = @"Y2 Axis";
    //y2.titleTextStyle            = axisTitleTextStyle;
    y2.titleOffset                 = 40.0;

    //graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
    self.graph.axisSet.axes = [NSArray arrayWithObjects:x, y,y2,nil];

Upvotes: 1

Views: 661

Answers (2)

Nilesh Kikani
Nilesh Kikani

Reputation: 2618

Sorry you can't add multiple x or y axis in one plot space.

so whatever you want to do is just make dynamic y or x-axis in your plot.

Just calculate that there is maximum value of y-axis is this and for x-axis this.

but you must have to use only one y-axis and x-axis for same plot space.

Upvotes: 1

Ben Boral
Ben Boral

Reputation: 355

The problem may be that you are adding both plot spaces to self.graph.defaultPlotSpace. Try using the addPlotSpace Method in CPTGraph. And also try using a unique CPTPlotSpace identifier attribute for each of your CPTPlotSpaces.

Upvotes: 0

Related Questions