Monday, June 1, 2009

Microsoft Chart Controls from code

Below are two methods that you can call from your console app or Win Forms app to create a chart using MS Chart Controls. You will needs Visual Studio 2008 SP1 (.NET 3.5.1 SP1). You will need to download the appropriate Microsoft Charting Control files. See here for a list of files to download, and here for a good article on what MS Charting Controls have to offer.

This example assumes you don’t want to use the visual designer in Visual Studio for some reason and that you want to control the entire lifecycle of the Windows Control.

The first example is called DatabaseLikeTest and is meant to simulate querying a database using LINQ to SQL, but the example could be applied to any kind of object that holds data like a DataSet or direct database query.

The second example is called HardCodedLikeTest and is just shows you how to do a very basic chart.

In both cases, the output is a PING file, though other formats could be output just by changing the ChartImageFormat of the SaveImage method.

using System.Windows.Forms.DataVisualization.Charting;

...

public class Thing
{
public int MyNum { get; set; }
public string MyDescription { get; set; }
}

private void DatabaseLikeTest()
{
// this could be a database
List<Thing> things = new List<Thing>();
things.Add(new Thing { MyDescription = "My Data 1", MyNum = 14 });
things.Add(new Thing { MyDescription = "My Data 2", MyNum = 34 });
things.Add(new Thing { MyDescription = "My Data 3", MyNum = 42 });
things.Add(new Thing { MyDescription = "My Data 4", MyNum = 18 });
things.Add(new Thing { MyDescription = "My Data 5", MyNum = 24 });
things.Add(new Thing { MyDescription = "Bogus Value", MyNum = 100 });

// this could be a linq to sql query here
var filteredThings = things.Where(p => p.MyNum < 50);

Chart myChart = new Chart();
myChart.Size = new Size(500, 500);

ChartArea myChartArea = new ChartArea();
myChart.ChartAreas.Add(myChartArea);

Series series = new Series("mySeries");

foreach (var thing in filteredThings)
{
series.Points.AddXY(thing.MyDescription, thing.MyNum);
}

myChart.Series.Add(series);

series.Points[2].Label = "High Data";
series.Points[2].Color = Color.Green;

Font font = new Font("Arial", 24, FontStyle.Bold);
Title title = new Title();
title.Text = "Test Chart";
title.Font = font;

myChart.Titles.Add(title);

myChart.SaveImage(@"C:\temp\test2.png", ChartImageFormat.Png);
}

private void HardCodedTest()
{
Chart myChart = new Chart();
myChart.Size = new Size(500, 500);

ChartArea myChartArea = new ChartArea();
myChart.ChartAreas.Add(myChartArea);

Series series = new Series("mySeries");
series.Points.Add(14);
series.Points.Add(34);
series.Points.Add(42);
series.Points.Add(18);
series.Points.Add(24);
myChart.Series.Add(series);

series.Points[0].AxisLabel = "My Data 1";
series.Points[1].AxisLabel = "My Data 2";
series.Points[2].AxisLabel = "My Data 3";
series.Points[3].AxisLabel = "My Data 4";
series.Points[4].AxisLabel = "My Data 5";

series.Points[2].Label = "High Data";
series.Points[2].Color = Color.Green;

Font font = new Font("Arial", 24, FontStyle.Bold);
Title title = new Title();
title.Text = "Test Chart";
title.Font = font;

myChart.Titles.Add(title);

myChart.SaveImage(@"C:\temp\test.png", ChartImageFormat.Png);
}

The chart will look something like this:

image


No comments: