轉(zhuǎn)帖|其它|編輯:郝浩|2011-10-13 14:13:21.000|閱讀 1393 次
概述:圖表ASP.NET MVC的實(shí)現(xiàn),是很容易的 。微軟發(fā)布了一個(gè)強(qiáng)大的ASP.NET 的圖表控制,支持豐富的圖表選項(xiàng)設(shè)置-包括列,點(diǎn),泡沫,餅圖,圓環(huán)圖,金字塔,漏斗,盒形圖,面積,范圍,AJAX的互動(dòng),以及更多 。 Microsoft圖表控件示例項(xiàng)目包括ASP.NET頁的圖表樣本超過200個(gè) 。在這篇文章中,我將展示如何在ASP.NET MVC中使用圖表控 件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
圖表ASP.NET MVC的實(shí)現(xiàn),是很容易的 。微軟發(fā)布了一個(gè)強(qiáng)大的ASP.NET 的圖表控制,支持豐富的圖表選項(xiàng)設(shè)置-包括列,點(diǎn),泡沫,餅圖,圓環(huán)圖,金字塔,漏斗,盒形圖,面積,范圍,AJAX的互動(dòng),以及更多 。 Microsoft圖表控件示例項(xiàng)目包括ASP.NET頁的圖表樣本超過200個(gè) 。在這篇文章中,我將展示如何在ASP.NET MVC中使用圖表控件。
我的示例項(xiàng)目是在ASP.NET MVC 2中。我 這里介紹一個(gè)非常簡單的項(xiàng)目,顯示了一個(gè)類的結(jié)果比較。兩個(gè)字段 - ID(這是唯一的一個(gè)學(xué)生)和GPA(平均成績) - 代表一個(gè)特定的學(xué)生的結(jié)果。 各種圖表結(jié)果顯示,學(xué)生的結(jié)果進(jìn)行比較。我希望把重點(diǎn)放在如何輕松地顯示相同的數(shù)據(jù)不同的結(jié)果。在這個(gè)項(xiàng)目中,您可以添加,編輯和刪除學(xué)生的成績,并動(dòng)態(tài) 顯示的變化。
要運(yùn)行該項(xiàng)目,必須安裝以下微軟NET Framework 3.5的Microsoft圖表控件組件。
代碼開始,你將需要引用的System.Web.UI.DataVisualization程序集 。
一旦你這樣做,這是相當(dāng)多的簡單圖表添加到視圖頁面。
<img src="/Chart/CreateChart?chartType=<%=System.Web.UI.DataVisualization.Charting.SeriesChartType.Column%>" alt="" />
代碼直接貼上
首先定義一個(gè)controller,提供以下方法實(shí)現(xiàn)
#region Chart Component
public FileResult CreateChart(SeriesChartType chartType)
{
IList<ResultModel> peoples = _resultService.GetResults();
Chart chart = new Chart();
chart.Width = 700;
chart.Height = 300;
chart.BackColor = Color.FromArgb(211, 223, 240);
chart.BorderlineDashStyle = ChartDashStyle.Solid;
chart.BackSecondaryColor = Color.White;
chart.BackGradientStyle = GradientStyle.TopBottom;
chart.BorderlineWidth = 1;
chart.Palette = ChartColorPalette.BrightPastel;
chart.BorderlineColor = Color.FromArgb(26, 59, 105);
chart.RenderType = RenderType.BinaryStreaming;
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.AntiAliasing = AntiAliasingStyles.All;
chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;
chart.Titles.Add(CreateTitle());
chart.Legends.Add(CreateLegend());
chart.Series.Add(CreateSeries(peoples,chartType));
chart.ChartAreas.Add(CreateChartArea());
MemoryStream ms = new MemoryStream();
chart.SaveImage(ms);
return File(ms.GetBuffer(), @"image/png");
}
[NonAction]
public Title CreateTitle()
{
Title title = new Title();
title.Text = "Result Chart";
title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
title.Font = new Font("Trebuchet MS", 14F, FontStyle.Bold);
title.ShadowOffset = 3;
title.ForeColor = Color.FromArgb(26, 59, 105);
return title;
}
[NonAction]
public Legend CreateLegend()
{
Legend legend = new Legend();
legend.Name = "Result Chart";
legend.Docking = Docking.Bottom;
legend.Alignment = StringAlignment.Center;
legend.BackColor = Color.Transparent;
legend.Font = new Font(new FontFamily("Trebuchet MS"), 9);
legend.LegendStyle = LegendStyle.Row;
return legend;
}
[NonAction]
public Series CreateSeries(IList<ResultModel>
results, SeriesChartType chartType)
{
Series seriesDetail = new Series();
seriesDetail.Name = "Result Chart";
seriesDetail.IsValueShownAsLabel = false;
seriesDetail.Color = Color.FromArgb(198, 99, 99);
seriesDetail.ChartType = chartType;
seriesDetail.BorderWidth = 2;
seriesDetail["DrawingStyle"] = "Cylinder";
seriesDetail["PieDrawingStyle"] = "SoftEdge";
DataPoint point;
foreach (ResultModel result in results)
{
point = new DataPoint();
point.AxisLabel =result.ID;
point.YValues = new double[] {double.Parse(result.GPA) };
seriesDetail.Points.Add(point);
}
seriesDetail.ChartArea = "Result Chart";
return seriesDetail;
}
[NonAction]
public ChartArea CreateChartArea()
{
ChartArea chartArea = new ChartArea();
chartArea.Name = "Result Chart";
chartArea.BackColor = Color.Transparent;
chartArea.AxisX.IsLabelAutoFit = false;
chartArea.AxisY.IsLabelAutoFit = false;
chartArea.AxisX.LabelStyle.Font = new Font("Verdana,Arial,Helvetica,sans-serif", 8F, FontStyle.Regular);
chartArea.AxisY.LabelStyle.Font = new Font("Verdana,Arial,Helvetica,sans-serif", 8F, FontStyle.Regular);
chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.Interval = 1;
return chartArea;
}
#endregion 圖表類的各種屬性,可以控制寬度,高度,邊框顏色,背景顏色,皮膚,調(diào)色板,等。最終形成圖片格式展現(xiàn)在頁面。
這里介紹的項(xiàng)目是ASP.NET MVC的圖表控件的一個(gè)小demo示例,最終展示如下:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載