Create 3D Charts and Graphs in .NET applications with MS Chart Control
Creating charts and graphs in .NET applications were no easy task before the release of Microsoft Chart Controls. It is now very simple and easy to create charts and graphs in .NET applications with Microsoft Chart Control that is embedded in Visual Studio 2010. The bad news is this is not supported in .NET framework 2.0. If you are lucky enough to work in .NET framework 3.5 or higher, then you can use this chart control to generate all kind of charts instantly.
If you are using Microsoft Visual Studio 2008, you can still install the add-on available for this chart control and include this chart control in your project. Let me explain in simple steps on how to create charts in windows form application to represent data from a sample access database in few easy steps.
Install Microsoft Chart Control in Visual Studio 2008
If you are using Visual Studio 2010, then you can skip this section and proceed to the next section.
1. Download MSChart_VisualStudioAddon.exe from here.
2. Run the executable file and install it.
3. Now go to ToolBox, Right Click on it, Select 'Choose Items'
4. You will find two Chart controls, one for web application and the other for Windows application. Click on the checkbox near the appropriate chart control based on your type of project.
Now you can see the Chart Control under the 'Data' category of your toolbox as shown below.
Create Charts & Graphs in Windows Forms Application Using Access Database
1. To Start with, I have created a sample database in the name CompanyRevenue in Microsoft Office Access. I also created a table in the name 'CountryRevenue' with three fields namely ID, Country and Revenue. This table consists of data that gives the revenue generated by the company's products in each country.
2. Create a new windows forms application and add chart control to the form by dragging and dropping the Chart Control on to your form.
3. You can see a sample bar chart appearing on your form in the design view. All you have to do now is just add the data to this chart and change some of the visual properties that would alter the way your chart is rendered. To do this,
- Right Click on Chart Control and Select 'Properties'.
- In the 'Properties' window, Click on 'Datasource' -> Add Project Data Source.
- This will pop up 'Database Configuration Wizard' shown below.
Select 'Database' -> Click Next -> Select 'DataSet' -> Click Next -> New Connection
Select "Microsoft Access Database File (OLE DB)" as Datasource and give the path
of the access database you are going to use in the 'Database File Name' textbox.
I have given the path of the CountryRevenue Database I created previously.
Click on "Test Connection" button and if you get an alert saying "Test
Succeeded", you are good to proceed with the next set of configurations.
- After you click 'OK' on the 'Add Connection' dialogue, the wizard will prompt you to include the necessary data objects for this data source. Here I am going to use 'Coutry' and 'Revenue' columns from country_revenue table. See screenshot below for reference.
Click on 'Finish' to complete database configuration.
- In the 'Properties' window, click on 'Series'. You will find a collection of properties under 'Series' -> Go to 'Data Source' -> 'XValueMember -> Select 'Country'. This means that X axis is going to represent values from 'Country' Column of the table. Do the same for 'YValeMember' and select 'Revenue'.
- To provide text to be displayed on the X and Y axes of your chart, in the 'Properties' window, click on 'Chart Area'. You will find a collection of properties under 'Chart Area'. Now from here click on 'Axes' property.
In 'Axes Collection Editor' dialogue, find the 'Title' Property and edit the title text for both 'X' and 'Y' axes. In the below screenshot I have set the 'Title' property for X axis to be 'Country'.
Thats it! We are good to run the project to view the generated chart with values from our database.
See the output chart in the below screenshot.
Create 3D charts using MS Chart Control
The above output clearly represents the data from our database but the chart is not a 3D chart though. Creating 3D charts is very simple, easy and can be done by just setting 'Enable3D' Property to 'True'. To do this, right click on the chart control -> Properties -> Chart Area -> Area 3D Style -> (Enable3D) -> True. See screenshot below.
Now we have 3D chart in our output.
Change type of Chart Displayed
You can generate many types of charts such as bar chart, pie chart, line graph, point graph etc. by following the above steps in addition with setting one more property to specify the type of chart displayed.
To select the type of chart you want to display,
1. Right click on the chart control -> Properties
2. Properties -> Series -> Chart -> Chart Type
3. Select the chart type you require from the drop down list and run the project.