Microsoft Charting 4.0 – Ploting a circle getting started

Posted: 2011-11-30 in .Net, Code and Computers

Ms charting is now included in the 4.0 library. This basically means to get started you no longer need to download anything special, even so you should download the older MS examples (found here). Its for VS2008 but the upgrade will go mostly smoothly, you will need to upgrade the solution to 4.0 framework to fix the missing references.

So…

First create a new window form application.
DO NOT use WPF atm as it requires the wpf toolkit and do not use ASP .Net as the web.config requires a lot of hacking (contry to what MS and Scott will tell you). For now lets keep it simple.

Open the Form1 window designer and drag in the chart control found under data from the toolkit. If you dont’ have it check your project is set to build for .Net 4.0 framework (not client)

Open the Form1.cs code page and drop in the code below.
Comments: Everything in charts are controlled by its added series. You control types and everything

 

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ChartType();
        }
        public void ChartType()
        {
            //set it to fill for now
            chart1.Dock = DockStyle.Fill;

            //Clear anything that was there
            chart1.Series.Clear();

            // Create a data series
            Series series1 = new Series();

            // Add data points to the first series
            series1.ChartType = SeriesChartType.FastLine;
           
            //get the points
            List<Tuple<double, double>> points = GetCircle(5);

            //add via looping
            //foreach (Tuple<double, double> xy in points)
            //{
            //    series1.Points.AddXY(xy.Item1, xy.Item2);
            //}

            //add with databind with linq for help
            series1.Points.DataBindXY(points.Select(e => e.Item1).ToList(), points.Select(e => e.Item2).ToList());

            // Add series to the chart
            chart1.Series.Add(series1);

        }

        private List<Tuple<double, double>> GetCircle(float radius)
        {
            List<Tuple<double, double>> list = new List<Tuple<double, double>>();
            for (double i = 0.0; i < 360.0; i += 0.1)
            {
                double angle = i * System.Math.PI / 180;
                double x = (radius * System.Math.Cos(angle));
                double y = (radius * System.Math.Sin(angle));
                list.Add(new Tuple<double, double>(x, y));
            }

            return list;
        }
      
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s