A simple and animated Pie Chart for your iOS app.

Related tags

Charts XYPieChart
Overview

XYPieChart

XYPieChart is an simple and easy-to-use pie chart for iOS app. It started from a Potion Project which needs an animated pie graph without interaction. All animation was implemented in the drawRect: method. After played with BTSPieChart, really like its code structure, it’s clean, has well named functions, structure like a UITableView. XYPieChart rewrote the code, based on CALayers for the animation. Compared to BTSPieChart, XYPieChart is a prettier version, it has a simpler insert/delete slices algorithm, different design of slice selection, more flexible to customize. Video Demo

Features

  • Customized color for slices (default color supported)
  • animated insert/delete slice(s)
  • slice selection animation
  • text label option of showing percentage or actual value
  • text label auto-hide when slice is too small
  • customizable text label font and relative postion
  • pie opening/closing animation
  • pie starting angle support
  • ARC support
  • interface builder support

Installation

  • Drag the XYPieChart/XYPieChart folder into your project.
  • Add the QuartzCore framework to your project.

Usage

(see sample Xcode project in /Demo)

Set PieChart properties:

[self.pieChart setDelegate:self];
[self.pieChart setDataSource:self];
[self.pieChart setStartPieAngle:M_PI_2];	//optional
[self.pieChart setAnimationSpeed:1.0];	//optional
[self.pieChart setLabelFont:[UIFont fontWithName:@"DBLCDTempBlack" size:24]];	//optional
[self.pieChart setLabelColor:[UIColor blackColor]];	//optional, defaults to white
[self.pieChart setLabelShadowColor:[UIColor blackColor]];	//optional, defaults to none (nil)
[self.pieChart setLabelRadius:160];	//optional
[self.pieChart setShowPercentage:YES];	//optional
[self.pieChart setPieBackgroundColor:[UIColor colorWithWhite:0.95 alpha:1]];	//optional
[self.pieChart setPieCenter:CGPointMake(240, 240)];	//optional

Implement Data Source Methods:

- (NSUInteger)numberOfSlicesInPieChart:(XYPieChart *)pieChart;
- (CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index;
- (UIColor *)pieChart:(XYPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index;	//optional
- (NSString *)pieChart:(XYPieChart *)pieChart textForSliceAtIndex:(NSUInteger)index;	//optional

Implement  Delegate Methods (OPTIONAL):

- (void)pieChart:(XYPieChart *)pieChart willSelectSliceAtIndex:(NSUInteger)index;
- (void)pieChart:(XYPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index;
- (void)pieChart:(XYPieChart *)pieChart willDeselectSliceAtIndex:(NSUInteger)index;
- (void)pieChart:(XYPieChart *)pieChart didDeselectSliceAtIndex:(NSUInteger)index;

Credits

XYPieChart is brought to you by XY Feng

Comments
  • Set a color to a slice : impossible?

    Set a color to a slice : impossible?

    @xyfeng Hello and thanks for your project. I made it work within my project, but I have a problem setting a colour to a slice. It seems impossible to set a custom colour to a slice. In my case, I am dealing with 2 slices only, but no matter what I try to do, the colours are always 2 shades of red, lighter and darker. I tried with @synthesize pieChart = _pieChart;and then with @synthesize pieChart = _pieChartCopy; but it nothing changed even when I change the array slice colours:

    self.sliceColors =[NSArray arrayWithObjects:
                           [UIColor colorWithRed:229/255.0 green:66/255.0 blue:115/255.0 alpha:1],
                           [UIColor colorWithRed:148/255.0 green:141/255.0 blue:139/255.0 alpha:1],
                           //[UIColor colorWithRed:50/255.0 green:205/255.0 blue:50/255.0 alpha:1],
                           //[UIColor colorWithRed:220/255.0 green:20/255.0 blue:60/255.0 alpha:1],
                           nil];
    

    Please consider my issue and I am looking forward to a response from you. Thanks in advance.

    opened by japer21 3
  • Allow reloadData if previous animation ongoing.

    Allow reloadData if previous animation ongoing.

    Using reloadData during an animation didn't reload the pie chart, meaning added slices would be missing. This makes reloadData work during animations.

    opened by nschum 2
  • Selecting a label to select a slice.

    Selecting a label to select a slice.

    I positioned the labels to appear outside the pie slices. Now I would like to be able to also select the text in order to select a pie slice. I tried getting coordinates of each text label but strangely each label has the same cgpoints.

    opened by randykittinger 2
  • if the slice size is not large enough to contain the slice text size, the text will not be shown

    if the slice size is not large enough to contain the slice text size, the text will not be shown

    see below, if the slice size is not large enough to contain the slice text size, it will not be displayed....

    I try to comment the codes below and allow to display text, but it seems that the text is covered by its sibling slices...... faint, is there any way to fix this?

    Can we put the text layer above all the slice layers ???? so it won't be covered. thansk.

    • (void)setShowPercentage:(BOOL)showPercentage { _showPercentage = showPercentage; for(SliceLayer _layer in _pieView.layer.sublayers) { CATextLayer *textLayer = [[layer sublayers] objectAtIndex:0]; [textLayer setHidden:!_showLabel]; if(!_showLabel) return; NSString *label; if(_showPercentage) label = [NSString stringWithFormat:@"%0.0f%%", layer.percentage_100]; else label = (layer.text)?layer.text:[NSString stringWithFormat:@"%0.0f", layer.value]; CGSize size = [label sizeWithFont:self.labelFont];

      if(M_PI*2*_labelRadius*layer.percentage < MAX(size.width,size.height))
      {
          [textLayer setString:@""];
      }
      else
      {
          [textLayer setString:label];
          [textLayer setBounds:CGRectMake(0, 0, size.width, size.height)];
      }
      

      } }

    opened by flypigz 2
  • Add options to adjust label color and shadow for better contrast.

    Add options to adjust label color and shadow for better contrast.

    I had trouble keeping the labels readable for brighter chart colors.

    This adds two ways to fix that: Changing the label color, or adding a text shadow for better contrast

    opened by nschum 2
  • [Feature] Add labelForSliceAtIndex method to XYPieChartDataSource

    [Feature] Add labelForSliceAtIndex method to XYPieChartDataSource

    Hey!

    I played with your component and i have to say nice work so far. The only thing that miss in this component is the ability to add texts on each slice.

    Cheers, I.

    opened by ItamarM 2
  • Possibly more accurate selection (probably not really an issue)

    Possibly more accurate selection (probably not really an issue)

    I was looking at this excellent control in in the simulator and noticed that the transition from one highlighted segment to another seemed a little off. I noticed that the touches were being resolved to self, rather than the _pieView, which may have been causing the slight offset issue. I have updated the code to resolve touches in the _pieView and now it changes they highlighted segment as soon as my pointer (running in the simulator) crosses a boundary.

    opened by AidenMontgomery 2
  • Applying transform to SliceLayers does not working.

    Applying transform to SliceLayers does not working.

    I wanted to customise display style for selected slices in a chart.

    I applied scale transform, but it's not working as expected by default.

    What I see is that bounds of SliceLayers are CGRectZero, so setting anchor point on layer does not help.

    I've managed to get the animation that I want by setting bounds and position values of SliceLayer objects accordingly, then applying scale transform.

    I think it would be appropriate to set bounds for SliceLayer s in reloadData method. What do you think? If it's ok, I could implement and send a pull request..

    Besides that, I propose to implement an interface which enables users to customise selected/deselected slice animation behaviours. Currently, you are applying a translation to position property of layers. However, it could be any generic CATransform3D struct, therefore the library will have a much more flexible interface.

    Maybe we can define two public CATransform3D properties like this:

    @property (nonatomic, assign) CATransform3D selectedSliceTransform; @property (nonatomic, assign) CATransform3D deselectedSliceTransform;

    I'm open to suggestions..

    By the way, thanks for the great library!

    opened by manuyavuz 1
  • Added support for a Pie Chart Data Source based on a dictionary.

    Added support for a Pie Chart Data Source based on a dictionary.

    Made it really easy to create a pie chart from a dictionary of entries, and added ability to add a key chart view of the pie chart based on the dictionary.

    The following is an example of setting up a pie chart based on a dictionary.

    _roundsWonLostPieChartView.colorDict      = @{@"Rounds Won": UIColor.redColor,                                         @"Rounds Lost" : UIColor.blackColor };
    _roundsWonLostPieChartView.dataSourceDict = @{@"Rounds Won": @(m_playerStats.roundsPlayed - m_playerStats.lostRounds), @"Rounds Lost" : @(m_playerStats.lostRounds)};
    
    _wonPointsInRoundPieChartView.colorDict      = @{@"Won 1 Point": UIColor.redColor,              @"Won 2 Points" : UIColor.blackColor,            @"Won 4 Points" : UIColor.blueColor };
    _wonPointsInRoundPieChartView.dataSourceDict = @{@"Won 1 Point": @(m_playerStats.wonRounds1pt), @"Won 2 Points" : @(m_playerStats.wonRounds2pt), @"Won 4 Points" : @(m_playerStats.wonRounds4pt) };
    _wonPointsInRoundPieChartView.keyOrder       = @[@"Won 1 Point", @"Won 2 Points", @"Won 4 Points"];
    

    The following is an example of dynamically creating a key legend based on the dictionary and adding it to the right of the pie chart.

        CGPoint point   = CGPointMake( xyPieChartView.frame.origin.x + xyPieChartView.frame.size.width, xyPieChartView.layer.position.y );
        UIView *keyView = [xyPieChartView makeLabelKeyViewAtPoint:point boxCornerRadius:2.0];
        CGRect  frame   = keyView.frame;
        frame.origin.y -= frame.size.height/2;
        keyView.frame = frame;
    
        [_scrollContentsView addSubview:keyView];
    
    opened by tcunning 1
  • pieChart:textForSliceAtIndex is not called

    pieChart:textForSliceAtIndex is not called

    Hi,

    I'm having an issue where pieChart:textForSliceAtIndex is not called. In my code, I created the pie chart programmatically and assigned delegate and dataSource but the method was not called. 'valueForSliceAtIndex' and 'colorForSliceAtIndex' were called though.

    Here's my code (I'm using RubyMotion btw)

    view = UIView.alloc.initWithFrame(self.view.bounds)
    view.backgroundColor = UIColor.whiteColor
    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
    
    @pieChart = XYPieChart.alloc.initWithFrame([[340, 100], [400, 400]])
    @pieChart.pieRadius = 200
    @pieChart.delegate = self
    @pieChart.dataSource = self
    view.addSubview(@pieChart)
    

    self.view.addSubview(view)

    Thanks.

    opened by trivektor 1
  • Adding an XYPieChart without Interface Builder

    Adding an XYPieChart without Interface Builder

    I might be missing something here but I can't seem to add a chart without using interfact builder. I've tried

    [self.view addSubview:_chart];

    but to no avail. Also tried looking for a subview in the _chart object that I could use but again no luck.

    opened by gearoidoceallaigh 1
  • How do I use the chart in UITableViewCell?

    How do I use the chart in UITableViewCell?

    Hi, I was trying to use the pie chart in UITableViewCell, and was wondering what are the equivalent functions/methods for viewWillAppear/viewDidAppear/viewWillDisappear/viewDidDisappear in order for the pie chart to appear? Thank you!

    opened by zq-gu 0
  • Radius

    Radius

    Hi, Thank you for making such a great library for pie charts. I wanted to know that is it possible to change the radius of the individual elements on the graph using your library. Thanks in advance.

    opened by Abhijith-Gopi 0
  • getting such output using XyPieChart

    getting such output using XyPieChart

    Hi, thank you for making such a cool library for pie charts. i came in a problem and i wanted to know if it can be done through this library. i need to draw this sort of pie chart, is it possible to make this using your library? thanks in advance

    wfbkc

    opened by farazhaider88 0
  • Getting warnings when updating to iOS 9.

    Getting warnings when updating to iOS 9. "Incompatible pointer types initializing 'CATextLayer..."

    Incompatible pointer types initializing 'CATextLayer *' with an expression of type 'CALayer * _Nullable'.

    Just need casting to (CATextLayer *) or (SliceLayer *)

    opened by crvo84 0
Owner
XY Feng
XY Feng
Simple and intuitive iOS chart library. Contribution graph, clock chart, and bar chart.

TEAChart Simple and intuitive iOS chart library, for Pomotodo app. Contribution graph, clock chart, and bar chart. Supports Storyboard and is fully ac

柳东原 · Dongyuan Liu 1.2k Nov 29, 2022
A simple pie chart for iOS.

EGPieChart Installation EGPieChart is available through CocoaPods. To install it, simply add the following line to your Podfile: pod 'EGPieChart' manu

Ethan Guan 3 Nov 29, 2021
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
This is pie chart that is very easy to use and customizable design.

CSPieChart Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation CSPieCh

iOSCS 40 Nov 29, 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
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
ANDLineChartView is easy to use view-based class for displaying animated line chart.

ANDLineChartView for iOS ANDLineChartView is easy to use view-based class for displaying animated line chart. Usage API is simple. Just implement foll

Andrzej Naglik 421 Dec 11, 2022
A simple and beautiful chart lib used in Piner and CoinsMan for iOS(https://github.com/kevinzhow/PNChart) Swift Implementation

PNChart-Swift PNChart(https://github.com/kevinzhow/PNChart) Swift Implementation Installation This isn't on CocoaPods yet, so to install, add this to

Kevin 1.4k Nov 7, 2022
A simple and beautiful chart lib used in Piner and CoinsMan for iOS

PNChart You can also find swift version at here https://github.com/kevinzhow/PNChart-Swift A simple and beautiful chart lib with animation used in Pin

Kevin 9.8k Jan 6, 2023
Demonstrate a way to build your own line chart without using any third-party library

LineChart This code demonstrate a way to build your own line chart without using any third-party library. It contains a simple yet effective algorithm

Van Hung Nguyen 0 Oct 17, 2021
Repository with example app for using Bar chart

Gráfico de Barras (Exemplo) Repositório com app exemplo para o uso do gráfico de Barras. O gráfico de barras é um gráfico com barras retangulares e co

Felipe Leite 0 Nov 5, 2021
iOS/iPhone/iPad Chart, Graph. Event handling and animation supported.

#EChart A highly extendable, easy to use chart with event handling, animation supported. ##Test How To Use Download and run the EChartDemo project is

Scott Zhu 646 Dec 27, 2022
JTChartView is the new lightweight and fully customizable solution to draw a chart

JTChartView JTChartView is the new lightweight and fully customizable solution to draw a curve and fill the space underneath it with a gradient. The r

Jakub Truhlář 124 Sep 7, 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
iOS Chart. Support animation, click, scroll, area highlight.

XJYChart XJYChart - A High-performance, Elegant, Easy-to-integrate Charting Framework. The Best iOS Objc Charts. chart more beautiful support chart sc

junyixie 868 Nov 16, 2022
FSLineChart A line chart library for iOS.

FSLineChart A line chart library for iOS. Screenshots Installing FSLineChart Add the contents of the FSLineChart project to your directory or simply a

Arthur 856 Nov 24, 2022
an iOS open source Radar Chart implementation

JYRadarChart an open source iOS Radar Chart implementation ##Screenshots Requirements Xcode 5 or higher iOS 5.0 or higher ARC CoreGraphics.framework D

Johnny Wu 416 Dec 31, 2022
Easy to use Spider (Radar) Chart library for iOS written in Swift.

DDSpiderChart Easy to use Spider (Radar) Chart library for iOS written in Swift. Requirements iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+ Xcode

Deniz Adalar 82 Nov 14, 2022
Line Chart library for iOS written in Swift

Swift LineChart Usage var lineChart = LineChart() lineChart.addLine([3, 4, 9, 11, 13, 15]) Features Super simple Highly customizable Auto scaling Touc

Mirco Zeiss 601 Nov 15, 2022