C# 利用ZedGraph控件画简单折线图示例

来源:互联网 发布:手机淘宝店铺装修流程 编辑:程序博客网 时间:2024/06/05 10:58

 

  1. 下载ZedGraph
  2. 官网下载地址  http://sourceforge.net/projects/zedgraph/files/

  3. 添加 ZedGraph.dll 和ZedGraph.Web.dll的引用
  4. 在控件库中添加ZedGraph控件

    右键点击工具箱-选择项- .Net Framework组件 - 浏览 - 找到ZedGraph.dll 和ZedGraph.Web.dll添加

    zedGraphControl 控件就出现在工具箱中

  5. 线图示例程序
  6. 从工具箱中拖出一个edGraphControl 控件  (edGraphControl 1
  7. 在Form1初始化之后调用函数createPane,并将zedGraphControl1作为参数
  8.         public Form1()

            {

                InitializeComponent();

                createPane(zedGraphControl1);

            }

  9. zedGraphControl1设置函数
  10.         public void createPane(ZedGraphControl zgc)

            {

                GraphPane myPane = zgc.GraphPane;

     

                //设置图标标题和x、y轴标题

                myPane.Title.Text = "机票波动情况";

                myPane.XAxis.Title.Text = "波动日期";

                myPane.YAxis.Title.Text = "机票价格";

     

                //更改标题的字体

                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生产一条曲线,标注是“东航”

                LineItem myCurve = myPane.AddCurve("东航",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();

     

                //重绘控件

                Refresh();

            }

  11. 结果演示:
  12.  

     

    在图上点位标注数字的方法:

    http://blog.csdn.net/dengta_snowwhite/archive/2011/01/12/6131265.aspx

原创粉丝点击