C# 使用zedgraph绘制 柱状图的详解(北极星小王子的博客)
生活随笔
收集整理的這篇文章主要介紹了
C# 使用zedgraph绘制 柱状图的详解(北极星小王子的博客)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.下載zedGraph控件:http://download.csdn.net/detail/allisnew/322251
2.添加引用 zedGraph.dll
3.在工具箱的最后面添加常規控件--瀏覽--選擇zedgraph.dll
4.編寫如下初始化代碼(也可以直接拖一個控件):
private ZedGraphControl zedGraphControl1 = new ZedGraphControl();
this.zedGraphControl1.Location = new System.Drawing.Point(36, 48);
this.zedGraphControl1.Name = "zedGraphControl1";
this.zedGraphControl1.ScrollGrace = 0;
this.zedGraphControl1.ScrollMaxX = 0;
this.zedGraphControl1.ScrollMaxY = 0;
this.zedGraphControl1.ScrollMaxY2 = 0;
this.zedGraphControl1.ScrollMinX = 0;
this.zedGraphControl1.ScrollMinY = 0;
this.zedGraphControl1.ScrollMinY2 = 0;
this.zedGraphControl1.Size = new System.Drawing.Size(427, 247);
this.zedGraphControl1.TabIndex = 0;
this.Controls.Add(zedGraphControl1);
登錄后復制
5.編寫繪圖代碼:
private void ShowChart()
{
zedGraphControl1.GraphPane.CurveList.Clear();
zedGraphControl1.GraphPane.GraphObjList.Clear();
// clearing not teste
GraphPane myPane = zedGraphControl1.GraphPane;
myPane.Title.Text = "消費者學歷統計"; //設計圖表的標題
myPane.XAxis.Title.Text = "學歷類型"; //X軸標題
myPane.YAxis.Title.Text = "人數"; //Y軸標題
// myPane.XAxis.Type = ZedGraph.AxisType.Date;
// Date wont work in our case
PointPairList PPLa = new PointPairList();
PointPairList PPLb = new PointPairList();
PointPairList PPLc = new PointPairList();
PointPairList PPLd = new PointPairList();
for (int i = 1; i < 2; i++)
{
PPLa.Add(i, i + 3);
PPLb.Add(i + 1, i + 4);
PPLc.Add(i + 2, i + 5);
PPLd.Add(i + 3, i + 6);
}
// dragged drawing baritems out of forloop
BarItem myBara = myPane.AddBar("A", PPLa, Color.Red);
BarItem myBarb = myPane.AddBar("B", PPLb, Color.Blue);
BarItem myBarc = myPane.AddBar("C", PPLc, Color.Gray);
BarItem myBard = myPane.AddBar("D", PPLd, Color.Black);
zedGraphControl1.AxisChange();
zedGraphControl1.Refresh();//這句話非常重要,否則不會立即顯示
}
登錄后復制
如需要橫坐標顯示文字:
private void ShowChart()
{
zedGraphControl1.GraphPane.CurveList.Clear();
zedGraphControl1.GraphPane.GraphObjList.Clear();
// clearing not teste
GraphPane myPane = zedGraphControl1.GraphPane;
// 畫圖面版標題
myPane.Title.Text = "收入統計";
// 畫圖面版X標題
myPane.XAxis.Title.Text = "區域";
myPane.XAxis.Scale.Min = 0;
//初始化數據
PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();
for (int i = 0; i < 5; i++)////這里的數量要和lable的一致,比如橫坐標顯示了5個lable,這里就要給5個
{
list.Add(i, i+1);
list2.Add(i, i + 2);
list3.Add(i, i + 3);
}
// 畫圖面版Y標題
myPane.YAxis.Title.Text = "銷售情況";
//柱的畫筆
// public BarItem AddBar(string 名稱, IPointList 數據, Color 顏色);
BarItem myCurve = myPane.AddBar("收入1", list, Color.Blue);
BarItem myCurve1 = myPane.AddBar("收入2", list2, Color.Purple);
BarItem myCurve2 = myPane.AddBar("收入3", list3, Color.YellowGreen);
//myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);//漸變
// BarItem myCurve2 = myPane.AddBar("買農藥", list2, Color.Red);
// myCurve2.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
// BarItem myCurve3 = myPane.AddBar("買化肥", list3, Color.Green);
// myCurve3.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green);
//myPane.XAxis.MajorTic.IsBetweenLabels = true;
//XAxis標注
string[] labels = { "產品1", "產品2", "產品3", "產品4", "產品5" };
myPane.XAxis.Scale.TextLabels = labels;
myPane.XAxis.Type = AxisType.Text;
//圖區以外的顏色
// myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
//背景顏色
// myPane.Chart.Fill = new Fill(Color.Red, Color.LightGoldenrodYellow, 45.0f);
zedGraphControl1.AxisChange();
zedGraphControl1.Refresh();
}
登錄后復制
以上就是C# 使用zedgraph繪制 柱狀圖的詳解的內容,更多相關內容請關注PHP中文網(www.php.cn)!
總結
以上是生活随笔為你收集整理的C# 使用zedgraph绘制 柱状图的详解(北极星小王子的博客)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绝了!电容这样理解真的简单!
- 下一篇: 什么是数字媒体(一.什么是逆向分析)