Getting started with IOS using Core plot to draw a statistical chart

Source: Internet
Author: User

This article is reproduced to

http://blog.csdn.net/zhibudefeng/article/details/7677457

The IOS (Iphone/ipad) component has two well-known, s7graphview, and core plots, all of which are hosted on Google, and hear that Core plot is strong because the former only supports graphs, the latter, graphs, pie charts, histograms, etc. and more active. Then focus on the use of Core Plot. It provides a library of components under Mac OS X and iOS, and I only use it in the iOS Chart library.

Core Plot can draw out the effect of the chart should look more: Http://code.google.com/p/core-plot/wiki/PlotExamples, believe that after watching most of the IOS chart can be used to meet you.

Configuration is very simple, first download the latest version of core plot from Http://code.google.com/p/core-plot/downloads/list, such as: Coreplot_0.4.zip, unzip Open, and then two steps:

1. Drag the libcoreplotcocoatouch.a in the directory Coreplot_0.4/binaries/ios and the entire subdirectory coreplotheaders from the Finder into the current project, select Copy Item into Destination Group's folder (if needed), ADD to targets select the corresponding target. At this point you can see the libcoreplot-cocoatouch.a in the Link Binary with Libraries in the project target Build phases page.

2. To the corresponding Target Build Settings page, add-objc-all_load to the other Linker Flags entry

[note] The Xcode I use is version 4.1. The Target settings for Xcode 3 have a slightly different location.

The configuration is complete, only need to use #import "coreplot-cocoatouch.h", below to experience a simple example, download the Coreplot package, although there are some examples, but still need a good understanding and get the fastest experience. For example, like this one of the simplest graph, the most basic code elements should have?

The main code is the following:

0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606 162636465666768697071727374757677787980 ////  Created by Unmi Qiu on 8/11/11.//  Copyright 2011 . All rights reserved.//#import <UIKit/UIKit.h>#import "CorePlot-CocoaTouch.h"@interface TestCorePlotViewController : UIViewController<CPTPlotDataSource> {    NSMutableArray *dataArray;}@end@implementation TestCorePlotViewController#pragma mark - View lifecycle- (void) viewDidAppear:(BOOL)animated {         //初始化数组,并放入十个 0 - 20 间的随机数    dataArray = [[NSMutableArray alloc] init];    for(int i=0; i< 10; i++){        [dataArray addObject:[NSNumber numberWithInt:rand()%20]];    }    CGRect frame = CGRectMake(10,10, 300,100);        //图形要放在一个 CPTGraphHostingView 中,CPTGraphHostingView 继承自 UIView    CPTGraphHostingView *hostView = [[CPTGraphHostingView alloc] initWithFrame:frame];         //把 CPTGraphHostingView 加到你自己的 View 中    [self.view addSubview:hostView];    hostView.backgroundColor = [UIColor blueColor];        //在 CPTGraph 中画图,这里的 CPTXYGraph 是个曲线图    //要指定 CPTGraphHostingView 的 hostedGraph 属性来关联    CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostView.frame];    hostView.hostedGraph = graph;        CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];    [graph addPlot:scatterPlot];    scatterPlot.dataSource = self; //设定数据源,需应用 CPTPlotDataSource 协议        //设置 PlotSpace,这里的 xRange 和 yRange 要理解好,它决定了点是否落在图形的可见区域    //location 值表示坐标起始值,一般可以设置元素中的最小值    //length 值表示从起始值上浮多少,一般可以用最大值减去最小值的结果    //其实我倒觉得,CPTPlotRange:(NSRange) range 好理解些,可以表示值从 0 到 20    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) scatterPlot.plotSpace;    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)                                                    length:CPTDecimalFromFloat([dataArray count]-1)];    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)                                                    length:CPTDecimalFromFloat(20)];        //下面省去了坐标与线型及其他图形风格的代码        [plotSpace release];    [graph release];    [hostView release];}//询问有多少个数据,在 CPTPlotDataSource 中声明的- (NSUInteger) numberOfRecordsForPlot:(CPTPlot *)plot {    return [dataArray count];}//询问一个个数据值,在 CPTPlotDataSource 中声明的- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {    if(fieldEnum == CPTScatterPlotFieldY){    //询问 Y 值时        return [dataArray objectAtIndex:index];    }else{                                    //询问 X 值时        return [NSNumber numberWithInt:index];    }}- (void) dealloc {    [dataArray release];    [super dealloc];}@end

For more detailed use of Core Plot, we'll continue with the introduction below.

Reference: 1. Http://www.e-string.com/content/simple-graph-using-core-plot
2. Http://www.e-string.com/content/simple-bar-chart-core-plot
3. http://www.jaysonjc.com/programming/pie-chart-drawing-in-iphone-using-core-plot-library.html

Test Applicationmac

Iphone

Ipad

Dropplot (MAC)

Aaplot (IPhone)

Function plotting

This was drawn from the following tutorial: "Using Core Plot in a iPhone application" (Switch on the Code)

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.