C# 使用zedgraph绘制 柱状图

来源:互联网 发布:bloom filter python 编辑:程序博客网 时间:2024/04/30 04:22

 

1.下载zedGraph控件:http://download.csdn.net/detail/allisnew/322251

2.添加引用 zedGraph.dll

3.在工具箱的最后面添加常规控件--浏览--选择zedgraph.dll

4.编写如下初始化代码(也可以直接拖一个控件):

1.private ZedGraphControl zedGraphControl1 =new ZedGraphControl();  2.  3.this.zedGraphControl1.Location =new System.Drawing.Point(36, 48);  4.            this.zedGraphControl1.Name ="zedGraphControl1";  5.            this.zedGraphControl1.ScrollGrace = 0; 6.            this.zedGraphControl1.ScrollMaxX = 0; 7.            this.zedGraphControl1.ScrollMaxY = 0; 8.            this.zedGraphControl1.ScrollMaxY2 = 0; 9.            this.zedGraphControl1.ScrollMinX = 0; 10.            this.zedGraphControl1.ScrollMinY = 0; 11.            this.zedGraphControl1.ScrollMinY2 = 0; 12.            this.zedGraphControl1.Size =new System.Drawing.Size(427, 247);  13.            this.zedGraphControl1.TabIndex = 0; 14.            this.Controls.Add(zedGraphControl1); 


5.编写绘图代码:

 

1.private void ShowChart() 2.       {  3.           zedGraphControl1.GraphPane.CurveList.Clear();  4.           zedGraphControl1.GraphPane.GraphObjList.Clear();  5.           // clearing not teste 6.  7.  8.           GraphPane myPane = zedGraphControl1.GraphPane;  9.  10.  11.           myPane.Title.Text = "消费者学历统计"; //设计图表的标题  12.           myPane.XAxis.Title.Text = "学历类型";//X轴标题  13.           myPane.YAxis.Title.Text = "人数";//Y轴标题  14.           //      myPane.XAxis.Type = ZedGraph.AxisType.Date; 15.           // Date wont work in our case 16.  17.  18.           PointPairList PPLa = new PointPairList(); 19.           PointPairList PPLb = new PointPairList(); 20.           PointPairList PPLc = new PointPairList(); 21.           PointPairList PPLd = new PointPairList(); 22.  23.  24.           for (int i = 1; i < 2; i++) 25.           {  26.               PPLa.Add(i, i + 3);  27.               PPLb.Add(i + 1, i + 4);  28.               PPLc.Add(i + 2, i + 5);  29.               PPLd.Add(i + 3, i + 6);  30.           }  31.  32.  33.           // dragged drawing baritems out of forloop 34.           BarItem myBara = myPane.AddBar("A", PPLa, Color.Red); 35.           BarItem myBarb = myPane.AddBar("B", PPLb, Color.Blue); 36.           BarItem myBarc = myPane.AddBar("C", PPLc, Color.Gray); 37.           BarItem myBard = myPane.AddBar("D", PPLd, Color.Black); 38.  39.  40.           zedGraphControl1.AxisChange();  41.           zedGraphControl1.Refresh();//这句话非常重要,否则不会立即显示 42.       }  


如需要横坐标显示文字:

1.private void ShowChart() 2.       {  3.           zedGraphControl1.GraphPane.CurveList.Clear();  4.           zedGraphControl1.GraphPane.GraphObjList.Clear();  5.           // clearing not teste 6.  7.  8.           GraphPane myPane = zedGraphControl1.GraphPane;  9.           // 画图面版标题  10.           myPane.Title.Text = "收入统计"; 11.           // 画图面版X标题  12.           myPane.XAxis.Title.Text = "区域"; 13.  14.  15.           myPane.XAxis.Scale.Min = 0;  16.           //初始化数据  17.           PointPairList list = new PointPairList(); 18.           PointPairList list2 = new PointPairList(); 19.           PointPairList list3 = new PointPairList(); 20.  21.  22.           for (int i = 0; i < 5; i++)////这里的数量要和lable的一致,比如横坐标显示了5个lable,这里就要给5个 23.           {  24.               list.Add(i, i+1);  25.               list2.Add(i, i + 2);  26.               list3.Add(i, i + 3);  27.           }  28.             29.  30.  31.           // 画图面版Y标题  32.           myPane.YAxis.Title.Text = "销售情况"; 33.           //柱的画笔  34.           //    public BarItem AddBar(string 名称, IPointList 数据, Color 颜色); 35.  36.           BarItem myCurve = myPane.AddBar("收入1", list, Color.Blue); 37.           BarItem myCurve1 = myPane.AddBar("收入2", list2, Color.Purple); 38.           BarItem myCurve2 = myPane.AddBar("收入3", list3, Color.YellowGreen); 39.  40.           //myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);//渐变 41.          // BarItem myCurve2 = myPane.AddBar("买农药", list2, Color.Red); 42.          // myCurve2.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red); 43.         //  BarItem myCurve3 = myPane.AddBar("买化肥", list3, Color.Green); 44.          // myCurve3.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green); 45.  46.  47.           //myPane.XAxis.MajorTic.IsBetweenLabels = true; 48.           //XAxis标注  49.           string[] labels = {"产品1", "产品2","产品3", "产品4","产品5" };  50.           myPane.XAxis.Scale.TextLabels = labels;  51.           myPane.XAxis.Type = AxisType.Text;  52.           //图区以外的颜色  53.          // myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f); 54.           //背景颜色  55.          // myPane.Chart.Fill = new Fill(Color.Red, Color.LightGoldenrodYellow, 45.0f); 56.  57.  58.           zedGraphControl1.AxisChange();  59.           zedGraphControl1.Refresh();  60.       }  


 

0 0