Demo of using a program to draw a pie chart for ios

Source: Internet
Author: User

Demo function: Use a program to draw a pie chart demo.

Demo Description: draws a pie chart based on the given title array, value array, and color array. The main code is in CLMView. m.

Demo screenshot:

 

 


Demo main code: drawing part


[Csharp]
-(Void) drawRect :( CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext ();
// Cgcontextsets

// Anti-aliasing
CGContextSetAllowsAntialiasing (context, TRUE );

// Int arr [5] = {20, 15, 35, 85, 60 };

Float sum = 0;


For (int j = 0; j <[valueArr count]; j ++)
{
Sum + = [[valueArr objectAtIndex: j] floatValue];
}


CGContextMoveToPoint (context, 160,230 );

Float currentangel = 0;

// Pie chart
CGContextSaveGState (context );
CGContextScaleCTM (context, 1.0, scaleY );
 
Currentangel = 0;

For (int I = 0; I <[valueArr count]; I ++)
{
 
Float startAngle = KDGREED (currentangel );

Currentangel + = [[valueArr objectAtIndex: I] floatValue]/sum;

Float endAngle = KDGREED (currentangel );


// Draw the slice above
CGContextMoveToPoint (context, 160,230 );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];

[[UIColor colorWithWhite: 1.0 alpha: 0.8] setStroke];

CGContextAddArc (context, 160,230,150, startAngle, endAngle, 0 );

CGContextClosePath (context );
CGContextDrawPath (context, kCGPathFill );



// Draw the side
Float starx = cos (startAngle) × 150 + 160;
Float stary = sin (startAngle) × 150 + 230;

Float endx = cos (endAngle) * 150 + 160;
Float endy = sin (endAngle) * 150 + 230;

// Float starty1 = stary + spaceHeight;
Float endy1 = endy + spaceHeight;


If (endAngle <K_PI)
{
}

// Only a radian of 3.14 can draw the thickness of the front.
Else if (startAngle <K_PI)
{
EndAngle = K_PI;
Endx = 10;
Endy1 = 230 + spaceHeight;
}

Else
Break;


// CGContextSetBlendMode (context, kCGBlendModeMultiply );


// Draw the thickness
CGMutablePathRef path = CGPathCreateMutable ();
CGPathMoveToPoint (path, nil, starx, stary );
CGPathAddArc (path, nil, 160,230,150, startAngle, endAngle, 0 );
CGPathAddLineToPoint (path, nil, endx, endy1 );

CGPathAddArc (path, nil, 160,230 + spaceHeight, 150, endAngle, startAngle, 1 );
CGContextAddPath (context, path );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];
[[UIColor colorWithWhite: 0.9 alpha: 1.0] setStroke];

CGContextDrawPath (context, kCGPathFill );

[[UIColor colorWithWhite: 0.1 alpha: 0.4] setFill];
CGContextAddPath (context, path );
CGContextDrawPath (context, kCGPathFill );

 
}


// Overall gradient
CGFloat componets [] = {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.1 };

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB ();

CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, componets, nil, 2 );

CGContextDrawRadialGradient (context, gradient, CGPointMake (160,230), 0, CGPointMake (160,230), 150, 0 );

CFRelease (colorspace );
CGGradientRelease (gradient );


CGContextRestoreGState (context );

// Draw text

For (int I = 0; I <[valueArr count]; I ++)
{
Float origionx = 50;
Float origiony = I * 30 + 200;

[[ColorArr objectAtIndex: I % [valueArr count] setFill];

CGContextFillRect (context, CGRectMake (origionx, origiony, 20, 20 ));
CGContextDrawPath (context, kCGPathFill );


If (I <[titleArr count])
{
NSString * title = [titleArr objectAtIndex: I];
[Title drawAtPoint: CGPointMake (origionx + 50, origiony) withFont: [UIFont systemFontOfSize: 16];
}

}

}

-(Void) drawRect :( CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext ();
// Cgcontextsets
 
// Anti-aliasing
CGContextSetAllowsAntialiasing (context, TRUE );
 
// Int arr [5] = {20, 15, 35, 85, 60 };
 
Float sum = 0;
 
 
For (int j = 0; j <[valueArr count]; j ++)
{
Sum + = [[valueArr objectAtIndex: j] floatValue];
}
 
 
CGContextMoveToPoint (context, 160,230 );
 
Float currentangel = 0;
 
// Pie chart
CGContextSaveGState (context );
CGContextScaleCTM (context, 1.0, scaleY );

Currentangel = 0;
 
For (int I = 0; I <[valueArr count]; I ++)
{

Float startAngle = KDGREED (currentangel );

Currentangel + = [[valueArr objectAtIndex: I] floatValue]/sum;

Float endAngle = KDGREED (currentangel );


// Draw the slice above
CGContextMoveToPoint (context, 160,230 );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];

[[UIColor colorWithWhite: 1.0 alpha: 0.8] setStroke];

CGContextAddArc (context, 160,230,150, startAngle, endAngle, 0 );

CGContextClosePath (context );
CGContextDrawPath (context, kCGPathFill );



// Draw the side
Float starx = cos (startAngle) × 150 + 160;
Float stary = sin (startAngle) × 150 + 230;

Float endx = cos (endAngle) * 150 + 160;
Float endy = sin (endAngle) * 150 + 230;

// Float starty1 = stary + spaceHeight;
Float endy1 = endy + spaceHeight;


If (endAngle <K_PI)
{
}

// Only a radian of 3.14 can draw the thickness of the front.
Else if (startAngle <K_PI)
{
EndAngle = K_PI;
Endx = 10;
Endy1 = 230 + spaceHeight;
}

Else
Break;


// CGContextSetBlendMode (context, kCGBlendModeMultiply );


// Draw the thickness
CGMutablePathRef path = CGPathCreateMutable ();
CGPathMoveToPoint (path, nil, starx, stary );
CGPathAddArc (path, nil, 160,230,150, startAngle, endAngle, 0 );
CGPathAddLineToPoint (path, nil, endx, endy1 );

CGPathAddArc (path, nil, 160,230 + spaceHeight, 150, endAngle, startAngle, 1 );
CGContextAddPath (context, path );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];
[[UIColor colorWithWhite: 0.9 alpha: 1.0] setStroke];

CGContextDrawPath (context, kCGPathFill );

[[UIColor colorWithWhite: 0.1 alpha: 0.4] setFill];
CGContextAddPath (context, path );
CGContextDrawPath (context, kCGPathFill );

}
 

// Overall gradient
CGFloat componets [] = {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.1 };
 
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB ();
 
CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, componets, nil, 2 );
 
CGContextDrawRadialGradient (context, gradient, CGPointMake (160,230), 0, CGPointMake (160,230), 150, 0 );
 
CFRelease (colorspace );
CGGradientRelease (gradient );
 
 
CGContextRestoreGState (context );
 
// Draw text
 
For (int I = 0; I <[valueArr count]; I ++)
{
Float origionx = 50;
Float origiony = I * 30 + 200;

[[ColorArr objectAtIndex: I % [valueArr count] setFill];

CGContextFillRect (context, CGRectMake (origionx, origiony, 20, 20 ));
CGContextDrawPath (context, kCGPathFill );


If (I <[titleArr count])
{
NSString * title = [titleArr objectAtIndex: I];
[Title drawAtPoint: CGPointMake (origionx + 50, origiony) withFont: [UIFont systemFontOfSize: 16];
}

}
 
}

 

 


Call method of CLMView. m:

 


[Csharp]
CLMView * cv = [[CLMView alloc] initWithFrame: CGRectMake (0, 0,320,440)]; // instantiate
 
// Specify the title Array
Cv. titleArr = [NSArray arrayWithObjects: @ "iphone", @ "sybian", @ "windowbile", @ "mego", @ "android", nil];
 
// Array of specified numeric ratios
Cv. valueArr = [NSArray arrayWithObjects: [NSNumber numberWithFloat: 20], [NSNumber numberWithFloat: 20], \
[NSNumber numberWithFloat: 20], [NSNumber numberWithFloat: 20], [NSNumber numberWithFloat: 20], nil];
// Specify the color array
Cv. colorArr = [NSArray arrayWithObjects: [UIColor yellowColor], [UIColor blueColor], [UIColor brownColor], [UIColor purpleColor], [UIColor orangeColor], nil];

[Self. view addSubview: cv];
[Cv release];

CLMView * cv = [[CLMView alloc] initWithFrame: CGRectMake (0, 0,320,440)]; // instantiate

// Specify the title Array
Cv. titleArr = [NSArray arrayWithObjects: @ "iphone", @ "sybian", @ "windowbile", @ "mego", @ "android", nil];

// Array of specified numeric ratios
Cv. valueArr = [NSArray arrayWithObjects: [NSNumber numberWithFloat: 20], [NSNumber numberWithFloat: 20], \
[NSNumber numberWithFloat: 20], [NSNumber numberWithFloat: 20], [NSNumber numberWithFloat: 20], nil];
// Specify the color array
Cv. colorArr = [NSArray arrayWithObjects: [UIColor yellowColor], [UIColor blueColor], [UIColor brownColor], [UIColor purpleColor], [UIColor orangeColor], nil];
 
[Self. view addSubview: cv];
[Cv release];

 

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.