New asp.net settings controls - will they work with MVC (in the end)?

Scott Gu has just published a new set of chart controls distributed by the .NET team. They look incredible: http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt. aspx

A million dollar question ... will it work with MVC, and if so, when?

+73
asp.net-mvc charts
Nov 26 '08 at 5:28
source share
6 answers

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}"); ... 
+94
Nov 26 '08 at 14:33
source share

For people who want to use chart management with MVC 3 using the Razor engine, see the following link

How to use MS graphics with MVC3 using Razor

+12
Feb 25 '11 at 16:09
source share

You can already use them with MVC, all you have to do is render them as images

+2
Nov 26 '08 at 5:33
source share

Make Usercontrol instead and give it the full Chart object and let it display it on its own:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Web.UI.DataVisualization.Charting.Chart>" %> <% Model.Page = this.Page; var writer = new HtmlTextWriter(Page.Response.Output); Model.RenderControl(writer); %> 

name it Chart.ascx and put it in the shared view folder.

now you will get all the additional html, for example, an image map, etc. free as well as caching.

in your controller:

 public ActionResult Chart(){ var c = new Chart(); //... return View(c); } 

in your view:

 <% Html.RenderPartial("Chart", Model); %> 
+1
Apr 25 '10 at 11:40
source share

This article is best for me in this article:

http://www.codecapers.com/post/Build-a-Dashboard-With-Microsoft-Chart-Controls.aspx

Does not give errors in relation to "Object not set for the object instance" or "Session ID was available, but the response flow was cleared" (and not the exact statement of errors).

I did not want to simply display them as an image, because if you perform sweeps or tooltips or other click actions on a chart, rendering as an image does not save any of this.

The key to my needs was to put the chart in the model, transfer the model to the view (or partial view) and put the asp: panel in the view and add the chart (s) to the panel in the layout layout.

By the way, this was with VS.net 2008 and MVC 2 on September 3, 2010 (the dates were what I found important when looking for answers due to changes that constantly occur with MVC).

+1
Sep 03 '10 at 16:29
source share

I tested MVC and still looks like it works with MVC.

0
Jul 31 '09 at 19:10
source share



All Articles