3 回答

TA貢獻1828條經(jīng)驗 獲得超3個贊
您可以通過兩種方式使用圖表控件:
從控制器生成圖像
通過生成圖表并將其作為操作返回的圖像(如查圖曼所說,我認為):
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視圖中(就像使用傳統(tǒng)的Web表單一樣)。為此,您必須連接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>
構建圖表時,無法在DataPoint元素內(nèi)運行代碼,因此要連接數(shù)據(jù),需要在View類中使用一個方法。這對我來說很好。以這種方式工作可使控件呈現(xiàn)由圖表控件http處理程序生成的圖像的URL。在部署中,您需要為其提供一個可寫文件夾以緩存圖像。
* VS 2010 / .NET 4支持*
為了在.NET 4中正常工作,您需要使用適當?shù)墓€令牌將圖表引用更改為版本4.0.0.0。
同樣,圖表控件現(xiàn)在可以生成指向當前請求路徑而不是請求路由的url。對我來說,這意味著所有圖表請求均導致404錯誤,因為/{Controller}/ChartImg.axd等價物被路線阻止了。為了解決這個問題,我添加了額外的IgnoreRoute調(diào)用來覆蓋我的用法-一個更通用的解決方案會更好:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("ChartImg.axd/{*pathInfo}");
routes.IgnoreRoute("{controller}/ChartImg.axd/{*pathInfo}");
routes.IgnoreRoute("{controller}/{action}/ChartImg.axd/{*pathInfo}");
...
- 3 回答
- 0 關注
- 488 瀏覽
添加回答
舉報