An iOS wrapper for ChartJS. Easily build animated charts by leveraging the power of native Obj-C code.

Related tags

Charts TWRCharts
Overview

TWRCharts

TWRCharts

An Obj-C wrapper for ChartJS. Easily build animated charts by leveraging the power of native code.

TWRCharts is yet another charting library for iOS. TWRCharts is basically an effort to port the famous ChartJS Javascript library to native Obj-C code; its power lies in the fact that it gives developers the flexibility to choose between loading a ChartJS Javascript file (more on this later) into a TWRChartView, or using native methods to build either a line / bar or circular (pie / doughnut) chart.

Loading the chart from a Javascript file is very easy though little configurable and dynamic, whereas by using the native extension the user can update and refresh data on the fly. The final choice is up to you!

Native code API does not yet support all type of charts provided by ChartJS; only line, bars, pies and doughnuts are currently available.

TWRCharts's main class is TWRChartView, a subclass of UIWebView backed by an HTML file that the user never has to deal with. The API has been engineered to make it feel like a fully native experience, both from a developer and an end user point of view.

TWRCharts Demo

Usage

Usage is easy.

Add the dependency to your Podfile:

platform :ios
pod 'TWRCharts'

Run pod install to install the dependencies.

Next, import the header file wherever you want to use the custom view:

#import <TWRCharts/TWRChart.h>

In the Xcode target "Build Phases" add the files (index.html and Chart.js) under "Copy Bundle Resources".

Creating the chart view

Just declare a TWRChartView property in your header file and instantiate it as you would do with a normal view by defining its frame rect. Then just add it to your controller's view hierarchy.

// Chart View
_chartView = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 64, 320, 300)];

// Optionally assign here a JS file (see below)

// Add the chart view to the controller's view
[self.view addSubview:_chartView];

Loading a chart from a JS file

Drop in your Xcode project a .js file and make sure it's been added to the resources that are being bundled with the project in the build phases of your project.

Then just get a handle on the file and set its path to the TWRChartView that's being added to the controller's view.

NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"js"];
[_chartView setChartJsFilePath:jsFilePath];

You can use any of the chart types currently supported by ChartJS. Here's an example of how you would load a Polar Chart.

var context = document.getElementById("canvas").getContext("2d");
var polarData = [
    {
        value : 30,
        color: "#D97041"
    },
    {
        value : 90,
        color: "#C7604C"
    },
    {
        value : 24,
        color: "#21323D"
    },
    {
        value : 58,
        color: "#9D9B7F"
    },
    {
        value : 82,
        color: "#7D4F6D"
    },
    {
        value : 8,
        color: "#584A5E"
    }
]

var polarArea = new Chart(context).PolarArea(polarData);

If you're planning on to use JS files to load your charts, be sure to make the following as the first line of your .js file:

var context = document.getElementById("canvas").getContext("2d");

This code retrieves the correct context from the HTML file that backs the TWRChartView.

Loading a chart using native Obj-C code

Depending on the type of chart you want to plot (bar / line / pie...) you need to instantiate different objects, but mainly you need to follow these steps:

  • Instantiate data objects;
  • Instantiate a chart object by passing the data objects along with labels;
  • Load the chart object onto the chart view.

Here's some example code:

// Build chart data
TWRDataSet *dataSet1 = [[TWRDataSet alloc] initWithDataPoints:@[@10, @15, @5, @15, @5]];
TWRDataSet *dataSet2 = [[TWRDataSet alloc] initWithDataPoints:@[@5, @10, @5, @15, @10]];

NSArray *labels = @[@"A", @"B", @"C", @"D", @"E"];

// Instantiate the chart object
TWRLineChart *line = [[TWRLineChart alloc] initWithLabels:labels
                                                 dataSets:@[dataSet1, dataSet2]
                                                 animated:NO];

// Load the chart object onto the view
[_chartView loadLineChart:line];

Data Sets

TWRDataSet (which represents the data for bar and line charts) can be instantiated with the following init method:

- (instancetype)initWithDataPoints:(NSArray *)dataPoints
                         fillColor:(UIColor *)fillColor
                       strokeColor:(UIColor *)strokeColor
                        pointColor:(UIColor *)pointColor
                  pointStrokeColor:(UIColor *)pointStrokeColor;

You can customize the fill and stroke colors for either the bar or the line chart. For the latter one you can also choose the point fill and point stroke colors.

At a minimum you have to provide the data points, which is an array of NSNumbers.

Line / Bar Charts

Line and bar charts can be instantiated as such:

- (instancetype)initWithLabels:(NSArray *)labels
                      dataSets:(NSArray *)dataSets
                      animated:(BOOL)animated;

When passing the chart objects to the chart view, you need to call one of the following methods called on your instance of TWRChartView according to the type of object you are dealing with:

- (void)loadBarChart:(TWRBarChart *)barChart;
- (void)loadLineChart:(TWRLineChart *)lineChart;

A sweet final touch: you even have an option to call the above methods with a completion handler to get a callback whenever the chart animation finishes. You wouldn't even guess that there's a bunch of Javascript code running underneath!

- (void)loadBarChart:(TWRBarChart *)barChart withCompletionHandler:(TWRAnimationCompletionBlock)block;
- (void)loadLineChart:(TWRLineChart *)lineChart withCompletionHandler:(TWRAnimationCompletionBlock)block;

Circular Charts

And finally, circular charts can be instantiated with the following method:

- (instancetype)initWithValues:(NSArray *)values
                        colors:(NSArray *)colors
                          type:(TWRCircularChartType)type
                      animated:(BOOL)animated;

You even get a chance to choose the chart type, either a pie chart (TWRCircularChartTypePie) or a doughnut (TWRCircularChartTypeDoughnut).

And again, once you have the chart object, you can add it to the chart view with one of the following two methods called on your instance of TWRChartView:

- (void)loadCircularChart:(TWRCircularChart *)circularChart;
- (void)loadCircularChart:(TWRCircularChart *)circularChart withCompletionHandler:(TWRAnimationCompletionBlock)block;

Requirements

TWRCharts requires iOS 6.x or greater.

License

Usage is provided under the MIT License. See LICENSE for the full details.

Comments
  • Fix bug, when commonInit never calls.

    Fix bug, when commonInit never calls.

    Fix bug, when commonInit never calls. Add call commonInit from init function. (It's needed, when you link TWRChartView with storyboard and init calls automatically)

    opened by skywinder 1
  • Charts not drawing even after integrating through cocoapods

    Charts not drawing even after integrating through cocoapods

    I have integrated TWRCharts through cocoapods. Also all of the code in my viewcontroller is correct but still i am not able to draw the graph on my view, not even from the 'js' file. Please help ASAP.

    opened by Mujtabaulhasankhan 0
  • New version of ChartJS

    New version of ChartJS

    Replaced chart.min.js by minimized (yuicompressor-2.4.8) version of improved version of this chart (from https://github.com/FVANCOP/ChartNew.js) Since (https://github.com/nnnick/Chart.js/) no longer support. And last update was 7 months ago. The new version has a lot of graphical improvements and bugfixes, also has backward compatibility with your version and support negative values very well.

    opened by skywinder 0
  • Update README.md to fix pods issue

    Update README.md to fix pods issue

    After installing pod to correct working I need manually add index.html and Chart.js to Copy Bundle Resources list. I don't know, is it possible to fix this through cocoapods configuration file. But since it not fixed - it's better to specify this in readme.

    opened by skywinder 0
  • Label on bar chart.

    Label on bar chart.

    I implemented your code in my project it looks great. I am stuck on just one thing I want to add label on the bar chart like as shown in the Image. ag8XN

    opened by Mujtabaulhasankhan 0
  • How to Show Legends in BarGraph

    How to Show Legends in BarGraph

    HI @chasseurmic ,

    I like your "TWRCharts" and i have already integrated in one of my application . It is really easy to integrate compare to other chart SDK's. Now i want to show the legends (Title or Explanation with colours) for BarGraph .

    Can you explain where i can set that property or values to show the legends . I have gone through all class files, i didn't get, from where i need to enable it.

    It would be better if you could help me out .

    Waiting for your reply.

    opened by SusheelYadav455 0
  • Combined bar chart with highest value is cut off.

    Combined bar chart with highest value is cut off.

    I am using a combined bar graph from this chart. But chart with the highest value is cut off. I have tried to decrease the height from iOS app, but nothing seems to happen. I think I have to reduce the height from JS. Because whatever height I specify, it always cut off. How can I solve this issue? Or please let us know that how can I decrease the height of the chart?

    I have attached the image of the chart. Please have a look. chartimage

    Thank you.

    opened by swapnil6132 0
  • webView not showing the extra label and bar in barchart.

    webView not showing the extra label and bar in barchart.

    I want to show the 12 month data in the webview but webview only showing the 5labels and 5bar. how can i show the extra label in the uiwebview. Help me to show the content.

    opened by mathivathanim 0
Owner
Michelangelo Chasseur
Michelangelo Chasseur
An overview of the different types of charts you can make with Swift Charts

Swift Charts Examples This repo aims to provide sample code for lots of different chart types for you to use as inspiration for your own projects. We

Jordi Bruin 1.2k Dec 30, 2022
Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.

One more heads up: As Swift evolves, if you are not using the latest Swift compiler, you shouldn't check out the master branch. Instead, you should go to the release page and pick up whatever suits you.

Daniel Cohen Gindi 26.3k Jan 3, 2023
SwiftCharts - Easy to use and highly customizable charts library for iOS

SwiftCharts Easy to use and highly customizable charts library for iOS Features: Bars - plain, stacked, grouped, horizontal, vertical Scatter Lines (s

Ivan Schütz 2.4k Jan 4, 2023
🎉 SwiftUI stock charts for iOS

SwiftUI Stock Charts Display interactive stock charts easily ?? Instalation In Xcode go to File -> Swift packages -> Add package dependency Copy and p

Dennis Concepción Martín 94 Dec 26, 2022
🎉 SwiftUI stock charts for iOS

?? SwiftUI stock charts for iOS

Dennis Concepción Martín 94 Dec 26, 2022
Light weight charts view generater for iOS. Written in Swift.

# ###Light weight charts view generater for iOS. Written in Swift. Requirements iOS 8.0+ XCode 7.3+ Installation CocoaPods $ pod init specify it in yo

Recruit Holdings. Media Technology Lab 982 Nov 16, 2022
Easy to use and highly customizable pie charts library for iOS

PieCharts Easy to use and highly customizable pie charts library for iOS Swift 4.2, iOS 8+ Video Features: Customizable slices Add overlays using simp

null 503 Dec 6, 2022
Core Charts | Basic Scrollable Chart Library for iOS

Core Charts | Basic Chart Library for iOS HCoreBarChart VCoreBarChart Requirements Installation Usage Appearance Customization Getting Started You nee

Çağrı ÇOLAK 71 Nov 17, 2022
FLCharts: Easy to use and highly customizable charts library for iOS

FLCharts Requirements Xcode 11 / Swift 5 iOS >= 11.0 Installation FLCharts is av

Francesco Leoni 250 Dec 26, 2022
Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.

Version 4.0.0, synced to MPAndroidChart #f6a398b Just a heads up: Charts 3.0 has some breaking changes. Please read the release/migration notes. Anoth

Daniel Cohen Gindi 26.3k Jan 8, 2023
Aplikasi iOS kasus Covid-19 di U.S dengan Storyboard, Framework Charts dari danielgindi, dan API dari covidtracking.com

Aplikasi iOS kasus Covid-19 di U.S dengan Storyboard, Framework Charts dari danielgindi, dan API dari covidtracking.com

DK 7 Aug 1, 2022
Easy to use and highly customizable pie charts library for iOS

PieCharts Easy to use and highly customizable pie charts library for iOS Swift 4.2, iOS 8+ Video Features: Customizable slices Add overlays using simp

null 503 Dec 6, 2022
SwiftUI Charts with custom styles

SwiftUI Charts Build custom charts with SwiftUI Styles Line Chart(data: [0.1, 0.3, 0.2, 0.5, 0.4, 0.9, 0.1]) .chartStyle( LineChartStyle(.

SpaceNation 609 Jan 6, 2023
A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.

⚡ A powerful & easy to use chart library for Android ⚡ Charts is the iOS version of this library Table of Contents Quick Start Gradle Maven Documentat

Philipp Jahoda 36k Jan 5, 2023
SwiftUICharts - A charts / plotting library for SwiftUI.

A charts / plotting library for SwiftUI. Works on macOS, iOS, watchOS, and tvOS and has accessibility features built in.

Will Dale 632 Jan 3, 2023
Health Sample app using Swift, RxSwift, Anchorage, Charts

HealthSample First run pod install, then build the project and run in your devices or simulators. This project has used RIBs, Swift, RxSwift, Anchorag

null 4 Feb 11, 2022
Using Swift Charts and Voiceover Chart Descriptor to compose music. 🤯

Chart de lune ?? Using Swift Charts and Voiceover Chart Descriptor to compose music. ?? Image source: https://hadikarimi.com/portfolio/claude-debussy-

An Trinh 31 Nov 21, 2022
🎈 Curated collection of advanced animations that I have developed using (Swift UI for iOS) and (React Native for iOS/Android). Source code is intended to be reused by myself for future projects.

?? Curated collection of advanced animations that I have developed using (Swift UI for iOS) and (React Native for iOS/Android). Source code is intended to be reused by myself for future projects.

Artem Moshnin 5 Apr 3, 2022