新asp.net图表控件 - 他们将与MVC(最终)工作的? [英] New asp.net charting controls - will they work with MVC (eventually)?

查看:285
本文介绍了新asp.net图表控件 - 他们将与MVC(最终)工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

斯科特谷刚发布一个新的集图表控件正在分发的.NET团队。他们看起来令人难以置信的:<一href="http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx">http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx

Scott Gu just posted about a new set of charting controls being 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

百万美元的问题是...他们将与MVC的工作,如果是的话,什么时候?

The million dollar question is ... will they work with MVC, and if so, when?

推荐答案

您可以使用图表控制有两种方式:

You can use the chart controls in two ways:

从生成图像一个Controller

通过生成的图表,它从一个动作返回作为图像(如Chatuman指的是我认为):

By generating the chart and returning it as an image from an action (as Chatuman is referring to 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风格

这样,你只包含图表在.aspx意见(就像使用传统的Web表单)。对于这一点,你就必须挂钩的相关位在你的web.config

This way you just include the chart in your .aspx views (just like with traditional web forms). For this you'll have to hook up the relevant bits in your web.config

<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>

构建图表时,您不能运行数据点元素在code,因此挂钩您的数据,你需要在视图类中的方法。这对我来说工作正常。通过这种方式使控制呈现一个网址由图表控制HTTP处理程序产生的图像。在您的部署,您需要提供一个可写文件夹到缓存中的图像。

You can't run code inside the DataPoint elements when building the chart, so to hook up your data you'll need a method in the View class. This works ok for me. Working this way makes the control render a URL to an image generated by the chart control http handler. In your deployment you'll need to provide a writable folder for it to cache the images.

* VS 2010 / .NET 4支持*

要得到这个工作,在.NET 4中,你需要更改与相应的公钥标记图表引用4.0.0.0版本。

To get this working in .NET 4 you need to change the chart references to version 4.0.0.0 with the appropriate public key token.

此外,它似乎图表控件现在生成的URL到当前请求的路径,而不是申请路径。对我来说,这意味着所有的图表请求导致404错误,因为 / {}控制器/ChartImg.axd 和等价物被封锁的路线。为了解决这个问题我补充说,包括我的用法额外IgnoreRoute电话 - 一个更通用的解决方案会更好:

Also it seems that the chart control now generates urls to the current request path rather than the request route. For me this meant that all the chart requests resulted in 404 errors because /{Controller}/ChartImg.axd and equivalents were blocked by routes. To fix this I added extra IgnoreRoute calls that cover my usages - 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}");
...

这篇关于新asp.net图表控件 - 他们将与MVC(最终)工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆