我想通过图表绘制一些线条,而不是折线图,该怎么办?

时间:2017-04-27 10:45:14

标签: ios objective-c charts

抱歉我的英语不好。

这是我对我的问题的描述。

我想通过图表(https://github.com/danielgindi/Charts)绘制一些线条。我有关于行的信息。

例如:

@interface Line : NSObject
@property (assign, nonatomic) float x1;//x of the first point
@property (assign, nonatomic) float x2;//x of the second point
@property (assign, nonatomic) float y1;//y of the first point
@property (assign, nonatomic) float y2;//y of the second point

-(instancetype)initWithX1:(float)x1 withY1:(float)y1 withX2:(float)x2 withY2:(float)y2;
@end

我将行的对象放入数组中,然后我想绘制它。

我该如何实现这个功能?

2 个答案:

答案 0 :(得分:0)

我找到了绘制图表的绝佳方法。

您可以从github下载图表演示,然后我找到多线图,就像这样 Multiple Line Chart

如果我改变线的颜色,那么我可以解决问题。这是我的代码:

    ((LineChartDataSet *)dataSets[0]).colors = @[[UIColor redColor], [UIColor clearColor]];

我重新运行项目,完美〜! enter image description here

答案 1 :(得分:0)

每个视图都有drawRect:方法。 只需创建新视图并覆盖其drawrect方法。

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

要执行绘图本身,您可以在里面使用UIBezierPath:

UIBezierPath * axis = [UIBezierPath bezierPath];
axis.lineWidth = 1;
[axis moveToPoint: startOfAxis];
[axis addLineToPoint: endOfAxis];
[[UIColor blackColor] setStroke];
[axis stroke];

或使用CGContextFillRect

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

// Colors
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);

// Drawing
CGContextFillRect(context, textRect);
CGContextStrokeRect(context, textRect);

CGContextRestoreGState(context);