MSChart on ASP.NET MVC 2

Posted by Adron on Stack Overflow See other posts from Stack Overflow or by Adron
Published on 2010-04-14T15:29:47Z Indexed on 2010/04/14 20:23 UTC
Read the original article Hit count: 942

Filed under:
|

I upgraded my MVC Application using MSChart to MVC 2 and have ended up with broken image links for the charts.

See my blog entry here: http://blog.adronbhall.com/post/2010/04/12/MVC-2-Breaks-my-Charts.aspx

I get no build errors anymore, and have completed the following steps.

First, I setup the following web.config lines.

add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" 
assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35"

and

add path="ChartImg.axd" verb="GET,HEAD" 
type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, 
System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35" validate="false"

(NOTE: I took the chevrons off so the lines would appear)

The next thing I did was create this page with the following code. Which should, according to it working in MVC<1, showed 4 charts.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import Namespace="Scorecard.Views" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Scorecard
</asp:Content>

<asp:Content ID="applicationTitle" ContentPlaceHolderID="ContentPlaceHolderApplicationName" runat="server">
    <%=Html.Encode(ViewData["ApplicationTitle"])%>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">

<h2>
    Web Analysis Scorecard
</h2>

<table>
    <tr>
        <td>
            <%
                ChartHelper chartHelper = new ChartHelper("Top Countries",
                                                          (double[])ViewData["TopCountryCounts"],
                                                          (string[])ViewData["TopCountries"],
                                                        SeriesChartType.Pie);
                Chart chartPieTwo = chartHelper.ResultingChart;

                // Explode data point with label "USA"
                chartPieTwo.Series["DefaultSeries"].Points[3]["Exploded"] = "true";

                chartHelper.RenderChart(this);
            %>
        </td>
        <td>
            <%
                chartHelper = new ChartHelper("View Cart Trend",
                                              (double[])ViewData["LineValues"],
                                              (string[])ViewData["TopEngines"],
                                              SeriesChartType.Line);
                chartHelper.RenderChart(this);
            %>
        </td>
    </tr>
    <tr>
        <td>
            <%  chartHelper = new ChartHelper("Yesterday's Page Views",
                          (double[])ViewData["ColumnStats"],
                          (string[])ViewData["ColumnStatHeaders"],
                          SeriesChartType.Column);
                chartHelper.RenderChart(this);
            %>
        </td>
        <td>
            <%
                double[] theValues = (double[])ViewData["ColumnStats"];
                double[] newValues = new double[] { 0, 0, 0, 0 };
                int count = 0;
                int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
                foreach (double d in theValues)
                {
                    newValues[count] += d * daysInMonth;
                    count++;
                }

                chartHelper = new ChartHelper("Current Month Page Views",
                                              newValues,
                                              (string[])ViewData["ColumnStatHeaders"],
                                              SeriesChartType.Bar);
                chartHelper.RenderChart(this);
            %>
        </td>
    </tr>
</table>
</form>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about mschart