2013-11-27 3 views
0

두 개의 다른 플롯 공간 (그래프에서)에 두 개의 플롯 (그림 및 막대 플롯)을 그리려고합니다. 두 플롯 모두 데이터 소스와 동일한 x 축을 공유합니다.다른 플롯의 y 범위를 사용하는 두 번째 플롯 - 코어 플롯

지금까지 두 가지 모두 성공적으로 플로팅 할 수있었습니다. 그러나 막대 그래프에서는 막대를 높이 교정 할 수 없습니다. 즉 모두 사용 가능한 최대 높이로 플롯됩니다. 나는 매우 어리석은 뭔가가 빠져 있다는 것을 안다. 그러나 나는 그것을 알아낼 수 없다. 이미 많은 답변을 시도했지만 문제를 해결할 수 없었습니다.

코드 :

-(void) setMajorIntervalForXAxis 
{ 
    if(totIntervalShown <= 3600 * 2) 
     ((CPTXYAxisSet *)graph.axisSet).xAxis.majorIntervalLength = CPTDecimalFromFloat(1800); 
    else if(totIntervalShown <= 3600 * 4) 
     ((CPTXYAxisSet *)graph.axisSet).xAxis.majorIntervalLength = CPTDecimalFromFloat(3600); 
    else 
     ((CPTXYAxisSet *)graph.axisSet).xAxis.majorIntervalLength = CPTDecimalFromFloat(7200); 

} 

-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated 
{ 
    // Setup scatter plot space 
    plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.allowsUserInteraction = NO; 

    barPlotSpace = [[CPTXYPlotSpace alloc]init]; 
    barPlotSpace.allowsUserInteraction = NO; 

    if(plotData.count > 0) 
    { 
     NSDate *finaldate = [formatter dateFromString:[[plotData objectAtIndex:plotData.count - 1] objectAtIndex:0]]; 
     totIntervalShown = [finaldate timeIntervalSinceDate:refDate]; 
    } 
    else 
    { 
     totIntervalShown = 0; 
    } 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(/*3600 * 7.5f*/totIntervalShown)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(MinPrice - margin) length:CPTDecimalFromFloat(MaxPrice - MinPrice + margin)]; 
    barPlotSpace.xRange = plotSpace.xRange; 
    barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(maxVolume + (maxVolume/5))]; 
    [graph addPlotSpace:barPlotSpace]; 

    // Axes 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
    CPTXYAxis *x   = axisSet.xAxis; 
    // x.majorIntervalLength   = CPTDecimalFromFloat(7200); 
    [self setMajorIntervalForXAxis]; 

    x.orthogonalCoordinateDecimal = CPTDecimalFromInt(margin + MinPrice); 
    x.majorGridLineStyle   = majorGridLineStyle; 
    x.minorGridLineStyle = minorGridLineStyle; 
    x.axisLineStyle = nil; 
    x.labelingPolicy = CPTAxisLabelingPolicyFixedInterval; 
    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0]; 

    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:axisFormatter]; 
    timeFormatter.referenceDate = refDate; 
    x.labelFormatter = timeFormatter; 

    CPTXYAxis *y = axisSet.yAxis; 
    y.labelingPolicy    = CPTAxisLabelingPolicyAutomatic; 
    y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(7200); 
    y.majorGridLineStyle   = majorGridLineStyle; 
    y.minorGridLineStyle = minorGridLineStyle; 
    y.axisLineStyle = nil; 
    // y.preferredNumberOfMajorTicks = 6; 
    y.axisConstraints = [CPTConstraints constraintWithLowerOffset:5]; 

    CPTXYAxis *rightY = [[CPTXYAxis alloc]init]; 
    rightY.labelingPolicy = CPTAxisLabelingPolicyAutomatic; 
    rightY.orthogonalCoordinateDecimal = CPTDecimalFromFloat(totIntervalShown); 
    rightY.majorGridLineStyle = majorGridLineStyle; 
    rightY.axisConstraints = [CPTConstraints constraintWithUpperOffset:20]; 
    rightY.coordinate = CPTCoordinateY; 
    rightY.axisLineStyle = nil; 
    rightY.majorIntervalLength = CPTDecimalFromLongLong(maxVolume/5); 
    rightY.plotSpace = barPlotSpace; 

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

    NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init]; 
    [numFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; 
    rightY.labelFormatter = numFormatter; 

    // Create a plot that uses the data source method 
    linePlot = [[CPTScatterPlot alloc] init]; 
    linePlot.identifier = @"Line Plot"; 

    CPTMutableLineStyle *lineStyle = [linePlot.dataLineStyle mutableCopy]; 
    lineStyle.lineWidth    = 1.0; 
    lineStyle.lineColor    = [CPTColor colorWithComponentRed:236/255.0 green:124/255.0 blue:41/255.0 alpha:1.0]; 
    linePlot.dataLineStyle = lineStyle; 
    linePlot.dataSource = self; 
    [graph addPlot:linePlot]; 

    // Create a bar line style 
    CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init]; 
    barLineStyle.lineWidth = 1.0; 
    barLineStyle.lineColor = [CPTColor colorWithComponentRed:1.0 green:0 blue:0 alpha:0.4]; 

    barPlot = [[CPTBarPlot alloc] init]; 
    barPlot.identifier = @"Bar Plot"; 
    barPlot.plotSpace = barPlotSpace; 
    barPlot.barBasesVary = NO; 
    barPlot.barsAreHorizontal = NO; 
    barPlot.barWidth = CPTDecimalFromCGFloat(0.5); 
    barPlot.dataSource = self; 
    barPlot.lineStyle = barLineStyle; 
    barPlot.fill = [CPTFill fillWithColor:[CPTColor redColor]]; 
    [graph addPlot:barPlot]; 
    //graph.axisSet.axes = [NSArray arrayWithObjects:x, y, rightY, nil]; 

    CPTMutableTextStyle *textstyle = [CPTMutableTextStyle textStyle]; 
    textstyle.fontName = @"Helvetica"; 
    textstyle.fontSize = 10.0; 
    textstyle.color = [CPTColor whiteColor]; 
    x.labelTextStyle = textstyle; 
    y.labelTextStyle = textstyle; 
    rightY.labelTextStyle = textstyle; 

} 

데이터 소스 :

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
    return plotData.count; 
} 

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
    NSNumber *num; 

    if([(NSString *)plot.identifier isEqualToString:@"Line Plot"]) 
    { 
     if(fieldEnum == CPTScatterPlotFieldY) 
     { 
      num = [NSNumber numberWithFloat:[[[plotData objectAtIndex:index] objectAtIndex:1] floatValue]]; 
     } 
     else 
     { 
      NSString *dateStrExtracted = [[plotData objectAtIndex:index] objectAtIndex:0]; 
      NSDate *dateExt = [formatter dateFromString:dateStrExtracted]; 
      float interval = [dateExt timeIntervalSinceDate:refDate]; 
      num = [NSNumber numberWithFloat:interval]; 
     } 
    } 
    else 
    { 
     if(fieldEnum == CPTBarPlotFieldBarLocation) 
     { 
      //num = [NSNumber numberWithFloat:[[[plotData objectAtIndex:index] objectAtIndex:1] floatValue]]; 
      NSString *dateStrExtracted = [[plotData objectAtIndex:index] objectAtIndex:0]; 
      NSDate *dateExt = [formatter dateFromString:dateStrExtracted]; 
      float interval = [dateExt timeIntervalSinceDate:refDate]; 
      num = [NSNumber numberWithFloat:interval]; 
     } 
     else if(fieldEnum == CPTBarPlotFieldBarTip) 
     { 
      num = [NSNumber numberWithLongLong:[[[plotData objectAtIndex:index] objectAtIndex:2] longLongValue]]; 

     } 
    } 

    return num; 
} 

값은 막대 플롯 팁에 대해 반환은 500과 같은, 10004005000 등하지만 모두가 같은 높이로 그려집니다. 나는 바 플롯이 산포도를위한 범위를 고려하고 있다고 생각한다. 아니면 뭔가가있을 수 있습니까? 내가 어디에서 잘못 될 수 있는지 안내해 주시겠습니까? 감사.

enter image description here

답변

1

두 번째 플롯 (막대 그래프는) 기본 plotspace의 yRange를 사용했다. 나는 변경 :

[graph addPlot:barPlot]; 

에 :

[graph addPlot:barPlot toPlotSpace:barPlotSpace]; 

이 내 문제를 해결했다. 그리고 yRange가 올바르게 사용되고 있습니다.