图表未在第二个y轴上显示标签(coreplot)

时间:2014-02-14 10:37:09

标签: iphone core-plot

我的图表中有多个y轴,我想为每个y轴显示不同的图表比例。我的图表显示了2 y轴,但第二个y轴没有显示其上的比例。

我在每个轴上添加了两个不同的绘图空间。它正在绘制数据,但没有显示第二个y轴的比例。

//plot configuration
-(void)configurePlots {
CPTColor *your_color = [CPTColor colorWithComponentRed:126 green:230 blue:255 alpha:0];


    // 1 - Get graph and plot space
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    CPTXYPlotSpace *plotSpace2 =[[CPTXYPlotSpace alloc] init];
    graph.plotAreaFrame.fill = [CPTFill fillWithColor:your_color];
    // 2 - Create the three plots
    CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init];
    aaplPlot.dataSource = self;
    aaplPlot.identifier = CPDTickerSymbolAAPL;
    CPTColor *aaplColor = [CPTColor redColor];
    [graph addPlot:aaplPlot toPlotSpace:plotSpace];

    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:aaplPlot, nil]];
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace.xRange = xRange;
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
    plotSpace.yRange = yRange;



    // 4 - Create styles and symbols
    CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
    aaplLineStyle.lineWidth = 0;
    aaplLineStyle.lineColor = aaplColor;
    aaplPlot.dataLineStyle = aaplLineStyle;
    CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    aaplSymbolLineStyle.lineColor = aaplColor;
    CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
    aaplSymbol.lineStyle = aaplSymbolLineStyle;
    aaplSymbol.size = CGSizeMake(3.0f, 3.0f);
    aaplPlot.plotSymbol = aaplSymbol;

    CPTScatterPlot *googPlot = [[CPTScatterPlot alloc] init];
    googPlot.dataSource = self;
    googPlot.identifier = CPDTickerSymbolGOOG;
    CPTColor *googColor = [CPTColor greenColor];
    [graph addPlotSpace:plotSpace2];
    [graph addPlot:googPlot toPlotSpace:plotSpace2];


    [plotSpace2 scaleToFitPlots:[NSArray arrayWithObjects:googPlot, nil]];

    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace2.xRange = plotSpace.xRange;
    CPTMutablePlotRange *y2Range = [plotSpace2.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
    plotSpace2.yRange = y2Range;

    CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
    googLineStyle.lineWidth = 0;
    googLineStyle.lineColor = googColor;
    googPlot.dataLineStyle = googLineStyle;
    googPlot.plotSpace = plotSpace2;
    CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    googSymbolLineStyle.lineColor = googColor;
    CPTPlotSymbol *googSymbol = [CPTPlotSymbol diamondPlotSymbol];
    googSymbol.fill = [CPTFill fillWithColor:googColor];
    googSymbol.lineStyle = googSymbolLineStyle;
    googSymbol.size = CGSizeMake(2.0f, 2.0f);
    googPlot.plotSymbol = googSymbol;



    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor blackColor];
    axisTitleStyle.fontName = @"Helvetica";
    axisTitleStyle.fontSize = 10.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle ];
    axisLineStyle.lineWidth = 1.0f;
    axisLineStyle.lineColor = [CPTColor blackColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor blackColor];
    axisTextStyle.fontName = @"Helvetica";
    axisTextStyle.fontSize = 8.0f;
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
    gridLineStyle.lineColor = [CPTColor grayColor];
    gridLineStyle.lineWidth = 1.0f;


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

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) (id)self.hostView.hostedGraph.axisSet;
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(FinalMinValueY);




    CPTXYAxis *x = axisSet.xAxis;
    CPTXYAxis *y = axisSet.yAxis;
    CPTXYAxis *y2 = [(CPTXYAxis *)[CPTXYAxis alloc] init] ;



    //x.title = @"Day of Month";
    x.titleTextStyle = axisTitleStyle;

    x.titleOffset = 4.0f;
    x.axisLineStyle = axisLineStyle;
    //x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 2.0f;
    x.tickDirection = CPTSignNegative;
    x.minorTickLineStyle = nil;

    CGFloat dateCount = [Reportdates count];
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xMinorLocations = [NSMutableSet setWithCapacity:dateCount];
    NSInteger i = 0;
    NSInteger intervalValue = 0;
    NSArray *ArrDatesForMajorTicks =[self CalculateXasisInterval];
    for (NSString *date in Reportdates) {

        NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
        [DateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
        NSDate *d = [DateFormatter dateFromString:date];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"MM/dd/yy"];
        NSString *st = [dateFormat stringFromDate:d];
        // NSLog(@"%@",st);

        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:st  textStyle:x.labelTextStyle];
        //label.rotation = M_PI/4;
        CGFloat location = i;

        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = x.majorTickLength;
        if(i==0 || i==intervalValue)
        {
            if (label) {
                [xLabels addObject:label];
                [xLocations addObject:[NSNumber numberWithFloat:location]];
            }
            intervalValue = intervalValue + FinalIntervalValueX;
        }
        else
        {

            [xMinorLocations addObject:[NSNumber numberWithFloat:location]];

        }
        i++;
    }

    x.axisLabels = xLabels;
    x.majorTickLocations = xLocations;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    // x.minorTickLocations =xMinorLocations;
    // 4 - Configure y-axis

    //y.title = @"Price";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -40.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    //y.alternatingBandFills = [NSArray arrayWithObjects:[CPTColor redColor], [CPTColor greenColor], nil];
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = 12.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.majorTickLength = 1.0f;
    y.minorTickLength = 1.0f;
    y.tickDirection = CPTSignPositive;
    //x.preferredNumberOfMajorTicks = 6;

    NSInteger majorIncrement = FinalIntervalValueY;
    NSInteger minorIncrement = FinalIntervalValueY;
    CGFloat yMax = FinalMaxValueY;  // should determine dynamically based on max price
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = FinalMinValueY; j <= yMax; j += minorIncrement) {
        NSUInteger mod =  j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            //label.offset = -y.majorTickLength - y.labelOffset;
            label.offset = -20;
            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;




    //y2 axis implementation.



    //y2.title = @"Price";
    y2.titleTextStyle = axisTitleStyle;
//    y2.titleOffset = -40.0f;
    CPTMutableTextStyle *axisTextStyleY2 = [[CPTMutableTextStyle alloc] init];
    axisTextStyleY2.color = [CPTColor redColor];
    axisTextStyleY2.fontName = @"Helvetica";
    axisTextStyleY2.fontSize = 18.0f;

    y2.axisLineStyle = axisLineStyle;
    y2.majorGridLineStyle = Nil;
    //y.alternatingBandFills = [NSArray arrayWithObjects:[CPTColor redColor], [CPTColor greenColor], nil];
    y2.labelingPolicy = CPTAxisLabelingPolicyNone;
    y2.labelTextStyle = axisTextStyleY2;
    y2.labelOffset = 12.0f;
    y2.majorTickLineStyle = axisLineStyle;
    y2.majorTickLength = 1.0f;
    y2.minorTickLength = 1.0f;
    y2.tickDirection = CPTSignPositive;
    y2.axisConstraints =  [CPTConstraints constraintWithUpperOffset:0];
    y2.labelingPolicy = CPTAxisLabelingPolicyNone;
    y2.minorTicksPerInterval = 0;

    y2.coordinate = CPTCoordinateY;
    y2.plotSpace = plotSpace2;

    majorIncrement = FinalIntervalValueY;
    minorIncrement = FinalIntervalValueY;
    yMax = FinalMaxValueY;  // should determine dynamically based on max price
    NSMutableSet *yLabels1 = [NSMutableSet set];
    NSMutableSet *yMajorLocations1 = [NSMutableSet set];
    NSMutableSet *yMinorLocations1 = [NSMutableSet set];
    for (NSInteger j = FinalMinValueY; j <= yMax; j += minorIncrement) {
        NSUInteger mod =  j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y2.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            //label.offset = -y.majorTickLength - y.labelOffset;
            label.offset = -20;
            if (label) {
                [yLabels1 addObject:label];
            }
            [yMajorLocations1 addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations1 addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }

    y2.axisLabels = yLabels;
    y2.majorTickLocations = yMajorLocations1;
    y2.minorTickLocations = yMinorLocations1;



    x.coordinate =  CPTCoordinateX;
    y.coordinate =  CPTCoordinateY;
    y2.coordinate = CPTCoordinateY;

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

}

-(void)configureAxes {
    // 1 - Create styles

}


//data binding.
#pragma mark - Rotation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    //  return [[[CPDStockPriceStore sharedInstance] datesInMonth] count];
    return [Reportdates count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSInteger valueCount = [Reportdates count];
    switch (fieldEnum) {
        case CPTScatterPlotFieldX:
            if (index < valueCount) {
                return [NSNumber numberWithUnsignedInteger:index];
            }
            break;

        case CPTScatterPlotFieldY:
            if ([plot.identifier isEqual:CPDTickerSymbolAAPL] == YES) {
                //              return [[[CPDStockPriceStore sharedInstance] monthlyPrices:CPDTickerSymbolAAPL] objectAtIndex:index];
                return [ChartValues objectAtIndex:index];
            } else if ([plot.identifier isEqual:CPDTickerSymbolGOOG] == YES) {
                //              return [[[CPDStockPriceStore sharedInstance] monthlyPrices:CPDTickerSymbolGOOG] objectAtIndex:index];
                return [ChartValues objectAtIndex:index];
            } else if ([plot.identifier isEqual:CPDTickerSymbolMSFT] == YES) {
                return [[[CPDStockPriceStore sharedInstance] monthlyPrices:CPDTickerSymbolMSFT] objectAtIndex:index];
            }
            break;
    }
    return [NSDecimalNumber zero];
}

1 个答案:

答案 0 :(得分:1)

刻度线只有1个像素长(y2.majorTickLength = 1.0f;),与轴线的宽度相同。由于它们使用相同的样式,因此在轴线顶部绘制时它们是不可见的。使刻度更长或不同颜色,以便您可以看到它们。