简体   繁体   中英

How to draw graph using core graphics

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

在此处输入图片说明

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.

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM