iOS - 核心图中绘制线的Z顺序

时间:2014-12-17 07:34:21

标签: core-plot ios8.1

我在我的iOS App中集成了Core Plot。我有几个问题,如下所述:    1)需要在y轴后面显示黄线和红线。    2)只需将y范围设为5,10,15。Link For Graph Image

以下是我的代码:

-(void)configureHost {
        self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(60, 80, 400, 200)];
}

-(void)configureGraph {
    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
    self.hostView.hostedGraph = graph;

    graph.plotAreaFrame.borderLineStyle = nil;
    graph.plotAreaFrame.cornerRadius = 10.0f ;

    graph.paddingLeft = 0.0;
    graph.paddingRight = 0.0;
    graph.paddingTop = 0.0;
    graph.paddingBottom = 0.0;

    [graph.plotAreaFrame setPaddingLeft:30.0f];
    [graph.plotAreaFrame setPaddingRight:20.0f];
    [graph.plotAreaFrame setPaddingTop:20.0f];
    [graph.plotAreaFrame setPaddingBottom:40.0f];
}

-(void)configurePlots {

    CPTGraph *lGraph = self.hostView.hostedGraph;
    CPTXYPlotSpace *lPlotSpace = (CPTXYPlotSpace *) lGraph.defaultPlotSpace;

    CPTScatterPlot *lDataLinePlot = [[CPTScatterPlot alloc] init];
    lDataLinePlot.dataSource = self;
    lDataLinePlot.identifier = CPDTickerSymbolBLUELINE;
    lDataLinePlot.interpolation = CPTScatterPlotInterpolationCurved;


    CPTColor *lDataLineColor = [CPTColor blueColor];
    [lGraph addPlot:lDataLinePlot toPlotSpace:lPlotSpace];
    [lGraph.defaultPlotSpace scaleToFitPlots:[lGraph allPlots]];


    CPTScatterPlot *lDangerLinePlot = [[CPTScatterPlot alloc] init];
    lDangerLinePlot.dataSource = self;
    lDangerLinePlot.identifier = CPDTickerSymbolDANGERREDLINE;
    lDangerLinePlot.interpolation = CPTScatterPlotInterpolationCurved;


    CPTColor *lDangerLineColor = [CPTColor redColor];
    [lGraph addPlot:lDangerLinePlot toPlotSpace:lPlotSpace];

    CPTScatterPlot *lYellowLinePlot = [[CPTScatterPlot alloc] init];
    lYellowLinePlot.dataSource = self;
    lYellowLinePlot.identifier = CPDTickerSymbolYELLOWLINE;
    lYellowLinePlot.interpolation = CPTScatterPlotInterpolationCurved;

    CPTColor *lYellowLineColor = [CPTColor yellowColor];
    [lGraph addPlot:lYellowLinePlot toPlotSpace:lPlotSpace];

    [lPlotSpace scaleToFitPlots:[NSArray arrayWithObjects:lYellowLinePlot,lDangerLinePlot,lDataLinePlot, nil]];


    min=0;
    lPlotSpace.yRange = [CPTPlotRange
                         plotRangeWithLocation:CPTDecimalFromCGFloat(min)
                         length:CPTDecimalFromCGFloat((15))];



   CPTMutablePlotRange *lRangeX = [lPlotSpace.xRange mutableCopy];
    [lRangeX expandRangeByFactor:CPTDecimalFromCGFloat(1.005f)];
    lPlotSpace.xRange = lRangeX;

    CPTMutablePlotRange *lRangeY = [lPlotSpace.yRange mutableCopy];
    [lRangeY expandRangeByFactor:CPTDecimalFromCGFloat(1.0f)];
    lPlotSpace.yRange = lRangeY;

    CPTColor *lAreaColor = [CPTColor colorWithComponentRed:255.0 green:255.0 blue:255.0 alpha:1.0];
    CPTGradient *lAreaGradient =[CPTGradient gradientWithBeginningColor:lAreaColor endingColor:[CPTColor clearColor]];
    lAreaGradient. angle = 90.0f ;



    CPTMutableLineStyle *lDataLineStyle = [lDataLinePlot.dataLineStyle mutableCopy];
    lDataLineStyle.lineWidth = 3;
    lDataLineStyle.lineColor = lDataLineColor;
    lDataLinePlot.dataLineStyle = lDataLineStyle;

    CPTMutableLineStyle *lYellowLineStyle = [lDataLinePlot.dataLineStyle mutableCopy];
    lYellowLineStyle.lineWidth = 1.5;
    lYellowLineStyle.lineColor = lYellowLineColor;
    lYellowLinePlot.dataLineStyle = lYellowLineStyle;

    CPTMutableLineStyle *lDataLineStyleSymbol = [CPTMutableLineStyle lineStyle];
    lDataLineStyleSymbol.lineColor = lDataLineColor;

    CPTMutableLineStyle *lYellowLineStyleSymbol = [CPTMutableLineStyle lineStyle];
    lYellowLineStyleSymbol.lineColor = lYellowLineColor;


    CPTMutableLineStyle *lDangerLineStyle = [lDangerLinePlot.dataLineStyle mutableCopy];
    lDangerLineStyle.lineWidth = 1.5;
    lDangerLineStyle.lineColor = lDangerLineColor;
    lDangerLinePlot.dataLineStyle = lDangerLineStyle;

    CPTMutableLineStyle *lDangerLineStyleSumbol = [CPTMutableLineStyle lineStyle];
    lDangerLineStyleSumbol.lineColor = lDangerLineColor;


    CPTMutableLineStyle *lDataSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    lDataSymbolLineStyle.lineColor = lDataLineColor;

    CPTMutableLineStyle *lYellowSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    lYellowSymbolLineStyle.lineColor = lYellowLineColor;
}

-(void)configureAxes {

    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor blackColor];
    axisTitleStyle.fontName = @"Helvetica";
    axisTitleStyle.fontSize = 13.0f;

    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 3.0f;
    axisLineStyle.lineColor = [CPTColor blackColor];

    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor blackColor];
    axisTextStyle.fontName = @"Bold";
    axisTextStyle.fontSize = 8.0f;

    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;

        /////X-AXIS DATE

    DateArray=[[NSMutableArray alloc]init];
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    format.dateFormat=@"MM/dd/yy";

    NSCalendar *calendar1 = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *components1 = [calendar1 components:NSCalendarUnitYear
                                     | NSCalendarUnitMonth | NSCalendarUnitDay
                                                 fromDate:[NSDate date]];
    for (int i = 0; i < 60; ++i)
        {
        NSDate *date1 = [calendar1 dateFromComponents:components1];
        NSString *getDate1=[format stringFromDate:date1];
        NSString *lReplaced = [getDate1 substringToIndex:5];
        lReplaced = [lReplaced stringByReplacingOccurrencesOfString:@"-"
                                                         withString:@"/"];

        NSString *match = @"/";
        NSMutableString *preTel;
        NSString *postTel;
        NSScanner *scanner = [NSScanner scannerWithString:lReplaced];
        [scanner scanUpToString:match intoString:&preTel];
        [scanner scanString:match intoString:nil];
        postTel = [lReplaced substringFromIndex:scanner.scanLocation];

        NSString *newStr1;
        NSString *newStr2;

        if ([preTel intValue] <10) {
            newStr1 = [preTel substringFromIndex:1];
        }else{
            newStr1=preTel;
        }

        if ([postTel intValue] <10) {
            newStr2 = [postTel substringFromIndex:1];
        }else{
            newStr2=postTel;
        }

        lReplaced =[NSString stringWithFormat:@"%@%@%@",newStr1,match,newStr2];
        [DateArray addObject:lReplaced];
        ++components1.day;
        }
    NSOrderedSet *mySet = [[NSOrderedSet alloc] initWithArray:DateArray];
    DateArray = [[NSMutableArray alloc] initWithArray:[mySet array]];

        ////////   configure x-Axes

    CPTAxis *x = axisSet.xAxis;
    x.titleTextStyle = axisTitleStyle;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.tickDirection = CPTSignPositive;
    CGFloat dateCount = [dayArray count];
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];

    int lIndex = 1;
    for (int i=0;i< [DateArray count];i++)
        {
            // NSLog(@"i VAL:%d",i);
        NSString *date = [DateArray objectAtIndex:i];
            // NSLog(@"date:%@",date);
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date  textStyle:x.labelTextStyle];
        CGFloat location = i*3.40;
            //NSLog(@"LOC:%f",location);
        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = -20.0f;
        [xLabels addObject:label];
        [xLocations addObject:[NSNumber numberWithFloat:location]];
        lIndex++;
        i = i+6;
        }
    x.axisLabels = xLabels;
    x.majorTickLocations = xLocations;

        //////// Configure Y-Axes
    CPTAxis *y = axisSet.yAxis;
    y.title = @"Hb";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -30.0f;
    y.axisLineStyle = axisLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
        // y.preferredNumberOfMajorTicks=5;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = -35.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.tickDirection = CPTSignPositive;


    NSInteger majorIncrement = 5;
    NSInteger minorIncrement = 5;
    CGFloat yMax =15.0f;  
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            label.offset = -20.0f;//-y.majorTickLength - y.labelOffset;
            if (label) {
                [yLabels addObject:label];
            }
            [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;
}

1 个答案:

答案 0 :(得分:2)

  1. 使用topDownLayerOrder移动图表前面的轴线。

    CPTPlotArea *plotArea = graph.plotAreaFrame.plotArea;
    plotArea.topDownLayerOrder = @[@(CPTGraphLayerTypeAxisLines)];
    
  2. yRange设置为5.0,长度为10.0:

    lPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(5.0)
                                                     length:CPTDecimalFromDouble(10.0)];