You can use chart controls in two ways:
Creating an image from the controller
By creating a chart and returning it as an image from an action (as Chathyuman says, I think):
Chart chart = new Chart(); chart.BackColor = Color.Transparent; chart.Width = Unit.Pixel(250); chart.Height = Unit.Pixel(100); Series series1 = new Series("Series1"); series1.ChartArea = "ca1"; series1.ChartType = SeriesChartType.Pie; series1.Font = new Font("Verdana", 8.25f, FontStyle.Regular); series1.Points.Add(new DataPoint { AxisLabel = "Value1", YValues = new double[] { value1 } }); series1.Points.Add(new DataPoint { AxisLabel = "Value2", YValues = new double[] { value2 } }); chart.Series.Add(series1); ChartArea ca1 = new ChartArea("ca1"); ca1.BackColor = Color.Transparent; chart.ChartAreas.Add(ca1); using (var ms = new MemoryStream()) { chart.SaveImage(ms, ChartImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); return File(ms.ToArray(), "image/png", "mychart.png"); }
WebForms Style
This way you simply include the diagram in your .aspx views (as in traditional web forms). To do this, you need to include the corresponding bits in the web.config file
<controls> ... <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </controls> <httpHandlers> ... <add path="ChartImg.axd" verb="GET,HEAD" validate="false" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </httpHandlers> <handlers> ... <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </handlers>
You cannot run code inside DataPoint elements when building a chart, so to connect your data you need a method in the View class. This works fine for me. Working in this way causes the control to display the URL of the image generated by the HTTP diagram management handler. In your deployment, you need to provide a recording folder in which it will cache images.
* Support for VS 2010 / .NET 4 *
To get this working in .NET 4, you need to change the diagram links to version 4.0.0.0 using the corresponding public key.
It also seems that now the chart control is generating URLs for the current request path, and not for the request route. For me, this meant that all chart requests resulted in 404 errors, because /{Controller}/ChartImg.axd and the equivalents were blocked by routes. To fix this, I added additional IgnoreRoute calls that cover my customs - a more general solution would be better:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("ChartImg.axd/{*pathInfo}"); routes.IgnoreRoute("{controller}/ChartImg.axd/{*pathInfo}"); routes.IgnoreRoute("{controller}/{action}/ChartImg.axd/{*pathInfo}"); ...