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
- The chart control is available in the toolbox. A chart control can have multiple
ChartArea
s, 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).
- 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).
- 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.
- A chart control can display lot of different types of chart like Bar, Line, Area etc.
- You can further modify the code to have 3 dimensional effect.
- 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.
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)));
}
}