0

我正在使用核心图形创建正弦图。我做了所有关于绘制图表的事情并且工作得很好,因为我只需要在两点连接处添加曲线。

我的图表如下图所示:

示例图表

我需要在下图中画一条线,但我不知道如何:

我要生成的图表

有人可以帮我解决这个问题吗?

我的 DrawRect 代码如下

- (void)drawRect:(CGRect)rect
{
    [self setClearsContextBeforeDrawing: YES];

    CGContextRef context = UIGraphicsGetCurrentContext();


    CGColorRef backColorRef = [UIColor blackColor].CGColor;
    CGFloat backLineWidth = 2.f;
    CGFloat backMiterLimit = 0.f;

    CGContextSetLineWidth(context, backLineWidth);
    CGContextSetMiterLimit(context, backMiterLimit);

    CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, backColorRef);

    CGContextSetLineJoin(context, kCGLineJoinRound);

    CGContextSetLineCap(context, kCGLineCapRound );

    CGContextSetBlendMode(context, kCGBlendModeNormal);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);



    int x = 320 ;
    int y = 180 ;

    for (int i=0; i<vDesc.count; i++)
    {

        CGPoint bPoint = CGPointMake(30, y);
        CGPoint ePoint = CGPointMake(x, y);

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
        [label setCenter:CGPointMake(bPoint.x-15, bPoint.y-30)];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[UIColor whiteColor]];
        [label setText:[vDesc objectAtIndex:i]];
        [self addSubview:label];

        CGContextMoveToPoint(context, bPoint.x, bPoint.y-30);
        CGContextAddLineToPoint(context, ePoint.x, ePoint.y-30);

        y -= 30;

    }
    NSLog(@"%d",hDesc.count);
    for (int i=0; i<hDesc.count; i++)
    {

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i*vInterval+5, 150, 30, 30)];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[UIColor whiteColor]];
        label.numberOfLines = 1;
        label.adjustsFontSizeToFitWidth = YES;
        label.minimumFontSize = 1.0f;
        [label setText:[hDesc objectAtIndex:i]];

        [self addSubview:label];
    }



    CGColorRef pointColorRef = [UIColor colorWithRed:24.0f/255.0f green:116.0f/255.0f blue:205.0f/255.0f alpha:1.0].CGColor;
    CGFloat pointLineWidth = 1.5f;
   // CGFloat pointMiterLimit = 5.0f;

    CGContextSetLineWidth(context, pointLineWidth);
   // CGContextSetMiterLimit(context, pointMiterLimit);


    CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, pointColorRef);

    CGContextSetLineJoin(context, kCGLineJoinRound);

    CGContextSetLineCap(context, kCGLineCapRound );

    CGContextSetBlendMode(context, kCGBlendModeNormal);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);

    if (array1.count!=0)
    {
        CGPoint p1 = [[array1 objectAtIndex:0] CGPointValue];
        //int i = 1;
        CGContextMoveToPoint(context, p1.x, 380-p1.y);

//         CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);
        for (int i=0; i<[array1 count]; i++)
        {
            p1 = [[array1 objectAtIndex:i] CGPointValue];
            CGPoint goPoint = CGPointMake(p1.x, 430-p1.y*vInterval/20);


            CGContextAddLineToPoint(context, goPoint.x, goPoint.y);
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];

            [bt setBackgroundColor:[UIColor redColor]];

            [bt setFrame:CGRectMake(0,0,3,3)];
            [bt setCenter:goPoint];
            [self addSubview:bt];
        }

    }
    else
    {
        NSLog(@"empty");
    }
    CGContextStrokePath(context);   

}
4

1 回答 1

2

Bezier 路径将是很好的开始,您可以从这里下载示例代码,对于曲线,您可以使用 [path addQuadCurveToPoint:nextPoint controlPoint:curvePoint]; 方法,对于这种方法,你可以在这里阅读

于 2013-04-26T05:36:42.470 回答