0

我在下面有以下代码来显示两个 Y 轴(一个在右侧,一个在左侧)。我希望能够根据不同的数据集绘制到每个图表(映射到相同的 X 值)。我似乎无法同时显示两个 CPTXYPlotSpaces。我将如何解决这个问题,以便我可以绘制到两个不同的绘图空间并同时查看图形线。

    -(void)configureHost
{
    CGRect bounds = self.view.bounds;
    bounds.size.height = bounds.size.height;
    self.hostView = [[CPTGraphHostingView alloc] initWithFrame:bounds];
    self.hostView.allowPinchScaling = YES;
    [self.view addSubview:self.hostView];
}

-(void)configureGraph
{
    self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
    [self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
    self.hostView.hostedGraph = self.graph;

    self.graph.paddingLeft = 20.0;
    self.graph.paddingTop = 20.0;
    self.graph.paddingRight = 20.0;
    self.graph.paddingBottom = 20.0;


}

NSString *const kPlot1 = @"Plot 1";
NSString *const kPlot2 = @"Plot 2";

-(void)configurePlots
{
    CGRect bounds = self.hostView.bounds;

    self.plotSpace1 = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
    self.plotSpace1.allowsUserInteraction = NO;

    self.scatterPlot1 = [[CPTScatterPlot alloc]init];
    self.scatterPlot1.identifier = kPlot1;
    self.scatterPlot1.dataSource = self;
    [self.graph addPlot:self.scatterPlot1 toPlotSpace:self.plotSpace1];

    self.scatterPlot2 = [[CPTScatterPlot alloc]init];
    self.scatterPlot2.identifier = kPlot2;
    self.scatterPlot2.dataSource =self;
    [self.graph addPlot:self.scatterPlot2 toPlotSpace:self.plotSpace2];

    [self.plotSpace1 scaleToFitPlots:@[self.scatterPlot1, self.scatterPlot2]];
    [self.plotSpace2 scaleToFitPlots:@[self.scatterPlot1, self.scatterPlot2]];

    CPTGraph *graph = self.hostView.hostedGraph;
    graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    [self setTitleDefaultsForGraph:graph withBounds:bounds];
    [self setPaddingDefaultsForGraph:graph withBounds:bounds];
    graph.plotAreaFrame.paddingTop = 20.0;
    graph.plotAreaFrame.paddingBottom = 50.0;
    graph.plotAreaFrame.paddingLeft = 50.0;
    graph.plotAreaFrame.paddingRight = 50.0;
    graph.plotAreaFrame.cornerRadius = 10.0;
    graph.plotAreaFrame.masksToBorder = NO;
    graph.plotAreaFrame.axisSet.borderLineStyle = [CPTLineStyle lineStyle];

    // Setup plot space
    self.plotSpace1.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(10.0)];
    self.plotSpace1.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(10.0)];


    self.plotSpace2 = [[CPTXYPlotSpace alloc] init];
    self.plotSpace2.xRange = self.plotSpace1.xRange;
    self.plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(10.0)];

    [graph addPlotSpace:self.plotSpace2];


}

-(void)setPaddingDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds
{
//    CGFloat boundsPadding = round( bounds.size.width / CPTFloat(20.0) ); // Ensure that padding falls on an integral pixel
//    
//    graph.paddingLeft = boundsPadding;
//    
//    if ( graph.titleDisplacement.y > 0.0 ) {
//        graph.paddingTop = graph.titleTextStyle.fontSize * 2.0;
//    }
//    else {
//        graph.paddingTop = boundsPadding;
//    }
//    
//    graph.paddingRight  = boundsPadding;
//    graph.paddingBottom = boundsPadding;
    graph.paddingTop = 0.0f;
    graph.paddingBottom = 0.0f;
    graph.paddingLeft = 0.0f;
    graph.paddingRight = 0.0f;
}

-(void)setTitleDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds
{
    graph.title = self.title;
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor grayColor];
    textStyle.fontName = @"Helvetica-Bold";
    textStyle.fontSize = round( bounds.size.height / CPTFloat(20.0) );
    graph.titleTextStyle = textStyle;
    graph.titleDisplacement = CPTPointMake( 0.0, textStyle.fontSize * CPTFloat(1.5) );
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
}

-(void)configureAxis
{
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

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

    // Text styles
    CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle];
    axisTitleTextStyle.fontName = @"Helvetica-Bold";
    axisTitleTextStyle.fontSize = 14.0;

    CPTXYAxis *x = axisSet.xAxis;
    x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);//Where the Y Axis meets the X axis
    x.majorIntervalLength = CPTDecimalFromDouble(1.5);//Interval for X Axis
    x.minorTicksPerInterval = 4;
    x.tickDirection = CPTSignNone;
    x.axisLineStyle = axisLineStyle;
    x.majorTickLength = 12.0;
    x.majorTickLineStyle = axisLineStyle;
    x.minorTickLength = 8.0;
    x.title = @"X Axis";
    x.titleTextStyle = axisTitleTextStyle;
    x.titleOffset = 25.0;
    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];


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

    CPTXYAxis *y = axisSet.yAxis;
    y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    y.minorTicksPerInterval = 9;
    y.tickDirection = CPTSignNegative;
    y.axisLineStyle = axisLineStyle;
    y.majorTickLength = 6.0;
    y.majorTickLineStyle = axisLineStyle;
    y.minorTickLength = 4.0;

    y.title = @"Y Axis";
    y.titleTextStyle = axisTitleTextStyle;
    y.titleOffset = 30.0;


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

    CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
    y2.coordinate = CPTCoordinateY;
    y2.plotSpace = self.plotSpace2;
    y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(10.0);//Where the Y Axis meets the X axis
    y2.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
    y2.preferredNumberOfMajorTicks = 6;
    y2.minorTicksPerInterval = 9;
    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 = 30.0;

    // Add the y2 axis to the axis set
    graph.axisSet.axes = @[x, y, y2];
}

#pragma mark CPTPlotDataSource

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{

    if ( [(NSString *)plot.identifier isEqualToString:kPlot1] ) {
        return 10;
    }
    else if ( [(NSString *)plot.identifier isEqualToString:kPlot2] ) {
        return 5;
    } else {
        return 0;
    }
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{

    switch (fieldEnum) {


        case CPTScatterPlotFieldX:
            if ([plot.identifier isEqual:kPlot1]) {
                return [NSNumber numberWithUnsignedInt:idx];
                break;
            }else if ([plot.identifier isEqual:kPlot2]){
                return [NSNumber numberWithUnsignedInt:idx];
                break;
            }

        case CPTScatterPlotFieldY:
            if ([plot.identifier isEqual:kPlot1]) {
                return @(idx *2);
            } else if ([plot.identifier isEqual:kPlot2]) {
                return @(idx +2);
            }

    }
    return 0;
}
4

1 回答 1

2

plotSpace2缩放后,您正在(重新)初始化。[self.plotSpace2 scaleToFitPlots:...]设置绘图空间后将调用移至。

于 2013-09-19T00:00:35.730 回答