GithubHelp home page GithubHelp logo

qingshan2346 / ios-linechart Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mruegenberg/ios-linechart

0.0 1.0 0.0 544 KB

Interactive linecharts for the simplicity-loving iOS developer.

License: Other

ios-linechart's Introduction

ios-linechart

Interactive linecharts for the simplicity-loving iOS developer.

You just want some linecharts, and Core Plot makes you think

fuuu

Search no more! ios-linechart is here!

Features

  • Simple API. Lets you draw the first chart by the time you would need to find sample code for Core Plot.
  • Interactivity. Lets users look at single datapoints.
  • Support non-uniform steps on the x axis

Installation

CocoaPods

The best way to get ios-linechart is to use CocoaPods.

Without CocoaPods

If, for some reason, you don't want that, copy the following files into your project:

  • LCLegendView.h, LCLegendView.m
  • LCLineChartView.h, LCLineChartView.m
  • LCInfoView.h, LCInfoView.m

Additionally, you will need to get some dependencies from the objc-utils project:

  • UIKit+DrawingHelpers.{h,m} (found in UIKit/Drawing/)

Just copy these into you project as well.

ios-linechart uses Core Graphics, so you'll need to add CoreGraphics.framework to your project.

Even if you don't use Cocoapods, it is recommended to use an official release, since the repository may be unstable in between releases. Just check out the newest tagged commit.

Usage

Basic usage can be seen in the demo app contained in the repository.

First, import the main header:

    #import "LCLineChartView.h"

Alternatively, you can import the abbreviation header, which provides names without the LC prefix:

    #import "LineChart.h"

Each chart line is contained in a LCLineChartData object, which specifies a range of its data on the x axis (xMin / xMax), a color, a title (which is displayed in the legend) and an itemCount, the number of data points:

    LCLineChartData *d = [LCLineChartData new];
    d.xMin = 1;
    d.xMax = 31;
    d.title = @"The title for the legend";
    d.color = [UIColor redColor];
    d.itemCount = 10;

Additionally, each LCLineChartData object also has a getData property. This is simply a block that takes the number of a data point as its argument and returns LCLineChartDataItem objects, which wrap the individual data points:

    NSMutableArray *vals = [NSMutableArray new];
    for(NSUInteger i = 0; i < d.itemCount; ++i) {
        [vals addObject:@((rand() / (float)RAND_MAX) * (31 - 1) + 1)];
    }
    [vals sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2];
    }];
    d.getData = ^(NSUInteger item) {
        float x = [vals[item] floatValue];
        float y = powf(2, x / 7);
        NSString *label1 = [NSString stringWithFormat:@"%d", item];
        NSString *label2 = [NSString stringWithFormat:@"%f", y];
        return [LCLineChartDataItem dataItemWithX:x y:y xLabel:label1 dataLabel:label2];
    };

The x and y properties of LCLineChartDataItem are, obviously, the x and y values of the data point. xLabel is the text displayed on the x axis when the data point is selected, dataLabel is usually just a textual representation of the y value, and is displayed in a bubble directly next to the data point when the user touches it.

Note that, to get a coherent chart, the x values for the items should be sorted. This will be the case anyway for most real-world data.

Finally, everything is packed up into a LCLineChartView:

    LCLineChartView *chartView = [[LCLineChartView alloc] initWithFrame:CGRectMake(20, 700, 500, 300)];
    chartView.yMin = 0;
    chartView.yMax = powf(2, 31 / 7) + 0.5;
    chartView.ySteps = @[@"0.0",
                         [NSString stringWithFormat:@"%.02f", chartView.yMax / 2],
                         [NSString stringWithFormat:@"%.02f", chartView.yMax]];
    chartView.data = @[d];

    [self.view addSubview:chartView];

The yMin and yMax properties should be self-evident. ySteps is an array of labels which are placed along the y axis. data is an array of all LineChartData objects to use. drawsDataPointOrnaments is a switch that determines whether circles are drawn around the actual data points.

Screenshots

Screenshot 1

Screenshot 2

FAQ

  • The sample project doesn't compile

    The sample project, at the moment, needs CocoaPods. Install Cocoapods, then run pod install while you are in the ios-linechart directory.

Contact

Travis CI build status

Bug reports and pull requests are welcome! Contact me via e-mail or just by opening an issue on GitHub.

ios-linechart's People

Contributors

ludwigschubert avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.