使用CorePlot Library绘制多个图形

时间:2012-06-04 04:44:08

标签: iphone objective-c ios

我必须绘制多个图形。我必须考虑绿色图形的一个Y值为红色,蓝色图形和Y2轴值。我正在使用核心平台库......我做了类似下面的内容并获得了图形如下图所示。但是我需要为y和y2设置不同的值。enter image description here我没有得到如何绘制...请帮助我......

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];

2 个答案:

答案 0 :(得分:1)

很抱歉,您无法在一个绘图空间中添加多个x或y轴。

所以无论你想做什么,只需在你的情节中制作动态y或x轴。

只计算y轴的最大值是这个,x轴是这个。

但是你必须只使用一个y轴和x轴用于相同的绘图空间。

答案 1 :(得分:0)

问题可能是您要将两个绘图空间添加到self.graph.defaultPlotSpace。尝试在CPTGraph中使用addPlotSpace方法。并且还尝试为每个CPTPlotSpace使用唯一的CPTPlotSpace标识符属性。