C#窗体应用中使用ZedGraph曲线插件绘制图表
生活随笔
收集整理的這篇文章主要介紹了
C#窗体应用中使用ZedGraph曲线插件绘制图表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
ZedGraph
ZedGraph 是一個開源的.NET圖表類庫, 全部代碼都是用C#開發的。它可以利用任意的數據集合創建2D的線性和柱形圖表。
控件下載
https://download.csdn.net/download/badao_liumang_qizhi/11578445
英文手冊下載
https://download.csdn.net/download/badao_liumang_qizhi/11578491
示例源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11578558
效果
?
實現
VS2013新建項目-Windows窗體程序。
然后在窗體設計頁面,打開工具箱(ctrl+alt+x)。
將上面下載的控件(以.dll結尾的)拖拽到工具箱中。
?
然后調整其大小使其充滿整個form。
打開Form1.cs進入代碼的編寫。
在Form初始化后編寫createPane,傳遞的參數是ZedGraph控件對象。
?public Form1(){InitializeComponent();//Form1初始化后創建設置控件的方法并將當前ZedGraph控件傳遞createPane(zedGraphControl1);}然后在方法createPane中。
//需要引入命名空間--using ZedGraph;public void createPane(ZedGraphControl zgc){GraphPane myPane = zgc.GraphPane;//設置圖表標題 和 x y 軸標題myPane.Title.Text = "霸道測試標題";myPane.XAxis.Title.Text = "X軸標題";myPane.YAxis.Title.Text = "Y軸標題";//更改標題的字體FontSpec myFont = new FontSpec("Arial",20,Color.Red,false,false,false);myPane.Title.FontSpec = myFont;myPane.XAxis.Title.FontSpec = myFont;myPane.YAxis.Title.FontSpec = myFont;// 造一些數據,PointPairList里有數據對x,y的數組Random y = new Random();PointPairList list1 = new PointPairList();for (int i = 0; i < 36; i++){double x = i;//double y1 = 1.5 + Math.Sin((double)i * 0.2);double y1 = y.NextDouble() * 1000;list1.Add(x, y1); //添加一組數據}// 用list1生產一條曲線,標注是“曲線1”LineItem myCurve = myPane.AddCurve("曲線1", list1, Color.Red, SymbolType.Star);//填充圖表顏色myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);//以上生成的圖標X軸為數字,下面將轉換為日期的文本string[] labels = new string[36];for (int i = 0; i < 36; i++){labels[i] = System.DateTime.Now.AddDays(i).ToShortDateString();}myPane.XAxis.Scale.TextLabels = labels; //X軸文本取值myPane.XAxis.Type = AxisType.Text;?? //X軸類型//畫到zedGraphControl1控件中,此句必加zgc.AxisChange();//在數據變化時繪圖//更新圖表zedGraphControl1.Invalidate();//重繪控件Refresh();}詳細見注釋,運行項目。
?
總結
以上是生活随笔為你收集整理的C#窗体应用中使用ZedGraph曲线插件绘制图表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2013中提示:没有可放置在工具箱上
- 下一篇: C#中提示:System.Runtime