Create Bar Charts using Microsoft C#

 

Introduction

Bar chart to represent & visualize big data is a great way to facilitate Data Analysis and derive conclusions.

Microsoft C# has the library tool to conveniently draw the bar charts. Create a window form project, drag Data -> Chart from Toolbox and we can start our project.

https://msdn.microsoft.com/en-us/library/dd456715.aspx

 

Input Database file: 'csv' file

To create bar chart, the program required input data. There are many type of input database files, such as text file, csv, etc.

In this coding, I am using 'csv' file.

Comma-separated values (CSV) file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by the delimiter, commas. The use of the comma as a field separator is the source of the name for this file format. Many applications including MS Excel, notepad can view/edit CSV file.

 To efficiently store data retrieved from '.csv' file to RAM, we can use c# 'List' nodes.

List<stringmyHeader = new List<string>();

List<Int32myData = new List<Int32>();

Read data line-by-line from 'Records.csv' using StreamReader.

using (StreamReader reader = new StreamReader("myData.csv"))

{

  while (!reader.EndOfStream)

  {

    var line = reader.ReadLine();

    //do something

  }

Each lines  from 'Records.csv' consist of one or more fields, separated by delimiter, commas. We can use c# inherited csv reading function to split a line into individual data.

var values = line.Split(',');

//var 'values' by default are string data type

 Variable 'values' are string type by default. Trim away any redundant spacebar in the front&end of variable 'values'. Save them into 'List' nodes

for (int i = 0; i < values.Count(); i++)

{

  values[i] = values[i].Trim();  //remove spacebar in front/end of values

  myHeader.Add(values[i]);

  myData.Add(Int32.Parse(values[i]));

}

 


C# Chart Toolbox

Upon dragging C# toolbox chart into the window Form, library 'System.Windows.Forms.DataVisualization.Charting' will be automatically added on top.

using System.Windows.Forms.DataVisualization.Charting;

CSharp toolbox

In chart properties, create new chart areas, legends, series and titles by clicking add button in Editor window.

Chart properties

ChartArea Collection Editor

Chart Legend Collection Editor

Chart Series Collection Editor

Place the below code into the program and we can start building our C# chart. The chart name 'chart1', series name 'test1'.

//Clear chart old series

foreach (var series in chart1.Series)

{

  series.Points.Clear();

}

chart1.Titles.Clear(); //Clear chart old title

Title title = chart1.Titles.Add("This is chart title");

title.Font = new System.Drawing.Font("Arial", 16, FontStyle.Bold);

chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;

chart1.ChartAreas["ChartArea1"].AxisX.Title = "X-axis title";

chart1.ChartAreas["ChartArea1"].AxisY.Title = "Y-axis title";

Int16 count = 0;

//Clear new chart series

foreach (string f in myList)

{

  chart1.Series["test1"].Points.AddXY(f, myList[count++]);

}

 


Source code

Simple sample code:  exe2-1.zip

Data file in '.csv': myData.csv

Sample code window

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This Web Page Created with PageBreeze Free HTML Editor