您好,登錄后才能下訂單哦!
MVC中圖表控件如何在ASP.NET中使用?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
首先定義一個controller,提供以下方法實現
#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
圖表類的各種屬性,可以控制寬度,高度,邊框顏色,背景顏色,皮膚,調色板,等。最終形成圖片格式展現在頁面。
這里介紹的項目是ASP.NET MVC的圖表控件的一個小demo示例,最終展示如下:
看完上述內容,你們掌握 MVC中圖表控件如何在ASP.NET中使用的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。