Graph 核心绘图osx未绘图

Graph 核心绘图osx未绘图,graph,plot,core-plot,Graph,Plot,Core Plot,我正在为一个学校项目编写一个简单的cocoa应用程序,该项目需要从串行端口绘制一些数据,并将其绘制成一个简单的图形。我选择了Core-Plot作为框架来生成和显示数据图,但是我无法让它工作 我设法画了轴,程序运行正常,但没有出现数据线。我使用以下代码: #import "Controller.h" #import <CorePlot/CorePlot.h> @implementation Controller -(void)dealloc { [plo

我正在为一个学校项目编写一个简单的cocoa应用程序,该项目需要从串行端口绘制一些数据,并将其绘制成一个简单的图形。我选择了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
#导入“Controller.h”
#进口
@执行控制器
-(无效)解除锁定
{
[数据发布];
[图表发布];
[super dealoc];
}
-(无效)从NIB中唤醒
{
[超级awakeFromNib];
//从主题创建图形
graph=[(CPTXYGraph*)[CPTXYGraph alloc]initWithFrame:CGRectZero];
CPTTheme*theme=[CPTTheme themeName:kCPTDarkGradientTheme];
[图形应用主题:主题];
hostView.hostedGraph=图形;
//设置散点图空间
CPTXYPlotSpace*plotSpace=(CPTXYPlotSpace*)graph.defaultPlotSpace;
plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1)长度:CPTDecimalFromDouble(10)];
plotSpace.yRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1)长度:CPTDecimalFromDouble(10.0)];
//斧头
//使用固定间隔策略标记x轴
CPTXYAxisSet*axisSet=(CPTXYAxisSet*)graph.axisSet;
CPTXYAxis*x=axisSet.xAxis;
x、 majorIntervalLength=CPTDecimalFromString(@“1”);
x、 正交坐标CIMAL=CPTDecimalFromString(@“0.0”);
x、 minorTicksPerInterval=2;
x、 标题=@“Tijd(s)”;
x、 标题偏移=30.0;
x、 titleLocation=CPTDecimalFromString(@“8.5”);
//使用自动标签策略标记y。
CPTXYAxis*y=axisSet.yAxis;
y、 labelingPolicy=CPTAxisLabelingPolicyAutomatic;
y、 正交坐标CIMAL=CPTDecimalFromString(@“0.0”);
y、 minorTicksPerInterval=2;
y、 首选的专业人数=8;
y、 labelOffset=10.0;
y、 标题=@“克拉赫特(北)”;
y、 标题偏移=30.0;
y、 titleLocation=CPTDecimalFromString(@“8”);
//创建使用数据源方法的绘图
CPTSatterPlot*数据源线性图=[[CPTSatterPlot alloc]init]autorelease];
dataSourceLinePlot.identifier=@“日期图”;
CPTMutableLineStyle*lineStyle=[[dataSourceLinePlot.dataLineStyle mutableCopy]自动释放];
lineStyle.lineWidth=3.f;
lineStyle.lineColor=[CPTColor greenColor];
dataSourceLinePlot.dataLineStyle=线型;
dataSourceLinePlot.dataSource=self;
[graph addPlot:dataSourceLinePlot];
//添加一些数据
NSMutableArray*contentArray=[NSMutableArray];
对于(整数i=0;i<10;i++){
id x=[NSDecimalNumber,带双精度:i];
id y=[NSDecimalNumber numberWithDouble:1.2];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@“x”,y,@“y”,nil];
NSLog(@“waarde%@”,contentArray);
}
NSLog(“绘图”);
plotData=[contentArray-retain];
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot*)绘图
{
返回plotData.count;
}
@结束
该代码基于其中一个示例

NSLog(@“waarde%@”,contentArray);返回一个外观良好的数组,其中包含正确的数据点。

请重试

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

这是因为您没有将其添加为视图而没有显示吗?

您是否实现了numberForPlot:field:recordIndex方法?谢谢,它解决了问题!你好,马克,谢谢你的回复,但是当我试图编译代码时,我收到了一条错误消息。在“Controller*”类型的对象上找不到属性“view”。有什么建议吗?啊,刚刚看到Andrew的评论,检查您是否已经实现了所有数据源方法,如numberForPlot:field:recordIndex和numberOfRecordsForPlot:Plot如果您能够绘制轴,问题是无法将其显示在屏幕上,可能是您错过了数据源方法,实际上我实现了这些,但是我输入错误了“(fieldEnum==cptspatterplotfieldx?@“x”:@“y”)“非常感谢您的帮助。