Xcode 4.2中的iOS核心图(iOS coreplot in Xcode 4.2)

编程入门 行业动态 更新时间:2024-10-07 02:26:44
Xcode 4.2中的iOS核心图(iOS coreplot in Xcode 4.2)

我有应用程序编译,但我看不到图表。

RaceDetailView.h

#import <UIKit/UIKit.h> #import "CorePlot-CocoaTouch.h" @interface RaceDetailView : UIView <CPTPlotDataSource, CPTPlotSpaceDelegate> @property (nonatomic, retain) NSArray *plotData; @property (nonatomic, retain) NSString *title; @property (nonatomic, retain) CPTGraphHostingView *layerHostingView; @property CGFloat labelRotation; - (void)setTitleDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds; - (void)setPaddingDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds; -(NSUInteger)numberOfRecords; -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index; @end

RaceDetailView.m

#import "RaceDetailView.h" @implementation RaceDetailView @synthesize layerHostingView; @synthesize labelRotation; @synthesize title; @synthesize plotData; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { title = @"my race chart"; plotData = [[NSMutableArray alloc] initWithObjects: [NSNumber numberWithDouble:20.0], [NSNumber numberWithDouble:30.0], [NSNumber numberWithDouble:60.0], nil]; CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:frame] autorelease]; //[self addGraph:graph toHostingView:layerHostingView]; layerHostingView.hostedGraph = graph; //[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTDarkGradientTheme]]; [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]]; [self setTitleDefaultsForGraph:graph withBounds:frame]; [self setPaddingDefaultsForGraph:graph withBounds:frame]; // Setup scatter plot space CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; plotSpace.allowsUserInteraction = YES; plotSpace.delegate = self; // Grid line styles CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; majorGridLineStyle.lineWidth = 0.75; majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75]; CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle]; minorGridLineStyle.lineWidth = 0.25; minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1]; CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle]; redLineStyle.lineWidth = 10.0; redLineStyle.lineColor = [[CPTColor redColor] colorWithAlphaComponent:0.5]; // Axes // Label x axis with a fixed interval policy CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; CPTXYAxis *x = axisSet.xAxis; x.majorIntervalLength = CPTDecimalFromString(@"0.5"); x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1.0"); x.minorTicksPerInterval = 2; x.majorGridLineStyle = majorGridLineStyle; x.minorGridLineStyle = minorGridLineStyle; x.title = @"X Axis"; x.titleOffset = 30.0; x.titleLocation = CPTDecimalFromString(@"1.25"); // Label y with an automatic label policy. CPTXYAxis *y = axisSet.yAxis; y.labelingPolicy = CPTAxisLabelingPolicyAutomatic; y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1.0"); y.minorTicksPerInterval = 2; y.preferredNumberOfMajorTicks = 8; y.majorGridLineStyle = majorGridLineStyle; y.minorGridLineStyle = minorGridLineStyle; y.labelOffset = 10.0; y.title = @"Y Axis"; y.titleOffset = 30.0; y.titleLocation = CPTDecimalFromString(@"1.0"); // Rotate the labels by 45 degrees, just to show it can be done. labelRotation = M_PI * 0.25; // Set axes //graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil]; graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil]; // Create a plot that uses the data source method CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; dataSourceLinePlot.identifier = @"Data Source Plot"; CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease]; lineStyle.lineWidth = 3.0; lineStyle.lineColor = [CPTColor greenColor]; dataSourceLinePlot.dataLineStyle = lineStyle; dataSourceLinePlot.dataSource = self; [graph addPlot:dataSourceLinePlot]; // Auto scale the plot space to fit the plot data // Extend the y range by 10% for neatness [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]]; CPTPlotRange *xRange = plotSpace.xRange; CPTPlotRange *yRange = plotSpace.yRange; [xRange expandRangeByFactor:CPTDecimalFromDouble(1.3)]; [yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)]; plotSpace.yRange = yRange; // Restrict y range to a global range CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(2.0f)]; plotSpace.globalYRange = globalYRange; // set the x and y shift to match the new ranges CGFloat length = xRange.lengthDouble; //xShift = length - 3.0; length = yRange.lengthDouble; //yShift = length - 2.0; // 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); dataSourceLinePlot.plotSymbol = plotSymbol; // Set plot delegate, to know when symbols have been touched // We will display an annotation when a symbol is touched dataSourceLinePlot.delegate = self; dataSourceLinePlot.plotSymbolMarginForHitDetection = 5.0f; // Add legend graph.legend = [CPTLegend legendWithGraph:graph]; graph.legend.textStyle = x.titleTextStyle; graph.legend.fill = [CPTFill fillWithColor:[CPTColor darkGrayColor]]; graph.legend.borderLineStyle = x.axisLineStyle; graph.legend.cornerRadius = 5.0; graph.legend.swatchSize = CGSizeMake(25.0, 25.0); graph.legendAnchor = CPTRectAnchorBottom; graph.legendDisplacement = CGPointMake(0.0, 12.0); } return self; } - (void)setTitleDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds { graph.title = title; CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; textStyle.color = [CPTColor grayColor]; textStyle.fontName = @"Helvetica-Bold"; textStyle.fontSize = round(bounds.size.height / 20.0f); graph.titleTextStyle = textStyle; graph.titleDisplacement = CGPointMake(0.0f, round(bounds.size.height / 18.0f)); // Ensure that title displacement falls on an integral pixel graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; } - (void)setPaddingDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds { float boundsPadding = round(bounds.size.width / 20.0f); // Ensure that padding falls on an integral pixel graph.paddingLeft = boundsPadding; if (graph.titleDisplacement.y > 0.0) { graph.paddingTop = graph.titleDisplacement.y * 2; } else { graph.paddingTop = boundsPadding; } graph.paddingRight = boundsPadding; graph.paddingBottom = boundsPadding; } -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { return [plotData count]; } -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { NSNumber *num; if (fieldEnum == CPTPieChartFieldSliceWidth) { num = [plotData objectAtIndex:index]; } else { return [NSNumber numberWithInt:index]; } return num; } @end

编辑:您可能会注意到这是来自CorePlot示例SimpleScatterPlot

I have the app compiling, but I see no graph.

RaceDetailView.h

#import <UIKit/UIKit.h> #import "CorePlot-CocoaTouch.h" @interface RaceDetailView : UIView <CPTPlotDataSource, CPTPlotSpaceDelegate> @property (nonatomic, retain) NSArray *plotData; @property (nonatomic, retain) NSString *title; @property (nonatomic, retain) CPTGraphHostingView *layerHostingView; @property CGFloat labelRotation; - (void)setTitleDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds; - (void)setPaddingDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds; -(NSUInteger)numberOfRecords; -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index; @end

RaceDetailView.m

#import "RaceDetailView.h" @implementation RaceDetailView @synthesize layerHostingView; @synthesize labelRotation; @synthesize title; @synthesize plotData; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { title = @"my race chart"; plotData = [[NSMutableArray alloc] initWithObjects: [NSNumber numberWithDouble:20.0], [NSNumber numberWithDouble:30.0], [NSNumber numberWithDouble:60.0], nil]; CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:frame] autorelease]; //[self addGraph:graph toHostingView:layerHostingView]; layerHostingView.hostedGraph = graph; //[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTDarkGradientTheme]]; [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]]; [self setTitleDefaultsForGraph:graph withBounds:frame]; [self setPaddingDefaultsForGraph:graph withBounds:frame]; // Setup scatter plot space CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; plotSpace.allowsUserInteraction = YES; plotSpace.delegate = self; // Grid line styles CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; majorGridLineStyle.lineWidth = 0.75; majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75]; CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle]; minorGridLineStyle.lineWidth = 0.25; minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1]; CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle]; redLineStyle.lineWidth = 10.0; redLineStyle.lineColor = [[CPTColor redColor] colorWithAlphaComponent:0.5]; // Axes // Label x axis with a fixed interval policy CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; CPTXYAxis *x = axisSet.xAxis; x.majorIntervalLength = CPTDecimalFromString(@"0.5"); x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1.0"); x.minorTicksPerInterval = 2; x.majorGridLineStyle = majorGridLineStyle; x.minorGridLineStyle = minorGridLineStyle; x.title = @"X Axis"; x.titleOffset = 30.0; x.titleLocation = CPTDecimalFromString(@"1.25"); // Label y with an automatic label policy. CPTXYAxis *y = axisSet.yAxis; y.labelingPolicy = CPTAxisLabelingPolicyAutomatic; y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1.0"); y.minorTicksPerInterval = 2; y.preferredNumberOfMajorTicks = 8; y.majorGridLineStyle = majorGridLineStyle; y.minorGridLineStyle = minorGridLineStyle; y.labelOffset = 10.0; y.title = @"Y Axis"; y.titleOffset = 30.0; y.titleLocation = CPTDecimalFromString(@"1.0"); // Rotate the labels by 45 degrees, just to show it can be done. labelRotation = M_PI * 0.25; // Set axes //graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil]; graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil]; // Create a plot that uses the data source method CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; dataSourceLinePlot.identifier = @"Data Source Plot"; CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease]; lineStyle.lineWidth = 3.0; lineStyle.lineColor = [CPTColor greenColor]; dataSourceLinePlot.dataLineStyle = lineStyle; dataSourceLinePlot.dataSource = self; [graph addPlot:dataSourceLinePlot]; // Auto scale the plot space to fit the plot data // Extend the y range by 10% for neatness [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]]; CPTPlotRange *xRange = plotSpace.xRange; CPTPlotRange *yRange = plotSpace.yRange; [xRange expandRangeByFactor:CPTDecimalFromDouble(1.3)]; [yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)]; plotSpace.yRange = yRange; // Restrict y range to a global range CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(2.0f)]; plotSpace.globalYRange = globalYRange; // set the x and y shift to match the new ranges CGFloat length = xRange.lengthDouble; //xShift = length - 3.0; length = yRange.lengthDouble; //yShift = length - 2.0; // 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); dataSourceLinePlot.plotSymbol = plotSymbol; // Set plot delegate, to know when symbols have been touched // We will display an annotation when a symbol is touched dataSourceLinePlot.delegate = self; dataSourceLinePlot.plotSymbolMarginForHitDetection = 5.0f; // Add legend graph.legend = [CPTLegend legendWithGraph:graph]; graph.legend.textStyle = x.titleTextStyle; graph.legend.fill = [CPTFill fillWithColor:[CPTColor darkGrayColor]]; graph.legend.borderLineStyle = x.axisLineStyle; graph.legend.cornerRadius = 5.0; graph.legend.swatchSize = CGSizeMake(25.0, 25.0); graph.legendAnchor = CPTRectAnchorBottom; graph.legendDisplacement = CGPointMake(0.0, 12.0); } return self; } - (void)setTitleDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds { graph.title = title; CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; textStyle.color = [CPTColor grayColor]; textStyle.fontName = @"Helvetica-Bold"; textStyle.fontSize = round(bounds.size.height / 20.0f); graph.titleTextStyle = textStyle; graph.titleDisplacement = CGPointMake(0.0f, round(bounds.size.height / 18.0f)); // Ensure that title displacement falls on an integral pixel graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; } - (void)setPaddingDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds { float boundsPadding = round(bounds.size.width / 20.0f); // Ensure that padding falls on an integral pixel graph.paddingLeft = boundsPadding; if (graph.titleDisplacement.y > 0.0) { graph.paddingTop = graph.titleDisplacement.y * 2; } else { graph.paddingTop = boundsPadding; } graph.paddingRight = boundsPadding; graph.paddingBottom = boundsPadding; } -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { return [plotData count]; } -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { NSNumber *num; if (fieldEnum == CPTPieChartFieldSliceWidth) { num = [plotData objectAtIndex:index]; } else { return [NSNumber numberWithInt:index]; } return num; } @end

Edit: You may note that this is from the CorePlot example SimpleScatterPlot.

最满意答案

为什么这个代码在UIView ? 此代码通常驻留在视图控制器( UIViewController )中。 您从中提取示例的Plot Gallery应用程序稍微复杂一些,因为它在多个位置使用图表 - 在主视图区域中,并且还为列表或浏览器视图制作缩略图图像。

你有没有设置layerHostingView ? 它在视图层次结构中是否可见?

检查数据源是否为@danielbeard建议。 散点图的正确字段名称是CPTScatterPlotFieldX和CPTScatterPlotFieldY 。

Why is this code in a UIView? This code normally resides in a view controller (UIViewController). The Plot Gallery app that you pulled the example from is a little more complicated because it uses the plots in multiple places--in the main view area and also to make the thumbnail images for the list or browser view.

Do you ever set the layerHostingView? Is it in the view hierarchy and visible?

Check your datasource as @danielbeard suggested. The correct field names for scatter plots are CPTScatterPlotFieldX and CPTScatterPlotFieldY.

更多推荐

本文发布于:2023-07-27 08:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   Xcode   coreplot   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!