Creating a Simple Line Graph in iPhone [closed]
-
21-06-2021 - |
题
I would like to create a simple Line Graph like the following,
I googled to find out some sample and I got CorePlot which looks too heavy. Can anyone suggest me how to create a kind of line graph like below or tutorial is also most welcome. Please
解决方案
or you can use this https://github.com/pyanfield/WSChart
其他提示
Well, first, why calling that graph simple? You can make it by drawing vertical lines along the X coordinate. You have (probably) an array with the top points (that show the weight progress), so you draw a line from a top point till the base line in a loop that will parse all your top points. Here is the code to draw a line, call it for each top point:
-(void)drawLineFromX:(CGPoint)topPoint{
UIGraphicsBeginImageContext(instanceToYourImageView.image.size);
[instanceToYourImageView.image drawInRect:CGRectMake(0, 0, instanceToYourImageView.image.size.width, instanceToYourImageView.image.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), topPoint.x, topPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), topPoint.x, topPoint.y + a_value_to_reach_the_base_line);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[instanceToYourImageView setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}
Here the CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
is black, but you can easily change it to the desired one.
Cheers!