views:

1156

answers:

5

Hello,

I'm trying to paint a simple bar chart via C# but I've never experimented with the Graphics and Drawing namespaces. I thought of generating a "start" and "end" graphic and then repeating an image somehow (to show a "length") but I have no idea how to do this.

I'd be really happy if you can point me in the right direction and/or if you have sample code to do this.

Thank you!

A: 

Here is a tutorial from CodeProject

Baget
Baget, i've seen that same tutorial on CodeProject but i don't think it's a great fit here. Alex was asking for samples to create a simple bar chart and a simple intro to .Net drawing. That CodeProject tutorial is a very complex example of drawing 3D, transparent pie charts with thousands of lines of code and dozens of objects. It's a great piece of work by the author but not great for beginners. Also, if you check the project's criticisms, its performance is horrible due to bad OOP decisions... So perhaps not such a great tutorial after all.
Paul Sasik
+3  A: 

why dont you give http://zedgraph.org/wiki/index.php?title=Main_Page a try. The time required to build (and test) your own graph controls will be too much

Eros
+2  A: 

I've got to agree with Eros. There are lots of very good graphing libraries to accomplish what you want. The best I've come across:

grenade
A: 

What's wrong with using a simple loop?

Cyril Gupta
+6  A: 

Alex, here is a very simple example to get you started. To test the code, just add a panel control to your form and create a paint event handler for it. (Double click on the panel in the designer should do it by default.) Then replace the handler code with th code below.

The code draws five bars of arbitrary length across the panel and the bar widths and heights are related to the panel widths and heights. The code is arbitrary but is a good and simple way to introduce .Net drawing capabilities.

void Panel1Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; int objCount = 5;

for (int n=0; n<objCount; n++)
{
 g.FillRectangle(Brushes.AliceBlue, 0, n*(panel1.Height/objCount), 
                 panel1.Width/(n+1), panel1.Height/objCount);
 g.DrawRectangle(new Pen(Color.Black), 0, n*(panel1.Height/objCount), 
                 panel1.Width/(n+1), panel1.Height/objCount);
 g.DrawString(n.ToString(), new Font("Arial", 10f), Brushes.Black, 
              2, 2+n*(panel1.Height/objCount));
}

}

Paul Sasik