1

I've tried different ways and other resources on how to create a progress bar with 3 different colours and show progress according the values provided from the server but i can't find anything. How can i create a progress bar like this one below. I want to show the progress according to the values i can get from the backend which can change for different users. Here is the example json. Green -> "Won", Red -> "Lost" and darkGray -> "undecided"

"condition": {
          "won": 2,
          "lost": 3,
          "undecided": 0
        }

enter image description here

sk123
  • 580
  • 1
  • 8
  • 29

2 Answers2

0

- (void)setCurrentProgressWithIndex1:(float)index1 index2:(float)index2 index3:(float)index3 {
    _index1 = index1;
    _index2 = index2;
    _index3 = index3;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self drawProgressWithContext:ctx];
}

- (void)drawProgressWithContext:(CGContextRef)ctx {
    float width = self.frame.size.width;
    //  index
    float start_index = 0.0f;
    if (_index1 != 0.0f) {
        UIBezierPath* bezierPath = UIBezierPath.bezierPath;
        bezierPath.lineCapStyle = kCGLineCapRound;
        bezierPath.lineJoinStyle = kCGLineJoinBevel;
        [bezierPath moveToPoint: CGPointMake(start_index, 0)];
        [bezierPath addLineToPoint: CGPointMake(start_index, 20)];
        [bezierPath addLineToPoint: CGPointMake(start_index + width * _index1, 20)];
        [bezierPath addLineToPoint: CGPointMake(start_index + width * _index1, 0)];
        [UIColor.redColor setFill];
        [bezierPath fill];
        start_index += width * _index1;
    }
    
    if (_index2 != 0.0f) {
        UIBezierPath* bezierPath = UIBezierPath.bezierPath;
        bezierPath.lineCapStyle = kCGLineCapRound;
        bezierPath.lineJoinStyle = kCGLineJoinBevel;
        [bezierPath moveToPoint: CGPointMake(start_index, 0)];
        [bezierPath addLineToPoint: CGPointMake(start_index, 20)];
        [bezierPath addLineToPoint: CGPointMake(start_index + width * _index2, 20)];
        [bezierPath addLineToPoint: CGPointMake(start_index + width * _index2, 0)];
        [UIColor.greenColor setFill];
        [bezierPath fill];
        start_index += width * _index2;
    }

    if (_index3 != 0.0f) {
        UIBezierPath* bezierPath = UIBezierPath.bezierPath;
        bezierPath.lineCapStyle = kCGLineCapRound;
        bezierPath.lineJoinStyle = kCGLineJoinBevel;
        [bezierPath moveToPoint: CGPointMake(start_index, 0)];
        [bezierPath addLineToPoint: CGPointMake(start_index, 20)];
        [bezierPath addLineToPoint: CGPointMake(start_index + width * _index3, 20)];
        [bezierPath addLineToPoint: CGPointMake(start_index + width * _index3, 0)];
        [UIColor.blueColor setFillx];
        [bezierPath fill];
    }
}

Hao Liang
  • 83
  • 3
-1

you can use this framework to get the output

https://github.com/minjoongkim/MJProgressView

another related framework

Shivam Parmar
  • 1,520
  • 11
  • 27
  • While this may answer the question, posting links-only to other sites is a bad idea... links can go away. Either provide enough detail to make the answer useful *if the link becomes invalid*, or make this a comment, not an answer... review [answer]. – DonMag Apr 01 '21 at 14:24