本文介绍了使用Core-Plot iOS无法在3个不同的视图中绘制3个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用core-plot库在3个不同的视图中绘制图形。问题在3个图形中,只有第3个图形可以正确显示...但是,如果我分别绘制每个图形,则它们总是会正确显示。任何线索都会有很大帮助。谢谢

I am using core-plot library to plot a graph in 3 different view..Problem is among all of 3 only 3rd graph get displayed correctly...But if I draw each graph separately then they always shows up right..Any clue would be of great help .Thanks

  //In ViewWillAppear  

  -(void) viewWillAppear:(BOOL)animated
 {

   dataPoints     = [[NSMutableArray alloc]init];

   // Create graph from theme
  CPTgraph1 = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:CGRectZero];
  CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
  [CPTgraph1 applyTheme:theme];

  CPTXYPlotSpace *plotSpace1;
  plotSpace1= (CPTXYPlotSpace *)CPTgraph1.defaultPlotSpace;

  CPTScatterPlot *dataSourceLinePlot1 = [(CPTScatterPlot *)[CPTScatterPlot alloc]       initWithFrame:CPTgraph1.bounds];

  CPTgraph2 = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:CGRectZero];
  //CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
  [CPTgraph2 applyTheme:theme];

  CPTXYPlotSpace *plotSpace2;
  plotSpace2= (CPTXYPlotSpace *)CPTgraph2.defaultPlotSpace;

  CPTScatterPlot *dataSourceLinePlot2 = [(CPTScatterPlot *)[CPTScatterPlot alloc] initWithFrame:CPTgraph2.bounds];

  CPTgraph3 = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:CGRectZero];
  //CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
  [CPTgraph3 applyTheme:theme];

  CPTXYPlotSpace *plotSpace3;
  plotSpace3= (CPTXYPlotSpace *)CPTgraph3.defaultPlotSpace;

  CPTScatterPlot *dataSourceLinePlot3 = [(CPTScatterPlot *)[CPTScatterPlot alloc] initWithFrame:CPTgraph3.bounds];

  [self plotGraph:graph1 withData:"SomeData" withGraph:CPTgraph1 withPlotSpace:plotSpace1 withCPTScatterPlot:dataSourceLinePlot1];

  [self plotGraph:graph2 withData:"Some Data" withGraph:CPTgraph2 withPlotSpace:plotSpace2 withCPTScatterPlot:dataSourceLinePlot2];

  [self plotGraph:graph3 withData:"Some Data" withGraph:CPTgraph3 withPlotSpace:plotSpace3 withCPTScatterPlot:dataSourceLinePlot3];

}


-(void)plotGraph :(CPTGraphHostingView *)graphView withData:(NSMutableArray*)refData    withGraph:(CPTXYGraph *) graph withPlotSpace:(CPTXYPlotSpace *) plotSpace withCPTScatterPlot:(CPTScatterPlot *)dataSourceLinePlot


{

[self readFromData:refData];


graphView.hostedGraph = graph;

graph.paddingLeft   = 0.0;
graph.paddingTop    = 0.0;
graph.paddingRight  = 0.0;
graph.paddingBottom = 0.0;

graph.plotAreaFrame.paddingLeft   = 10.0;
graph.plotAreaFrame.paddingTop    = 10.0;
graph.plotAreaFrame.paddingRight  = 10.0;
graph.plotAreaFrame.paddingBottom = 10.0;

graph.plotAreaFrame.plotArea.fill = graph.plotAreaFrame.fill;
graph.plotAreaFrame.fill          = nil;

graph.plotAreaFrame.borderLineStyle = nil;
graph.plotAreaFrame.cornerRadius    = 0.0;


plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(minimumValueForXAxis)
                                                length:CPTDecimalFromDouble( maximumValueForXAxis)];

  plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(minimumValueForYAxis)
                                                length:CPTDecimalFromDouble(ceil(  (maximumValueForYAxis - minimumValueForYAxis) / majorIntervalLengthForY ) * majorIntervalLengthForY)];



CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];

CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];

CPTXYAxis *x = axisSet.xAxis;
x.minorTicksPerInterval = 9;
x.majorIntervalLength   = CPTDecimalFromDouble(majorIntervalLengthForX);
x.labelOffset           = 5.0;


x.axisConstraints       = [CPTConstraints constraintWithLowerOffset:0.0];

CPTXYAxis *y = axisSet.yAxis;
y.minorTicksPerInterval = 9;
y.majorIntervalLength   = CPTDecimalFromDouble(majorIntervalLengthForY);
y.labelOffset           = 5.0;
y.majorGridLineStyle    =  majorGridLineStyle;
y.minorGridLineStyle    =  minorGridLineStyle;

y.axisConstraints       = [CPTConstraints constraintWithLowerOffset:0.0];

dataSourceLinePlot.identifier = @"Data Source Plot";

CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth              = 1.0;
lineStyle.lineColor = [CPTColor orangeColor];
dataSourceLinePlot.dataLineStyle = lineStyle;

dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot toPlotSpace:plotSpace];


}

方法readFromData只会计算x, y值并将其存储在dataPoints数组中。

Method readFromData will just calculate the x , y values and store it in dataPoints array.

仅需详细说明一点...图形确实会在第一视图和第二视图中显示,但似乎超出范围,因为我只能看到它的一部分...但是第三个图形正确显示。.我想它与范围或轴有关。

Just elaborate a bit more...graph does shows up in first and second views but seems out of range cause i can only see part of it...but the third graph gets displayed correctly..i guess it has something to do with range or axis..

推荐答案

所有三个图都使用它们的绘图空间具有相同的绘图范围。您的绘图数据是否在该范围内?如果没有,则需要为每个图形计算不同的绘图范围。

All three plots use the same plot ranges for their plot spaces. Does your plot data fit in that range? If not, you'll need to calculate different plot ranges for each graph.

这篇关于使用Core-Plot iOS无法在3个不同的视图中绘制3个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:07