ASP.NET MVC Charts
There are a few references online about how to get the new ASP.NET Charting controls working with the MVC framework. However, some of them are outdated, and the corresponding information to make it work with MVC2 are spread out. So in an effort to organize the info all in one place, here’s a quick step by step guide on how to get this working
First step is to add a reference to the System.Web.DataVisualization assembly
Next create a new charting controller (or simply add the action method below to an existing controller).
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;
namespace Phoenix.Portal.Controllers
{
public class ChartController : Controller
{
private Font font = new Font("Trebuchet MS", 14, FontStyle.Bold);
private Color color = Color.FromArgb(26, 59, 105);
public ActionResult Index()
{
// Define the Data ... this simple example is just a list of values from 0 to 50
List<float> values = new List<float>();
for (int i = 0; i < 50; i++)
{
values.Add(i);
}
// Define the Chart
Chart Chart2 = new Chart()
{
Width = 800,
Height = 296,
RenderType = RenderType.BinaryStreaming,
Palette = ChartColorPalette.BrightPastel,
BorderlineDashStyle = ChartDashStyle.Solid,
BorderWidth = 2,
BorderColor = color
};
Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
Chart2.Titles.Add(new Title("Do The Wave!", Docking.Top, font, color));
Chart2.ChartAreas.Add("Waves");
Chart2.Legends.Add("Legend");
//Bind the model data to the chart
var series1 = Chart2.Series.Add("Sin");
var series2 = Chart2.Series.Add("Cos");
foreach (int value in values)
{
series1.Points.AddY((float)Math.Sin(value * .5) * 100f);
}
foreach (int value in values)
{
series2.Points.AddY((float)Math.Cos(value * .5) * 100f);
}
// Stream the image to the browser
MemoryStream stream = new MemoryStream();
Chart2.SaveImage(stream, ChartImageFormat.Png);
stream.Seek(0, SeekOrigin.Begin);
return File(stream.ToArray(), "image/png");
}
}
}
Finally, show the chart by pointing to the controller’s path:
<img src="/chart" />
And that’s it! You get a sweet chart as seen below:

Nick Said,
June 29, 2010 @ 3:17 pm
I am getting an Error “The type or namespace name ‘DataVisualization’ does not exist in the namespace ‘System.Web.UI’ How do I add an assembly reference? I am new to ASP.NET MVC and VS2010 so I am trying to find out what I am missing or does someone know of a good charting tool to use with MVC in VS2010?
Joel Martinez Said,
July 7, 2010 @ 7:51 am
For the benefit of others reading this, I chatted with Nick over email … I sent him a sample VS project that implements the steps described above and he was able to resolve the issue in his code
Carlos Hernandez Said,
August 18, 2010 @ 11:30 am
Can you send me the sample too? I´m sure I´m gonna run into Nick´s problem as well.
Another question: does this code work for MVC2?
Thx.
Joel Martinez Said,
August 19, 2010 @ 8:28 pm
Yes it works for mvc 2. I literally just copied the code from the article into a new project and it worked. Let me know if you have a specific problem
@5&33p Said,
August 20, 2010 @ 2:19 pm
Sir,
Can I embed this image into a Master page..?
And, Can we create a HTML helper for this Chart..?? Can u guide me in this regard.!
As I am novice to MVC kindly spare wid my doubts, if they are very small ones…!
Joel Martinez Said,
August 20, 2010 @ 4:12 pm
Yes, since the image src is simply set to the URL of the chart action method, you can easily include it in a master page. As far as an HTML helper goes, you could if you wanted to. Would just have to add a new extension method. Let me know if you have any other questions
Marc Said,
February 28, 2012 @ 7:46 am
Hi,
I want to change de ChartType to Line, How can I do it??