Using Cocoaspod to use Coreplot in Swift projects

Source: Internet
Author: User
Tags install cocoapods

Recently in the study of the available third-party icon controls, and then after searching the web, the feeling is more powerful is Coreplot , so began to study how to deploy to the program. Online about this aspect of the tutorial relatively few, most of them are objective-c, and history is relatively long, and later found a foreigner wrote blog, tell how to use Coreplot in Swift(http://blog.alwold.com /2014/08/07/using-coreplot-with-swift-in-ios/), studied for a long time, finally successfully installed. Here's a step-by-step introduction.

1. Establishment of the project

First build a simple Swift project, project name Coreplot, remember to choose language for Swift. Here is no longer elaborate, unclear students can read my previous blog

2. installing Cocoapods

Here I focus on how to install Cocoapods, because the installation process took me a long time, and there are a lot of points in the middle also rarely mentioned.

Here are two Chinese blog is quite good, all from the merits, for everyone to understand what cocoapods is.

cocoapods in detail--use the article http://blog.csdn.net/wzzvictory/article/details/18737437

cocoapods Installation and use tutorial http://code4app.com/article/cocoapods-install-usage

About Cocoapods is what and its strong I will not say more, two blog has introduced, I focus on my installation process.

1>

Open Terminal, enter the following command in turn

Gem sources--remove Https://rubygems.org/gem sources-a http://ruby.taobao.org/

This is to access the Taobao Ruby image, because GFW, there is no direct access to cocoapods in the country, so we need to set our source to Taobao a mirror site. Note distinguish between HTTPS and HTTP

In order to verify that the Ruby image can be entered

Gem Sources-l
Note is the letter Hijk l MN L, not the number 1
The next step is to show

Current SOURCES ***http://ruby.taobao.org/

Note that there must be only one in this, if you still have rubygems.org in the inside to indicate that no remove succeeds, try the above command again.

2>

Enter the following command to start the installation Cocoapods

sudo gem install Cocoapods

Note that after the input is completed, it may take a while to start the download and installation, and I was waiting for about 3 minutes before the installation was complete.

During installation execution, will you ask me if I want to update rake, enter Y

3>

After the installation process is complete, execute the command

Pod Setup
there's a 2, 3-minute look.

If there is no error in the middle, it means that you have successfully installed.

Try to check out the Coreplot we'll be installing later:

Pod Search Coreplot
I 'm going to tell you something like that.


You can see that the latest coreplot is the 1.5.1 version, and you can also see the source address on GitHub.

Here, Cocoapods installation is complete, and then we continue our steps


3.

In the terminal, CD to the directory where your Swift project resides, and then execute the following command

Pod Init

If everything works, in your project directory, a file name called Podfile, which is equivalent to the Cocoapods profile, tells us which third-party controls will be introduced in the project.

Next is a very important step, with vim or Xcode to open and edit the Podfile file, you must not directly double-click the file open and edit, I was on this eat a loss, has been a long time has been prompt error, Because your textedit will change some of the original formats you can't see, and so on.

Execute the following command to open and modify Podfile with Xcode

Open-a Xcode Podfile


here is my modified file:

# Uncomment this line to define a global platform for your Projectplatform:ios, ' 8.0 ' target ' coreplot ' dopod ' Coreplot ', ' ~> 1.5.1 ' endtarget ' coreplottests ' doend

As you can see at Target Coreplot, the new project we started with, we added pod Coreplot and the corresponding version number.

Save and close Podfile

4.

Execute the command at the terminal:

Pod Install

after waiting patiently for a period of time, you will get the following result:



indicates that our Coreplot has been installed.

Note the last line of words:

[!] Form now on use ' coreplot.xcworkspace '.

5. Close the first step of the Xcode project we created.

Looking back at our project catalog, you will find a lot of new things, including a file called Xxx.xcworkspace , is the next time we open the project directly double-click the file.

We double-click to open the project.

6.

Use my previous blog to introduce the method of building the xxx-bridging-header.h file.

is to create a new class in our engineering catalog and choose to use Objective-c to do a bit language, so Xcode will automatically help us build the bridge header file, do not need to do the manual operation.

7.

In the newly built coreplot-bridging-header.h file, add the following:

#import "COREPLOT-COCOATOUCH.H"

8.

Open Storyboard, then add a viewin Viewcontroller, this view will be the hosting view of our Coreplot control, modify the custom class of this view to Cptgraphhostingview


9. Establish outlet

Step does not draw, in storyboard assistance editor mode, CTRL drag the newly added view to the Viewcontroller.swift file, named Graphview

.

We compile the project first, Shift+commond+b, after the successful compilation, there may be some warning, I changed the version from 8.3 to 8.1, because 1.5.1 's coreplot seems to support up to 8.1

Next we add the following code into the Viewdidload method:

<span style= "White-space:pre" ></span>//creat graph        var graph = cptxygraph (Frame:cgrectzero)        Graph.title = "Hello Graph"        graph.paddingleft = 0        graph.paddingtop = 0        graph.paddingright = 0        Graph.paddingbottom = 0        //hide The axes        var axes = Graph.axisset as Cptxyaxisset        var lineStyle = Cptmutablelin Estyle ()        linestyle.linewidth = 0        axes.xAxis.axisLineStyle = lineStyle        Axes.yAxis.axisLineStyle = LineStyle                //add a pie plot        var pie = Cptpiechart ()        pie.datasource = self        Pie.pieradius = ( Self.view.frame.size.width * 0.9)/2        graph.addplot (PIE)                self.graphView.hostedGraph = graph

One by one .

Add DataSource delegate, add agent at the beginning: Cptplotdatasource

.

To edit the Proxy method:

Func Numberofrecordsforplot (plot:cptplot!), UInt {        return 4    }        func Numberforplot (plot:cptplot!, Field Fieldenum:uint, Recordindex idx:uint), nsnumber! {        return idx+1    }

The whole viewcontroller.swift looks like this.

Import Uikitclass Viewcontroller:uiviewcontroller,cptplotdatasource {@IBOutlet weak var Graphview:cptgraphhostingvie    W! Override Func Viewdidload () {super.viewdidload ()//Do any additional setup after loading the view, typical        Ly from a nib.  creat graph var graph = cptxygraph (frame:cgrectzero) graph.title = "Hello graph" graph.paddingleft        = 0 Graph.paddingtop = 0 graph.paddingright = 0 graph.paddingbottom = 0//hide the axes        var axes = Graph.axisset as cptxyaxisset var lineStyle = Cptmutablelinestyle () linestyle.linewidth = 0        Axes.xAxis.axisLineStyle = LineStyle Axes.yAxis.axisLineStyle = LineStyle//add a pie plot        var pie = Cptpiechart () Pie.datasource = self Pie.pieradius = (Self.view.frame.size.width * 0.9)/2 Graph.addplot (PIE) self.graphView.hostedGraph = graph}//DataSource delegate func numberofrEcordsforplot (plot:cptplot!), UInt {return 4} func numberforplot (plot:cptplot!, field Fieldenum: UInt, Recordindex idx:uint), nsnumber!         {return idx+1} override func didreceivememorywarning () {super.didreceivememorywarning ()    Dispose of any resources the can be recreated. }}

Then run your program and you'll see a pie painted on the screen.



Using Coreplot can also draw column chart, line chart and so on, I have just started to learn, together.

Using Cocoaspod to use Coreplot in Swift projects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.