IOS development framework Core PlotOpen SourceFrameworkUsage is the content to be introduced in this article, mainly to learnIOS developmentMediumFramework. IPhone graphicsFrameworkNot many. Two well-known s7graphview andCore Plot. Coincidentally, both of them are Google's. The former is simple to use, but has a single function, and can only draw graphs. The latter is an open-source project, and the project is still being updated. It is much more complicated to use, and different versions may vary, including attributes, methods, and even class names.
The Chinese online data used by Core Plot is not lacking, but not at all. The only detailed article introduced here is "Using Core Plot in an iPhone Application". The original Article is in English: http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application. However, the time is really too old. It was published in May, and the original text is no longer applicable in many places. So I spent a lot of effort to get the code in the original article through, do not dare to exclusive, share with you.
1. download and install the Core Plot framework
The original article introduces the "source code" version. First download and install Mercurial is very simple, there is a standard Mac installation package download in the http://www.selenic.com/mercurial/wiki/), then use the command:
- hg clone http://core-plot.googlecode.com/hg/ core-plot
You can download the source code of the Core Plot project to the core-plot directory.
As of the time of this article, Core Plot has provided the Mac standard installer CorePlotInstaller_0.2.2.zip. You can directly install it using the installer, you can easily install Core Plot directly to the local machine using the SDK. For details about how to use the SDK, see Core Plot SDK usage in the next blog.
2. How to use Core Plot in a project
Due to iOS restrictions, Core Plot links to iPhone applications in the form of static libraries. The CorePlot-CocoaTouch. xcodeproj file exists in the core-plot/framework Directory, which is a static library project. With regard to the use of static libraries, the previous article "encapsulate your own control library: iPhone static library application" has been introduced, and the usage is the same.
1. Create a Windows-base Application project.
2. Use Add-> Existing Files ..., Add CorePlot-CocoaTouch.xcodeproj to a new project.
3, The libCorePlot-CocoaTouch.a on the rightmost "add to target" box.
4. select Target "info-> General" to add reference to the project CorePlot-CocoaTouch ).
5. Select "info-> Build" for the new project, and add the Search path of the Core Plot Header file to "Header Search Paths", for example,/Users/kmyhy/core-plot/framework. Note that this is not mentioned in the original English text of the "Recursive" check box ). At the same time, you must add two options in Other Linker Flags:-ObjC and-all_load. 2nd options are missing in the original English text ).
6. Create a New ViewController, such as TestViewController. In this example, the "With Xib" option is selected. In the original English version, it is required to change the xib View object from UIView to CPLayerHostingView in Interface Build. In fact, it should be CPGraphHostingView ). However, there is no need to modify it in the source code.
7.. h file:
- #import <UIKit/UIKit.h>
- #import "CorePlot-CocoaTouch.h"
- @interface TestViewController : UIViewController <CPPlotDataSource>{
- CPXYGraph * graph ;
- }
- @end
8.. m file:
- # Import "TestViewController. h"
- @ Implementation TestViewController
- -(NSUInteger) numberOfRecordsForPlot :( CPPlot *) plot {
- Return 51;
- }
- -(NSNumber *) numberForPlot :( CPPlot *) plot field :( NSUInteger) fieldEnum recordIndex :( NSUInteger) index {
- Double val = (index/5.0)-5;
- If (fieldEnum = CPScatterPlotFieldX)
- {Return [NSNumber numberWithDouble: val];}
- Else
- {
- If (plot. identifier = @ "X Squared Plot ")
- {Return [NSNumber numberWithDouble: val * val];}
- Else
- {Return [NSNumber numberWithDouble: 1/val];}
- }
- }
-
- -(Void) viewDidLoad {
- // [Super viewDidLoad];
- Graph = [[CPXYGraph alloc] initWithFrame: self. view. bounds];
-
- // The original CPLayerHostingView is replaced by CPGraphHostingView
- Self. view = [[CPGraphHostingView alloc] initWithFrame: [UIScreen mainScreen]. bounds];
-
- CPGraphHostingView * hostingView = (CPGraphHostingView *) self. view;
- HostingView. hostedGraph = graph;
- Graph. paddingLeft = 20.0;
- Fig. paddingTop = 20.0;
- Graph. paddingRight = 20.0;
- Graph. paddingBottom = 20.0;
-
- CPXYPlotSpace * plotSpace = (CPXYPlotSpace *) graph. defaultPlotSpace;
- PlotSpace. xRange = [CPPlotRange plotRangeWithLocation: CPDecimalFromFloat (-6)
- Length: CPDecimalFromFloat (12)];
- PlotSpace. yRange = [CPPlotRange plotRangeWithLocation: CPDecimalFromFloat (-5)
- Length: CPDecimalFromFloat (30)];
- CPLineStyle * lineStyle = [CPLineStyle lineStyle];
-
- // The lineColor and lineWidth of CPLineStyle have become read-only.
- // LineStyle. lineColor = [CPColor blackColor];
- // LineStyle. lineWidth = 2.0f;
-
- CPXYAxisSet * axisSet = (CPXYAxisSet *) graph. axisSet;
-
- // The majorIntervalLength type is changed from NSDecimalNumber to NSDecimal.
- AxisSet. xAxis. majorIntervalLength = [[NSDecimalNumber decimalNumberWithString: @ "5"] decimalValue];
- AxisSet. xAxis. minorTicksPerInterval = 4;
- AxisSet. xAxis. majorTickLineStyle = lineStyle;
- AxisSet. xAxis. minorTickLineStyle = lineStyle;
- AxisSet. xAxis. axisLineStyle = lineStyle;
- AxisSet. xAxis. minorTickLength = 5.0f;
- AxisSet. xAxis. majorTickLength = 7.0f;
-
- // The axisLableOffset attribute is replaced by labelOffset.
- AxisSet. xAxis. labelOffset = 3.0f;
- // AxisSet. xAxis. axisLabelOffset = 3.0f;
-
- AxisSet. yAxis. majorIntervalLength = [[NSDecimalNumber decimalNumberWithString: @ "5"] decimalValue];
- AxisSet. yAxis. minorTicksPerInterval = 4;
- AxisSet. yAxis. majorTickLineStyle = lineStyle;
- AxisSet. yAxis. minorTickLineStyle = lineStyle;
- AxisSet. yAxis. axisLineStyle = lineStyle;
- AxisSet. yAxis. minorTickLength = 5.0f;
- AxisSet. yAxis. majorTickLength = 7.0f;
-
- // The axisLableOffset attribute is replaced by labelOffset.
- AxisSet. yAxis. labelOffset = 3.0f;
- // AxisSet. yAxis. axisLabelOffset = 3.0f;
-
- // The bounds attribute of CPPlotSpace is no longer valid
- CPScatterPlot * xSquaredPlot = [[CPScatterPlot alloc]
- InitWithFrame: self. view. bounds] autorelease];
- // InitWithFrame: graph. defaultPlotSpace. bounds] autorelease];
- XSquaredPlot. identifier = @ "X Squared Plot ";
-
- // The lineColor and lineWidth of CPLineStyle have become read-only.
- // XSquaredPlot. dataLineStyle. lineWidth = 1.0f;
- // XSquaredPlot. dataLineStyle. lineColor = [CPColor redColor];
- XSquaredPlot. dataSource = self;
- [Graph addPlot: xSquaredPlot];
-
- CPPlotSymbol * greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
- GreenCirclePlotSymbol. fill = [CPFill fillWithColor: [CPColor greenColor];
- GreenCirclePlotSymbol. size = CGSizeMake (2.0, 2.0 );
- XSquaredPlot. plotSymbol = greenCirclePlotSymbol;
-
- // The bounds attribute of CPPlotSpace is no longer valid
- CPScatterPlot * xInversePlot = [[CPScatterPlot alloc]
- InitWithFrame: self. view. bounds] autorelease];
- // InitWithFrame: graph. defaultPlotSpace. bounds] autorelease];
- XInversePlot. identifier = @ "X Inverse Plot ";
- // The lineColor and lineWidth of CPLineStyle have become read-only.
- // XInversePlot. dataLineStyle. lineWidth = 1.0f;
- // XInversePlot. dataLineStyle. lineColor = [CPColor blueColor];
- XInversePlot. dataSource = self;
- [Graph addPlot: xInversePlot];
- }
- -(Void) dealloc {
- [Super dealloc];
- }
- @ End
Check the code carefully and you will find that the code in the original text has been modified and adjusted by me.
AppendixCore Plot frameworkClass Hierarchy Diagram to facilitate understanding the use of each object in the Code:
Note that the colors of the classes on the right correspond to those of the layers on the left ,:
Summary:IOS development framework:Core PlotOpen SourceFrameworkI hope this article will be helpful to you!