Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a PIe Chart in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 343
    Comment on it

    The main use of a pie chart is to show comparison. When items are presented on a pie chart, you can easily see which item is the most popular and which is the least popular.

    you can create the pie chart in iOS by using the following methods. Here we use the core Plot framework .First we have to create the model class for the data that we want to display in our pie chart and then use these methods.some delegates are also available to customize the chart like different color for different section of pie chart.

    -
    
    
        (void)initPlot {
            [self configureHost];
            [self configureGraph];
            [self configureChart];
            [self configureLegend];
        }
    
        -(void)configureHost {
            CGRect parentRect = self.view.bounds;
            CGSize toolbarSize = self.toolbar.bounds.size;
            parentRect = CGRectMake(parentRect.origin.x,
                                    (parentRect.origin.y + toolbarSize.height),
                                    parentRect.size.width,
                                    (parentRect.size.height - toolbarSize.height));
            // 2 - Create host view
            self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:parentRect];
            self.hostView.allowPinchScaling = YES;
            [self.view addSubview:self.hostView];
        }
    
        -(void)configureGraph {
            CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
            self.hostView.hostedGraph = graph;
            graph.paddingLeft = 0.0f;
            graph.paddingTop = 0.0f;
            graph.paddingRight = 0.0f;
            graph.paddingBottom = 0.0f;
            graph.axisSet = nil;
            // 2 - Set up text style
            CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
            textStyle.color = [CPTColor grayColor];
            textStyle.fontName = @"Helvetica";
            textStyle.fontSize = 16.0f;
            // 3 - Configure title
            NSString *title = @"Pie-Chart";
            graph.title = title;
            graph.titleTextStyle = textStyle;
            graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
            graph.titleDisplacement = CGPointMake(0.0f, -12.0f);
            // 4 - Set theme
            self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
            [graph applyTheme:self.selectedTheme];
        }
    
        -(void)configureChart {
            // 1 - Get reference to graph
            CPTGraph *graph = self.hostView.hostedGraph;
            // 2 - Create chart
            CPTPieChart *pieChart = [[CPTPieChart alloc] init];
            pieChart.dataSource = self;
            pieChart.delegate = self;
            pieChart.pieRadius = (self.hostView.bounds.size.height * 0.5) / 2; //set the radius of pie circle
            pieChart.identifier = graph.title;
            pieChart.startAngle = M_PI_4;
            pieChart.sliceDirection = CPTPieDirectionClockwise;
            // 3 - Create gradient
            CPTGradient *overlayGradient = [[CPTGradient alloc] init];
            overlayGradient.gradientType = CPTGradientTypeRadial;
            overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9];
            overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0];
            pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
            // 4 - Add chart to graph    
            [graph addPlot:pieChart];
        }
    
        -(void)configureLegend {
            // 1 - Get graph instance
            CPTGraph *graph = self.hostView.hostedGraph;
            // 2 - Create legend
            CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
            // 3 - Configure legend
            theLegend.numberOfColumns = 1;
            theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
            theLegend.borderLineStyle = [CPTLineStyle lineStyle];
            theLegend.cornerRadius = 5.0;
            // 4 - Add legend to graph
            graph.legend = theLegend;
            graph.legendAnchor = CPTRectAnchorRight;
            CGFloat legendPadding = -(self.view.bounds.size.width / 10);
            graph.legendDisplacement = CGPointMake(legendPadding, 80.0);
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: