核心情节如何绘制两条线并使用不同的数据

时间:2013-07-24 06:53:51

标签: ios

我创建了一个核心情节图,有一个x轴,两个y轴,现在,我想有两条线,第一个使用第一个y轴,第二个使用第二个y轴,但它没有'工作。谁可以指导我?

    - (void)viewDidLoad
{
    [super viewDidLoad];
    graph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(10, 100, 300, 300)];
    //    graph.backgroundColor = [CPTColor clearColor].cgColor;
    CPTTheme * theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
    [graph applyTheme:theme];

    CPTGraphHostingView * hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(10, 100, 300, 300)];
    hostingView.hostedGraph = graph;

    [self.view addSubview:hostingView];
    [hostingView release];

    graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];

    // Plot area
//    graph.plotAreaFrame.fill          = [CPTFill fillWithColor:[CPTColor clearColor]];
    graph.plotAreaFrame.paddingTop    = 10;
    graph.plotAreaFrame.paddingBottom = 50;
    graph.plotAreaFrame.paddingLeft   = 20.0;
    graph.plotAreaFrame.paddingRight  = 20.0;
    graph.plotAreaFrame.cornerRadius  = 10.0;
    graph.plotAreaFrame.masksToBorder = NO;

//    graph.plotAreaFrame.axisSet.borderLineStyle = [CPTLineStyle lineStyle];

    graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithColor:[CPTColor clearColor]];

    // Setup plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1) length:CPTDecimalFromDouble(7)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(53.0) length:CPTDecimalFromDouble(4)];

    // Line styles
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 3.0;
    axisLineStyle.lineCap   = kCGLineCapRound;




    // Label x axis with a fixed interval policy
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x          = axisSet.xAxis;
    x.separateLayers              = YES;//
    x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(53);
    x.majorIntervalLength         = CPTDecimalFromDouble(1);
    x.minorTicksPerInterval       = 1;
    x.tickDirection               = CPTSignNegative;
    x.axisLineStyle               = axisLineStyle;
    x.majorTickLength             = 2.0;
    x.majorTickLineStyle          = axisLineStyle;

    // Label y with an automatic labeling policy.
    axisLineStyle.lineColor = [CPTColor greenColor];

    CPTXYAxis *y = axisSet.yAxis;
    y.labelingPolicy        = CPTAxisLabelingPolicyAutomatic;
    y.separateLayers        = YES;//
    y.minorTicksPerInterval = 0;
    y.tickDirection         = CPTSignNegative;
    y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(1.0);
    y.axisLineStyle         = axisLineStyle;
    y.majorTickLength       = 6.0;
    y.majorTickLineStyle    = axisLineStyle;


    // Label y2 with an equal division labeling policy.
    axisLineStyle.lineColor = [CPTColor orangeColor];

    CPTXYPlotSpace * plotSpace2 = [[CPTXYPlotSpace alloc] init];
    plotSpace2.xRange = plotSpace.xRange;
    plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1) length:CPTDecimalFromFloat(5)];
    [graph addPlotSpace:plotSpace2];

    CPTXYAxis *y2 = [[[CPTXYAxis alloc] init] autorelease];
    y2.coordinate                  = CPTCoordinateY;
    y2.plotSpace                   = plotSpace2;
    y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(8.0);
    y2.labelingPolicy              = CPTAxisLabelingPolicyAutomatic;//
    y2.separateLayers              = NO;
    y2.preferredNumberOfMajorTicks = 0;
    y2.minorTicksPerInterval       = 0;
    y2.tickDirection               = CPTSignPositive;
    y2.axisLineStyle               = axisLineStyle;
    y2.majorTickLength             = 6.0;
    y2.majorTickLineStyle          = axisLineStyle;
    y2.minorTickLength             = 4.0;
    y2.titleOffset                 = 30.0;

    //

    CPTMutableLineStyle * lineStyle1 = [[CPTMutableLineStyle lineStyle] retain];
    lineStyle1.lineWidth = 3.0f;
    lineStyle1.lineColor = [CPTColor blueColor];



    dataSourceLinePlot1 = [[CPTScatterPlot alloc] init];

    dataSourceLinePlot1.identifier = @"1plot";

    CPTPlotSymbol *plotSymbol1 = [CPTPlotSymbol ellipsePlotSymbol];
    plotSymbol1.size = CGSizeMake(10, 10);
    plotSymbol1.fill = [CPTFill fillWithColor:[CPTColor redColor]];
    plotSymbol1.lineStyle = lineStyle1;

    CPTMutableShadow *shadow1 = [CPTMutableShadow shadow];
    shadow1.shadowColor = [CPTColor blueColor];
    shadow1.shadowBlurRadius = 10.0;
    dataSourceLinePlot1.shadow = shadow1;

    dataSourceLinePlot1.plotSymbol = plotSymbol1;
    dataSourceLinePlot1.dataLineStyle = lineStyle1;
    dataSourceLinePlot1.dataSource = self;
    [graph addPlot:dataSourceLinePlot1];


    //-----------------------------------
    CPTMutableLineStyle * lineStyle2 = [[CPTMutableLineStyle lineStyle] retain];
    lineStyle2.lineWidth = 3.0f;
    lineStyle2.lineColor = [CPTColor greenColor];
    dataSourceLinePlot2 = [[CPTScatterPlot alloc] init];


    dataSourceLinePlot2.identifier = @"2plot";

    CPTPlotSymbol * plotSymbol2 = [CPTPlotSymbol ellipsePlotSymbol];
    plotSymbol2.size = CGSizeMake(10, 10);
    plotSymbol2.fill = [CPTFill fillWithColor:[CPTColor redColor]];
    plotSymbol2.lineStyle = lineStyle2;

    CPTMutableShadow *shadow2 = [CPTMutableShadow shadow];
    shadow2.shadowColor = [CPTColor greenColor];
    shadow2.shadowBlurRadius = 10.0;
    dataSourceLinePlot2.shadow = shadow2;

    dataSourceLinePlot2.plotSymbol = plotSymbol2;
    dataSourceLinePlot2.dataLineStyle = lineStyle2;
    dataSourceLinePlot2.dataSource = self;
    [graph addPlot:dataSourceLinePlot2];

    // Add the y2 axis to the axis set
    graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];


    dataArray1 = [[NSMutableArray alloc] init];
    dataArray2 = [[NSMutableArray alloc] init];
    for(int i=0; i< 7; i++){
        float a = (float)(random()%40 + 530) / 10;
        [dataArray1 addObject:[NSNumber numberWithFloat:a]];
    }
    for (int i = 0; i < 7; i++) {
//        float a = (float)(random()%40 + 530) / 10;
        [dataArray2 addObject:[NSNumber numberWithFloat:4]];
    }
NSLog(@"array1 = %@,array2 = %@",dataArray1,dataArray2);
}

有我的代表:

  -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
    {
        NSLog(@"%d",[dataArray1 count]);
        if ([[plot identifier] isEqual:@"1plot"]) {
            return [dataArray1 count];
        }
        return [dataArray2 count];
    }

- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    if(fieldEnum == CPTScatterPlotFieldY){    
        if ([[plot identifier] isEqual:@"1plot"]) {
            return [dataArray1 objectAtIndex:index];
        }else{
            return [dataArray2 objectAtIndex:index];
        }
    }else{
        return [NSNumber numberWithFloat:index];
    }
}

1 个答案:

答案 0 :(得分:0)

在数组放入任何数据之前,您将数据源委托设置为self。您需要提前初始化数组或调用[graph reloadData];