Click here to Skip to main content
15,886,032 members
Articles / Programming Languages / C#
Article

3D Bar Chart

Rate me:
Please Sign up or sign in to vote.
3.38/5 (21 votes)
19 May 20032 min read 118.3K   1.9K   49   5
Converting a 2D chart into a 3D chart

Sample screenshot

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.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
I enjoy messing with application development and some Web development technologies. Right now I work in Virginia as a contractor to the government.

I am a Microsoft Cert. Professional and I got my 4 year BS degree.

See web site for resume and other code samples.

Comments and Discussions

 
GeneralGreat job! Pin
JohannesM3-Jul-07 1:13
JohannesM3-Jul-07 1:13 
Generallmd Pin
kurnikov11-Apr-07 6:35
kurnikov11-Apr-07 6:35 
Generalgood Pin
jabby30-May-05 18:22
jabby30-May-05 18:22 
GeneralA Better 3D UI Pin
MJDamron23-May-03 1:32
MJDamron23-May-03 1:32 
GeneralRe: A Better 3D UI Pin
prrusa9-Jun-05 15:35
prrusa9-Jun-05 15:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.