Im trying to get a chart on my view and I´m displaying it like this in my view:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ChartResult</h2>
<% using (Html.BeginForm("HandleChartType", "Chart")) %>
<% { %>
<%= Html.DropDownList("ListItems", "Select Chart Type")%>
<input type="submit" value="Set Chart" />
<%} %>
<% myChart.Controls.Add(ViewData["Chart"] as Chart); %>
<asp:Panel ID="myChart" runat="server"></asp:Panel>
<!--<img src="/Chart/CreateChart" alt="" />-->
<h2>FormResults</h2>
</asp:Content>
This line <% myChart.Controls.Add(ViewData["Chart"] as Chart); %> generates the error开发者_如何学JAVA message OutputStream is not available when a custom TextWriter is used
This is the code from the controller:
public ActionResult ChartResult()
{
List<string> items = GetFilteredChartTypes();
ViewData["ListItems"] = new SelectList(items);
Chart myChart = CreateChart(SeriesChartType.Column);
ViewData["Chart"] = myChart;
return View();
}
The CreateChart function just creates a chart with Column as chart type. Why do I get this error, OutputStream is not available when a custom TextWriter is used?
I think that problem is in wrong approach. When building charts you have two options.
One way is to generate chart image and then your view will consist of <img />
tag with src
attribute set to url of controller/action which generates chart image.
Another way is to build controller with JSON action, which returns chart data only. This is more flexible way because you'll get freedom to choose any of ready-to-go javascript libraries that visualize charts (html5 canvas or svg)
精彩评论