Click here to Skip to main content
Click here to Skip to main content

Charting in .NET 4

By , 19 Sep 2012
Rate this:
Please Sign up or sign in to vote.

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

About the Author

Trilok Arora

United States United States
No Biography provided

Comments and Discussions

 
QuestionHow to download your example PinmemberRoberto Guerzoni20-Sep-12 20:38 
AnswerRe: How to download your example PinmemberTrilok Arora21-Sep-12 10:32 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 19 Sep 2012
Article Copyright 2012 by Trilok Arora
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid