【问题标题】:Core plot osx not plotting核心情节 osx 不绘图
【发布时间】:2012-12-02 20:03:30
【问题描述】:

我正在为一个学校项目编写一个简单的可可应用程序,该应用程序需要绘制来自串行端口的一些数据并将其绘制成一个简单的图形。我选择了 Core-Plot 作为框架来生成和显示数据图,但是我无法让它工作。

我设法绘制了轴并且程序运行良好,但没有出现数据线。我使用以下代码:

#import "Controller.h"
#import <CorePlot/CorePlot.h>

@implementation Controller

-(void)dealloc
{
    [plotData release];
    [graph release];
    [super dealloc];
}

-(void)awakeFromNib
{
    [super awakeFromNib];

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

// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1) length:CPTDecimalFromDouble(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1) length:CPTDecimalFromDouble(10.0)];


// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x          = axisSet.xAxis;
x.majorIntervalLength         = CPTDecimalFromString(@"1");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
x.minorTicksPerInterval       = 2;

x.title         = @"Tijd (s)";
x.titleOffset   = 30.0;
x.titleLocation = CPTDecimalFromString(@"8.5");

// Label y with an automatic label policy.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy              = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
y.minorTicksPerInterval       = 2;
y.preferredNumberOfMajorTicks = 8;
y.labelOffset                 = 10.0;

y.title         = @"Kracht (N)";
y.titleOffset   = 30.0;
y.titleLocation = CPTDecimalFromString(@"8");

// Create a plot that uses the data source method
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"Date Plot";

CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth              = 3.f;
lineStyle.lineColor              = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;

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

// Add some data
NSMutableArray *contentArray = [NSMutableArray array];
for ( NSUInteger i = 0; i < 10; i++ ) {
    id x = [NSDecimalNumber numberWithDouble:i];
    id y = [NSDecimalNumber numberWithDouble:1.2];
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
    NSLog(@"waarde %@", contentArray);
}
NSLog(@"plotting");
plotData = [contentArray retain];
}


-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return plotData.count;
}


@end

代码基于其中一个示例。

NSLog(@"waarde %@", contentArray);返回一个漂亮的数组,包含正确的数据点。

【问题讨论】:

  • 你实现了 numberForPlot:field:recordIndex 方法吗?
  • 谢谢,问题解决了!

标签: graph plot core-plot


【解决方案1】:

试试

hostView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
[self.view addSubview:hostView];

是否因为您没有将其添加为视图而导致它没有出现?

【讨论】:

  • 您好 Mark,感谢您的回复,但是当我尝试编译代码时收到错误消息。 “在'Controller *'类型的对象上找不到属性'view'”。有什么建议吗?
  • 啊,刚看到Andrew的cmets,检查一下你是否已经实现了所有的数据源方法,例如numberForPlot:field:recordIndex和numberOfRecordsForPlot:plot
  • 如果你能画出轴,问题是没有把它放到屏幕上,可能是你错过了数据源方法
  • 谢谢,我确实实现了这些,但我打错了 "(fieldEnum == CPTScatterPlotFieldX ?@"x" : @"y")" 非常感谢所有的帮助。