kiran
kiran

Reputation: 4409

How to draw graph using core graphics

I'd like to be able to draw a graph using just the Apple Framework.

enter image description here

Is this possible using just CoreGraphics, or do I need to use any other framework?

I don't want to use any third party framework for drawing the graph.

Upvotes: 2

Views: 2193

Answers (2)

jrturton
jrturton

Reputation: 119242

Yes, of course it is possible. A graph like that is just a line. You can use a UIBezierPath or CGPath to draw this. Typically you'd have an array of the points on your graph, then just add lines to each point when building your path.

WWDC 2011 session 129 goes through how the graph in the Stocks app is drawn and made "fancy", all using core graphics.

Upvotes: 6

Ivor Prebeg
Ivor Prebeg

Reputation: 978

This would be a sample code to do something with CoreGraphics

CGContextBeginPath(context);
CGContextMoveToPoint(context, startX, startY);
CGContextAddLineToPoint(context, nextX, nextY);
// [...] and so on, for all line segments
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] cgColor]);
CGContextStrokePath(context);

If you want to draw axis, labels, I suggest using CorePlot http://code.google.com/p/core-plot/

Upvotes: 1

Related Questions