【问题标题】:How to set Y axis range when using multiple graphs in Coreplot在Coreplot中使用多个图形时如何设置Y轴范围
【发布时间】:2012-02-08 16:15:38
【问题描述】:

我正在使用 coreplot 在一个视图中绘制两个图表。当用户选择接收不同类型的数据时,正在绘制的数据会发生变化。

我收到的数据被正确绘制。但是,我想在需要时更改 Y 轴之一的范围。 (数据可能从 0 到 40 或从 0.0 到 1.0)。

如何实现此功能,以便当用户在 y 轴上绘制从 0.0 到 1.0 的数据时,它的范围也会更改为这些值

这是我的代码片段:

- (void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme fromGraph:(int)graphNumber
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    CGRect bounds = layerHostingView.bounds;
layerHostingView.backgroundColor = [UIColor clearColor];

    // Create the graph and assign the hosting view.
    graph = [[CPTXYGraph alloc] initWithFrame:bounds];
    layerHostingView.hostedGraph = graph;

    [graph applyTheme:theme];
    graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];

    graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
    graph.plotAreaFrame.borderLineStyle = nil; 

    graph.plotAreaFrame.masksToBorder = NO;

    // chang the chart layer orders so the axis line is on top of the bar in the chart.
    NSArray *chartLayers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:CPTGraphLayerTypePlots],
                                                            [NSNumber numberWithInt:CPTGraphLayerTypeMajorGridLines], 
                                                            [NSNumber numberWithInt:CPTGraphLayerTypeMinorGridLines],  
                                                            [NSNumber numberWithInt:CPTGraphLayerTypeAxisLines], 
                                                            [NSNumber numberWithInt:CPTGraphLayerTypeAxisLabels], 
                                                            [NSNumber numberWithInt:CPTGraphLayerTypeAxisTitles], 
                                                            nil];
graph.topDownLayerOrder = chartLayers;    
    [chartLayers release];

// Add plot space for horizontal bar charts
    graph.paddingLeft = 45.0;
    graph.paddingTop = 40.0;
    graph.paddingRight = 45.0;
    graph.paddingBottom = 60.0;

 // Setup plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate = self;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(28.0f)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(45)];

    CPTScatterPlot *highPlot2 = [[[CPTScatterPlot alloc] init] autorelease];
    highPlot2.identifier = @"Graph2";

CPTMutableLineStyle *highLineStyle2 = [[highPlot2.dataLineStyle mutableCopy] autorelease];
    highLineStyle2.lineWidth = 1.f;
    highLineStyle2.lineColor = [CPTColor colorWithComponentRed:0.00f green:0.00f blue:0.00f alpha:0.0f];
    highPlot2.dataLineStyle = highLineStyle2;
    highPlot2.dataSource = self;

CPTFill *areaFill2 = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.00f green:0.92f blue:0.0f alpha:0.58f]];
    highPlot2.areaFill = areaFill2;
    highPlot2.shadow = Shadow;
    highPlot2.areaBaseValue = CPTDecimalFromString(@"0");
    [graph addPlot:highPlot2];

// Setup grid line style
    CPTMutableLineStyle *majorXGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorXGridLineStyle.lineWidth = 1.0f;
    majorXGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.60f];
CPTMutableTextStyle *TextStyleBlack = [CPTMutableTextStyle textStyle];
    TextStyleBlack.color     = [CPTColor blackColor];
    TextStyleBlack.fontSize  = 15.0;

// Setup x-Axis.
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.majorGridLineStyle = majorXGridLineStyle;
    x.majorIntervalLength = CPTDecimalFromString(@"1");
    x.minorTicksPerInterval = 1;

    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    x.title = @"Days";
    x.timeOffset = 30.0f;
    NSArray *exclusionRanges = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(0)], nil];
    x.labelExclusionRanges = exclusionRanges;

    NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:[sampleDays count]];

    int idx = 0;
    for (NSString *day in sampleDays)
    {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:day textStyle:x.labelTextStyle];
        label.tickLocation = CPTDecimalFromInt(idx);
        label.offset = 5.0f;
        [labels addObject:label];
        [label release];
        idx++;
    }
    x.axisLabels = [NSSet setWithArray:labels];
    [labels release];

// Setup y-Axis.
    CPTMutableLineStyle *majorYGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorYGridLineStyle.lineWidth = 1.0f;
majorYGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.60];
CPTXYAxis *y1 = axisSet.yAxis;
    y1.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"10"]decimalValue];
    y1.labelShadow = Shadow;
    y1.minorTicksPerInterval = 0;
    y1.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    y1.labelTextStyle = TextStyleYellow;
    y1.labelOffset = 15;
    y1.axisLineStyle = nil;
NSArray *yExlusionRanges = [NSArray arrayWithObjects:
                                [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(0)],
                                nil];
    y1.labelExclusionRanges = yExlusionRanges;

//setup second y axis
CPTXYAxis *y2 = [[(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero] autorelease];
y2.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    y2.orthogonalCoordinateDecimal = CPTDecimalFromString(@"27");
    y2.minorTicksPerInterval = 0;
    y2.preferredNumberOfMajorTicks = 4;
    y2.majorGridLineStyle = majorYGridLineStyle;
 y2.labelOffset = - 40.0;    
    y2.coordinate = CPTCoordinateY;
    y2.plotSpace = graph.defaultPlotSpace;
    y2.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"10"]decimalValue];
y2.minorTickLineStyle = nil;
y2.axisLineStyle = nil;

//add all axis to the graph
graph.axisSet.axes = [NSArray arrayWithObjects:x, y1, y2,nil];

// Create a high plot area
    CPTScatterPlot *highPlot = [[[CPTScatterPlot alloc] init] autorelease];
    highPlot.identifier = kHighPlot;

    CPTMutableLineStyle *highLineStyle = [[highPlot.dataLineStyle mutableCopy] autorelease];
    highLineStyle.lineWidth = 1.f;
    highLineStyle.lineColor = [CPTColor colorWithComponentRed:0.00f green:0.00f blue:0.00f alpha:0.0f];
    highPlot.dataLineStyle = highLineStyle;
    highPlot.dataSource = self;

    //fillcolor
    CPTFill *areaFill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.00f green:0.97f blue:0.0f alpha:0.41f]];
    highPlot.areaFill = areaFill;
    highPlot.shadow = Shadow;
    highPlot.areaBaseValue = CPTDecimalFromString(@"0");
    [graph addPlot:highPlot];

// Create the Savings Marker Plot
    selectedCoordination = 2;

    touchPlot = [[[CPTScatterPlot alloc] initWithFrame:CGRectNull] autorelease];
    touchPlot.identifier = kLinePlot;

    touchPlot.dataSource = self;
    touchPlot.delegate = self;

    [self applyTouchPlotColor];

    [graph addPlot:touchPlot];

    [pool drain];

}

编辑:

解决方案是: A. 在初始化绘图空间之前删除 graph.defaultPlotSpace。 B. 添加两个绘图空间

[graph addPlotSpace:plotSpace2];
[graph addPlotSpace:plotSpace];

C.将两个地块分配给它自己的地块空间

[graph addPlot:highPlot toPlotSpace:plotSpace];
[graph addPlot:highPlot2 toPlotSpace:plotSpace2];

【问题讨论】:

    标签: iphone graph range core-plot


    【解决方案1】:

    绘图范围是绘图空间的一部分。只要您想更改显示范围,只需更新yRange。您可以随时执行此操作,而不仅仅是在设置图表时。

    【讨论】:

    • 当我更改 yRange 或 xRange 时,我的两个 y 轴都会更改为这些参数。即使我用 CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 声明其中两个创建 plotspace1 和 CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; // 创建绘图空间二并将它们分配给特定轴 y1.plotSpace = plotSpace;和 y2.plotSpace = plotSpace2;还有其他选择吗?
    • defaultPlotSpace 将始终返回相同的绘图空间——它不会创建新的绘图空间。您可以分配/初始化一个新的并使用[graph addPlotSpace:plotSpace2]; 将其添加到图表中。
    • 当我使用 (CPTXYPlotSpace *)graph.defaultPlotSpace; 时轴反应良好并将其分配给 y 轴。然而情节本身并没有改变。当我分配并初始化 CPTXYPlotspace 并将其分配给轴并绘制所有标签时,标签位于左下角并且不显示任何绘图。想我快到了。有什么建议吗?
    • @BarryK88 我现在面临同样的问题。你有想过这个吗?
    • 根据标签在y轴上的最大宽度需要设置轴的padding。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2019-09-06
    相关资源
    最近更新 更多