2017-04-27 22 views
0

對不起,我英文很差。我想通過圖表繪製一些線條,而不是線形圖表,怎麼辦?

這是我對我的問題的描述。

我想畫一些圖表(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 

我把線的對象到陣列,然後我要畫它。

我該如何實現這個功能?

+0

嘗試圖表視圖後增加透明視圖和執行** drawInRect:**上這個觀點。 – Sergey

+0

@Sergey你能提供更多的信息給我嗎?方法drawInRect的哪個類:屬於? –

+0

你看過圖表演示嗎? – Koen

回答

0

我找到一個很好的繪製圖表的方法。

您可以從GitHub下載圖表演示,然後我找到多線圖,這樣 Multiple Line Chart

如果我改變線條的顏色,然後我就可以解決問題。這是我的代碼:

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

我重新運行該項目,完美〜! enter image description here

0

每個視圖都有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); 
相關問題