Click here to Skip to main content
15,868,102 members
Articles / Web Development / ASP.NET
Tip/Trick

Charting in .NET 4

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
19 Sep 2012CPOL1 min read 23K   11   3
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.

C#
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionfilter chart .net Pin
Member 1172084026-May-15 10:36
Member 1172084026-May-15 10:36 
QuestionHow to download your example Pin
Roberto Guerzoni20-Sep-12 20:38
professionalRoberto Guerzoni20-Sep-12 20:38 
AnswerRe: How to download your example Pin
Trilok Arora21-Sep-12 10:32
Trilok Arora21-Sep-12 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.