zedgraph繪圖(修改)
阿新 • • 發佈:2017-05-26
pri turn dig nat zorder 錯誤信息 span .aspx col
轉自原文 zedgraph繪圖(修改)
首先先下載 zedgraph.dll和zedgraph.web.DLL兩個文件
添加項目並引用
首先添加一個用戶控件 WebUserDrawGrap.ascx
html頁面:
1 2 3 |
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserDrawGrap.ascx.cs" Inherits="CraigBlog.Net.zedGraph.WebUserDrawGrap" %>
<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %> < ZGW:ZEDGRAPHWEB id="zedGraphControl" runat="server" width="600" Height="375" RenderMode="ImageTag"/>
|
代碼 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI;View Codeusing System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Drawing; using ZedGraph; using ZedGraph.Web; using System.Collections.Generic; namespace CraigBlog.Net.zedGraph { /// <summary> /// 顯示統計圖形類型/// </summary> public enum AnalyticsType { Line, //折線圖 Bar, //柱狀圖 Pie //餅圖 }; public partial class WebUserDrawGrap : System.Web.UI.UserControl { private List<Color> defaultColors = new List<Color>();/// 默認顏色種類 private int Count;/// 統計的個數 public string Title;/// 統計圖的名稱 public string XAxisTitle;///橫軸的名稱(餅圖不需要) public string YAxisTitle;/// 縱軸的名稱(餅圖不需要) public AnalyticsType Type;/// 顯示的曲線類型:Line,Bar,Pie public List<PointPairList> DataSource = new List<PointPairList>();/// 折線圖和柱狀圖的數據源 public List<double> ScaleData = new List<double>();/// 餅圖的數據源 public List<Color> Colors = new List<Color>();/// 各段數據的顏色 public List<string> NameList = new List<string>();/// 各段數據的名稱 public List<string> LabelList = new List<string>(); /// 用於柱狀圖,每個圓柱體表示的含義 public List<double> ValueDouble = new List<double>();//用於定義柱形表示的值 private void InitDefaultColors() { defaultColors.Add(Color.Red); defaultColors.Add(Color.Green); defaultColors.Add(Color.Blue); defaultColors.Add(Color.Yellow); defaultColors.Add(Color.YellowGreen); defaultColors.Add(Color.Brown); defaultColors.Add(Color.Aqua); defaultColors.Add(Color.Cyan); defaultColors.Add(Color.DarkSeaGreen); defaultColors.Add(Color.Indigo); } /// <summary> /// 如果屬性為空則初始化屬性數據 /// </summary> private void InitProperty() { InitDefaultColors(); if (string.IsNullOrEmpty(Title)) { Title = "未命名統計圖"; } if (string.IsNullOrEmpty(XAxisTitle)) { XAxisTitle = "橫軸"; } if (string.IsNullOrEmpty(YAxisTitle)) { YAxisTitle = "縱軸"; } if (Type == AnalyticsType.Pie) { Count = ScaleData.Count; } else { Count = DataSource.Count; } if (Colors.Count == 0 || Colors.Count != Count) { Random r = new Random(); int tempIndex = 0; List<int> tempIndexList = new List<int>(); for (int i = 0; i < Count; i++) { tempIndex = r.Next(defaultColors.Count); if (tempIndexList.Contains(tempIndex)) { i--; } else { tempIndexList.Add(tempIndex); Colors.Add(defaultColors[tempIndex]); } } } if (NameList.Count == 0) { if (Type == AnalyticsType.Bar) { for (int i = 0; i < DataSource[0].Count; i++) { NameList.Add("第" + i.ToString() + "組"); } } else { for (int i = 0; i < Count; i++) { NameList.Add("第" + i.ToString() + "組"); } } } if (LabelList.Count == 0) { for (int i = 0; i < Count; i++) { LabelList.Add("含義" + i.ToString()); } } } protected void Page_Load(object sender, EventArgs e) { zedGraphControl.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(zedGraphControl_RenderGraph); } /**/ /// <summary> /// 畫圖 /// </summary> /// <param name="webObject"></param> /// <param name="g"></param> /// <param name="pane"></param> private void zedGraphControl_RenderGraph(System.Drawing.Graphics g, ZedGraph.MasterPane pane) { InitProperty(); GraphPane myPane = pane[0]; myPane.Title.Text = Title; myPane.XAxis.Title.Text = XAxisTitle; myPane.YAxis.Title.Text = YAxisTitle; switch (Type) { case AnalyticsType.Line: DrawLine(myPane); break; case AnalyticsType.Bar: DrawBar(myPane); break; case AnalyticsType.Pie: DrawPie(myPane); break; default: break; } pane.AxisChange(g); System.IO.MemoryStream st = new System.IO.MemoryStream(); myPane.GetImage().Save(st, System.Drawing.Imaging.ImageFormat.Jpeg);//得到圖片流 此處是得到該圖片的圖片流-可以將該流擴展到excel表格中(註意:需在項目目錄中新建一個存放圖片的文件夾ZedGraphImages) } #region Draw /// <summary> /// 畫折線圖 /// </summary> /// <param name="graphPane"></param> private void DrawLine(GraphPane graphPane) { for (int i = 0; i < Count; i++) { graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None); } CreateBarLabels(graphPane, "f0", ValueDouble); graphPane.XAxis.Scale.TextLabels = NameList.ToArray(); graphPane.XAxis.Type = AxisType.Text; graphPane.YAxis.Scale.MajorStep = 20; graphPane.YAxis.MinorGrid.IsVisible = true; graphPane.YAxis.MinorGrid.DashOff = 0; graphPane.YAxis.Title.FontSpec.Angle = 90; graphPane.YAxis.Title.FontSpec.FontColor = defaultColors[0]; } /// <summary> /// 畫柱狀圖 /// </summary> /// <param name="graphPane"></param> private void DrawBar(GraphPane graphPane) { for (int i = 0; i < Count; i++) { graphPane.AddBar(LabelList[i], DataSource[i], Colors[i]).Bar.Fill = new Fill(Colors[i], Color.White, Colors[i]); } CreateBarLabels(graphPane, "f0", ValueDouble); graphPane.XAxis.MajorTic.IsBetweenLabels = true; string[] labels = NameList.ToArray(); graphPane.XAxis.Scale.TextLabels = labels;//x軸的顯示的文本集合 graphPane.XAxis.Type = AxisType.Text; graphPane.XAxis.MajorGrid.IsVisible = false;//x軸柵格線是否可見 graphPane.XAxis.MajorGrid.DashOff = 0;//柵格線的效果,同下 graphPane.YAxis.Scale.BaseTic = 0;//刻度的初始開始值 graphPane.YAxis.Scale.MajorStep = 20;//設置刻度的步進值 graphPane.YAxis.MajorGrid.IsVisible = true; //柵格線是否可見 graphPane.YAxis.MajorGrid.DashOff = 0;//設置的柵格線的效果。0表示為實線 graphPane.YAxis.MajorGrid.PenWidth = 1;//設置柵格線的線條的寬度 graphPane.YAxis.Title.FontSpec.Angle = 90;//設置標題的顯示,順時針旋轉90度 graphPane.Fill = new Fill(Color.White, Color.FromArgb(50, Color.Beige), 45.0f); graphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); } /// <summary> /// 畫餅圖 /// </summary> /// <param name="graphPane"></param> private void DrawPie(GraphPane graphPane) { graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f); graphPane.YAxis.IsVisible = false; graphPane.XAxis.IsVisible = false; graphPane.Chart.Fill.Type = FillType.None; graphPane.Legend.Position = LegendPos.Float; graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top); graphPane.Legend.FontSpec.Size = 16f; graphPane.Legend.IsHStack = false; for (int i = 0; i < Count; i++) { PieItem pieitme = graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.Wheat, 45f, 0, NameList[i] + ScaleData[i]); pieitme.LabelType = PieLabelType.Percent;//設置顯示的類型、Percent(百分比) } } /// <summary> /// 如果系統出錯,顯示錯誤信息 /// </summary> /// <param name="graphPane"></param> /// <param name="message"></param> private void DrawMessage(GraphPane graphPane, string message) { TextObj text = new TextObj(message, 200, 200); text.Text = message; graphPane.GraphObjList.Add(text); } /// <summary> /// 為柱狀圖添加標簽 /// </summary> /// <param name="graphPane"></param> /// <param name="valueFormat"></param> /// <param name="valueDouble"></param> private void CreateBarLabels(GraphPane graphPane, string valueFormat, List<double> valueDouble) { for (int j = 0; j < valueDouble.Count; j++) { PointPair pt = new PointPair(j + 1, valueDouble[j]); TextObj text = new TextObj(pt.Y.ToString(valueFormat), pt.X, pt.Y>(double)10?pt.Y-10:pt.Y, CoordType.AxisXYScale, AlignH.Left, AlignV.Center); text.ZOrder = ZOrder.A_InFront; text.FontSpec.Border.IsVisible = false; text.FontSpec.Fill.IsVisible = false; text.FontSpec.Angle = 1; //數值字體傾斜度 text.FontSpec.Size = 16; text.FontSpec.FontColor = Color.Black; text.FontSpec.IsBold = true; text.Location.CoordinateFrame = CoordType.AxisXY2Scale; text.Location.AlignH = AlignH.Center; text.Location.AlignV = AlignV.Center; graphPane.GraphObjList.Add(text); } } #endregion } }
--然後新建一個aspx頁面:ZDrawGrap.aspx
將用戶控件拖到頁面
ZDrawGrap.aspx .cs程序如下:
代碼 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections.Generic; using System.Drawing; using ZedGraph; namespace CraigBlog.Net.zedGraph { public partial class ZDrawGrap : System.Web.UI.Page { Dictionary<string, int> dic = new Dictionary<string, int>(); //創建數據源 protected void Page_Load(object sender, EventArgs e) { dic.Add("類別一", 20); dic.Add("類別二", 10); dic.Add("類別三", 25); dic.Add("類別四", 6); dic.Add("類別五", 13); dic.Add("類別六", 95); //柱狀圖 DrawBar(DrawGrap1); //餅圖 DrawPie(DrawGrap2); //曲線圖 DrawLine(DrawGrap3); } private void DrawBar(WebUserDrawGrap DrawGrap1) { string Ytitle = "用戶訪問量"; DrawGrap1.Type = AnalyticsType.Bar; DrawGrap1.Title = "用戶訪問柱狀圖"; DrawGrap1.XAxisTitle = "類別"; DrawGrap1.YAxisTitle = Ytitle; // DrawGrap1.YAxisTitle = "用\n戶\n訪\n問\n數\n量";//設置標題呈現的樣式 char[] ArrayChar = Ytitle.ToCharArray(); DrawGrap1.YAxisTitle = ForeachChar(ArrayChar); ZedGraph.PointPairList list = new ZedGraph.PointPairList(); for (int i = 0; i < dic.Count; i++) { KeyValuePair<string, int> keyPair = dic.ElementAt(i); list.Add((double)i, (double)keyPair.Value);//繪制柱形 DrawGrap1.NameList.Add(ForeachChar(keyPair.Key.ToCharArray())); DrawGrap1.ValueDouble.Add((double)keyPair.Value); } DrawGrap1.LabelList.Add("Color Items"); DrawGrap1.DataSource.Add(list); } private string ForeachChar(char[] array) { string temp = string.Empty; foreach (char item in array) { temp += item.ToString() +"\n"; } return temp; } private void DrawPie(WebUserDrawGrap DrawGrap1) { DrawGrap1.Type = AnalyticsType.Pie; DrawGrap1.Title = "用戶訪問餅圖"; for (int i = 0; i < dic.Count; i++) { KeyValuePair<string, int> keyPair = dic.ElementAt(i); DrawGrap1.ScaleData.Add((double)keyPair.Value); DrawGrap1.NameList.Add(keyPair.Key); } } private void DrawLine(WebUserDrawGrap DrawGrap1) { DrawGrap1.Type = AnalyticsType.Line; DrawGrap1.Title = "用戶訪問曲線圖"; DrawGrap1.XAxisTitle = "類別"; DrawGrap1.YAxisTitle = "用\n戶\n訪\n問\n數\n量"; //y軸標題豎著排 ZedGraph.PointPairList list = new ZedGraph.PointPairList(); for (int i = 0; i < dic.Count; i++) { KeyValuePair<string, int> keyPair = dic.ElementAt(i); list.Add(new PointPair((double)i,(double)keyPair.Value)); DrawGrap1.ValueDouble.Add((double)keyPair.Value); DrawGrap1.NameList.Add(keyPair.Key); } DrawGrap1.LabelList.Add("Color Items"); DrawGrap1.DataSource.Add(list); } } }View Code
zedgraph繪圖(修改)