我遇到了CorePlot的一些问题。我尝试将多个scatterPlot绘制成一个图形。这可以按预期工作,但是当我开始滚动或缩放图表时,整个应用程序会增加内存使用量,最高可达900MB崩溃。我想我必须做一些释放物品,但我不知道怎么做。基本上我用不同的绘图标识符绘制每一行,并将相关数据放入数据源。
这是我得到的:(在这个示例代码中,我只是为了测试目的而使用静态值减少了ax范围。)
- (void)setupGraph {
// Create graph from theme
self.graph = [[CPTXYGraph alloc] initWithFrame:self.scatterPlotView.bounds];
self.graph.plotAreaFrame.masksToBorder = NO;
self.scatterPlotView.hostedGraph = self.graph;
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainBlackTheme];
[self.graph applyTheme:theme];
self.graph.paddingLeft = 0.0;
self.graph.paddingTop = 0.0;
self.graph.paddingRight = 0.0;
self.graph.paddingBottom = 0.0;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(30)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(15)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(15)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
CPTXYAxis *y = axisSet.yAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
}
此方法加载我的数据并绘制散点图
- (void)loadSignals {
//Data loading logic goes here nothing special just one array for each plot
for (Signal *sig in [signals allValues]) {
[self constructScatterPlot:sig.name];
}
}];
这是绘图发生的地方:
- (void)constructScatterPlot:(NSString *)identifier {
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.identifier = identifier;
lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.miterLimit = 1.0;
lineStyle.lineWidth = 1.0;
lineStyle.lineColor = [CPTColor redColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.dataSource = self;
boundLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;
boundLinePlot.interpolation = CPTScatterPlotInterpolationStepped;
[self.graph addPlot:boundLinePlot];
}
......这就是数据源获取它的价值所在的地方:
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSString *plotIdent = (NSString *) plot.identifier;
if (![plotIdent isEqualToString:self.currentIdent]) {
self.countPlot++;
self.currentIdent = plotIdent;
}
Signal *newSig = [self.signals objectForKey:plotIdent];
Value * newValue = [newSig valueAtTime:index];
NSNumber *number = [NSNumber numberWithInteger:[newValue.value integerValue]];
if ( fieldEnum == CPTScatterPlotFieldY ) {
return number;
}
if ( fieldEnum == CPTScatterPlotFieldX ) {
return [NSNumber numberWithInteger:index];
}
return nil;
}
首先,我想知道这是否是将多个绘图(在我的情况下最多为40)绘制到一个图形中的正确方法?如果是这样,我该怎么做才能优化我的情节表现?
输出应如下所示:
答案 0 :(得分:2)
每个绘图都是CALayer
,覆盖了图形的整个绘图区域,因此其中40个可能会占用大部分可用的视频内存。如果它们都使用相同的线条样式,您可以在同一个绘图上绘制所有线条并以这种方式保存一些内存。在线段之间插入一个额外的数据点,并从数据源返回nil
或@(NAN)
以打破该行。
答案 1 :(得分:2)
我发现了这个用途
“尝试在主机视图上将collapsesLayers设置为YES”
Application get stuck when drawing nearly 40 Scatter Plots using Core Plot in IPad