我正在为一个学校项目编写一个简单的可可应用程序,该应用程序需要从串行端口绘制一些数据并将其绘制成一个简单的图形。我选择了 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); 返回一个好看的数组,包含正确的数据点。