在iOS中绘制气泡图?

时间:2016-02-11 07:29:44

标签: ios objective-c graph bubble-chart

希望你们做得好。

如何绘制类似于下图的图形?

enter image description here

更具体地说,我的图表将分别具有橙色,绿色和红色的低,正常和高风险。每个包含x轴信息的气泡。

我想要完全像上面的图像,任何帮助将不胜感激,也请具体发布一些第三方库和所有。

如何实现上述图表?

注意 - 在X轴(日期)和Y轴[气泡中的(值)]。

修改 y轴由某个范围固定,x轴是来自服务器的NSMutableArray

提前致谢。

1 个答案:

答案 0 :(得分:2)

绘制图形的第一步是创建X-Y轴布局。您可以使用UIBezierPathCAShapeLayer绘制X和Y轴线。

    UIBezierPath *graphPath = [[UIBezierPath alloc]init];
    [graphPath setLineWidth:GRAPH_LAYOUT_LINE_WIDTH];
    [[UIColor blackColor] setStroke];
    [graphPath moveToPoint:CGPointMake(ORIGN_X, ORIGN_Y)];
    [graphPath addLineToPoint:CGPointMake((ORIGN_X + TOTAL_X_DIST),ORIGN_Y)];
    [graphPath stroke]; // drawing path

    //Creating graph layout
    CAShapeLayer *graphLayout = [CAShapeLayer layer];
    graphLayout.fillColor = [[UIColor clearColor] CGColor];
    graphLayout.strokeColor = [GRAPH_LAYOUT_COLOR CGColor];
    graphLayout.lineWidth = GRAPH_LAYOUT_LINE_THICKNESS;
    graphLayout.path = [graphPath CGPath];
    [self.layer addSublayer:graphLayout];

重复以上代码绘制Y轴

其次,您需要创建X和Y轴刻度

    float minX = 0;
    float maxX = 10;
    float minY = 0;
    float maxY = 100;

    float x1Unit = 1;
    float y1Unit = 1;

    self.spacingX = TOTAL_X_DIST/((maxX - minX)/x1Unit);
    self.spacingY = TOTAL_Y_DIST/((maxY - minY+1)/y1Unit);

现在你需要得到一个数组中X和Y轴的值(笛卡尔坐标),这里我只是对点进行硬编码

//Enter the co-ordinates
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(0, 50)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(1, 20)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(2, 35)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(3, 55)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(4, 80)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(5, 60)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(6, 85)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(7, 50)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(8, 30)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(9, 10)]];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(10, 05)]];

接下来,我们需要将笛卡尔坐标值转换为相对于父视图的x和y值。

// Creating (x,y) points based on superview coordinate system
    for (NSValue *point in self.pointsArray)
    {
        CGPoint coordinate;
        coordinate.x = (STARTING_X*x1Unit + ([point CGPointValue].x * self.spacingX) + self.spacingX/1.5)/x1Unit;
        coordinate.y = (STARTING_Y*y1Unit - ([point CGPointValue].y * self.spacingY))/y1Unit;

        [self.coordinateArray addObject:[NSValue valueWithCGPoint:coordinate]];

    }

现在是时候添加漂亮的气泡了

for (int a = 0; a < [self.coordinateArray count]; a++)
{
    CGPoint point = [[self.coordinateArray objectAtIndex:a] CGPointValue];

//Creating bubble for points on the graph.
UIView *bubble = [[UIView alloc] initWithFrame:BUBBLE_FRAME];
bubble.Layer.cornerRadius = BUBBLE_FRAME_WIDTH/2;

//Giving the color for the bubble based on the Y-Axis value
if (point.y <= 50)
{
    bubble.backgroundColor = RED_COLOR;
}
else if (point.y > 50 && point.y <= 80)
{
    bubble.backgroundColor = YELLOW_COLOR;
}
else
{
    bubble.backgroundColor = GREEN_COLOR;
}

[self addSubview:bubble]; 
}

希望这有助于你