3D Bar Chart

Introduction

Currently, l am in Virginia. Cramped up in this tiny nom with my old laptop. I only dare to install Windows 98 on it - which means I only had the .Net Framework to work with. A week ago at work, I noticed my 3D Pie Chart had been published I soon ventured out to see how difficult it would be to do a 3D bar chart.

I did some test projects which are available above. I took an old VS.NET project and stripped it down so I could easily use it as a template. The projects' images or charts looked sweet using my high performance laptop (kidding). I hope they look Ok on a good monitor. (I only had a temporary job, so I’m stuck with my laptop.) I was left to using the good old command line and Notepad.

After adding the .NET directory to the PATH, I ran the MAKE batch file to build the template - which basically consisted of a form and a button.

Creating the graphs

I could have put some Image or Picture control and built the graph in memory (Bitmap) as I did in the 3D Pie chart article. It was just for a demonstration so all I needed was a device context (DC). The 3D illustration for the bar chart didn't look as effective as the pie chart. Maybe it is a result for my decision to drop the white background.

Anyway, to create a Graphics object to use the form's DC you use the FromHwnd method not FromHdc. An HWnd is a window handle and is provided as a property of a form.

The Graphics.FillPolygon method takes an array of Point objects and a brush. Remember that (X,Y) position or coordinates(0,0) is the top left corner of the client area of the window.

This demo draws ten bars with random depths or heights of individual bars. No legend information was designed into the demonstration.

The 3D illustration can be achieved by drawing a series of polygons (referred to as slices) with the hatched brush. Every slice is a pixel apart from the other. Each bar is 5 pixels apart.

Once I had the four desired points of my polygon (a rhombus) I drew the bottom slice, decremented the Y values of each Point in the array (which in turn, drew the slices in an upwards fashion), and drew the final slice with the solid brush. In the code, I create a new hatched brush on every slice – this is not necessary but I didn’t want to fix it.

Pressing the Run button again, clears the previous drawing and begins anew. Notice if you minimize the window the drawing is also erased.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

I’ve been thinking that a better UI for the 3D bar graph would be thus. If you draw instead of a bunch of rhombus shaped slices, you draw one elongated rhombus shape for one side (using the brush below) and a rectangle (in this example) with a hatched brush of say %75 for the other side.