Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wanted to draw a vertical single bar graph. I was trying to do it using DrawRect, but could not able to do so. Can nay one hlep me to knwo if this can be done easily by providing start and end point in view to change the color.

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you have a custom view, it's just a matter of drawing the lines and rectangles that you want. For example:

- (void)drawRect:(CGRect)rect
{
    CGAffineTransform t = CGAffineTransformMakeScale(1, -1);
    self.transform = t;
    CGFloat baseline = 50;
    CGFloat inset = 40;
    CGFloat barWidth = 20;
    CGFloat barHeight = 80;

    CGRect r = CGRectMake(inset + barWidth, baseline, barWidth, barHeight);
    UIBezierPath *p = [UIBezierPath bezierPathWithRect:r];
    [[UIColor redColor] set];
    [p fill];
    p = [UIBezierPath bezierPath];

    [p moveToPoint:CGPointMake(inset, baseline)];
    [p addLineToPoint:CGPointMake(inset + 3 * barWidth, baseline)];
    [[UIColor blackColor] set];
    [p stroke];
}

produces this:

Sample bar graph

I've created a UIView subclass with the -drawRect: method above and created an instance of that view that's the size of the window. Note that I flipped the coordinate system using a transform -- you don't have to do that, but drawing with the origin at the lower left corner can be easier.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...