Using mschart C#

There are variety of possibilities to display data graphically if we know how to use ms chart control in C#. So using mschart in C# will be explained with mschart example in C#.

We need to include namespace for using mschart in C#:

using System.Windows.Forms.DataVisualization.Charting;

Then we need to clear previous graph from mschart control:

chart1.Series.Clear();

Then, we can add new series.

chart1.Series.Add(“Over Time”);

We define chart type with SeriesChartType property setting.

chart1.Series[“Over Time”].ChartType = SeriesChartType.Point;

Then we can change marker size.

chart1.Series[“Over Time”].MarkerSize = 7;

Then we can give tool tip so that we can read data values from the graph.

chart1.Series[“Over Time”].ToolTip = (“VALUE : #VALY,\n DATE : #VALX”);

read-data-values-from-the-graph

Then we can enable zooming the graph by using mouse to define zooming area with following code:

chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;

chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;

chart1.ChartAreas[0].CursorX.Interval = 0.1;
chart1.ChartAreas[0].CursorY.Interval = 0.1;

define-zooming-area-with-the-mouse

When we get data zoomed, we can reduce zoom later by clicking on following points:

reduce-zoom-by-clicking

Later on, we can add arbitrary number of series. We put data to series with code similar to this one:

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

chart1.Series[“Over Time”].Points.AddXY(Convert.ToDateTime(alDataDate[i].ToString()), Convert.ToDouble(alData[i].ToString()));

double dbl_i = (double)i;

chart1.Series[“Over Length”].Points.AddXY(Convert.ToDouble(dbl_i), Convert.ToDouble(alDataLength[i].ToString()));

}

To X axes we can give either datetime or double data.

StripLine-Class-MSChart

StripLine sl_rectangle = new StripLine();

sl_rectangle.IntervalOffset = 1.0 + 2.0 * 1.0;
sl_rectangle.BackColor = Color.LightGray;
sl_rectangle.StripWidth = Convert.ToDouble(2.0);
chart1.ChartAreas[0].AxisX.StripLines.Add(sl_rectangle);

We can also fix boundaries and set scale with following code:

chart1.ChartAreas[0].AxisX.Minimum = dblLowerX;
chart1.ChartAreas[0].AxisX.Maximum = dblUpperX;
chart1.ChartAreas[0].AxisX.Interval = 1;

chart1.ChartAreas[0].AxisY.Minimum =  dblLowerY;
chart1.ChartAreas[0].AxisY.Maximum =  dblUpperY;
chart1.ChartAreas[0].AxisY.Interval = 1;

 

Read how to utilize FinancialFormula Forecasting in MSChart to interpolate data with linear regression.

 

External links:

How to use MS Chart Control on Microsoft
MSchart Example C# on Stackoverflow
Using MSchart C# on Codeproject

Posted in c-sharp-code-examples Tagged with: , ,

Leave a Reply

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

*