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繪製 柱狀圖的詳解的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!