Skip to main content

Cloud

Clustered Column Chart – WinForms Chart Control

A Clustered column chart is used to compare values across a few categories. Follow the steps below to do this.
Step 1: Create a sample Excel sheet that has data based on group.
Example: Stud.xlsx
clust_chart_excel_data
Step 2: Drag the chart control to the form (sample screenshots below).
clus_chart_windform
Step 3: Double click the “Click” button and paste the below code there.
using System.Windows.Forms.DataVisualization.Charting;
using Excel = Microsoft.Office.Interop.Excel;
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
Object obj;
string str;
int rCnt = 0;
int cCnt = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(“D:\\anuasp.net\\Stud.xlsx”, 0, true, 5, “”, “”, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
Excel.Range rng ;
List lstStringNames = new List();
List lstdoubleScore1 = new List();
List lstdoubleScore2 = new List();
List lstdoubleScore3 = new List();
int rowRng = range.Rows.Count;
int colRng = range.Columns.Count;
for (int i = 2; i <= rowRng; i++)
{
lstStringNames.Add(range.Cells[1, i].Value2);
}
for (int i = 2; i <= rowRng; i++)
{
for (int j = 2; j <= colRng; j++)
{
if (i == 2)
{
lstdoubleScore1.Add(range.Cells[i, j].Value2);
}
else if (i == 3)
{
lstdoubleScore2.Add(range.Cells[i, j].Value2);
}
else
{
lstdoubleScore3.Add(range.Cells[i, j].Value2);
}
}
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
chart1.Series.Add(“Micheal”);
chart1.Series.Add(“Andrea”);
chart1.Series.Add(“Ben John”);
chart1.Series[“Micheal”].Points.DataBindXY(lstStringNames, lstdoubleScore1);
chart1.Series[“Andrea”].Points.DataBindXY(lstStringNames, lstdoubleScore2);
chart1.Series[“Ben John”].Points.DataBindXY(lstStringNames, lstdoubleScore3);
chart1.Series[“Micheal”].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[“Andrea”].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[“Ben John”].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show(“Unable to release the Object ” + ex.ToString());
}
finally
{
GC.Collect();
}
}
Description of this program:
The program basically reads the data from the Excel file and stores it into four different lists. The first list stores the quarters. Second, third and fourth stores the scores of each employee. The chart’s property (DataBindXY) binds the X-value and Y-values of the collection’s data points to the first columns of the specified data source. The Column chart type uses a sequence of columns to compare values across categories. Finally we have to properly clean up Excel interop objects or release Excel COM objects. (See the method releaseObject(object obj)).
Output
clust_chart_output

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Anitha Padmalaya

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram