IOS getting started with using core plot to draw statistical charts

Source: Internet
Author: User

The IOs (iPhone/iPad) component has two famous widgets: s7graphview and
Core plot, they are all code hosted on Google. I heard that core plot is strong, because the former only supports graphs, while the latter supports graphs, pie charts, and bar charts, and is very active. Focus on the use of core plot. It provides Component Libraries for Mac OS x and iOS, and I only use its iOS chart library.

The core plot can draw more charts: http://code.google.com/p/core-plot/wiki/plotexamples. you can use it to meet your requirements.

Configuration is actually very simple, first download the latest version of core plot from the http://code.google.com/p/core-plot/downloads/list, such as the current is: coreplot_0.4.zip, unzip the open, then two steps:

1. set libcoreplotcoatouch in the directory coreplot_0.4/binaries/IOS. A and the entire subdirectory coreplotheaders are dragged from the Finder to the current project, select copy item into destination group's folder (if needed), and add to targets to select the target. Now you can see libCorePlot-CocoaTouch.a in link binary with libraries on the build phases page in the target project.

2. Go to the build settings page of the target, and add-objc-all_load to the other linker flags item.

[Note] the xcode I use is version 4.1. The position of the target setting item in xcode 3 is slightly different.

The configuration is done in this way, when using only # import "CorePlot-CocoaTouch.h", the following is the simplest example to experience, although there are some examples in the downloaded coreplot package, however, it is still necessary to make people understand and get the fastest experience. For example, what are the most basic code elements of such a simple graph?

Original link: http://unmi.cc/ios-coreplot-chart-started, from: yeye Huang Ying unmi blog

The main code is as follows:

0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 ////  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 {         // Initialize the array and add ten random numbers ranging from 0 to 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);         // Place the image in a cptgraphhostingview. cptgraphhostingview inherits from uiview.    CPTGraphHostingView *hostView = [[CPTGraphHostingView alloc] initWithFrame:frame];         // Add cptgraphhostingview to your view    [self.view addSubview:hostView];    hostView.backgroundColor = [UIColor
blueColor];         // Draw a graph in cptgraph. The cptxygraph is a graph.    // Specify the hostedgraph attribute of the cptgraphhostingview to associate    CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostView.frame];    hostView.hostedGraph = graph;         CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];    [graph addPlot:scatterPlot];    scatterPlot.dataSource =
self;
// Set the data source. The cptplotdatasource protocol must be applied.         // Set plotspace. The xrange and yrange must be understood. It determines whether the vertex falls into the visible area of the image.    // The Location value indicates the starting value of the coordinate. Generally, you can set the minimum value in the element.    // The length value indicates the number of flops from the start value. Generally, you can use the maximum value minus the minimum value.    // In fact, I think cptplotrange :( nsange) range is better understood and can indicate that the value ranges from 0 to 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)];         // The code for coordinates, line types, and other graphic styles is saved below         [plotSpace release];    [graph release];    [hostView release];} // Ask how many pieces of data are declared in cptplotdatasource- (NSUInteger) numberOfRecordsForPlot:(CPTPlot *)plot {    return
[dataArray count];} // Ask the data values one by one, which are declared in cptplotdatasource- (NSNumber
*) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    if(fieldEnum == CPTScatterPlotFieldY){   
// When querying the Y value        return
[dataArray objectAtIndex:index];    }else{                                   
// When querying the X value        return
[NSNumber
numberWithInt:index];
    }} - (void) dealloc {    [dataArray release];    [super
dealloc];} @end

For more detailed use of core plot, we will continue to introduce it below.

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.