CorePLot中有多个绘图空间的难度

时间:2013-10-28 19:30:45

标签: core-plot

我有几个图表要相互绘制,我需要它们共享一个x轴的时间,但是有不同的Y轴...为此,我知道我必须创建一个新的绘图空间第2到第n个图形,并将这些绘图空间添加到图形中:

以下是我的“向图表中添加新图表”功能

的片段
BOOL isNew=FALSE;
CPTXYPlotSpace *defaultPlotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
CPTXYPlotSpace *plotSpace=nil;
if([g_Data count]==1){
    plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    NSLog(@"Reuse existing plot space");
}
else
{
    NSLog(@"New plot space");
    plotSpace = [[CPTXYPlotSpace alloc] init];
    isNew=TRUE;
}

[dataDictionary setObject:plotSpace forKey:@"plotspace"];



float xSize=[mainGraphView bounds].size.width;
float ySize=[mainGraphView bounds].size.height;

float XRatio=(xSize-50.0)/xSize;
float YRatio=(ySize-50.0)/ySize;

NSDecimalNumber *plotWidth=[x_max decimalNumberBySubtracting:x_min];
plotWidth=[plotWidth decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%0.2f",XRatio]]];
NSDecimalNumber *plotX=[x_min decimalNumberBySubtracting:[plotWidth decimalNumberBySubtracting:[x_max decimalNumberBySubtracting:x_min]]];

NSDecimalNumber *plotHeight=[y_max decimalNumberBySubtracting:y_min];
plotHeight=[plotHeight decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%0.2f",YRatio]]];
NSDecimalNumber *plotY=[y_min decimalNumberBySubtracting:[plotHeight decimalNumberBySubtracting:[y_max decimalNumberBySubtracting:y_min]]];




NSLog(@"%@: XRange: Original: %@ x %0.2f becomes %@",identifier,[x_max decimalNumberBySubtracting:x_min],XRatio,plotWidth);
NSLog(@"%@: YRange: %@-%@ is %@ x %0.2f becomes %@",identifier,y_min,y_max,[y_max decimalNumberBySubtracting:y_min],YRatio,plotHeight);

if(isNew){
    plotSpace.xRange = defaultPlotSpace.xRange;
}
else
{
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:[plotX decimalValue] length:[plotWidth decimalValue]];
}
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:[plotY decimalValue]
                                                length:[plotHeight decimalValue]];
NSLog(@"**********YPLOT IS FROM %@ len %@",plotY,plotHeight );



plotSpace.allowsUserInteraction=TRUE;
if(isNew)[graph addPlotSpace:plotSpace];



CPTScatterPlot *newPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];
newPlot.identifier     = identifier;
newPlot.dataSource =       self;



CPTMutableLineStyle *lineStyle = [newPlot.dataLineStyle mutableCopy];
lineStyle.lineWidth              = 2.0;
lineStyle.lineColor              = graphColor;
newPlot.dataLineStyle = lineStyle;
[g_Plots addObject:newPlot];

[graph addPlot:newPlot toPlotSpace:plotSpace];



NSLog(@"Adding plot to graph system");
[graph addPlot:newPlot];




and my log:



     2013-10-28 15:24:35.166 BodyMetrics[2234:303] -------[NEW PLOT FOR Pulse (bpm)]-----------------------------------------------------------
    2013-10-28 15:24:35.169 BodyMetrics[2234:303] 85 rows for data: BM_Measure measuredate measuretype = 'Heart Pulse'
    2013-10-28 15:24:35.173 BodyMetrics[2234:303] The size of the arrays for x and y are: 85;85
    2013-10-28 15:24:35.173 BodyMetrics[2234:303] XMIN: 1380566085  XMAX: 1382980783
    2013-10-28 15:24:35.173 BodyMetrics[2234:303] YMIN: 43  YMAX: 154
    2013-10-28 15:24:35.173 BodyMetrics[2234:303] Reuse existing plot space
    2013-10-28 15:24:35.174 BodyMetrics[2234:303] Pulse (bpm): XRange: Original: 2414698 x 0.95 becomes 2541787.36842105263157894736842105263157
    2013-10-28 15:24:35.174 BodyMetrics[2234:303] Pulse (bpm): YRange: 43-154 is 111 x 0.90 becomes 123.333333333333333333333333333333333333
    2013-10-28 15:24:35.174 BodyMetrics[2234:303] **********YPLOT IS FROM 30.666666666666666666666666666666666667 len 123.333333333333333333333333333333333333
    2013-10-28 15:24:35.175 BodyMetrics[2234:303] Adding plot to graph system
    2013-10-28 15:24:35.176 BodyMetrics[2234:303] Added... 1
    2013-10-28 15:24:35.176 BodyMetrics[2234:303] Configure axes
    2013-10-28 15:24:35.177 BodyMetrics[2234:303] Now addign Y axes...
    2013-10-28 15:24:35.177 BodyMetrics[2234:303] Creating axis for Pulse (bpm)
    2013-10-28 15:24:35.177 BodyMetrics[2234:303] Reusing the existing Y Axis...
    2013-10-28 15:24:35.178 BodyMetrics[2234:303] This axes is from 43 to 154 being a range of 111 so the major ticks will bat 10.3 floats away, for an interval length of 10.7766990291262135922330097087378640776
    2013-10-28 15:24:35.178 BodyMetrics[2234:303] About to add the following axes: 2 (
        "<<<CPTXYAxis: 0x10040ca80> bounds: {{0, 0}, {959, 515}}> with range: <<CPTPlotRange: 0x60800027cc40> {1380438995.6315789473684210526315789474, 2541787.36842105263157894736842105263157}> viewCoordinates: {0, 50} to {959, 50}>",
        "<<<CPTXYAxis: 0x10040d8f0> bounds: {{0, 0}, {959, 515}}> with range: <<CPTPlotRange: 0x60800027cd00> {30.666666666666666666666666666666666667, 123.333333333333333333333333333333333333}> viewCoordinates: {50, 0} to {50, 515}>"
    ) 
    2013-10-28 15:24:35.179 BodyMetrics[2234:303] Added
    2013-10-28 15:24:35.179 BodyMetrics[2234:303] -------[NEW PLOT FOR Weight (Kg)]-----------------------------------------------------------
    2013-10-28 15:24:35.181 BodyMetrics[2234:303] 44 rows for data: BM_Measure measuredate measuretype = 'Weight'
    2013-10-28 15:24:35.183 BodyMetrics[2234:303] The size of the arrays for x and y are: 44;44
    2013-10-28 15:24:35.183 BodyMetrics[2234:303] XMIN: 1380330622  XMAX: 1382957809
    2013-10-28 15:24:35.183 BodyMetrics[2234:303] YMIN: 83.225  YMAX: 85.701
    2013-10-28 15:24:35.183 BodyMetrics[2234:303] New plot space
    2013-10-28 15:24:35.183 BodyMetrics[2234:303] Weight (Kg): XRange: Original: 2627187 x 0.95 becomes 2765460
    2013-10-28 15:24:35.184 BodyMetrics[2234:303] Weight (Kg): YRange: 83.225-85.701 is 2.476 x 0.90 becomes 2.75111111111111111111111111111111111111
    2013-10-28 15:24:35.184 BodyMetrics[2234:303] **********YPLOT IS FROM 82.9498888888888888888888888888889 len 2.75111111111111111111111111111111111111
    2013-10-28 15:24:35.184 BodyMetrics[2234:303] Adding plot to graph system
    2013-10-28 15:24:35.184 BodyMetrics[2234:303] Added... 2
    2013-10-28 15:24:35.185 BodyMetrics[2234:303] Configure axes
    2013-10-28 15:24:35.185 BodyMetrics[2234:303] Now addign Y axes...
    2013-10-28 15:24:35.185 BodyMetrics[2234:303] Creating axis for Pulse (bpm)
    2013-10-28 15:24:35.185 BodyMetrics[2234:303] Reusing the existing Y Axis...
    2013-10-28 15:24:35.185 BodyMetrics[2234:303] This axes is from 43 to 154 being a range of 111 so the major ticks will bat 10.3 floats away, for an interval length of 10.7766990291262135922330097087378640776
    2013-10-28 15:24:35.186 BodyMetrics[2234:303] Creating axis for Weight (Kg)
    2013-10-28 15:24:35.186 BodyMetrics[2234:303] Adding a new Y axis...
    2013-10-28 15:24:35.186 BodyMetrics[2234:303] This axes is from 83.225 to 85.701 being a range of 2.476 so the major ticks will bat 10.3 floats away, for an interval length of 0.240388349514563106796116504854368932038
    2013-10-28 15:24:35.187 BodyMetrics[2234:303] About to add the following axes: 2 (
        "<<<CPTXYAxis: 0x10040ca80> bounds: {{0, 0}, {959, 515}}> with range: <<CPTPlotRange: 0x60800027cc40> {1380438995.6315789473684210526315789474, 2541787.36842105263157894736842105263157}> viewCoordinates: {0, 50} to {959, 50}>",
        "<<<CPTXYAxis: 0x10040d8f0> bounds: {{0, 0}, {959, 515}}> with range: <<CPTPlotRange: 0x60800027cd00> {30.666666666666666666666666666666666667, 123.333333333333333333333333333333333333}> viewCoordinates: {50, 0} to {50, 515}>"
    ) 
    2013-10-28 15:24:35.187 BodyMetrics[2234:303] Added

但是我的图表无法正确缩放:

enter image description here

1 个答案:

答案 0 :(得分:1)

您要将图表添加到图表两次,首先使用[graph addPlot:newPlot toPlotSpace:plotSpace];,然后再使用[graph addPlot:newPlot];。第二个调用将绘图移回到为第一个绘图配置的默认绘图空间。

删除对-addPlot:的多余调用。