• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 创建 CorePlot 图形

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:48:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我发布了很多代码,但问题相对简单: 我正在尝试将 CTPXYGraph 创建为独立对象,然后将其用作我拥有的另一个 ViewController 中的 hostsGraph。

由于某种原因,它崩溃了! (让我使用 EXC_BAD_ACCESS 信号汇编代码)

View Controller :

RealTimePlot *plot = [[RealTimePlot alloc] init];
CTPXYGraph *graph = [plot createGraph];
self.graphHostingView.hostedGraph = graph;

图形对象:

#import "CorePlot-CocoaTouch.h"

@interface RealTimePlot : NSObject <CPTPlotDataSource>
{  
NSMutableArray *dataForPlot;
}

@property (readwrite, strong) NSMutableArray *dataForPlot;

- (CPTXYGraph *) createGraph;

@end

实现:

@implementation RealTimePlot
@synthesize dataForPlot;

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

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

// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange                = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];
plotSpace.yRange                = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(3.0)];

// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x          = axisSet.xAxis;
x.majorIntervalLength         = CPTDecimalFromString(@"0.5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
x.minorTicksPerInterval       = 2;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
                            [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
                            [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
                            [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(2.99) length:CPTDecimalFromFloat(0.02)],
                            nil];
x.labelExclusionRanges = exclusionRanges;

CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength         = CPTDecimalFromString(@"0.5");
y.minorTicksPerInterval       = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
exclusionRanges               = [NSArray arrayWithObjects:
                                 [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
                                 [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
                                 [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(3.99) length:CPTDecimalFromFloat(0.02)],
                                 nil];
y.labelExclusionRanges = exclusionRanges;

// Create a blue plot area
CPTScatterPlot *boundLinePlot  = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit        = 1.0f;
lineStyle.lineWidth         = 3.0f;
lineStyle.lineColor         = [CPTColor blueColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.identifier    = @"Blue Plot";
boundLinePlot.dataSource    = self;
[graph addPlot:boundLinePlot];

// Do a blue gradient
CPTColor *areaColor1       = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
areaGradient1.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
boundLinePlot.areaFill      = areaGradientFill;
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];

// Add plot symbols
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill          = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle     = symbolLineStyle;
plotSymbol.size          = CGSizeMake(10.0, 10.0);
boundLinePlot.plotSymbol = plotSymbol;

// Create a green plot area
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
lineStyle                        = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth              = 3.f;
lineStyle.lineColor              = [CPTColor greenColor];
lineStyle.dashPattern            = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.identifier    = @"Green Plot";
dataSourceLinePlot.dataSource    = self;

// Put an area gradient under the plot above
CPTColor *areaColor       = [CPTColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.8];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]];
areaGradient.angle               = -90.0f;
areaGradientFill                 = [CPTFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill      = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPTDecimalFromString(@"1.75");

// Animate in the new plot, as an example
dataSourceLinePlot.opacity = 0.0f;
[graph addPlot:dataSourceLinePlot];

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath"opacity"];
fadeInAnimation.duration            = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode            = kCAFillModeForwards;
fadeInAnimation.toValue             = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey"animateOpacity"];

// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for ( i = 0; i < 60; i++ ) {
    id x = [NSNumber numberWithFloat:1 + i * 0.05];
    id y = [NSNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
self.dataForPlot = contentArray;

return graph;
}



Best Answer-推荐答案


RealTimePlot *plot = [[RealTimePlot alloc] init];

正在解除分配。

原来 CPTGraphHostingView 没有保留它的 hostsGraph ivar。

关于iphone - 创建 CorePlot 图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607233/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap