无法同时显示两个CorePlot图 [英] Unable to have two CorePlot graphs show simultaneously
问题描述
我在同一个视图中出现两个不同的CorePlot图形时遇到问题。我在视图中设置了两个托管视图,并且所有参考插座均已正确连接。只有其中一张图可以正确显示。将快速NSLog添加到 numberForPlot
方法可显示仅填充了第一个图形(图形
和 graphView
),而不是第二个(趋势
和 trendView
)。代码如下:
I'm having an issue having two different CorePlot graphs show up in the same view. I've set up two hosting views in my view and all the referencing outlets are connected correctly. Only one of the graphs shows up correctly. Adding a quick NSLog to the numberForPlot
method shows that only the first graph is being populated (graph
and graphView
) and not the second (trend
and trendView
). Here's the code:
- (void) graphSetup {
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
graph = (CPTXYGraph *)[theme newGraph];
graphView.hostedGraph = graph;
graphView.allowPinchScaling = YES;
graph.paddingLeft = -5;
graph.paddingTop = 0;
graph.paddingRight = -5;
graph.paddingBottom = -5;
graph.fill = nil;
graph.plotAreaFrame.fill = nil;
int a = 0;
for (int i = 0; i < [romAveragesArray count]; i++) {
if ([romAveragesArray objectAtIndex:i] != NULL)
a++;
}
int localMax = 0;
int localMin = 0;
for (int a = 0; a < [romAveragesArray count]; a++) {
if ([[romAveragesArray objectAtIndex:a] getValue] > localMax)
localMax = [[romAveragesArray objectAtIndex:a] getValue];
else if ([[romAveragesArray objectAtIndex:a] getValue] < localMin)
localMin = [[romAveragesArray objectAtIndex:a] getValue];
}
for (int a = 0; a < [obaAveragesArray count]; a++) {
if ([[obaAveragesArray objectAtIndex:a] getValue] > localMax)
localMax = [[obaAveragesArray objectAtIndex:a] getValue];
else if ([[obaAveragesArray objectAtIndex:a] getValue] < localMin)
localMin = [[obaAveragesArray objectAtIndex:a] getValue];
}
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)
length:CPTDecimalFromInt(145)];
NSLog(@"%d",a);
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)
length:CPTDecimalFromInt(localMax+15)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromFloat(10);
x.minorTicksPerInterval = 2;
x.borderWidth = 0;
x.labelExclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100)
length:CPTDecimalFromFloat(300)],
nil];;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromFloat(10);
y.minorTicksPerInterval = 1;
y.labelExclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100)
length:CPTDecimalFromFloat(300)],
nil];
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.identifier = @"Obama";
dataSourceLinePlot.dataSource = self;
CPTMutableLineStyle *style = [CPTMutableLineStyle lineStyle];
style.lineColor = [CPTColor colorWithComponentRed:0.3215686274509804 green:0.6078431372549019 blue:1.0 alpha:1.0];
dataSourceLinePlot.dataLineStyle = style;
[graph addPlot:dataSourceLinePlot];
CPTScatterPlot *dataSourceLinePlot1 = [[CPTScatterPlot alloc] init];
dataSourceLinePlot1.identifier = @"Rom";
dataSourceLinePlot1.dataSource = self;
CPTMutableLineStyle *style1 = [CPTMutableLineStyle lineStyle];
style1.lineColor = [CPTColor colorWithComponentRed:0.9803921568627451 green:0.47058823529411764 blue:0.47058823529411764 alpha:1.0];
dataSourceLinePlot1.dataLineStyle = style1;
[graph addPlot:dataSourceLinePlot1];
[self trendSetup];
[self postStats];
}
- (void) trendSetup {
CPTTheme *theme1 = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
trend = (CPTXYGraph *) [theme1 newGraph];
trendView.allowPinchScaling = YES;
trendView.hostedGraph = trend;
trend.paddingLeft = -5;
trend.paddingRight = -5;
trend.paddingTop = 0;
trend.paddingBottom = -5;
trend.fill = nil;
trend.plotAreaFrame.fill = nil;
int obaMax, localMax;
for (int i = 0; i < [obaArray count]; i++)
obaMax += [[obaArray objectAtIndex:i] getValue];
int romMax;
for (int i = 0; i < [romArray count]; i++)
romMax += [[romArray objectAtIndex:i] getValue];
if (romMax > obaMax)
localMax = romMax;
else
localMax = obaMax;
CPTXYPlotSpace *trendSpace = (CPTXYPlotSpace *)trend.defaultPlotSpace;
trendSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(504)];
trendSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(localMax)];
CPTXYAxisSet *trendSet = (CPTXYAxisSet *)trend.axisSet;
CPTXYAxis *trendX = trendSet.xAxis;
trendX.majorIntervalLength = CPTDecimalFromInt(10);
trendX.minorTicksPerInterval = 2;
trendX.borderWidth = 0;
trendX.labelExclusionRanges = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100) length:CPTDecimalFromFloat(300)], nil];;
CPTXYAxis *trendY = trendSet.xAxis;
trendY.majorIntervalLength = CPTDecimalFromInt(10);
trendY.minorTicksPerInterval = 2;
trendY.borderWidth = 0;
trendY.labelExclusionRanges = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100) length:CPTDecimalFromFloat(300)], nil];;
CPTScatterPlot *trendSourceLinePlot = [[CPTScatterPlot alloc] init];
trendSourceLinePlot.identifier = @"Obama Trend";
trendSourceLinePlot.dataSource = self;
CPTMutableLineStyle *style3 = [CPTMutableLineStyle lineStyle];
style3.lineColor = [CPTColor colorWithComponentRed:0.3215686274509804 green:0.6078431372549019 blue:1.0 alpha:1.0];
trendSourceLinePlot.dataLineStyle = style3;
[trend addPlot:trendSourceLinePlot];
CPTScatterPlot *trendSourceLinePlot1 = [[CPTScatterPlot alloc] init];
trendSourceLinePlot1.identifier = @"Rom Trend";
trendSourceLinePlot1.dataSource = self;
CPTMutableLineStyle *style4 = [CPTMutableLineStyle lineStyle];
style4.lineColor = [CPTColor colorWithComponentRed:0.9803921568627451 green:0.47058823529411764 blue:0.47058823529411764 alpha:1.0];
trendSourceLinePlot1.dataLineStyle = style4;
[trend addPlot:trendSourceLinePlot1];
}
推荐答案
我发现您的代码不太正确,这就是Eric问您两个托管视图是否可见的原因。
Here is something I found not-so-right with your code, and I think this is the reason Eric asked you if both hosting views are visible.
您正在创建两个 CPTXYGraph
一个称为 graph
的另一个称为趋势
。我不知道您如何在Interface Builder中设置视图,但是我不会创建两个 CPTXYGraph
s。我会添加 trendSourceLinePlot
& trendSourceLinePlot1
到趋势
或图
。
You are creating two CPTXYGraph
s one called graph
another called trend
. I don't know how you set up the Views in Interface Builder, but I would NOT create two CPTXYGraph
s. I would add trendSourceLinePlot
& trendSourceLinePlot1
to either trend
or graph
.
我想,您需要做的是将它们显示在相同的 CPTXYGraph
中。因此,这是我的建议...
I think, what you need to do is to present them in the same CPTXYGraph
. So, here is my suggestion...
-
删除
trentSetup $ c中的以下行$ c>
CPTTheme *theme1 = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
trend = (CPTXYGraph *) [theme1 newGraph];
trendView.allowPinchScaling = YES;
trendView.hostedGraph = trend;
trend.paddingLeft = -5;
trend.paddingRight = -5;
trend.paddingTop = 0;
trend.paddingBottom = -5;
trend.fill = nil;
trend.plotAreaFrame.fill = nil;
替换仅以下行
[trend addPlot:trendSourceLinePlot];
[trend addPlot:trendSourceLinePlot1];
分别与
[graph addPlot:trendSourceLinePlot];
[graph addPlot:trendSourceLinePlot1];
这也应该加快加载过程。
It also should speed up the loading process.
这篇关于无法同时显示两个CorePlot图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!