ASP.NET MVC中圖表控件的使用方法
微軟發(fā)布了一個(gè)強(qiáng)大的ASP.NET的圖表控件,支持豐富的圖表選項(xiàng)設(shè)置-包括列,點(diǎn),泡沫,餅圖,圓環(huán)圖,金字塔,漏斗,盒形圖,面積,范圍,AJAX的互動(dòng),以及更多。Microsoft圖表控件示例項(xiàng)目包括ASP.NET頁(yè)的圖表樣本超過(guò)200個(gè)。在這篇文章中,我將展示如何在ASP.NET MVC中使用圖表控件。
這里介紹一個(gè)非常簡(jiǎn)單的項(xiàng)目,顯示了一個(gè)類的結(jié)果比較。兩個(gè)字段 - ID(這是唯一的一個(gè)學(xué)生)和GPA(平均成績(jī)) - 代表一個(gè)特定的學(xué)生的結(jié)果。各種圖表結(jié)果顯示,學(xué)生的結(jié)果進(jìn)行比較。我希望把重點(diǎn)放在如何輕松地顯示相同的數(shù)據(jù)不同的結(jié)果。在這個(gè)項(xiàng)目中,您可以添加,編輯和刪除學(xué)生的成績(jī),并動(dòng)態(tài)顯示的變化。
要運(yùn)行該項(xiàng)目,必須安裝以下微軟NET Framework 3.5的Microsoft圖表控件組件。
代碼開(kāi)始,你將需要引用的System.Web.UI.DataVisualization程序集 。
一旦你這樣做,這是相當(dāng)多的簡(jiǎn)單圖表添加到視圖頁(yè)面。
<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)在頁(yè)面。
這里介紹的項(xiàng)目是ASP.NET MVC的圖表控件的一個(gè)小demo示例,最終展示如下:
以上就是告訴大家如何使用ASP.NET MVC中的圖表控件,希望對(duì)大家的學(xué)習(xí)有所幫助。
- .Net創(chuàng)建Excel文件(插入數(shù)據(jù)、修改格式、生成圖表)的方法
- asp.net中一款極為簡(jiǎn)單實(shí)用的圖表插件(jquery)
- ASP.NET 統(tǒng)計(jì)圖表控件小結(jié)
- asp.net微軟圖表控件使用示例代碼分享
- ASP.NET中實(shí)時(shí)圖表的實(shí)現(xiàn)方法分享
- HighCharts圖表控件在ASP.NET WebForm中的使用總結(jié)(全)
- jquery jqPlot API 中文使用教程(非常強(qiáng)大的圖表工具)
- javascript實(shí)現(xiàn)的柱狀統(tǒng)計(jì)圖表
- JavaScript可視化圖表庫(kù)D3.js API中文參考
- ASP.NET中制作各種3D圖表的方法
相關(guān)文章
詳解在Windows下如何使用AspNetCore Api 和consul
這篇文章主要介紹了詳解在Windows下如何使用AspNetCore Api 和consul,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06解決asp.net mvc UpdateModel更新對(duì)象后出現(xiàn)null問(wèn)題的方法
這篇文章主要介紹了解決asp.net mvc UpdateModel 更新對(duì)象后出現(xiàn)null問(wèn)題的方法,需要的朋友可以參考下2015-11-11動(dòng)態(tài)加載用戶控件至DataList并為用戶控件賦值實(shí)例演示
本文借用使用通用的新聞例子演示動(dòng)態(tài)加載用戶控件至DataList并為用戶控件賦值,感興趣的朋友可以了解下2013-01-01asp.net 頁(yè)面之間傳遞參數(shù)的幾種方法
因?yàn)樵陧?xiàng)目中需要在兩個(gè)頁(yè)面之間傳遞一些參數(shù),所以總結(jié)出以下幾個(gè)傳遞參數(shù)的方法2009-06-06ASP.NET 根據(jù)漢字獲取漢字拼音的首字母(含多音字)
本文分享了一個(gè)函數(shù),這個(gè)函數(shù)可以根據(jù)漢字的字符串獲取其拼音的首字母,以便我們?cè)趯?shí)際開(kāi)發(fā)中使用。2016-04-04ASP.NET MVC實(shí)現(xiàn)圖片上傳、圖片預(yù)覽顯示
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC實(shí)現(xiàn)圖片上傳、圖片預(yù)覽顯示的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法
這篇文章介紹了ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03.net MVC使用IPrincipal進(jìn)行Form登錄即權(quán)限驗(yàn)證(3)
這篇文章主要為大家詳細(xì)介紹了.net MVC使用IPrincipal進(jìn)行Form登錄即權(quán)限驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04ASP.NET MVC結(jié)合JavaScript登錄、校驗(yàn)和加密
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC結(jié)合JavaScript登錄、校驗(yàn)和加密的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08