Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Charting in .NET 4

0.00/5 (No votes)
19 Sep 2012 1  
Introduction to Charts in .NET 4

Introduction

This article gives a basic introduction to charting solutions available in .NET 4. The code will plot a different type of chart and data point depending upon user input.

Using the code

  1. The chart control is available in the toolbox. A chart control can have multiple ChartAreas, i.e., more than one chart can be displayed in a chart control. The image below gives details of all the available properties of the chart control (a picture is worth a thousand words).
  2. To plot a chart, values for X-Axis and Y-Axis can be provided by either binding data source to chart or add data-points one by one (We will use this approach).
  3. When you add data points to a series, each DataPoint instance may have exactly one X value, defined by the XValue property, and one or more Y values, each defined by the YValues property.
  4. A chart control can display lot of different types of chart like Bar, Line, Area etc.
  5. You can further modify the code to have 3 dimensional effect.
  6. This UML shows relationship among the chart class.

Chart co-ordinates
---------------------

The unit of measure is a percentage of the chart picture's width and height. Coordinate values must be between 0 and 100. Relative coordinates ensure that objects remain relative to one another when a chart is resized.

// The actual code accepts 5 point user input for x-axis and y-axis, the type of chart and 
// plots the chart accordingly. 
 
 
private void btnPlotChart_Click(object sender, EventArgs e)
{
    DynamicChart.Series["UserDataSeries"].Points.Clear();
    DynamicChart.Series["UserDataSeries"].ChartType = (SeriesChartType)comboBoxChartType.SelectedItem;

    if (DataPointOK())
    {
        DynamicChart.Series["UserDataSeries"].Points.Add(
          new DataPoint(Convert.ToInt32(txtX1.Text), Convert.ToInt32(txtY1.Text)));
        DynamicChart.Series["UserDataSeries"].Points.Add(
          new DataPoint(Convert.ToInt32(txtX2.Text), Convert.ToInt32(txtY2.Text)));
        DynamicChart.Series["UserDataSeries"].Points.Add(
          new DataPoint(Convert.ToInt32(txtX3.Text), Convert.ToInt32(txtY3.Text)));
        DynamicChart.Series["UserDataSeries"].Points.Add(
          new DataPoint(Convert.ToInt32(txtX4.Text), Convert.ToInt32(txtY4.Text)));
        DynamicChart.Series["UserDataSeries"].Points.Add(
          new DataPoint(Convert.ToInt32(txtX5.Text), Convert.ToInt32(txtY5.Text)));

    }
}

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