Microsoft released today a new feature for ASP.NET - free chart controls (which are based on the Dundas Chart Controls). There are many nice looking charts in this download included:
The best thing is: It should work with ASP.NET MVC!
Download links for the ASP.NET Charts (free) :
- Download the free Microsoft Chart Controls
- Download the VS 2008 Tool Support for the Chart Controls
- Download the Microsoft Chart Controls Samples
- Download the Microsoft Chart Controls Documentation
Edit the Web.Config
To enable the controls you have to edit the web.config file.
Add this under the controls tag (path: "<system.web><pages><controls>") :
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
And add this httpHandler (under "<httpHandlers>") :
<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
Via: Combining ASP.NET MVC and ASP.NET Charting Controls
Add a chart control to a view:
Option A: ASP.NET Control + Code behind
If you use this option, you will have to add some code lines in the code behind file of your view. This shouldn´t be a big problem, because the controller is still responsible for the logic.
I took the "GettingStarted" Control from the samples:
Index.aspx:
<asp:chart id="Chart1" runat="server" Height="296px" Width="412px" Palette="BrightPastel" imagetype="Png" BorderDashStyle="Solid" BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2" backcolor="#D3DFF0" BorderColor="26, 59, 105"> <Titles> <asp:Title Text="With datasource in code behind" /> </Titles> <legends> <asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"></asp:Legend> </legends> <borderskin skinstyle="Emboss"></borderskin> <series> <asp:Series Name="Column" BorderColor="180, 26, 59, 105"> </asp:Series> </series> <chartareas> <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom"> <area3dstyle Rotation="10" perspective="10" Inclination="15" IsRightAngleAxes="False" wallwidth="0" IsClustered="False"></area3dstyle> <axisy linecolor="64, 64, 64, 64"> <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" /> <majorgrid linecolor="64, 64, 64, 64" /> </axisy> <axisx linecolor="64, 64, 64, 64"> <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" /> <majorgrid linecolor="64, 64, 64, 64" /> </axisx> </asp:ChartArea> </chartareas> </asp:chart>
I don´t know the exactly meaning of each line (read the documentation to lern more about it), but the important point is that we have a "Serie" with name " "Column" - this will represent your data as a bar in your bar chart.
Index.aspx.cs:
public partial class Index : ViewPage { protected void Page_Load(object sender, System.EventArgs e) { foreach (int value in (List<int>)this.ViewData["Chart"]) { this.Chart1.Series["Column"].Points.Add(value); } } }
Result:
Option B: Inline ASP.NET Control
You could also create the control without a code behind file, just create the control on the fly via inline code:
<p> <% System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart(); Chart2.Width = 412; Chart2.Height = 296; Chart2.RenderType = RenderType.ImageTag; Chart2.Palette = ChartColorPalette.BrightPastel; Title t = new Title("No Code Behind Page", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105)); Chart2.Titles.Add(t); Chart2.ChartAreas.Add("Series 1"); // create a couple of series Chart2.Series.Add("Series 1"); Chart2.Series.Add("Series 2"); // add points to series 1 foreach (int value in (List<int>)ViewData["Chart"]) { Chart2.Series["Series 1"].Points.AddY(value); } // add points to series 2 foreach (int value in (List<int>)ViewData["Chart"]) { Chart2.Series["Series 2"].Points.AddY(value + 1); } Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss; Chart2.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105); Chart2.BorderlineDashStyle = ChartDashStyle.Solid; Chart2.BorderWidth = 2; Chart2.Legends.Add("Legend1"); // Render chart control Chart2.Page = this; HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output); Chart2.RenderControl(writer); %> </p>
Result:
Both controls together:
I think this is a very nice feature (and play well together with ASP.NET MVC) and you could add nice charts without buy a 3rd party licence.