15

I want to plot a bar-graph in my application. How should I do it exactly ?(I have no idea in Graphs). Please suggest some steps to start with and go on.

Thanks

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Akshay
  • 2,973
  • 6
  • 43
  • 75

8 Answers8

23

depends on what you really want. If you just need a bunch of ugly bars core-plot might be a bit too much for you.

It takes this much code to implement a bargraph. I think this is like half the code you would need for core-plot datasources. And even a nice implementation will take less time than integrating core-plot into your project.
Core-plot is a big fat monster. Like all those "I can do everything you want"-frameworks.

- (void)drawRect:(CGRect)rect {
    CGFloat height = self.bounds.size.height;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
    CGFloat barWidth = 30;
    int count = 0;
    for (NSNumber *num in values) {
        CGFloat x = count * (barWidth + 10);
        CGRect barRect = CGRectMake(x, height - ([num floatValue] * height), barWidth, [num floatValue] * height);
        CGContextAddRect(context, barRect);
        count++;
    }
    CGContextFillPath(context);
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • 2
    Seconded; CoreGraphics is actually remarkably easy to work with despite being extremely flexible. Using it in a custom UIView subclass is pretty much trivial. It's just a bunch of drawing primitives though, so building up to a bar graph would be an exercise left to you. As fluchpunkt shows, that's doesn't require much, at least for a simple output. – Tommy Mar 02 '11 at 14:31
  • 4
    +1 for writing the truth and backing it up with code. – Caleb Mar 17 '11 at 06:31
  • Wow, that was so easy. And for a simple bar graph, perfect. Thank you! – coco Aug 16 '12 at 20:22
13

Use this buddy. :)

EDIT

Installing Core Plot might be some headache for you, if you need help in that, let me know.

If you need very simple graphs you can go for ECGraph

Vaibhav Tekam
  • 2,344
  • 3
  • 18
  • 27
  • Thanks Vaibhav :) Can you provide me more information on Graphs except using CorePlot ? Thanks Again – Akshay Mar 02 '11 at 10:05
  • 4
    Welcome always Akshay! :) ECGraph which I told you is also simple graphing framework, you can also try http://code.google.com/p/s7graphview/. This is again a simple and good looking framework.Also you can check these ones https://github.com/devinross/tapkulibrary, http://www.vvi.com/apps/graph/, http://sebkade.wordpress.com/2010/05/06/basic-graph-class-for-iphone/ – Vaibhav Tekam Mar 02 '11 at 10:22
  • Thanks again Vaibhav. Now can you help me with some simple Dynamic bar Graphs(charts) ? How to create them ? Not very difficult , only simple dynamic Bar Chart ? – Akshay Mar 02 '11 at 12:13
  • Using which framework Akshay? – Vaibhav Tekam Mar 02 '11 at 13:00
  • I don't exactly know which framework to use ? Please guide me (Very new to graphs and things). – Akshay Mar 02 '11 at 13:03
  • I am using Core Plot for drawing Graphs, you can start with ECGraph. GO to following link and try downloading the sample code - http://code.google.com/p/ecgraph/ – Vaibhav Tekam Mar 03 '11 at 05:13
  • +1 for Nice answer regarding graph... Need ur help in installing Coreplot graph and including it in the project in ios 4.3 and using it. I will post a separate Question, surely if you are ready with answer ;) – mAc Jun 05 '12 at 09:34
5

You can check out the PowerPlot library. It is a powerful choice for native charts on iOS, here are a two sample bar graphs:

Bar chart - German Federal Election 2009 Bar chart - Web traffic

Using PowerPlot, the code needed to generate these two graphs is available here (upper chart) and here (lower chart).

user8472
  • 3,268
  • 3
  • 35
  • 62
4

There are several commercial tools for iOS charting, all of which can render bar graphs:

Full Disclosure - I work for Scott Logic, which is the parent company of ShinobiControls

mjollneer
  • 1,005
  • 2
  • 19
  • 36
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • i would say that the nearly 1000$ tag is a bit of a bumper on the road here ;-) – Sebastian Flückiger Mar 17 '12 at 18:10
  • @SebastianFlückiger you get what you pay for really! If you go free, CorePlot is pretty decent. But if you want something really fast and powerful, you have to pay for it. – ColinE Mar 17 '12 at 18:57
  • 3
    thats clear - but the questioneer did not really sound like he was searching for a fully professional solution =) but honestly shinobi seems pretty awesome, just read through their page. but 1k$ is a lot of money for doing some graph bars ;) – Sebastian Flückiger Mar 17 '12 at 19:04
3

Refer Raywenderlich core plot tutorial it would help you lot to understand core-plot and how to crate various type of graph using it. linke for the same is http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2

user1548843
  • 670
  • 12
  • 20
1

CorePlot is the best Graph plotting library in iOS. Here are some screenshot examples on what you can create with it.

If you want to do it without using external libraries, you can draw it using low level drawing framework like Core graphics. Apple's documentation regarding custom drawing is here.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
0

This third party project worked nice for me and I found the design much better than most other projects. Unfortunately is paid, but it's always an option :)

http://www.binpress.com/app/ios-bar-chart-view/1836

0

I've been puzzling this for awhile now. In many cases, subviews will work. Just use algebra and voila, sized just fine.

For large ones, you might want to avoid views, and go deeper into the draw functions, then use algebra

Stephen J
  • 2,367
  • 2
  • 25
  • 31