C#图表控件ZedGraph使用

最近从java转到C#下开发PC端的桌面程序,之前也尝试用java GUI写桌面程序,发现java写桌面程序还是诸多不便变,虽然最后也写出来了,但是决心还是另起平台,有了一定的java基础,来学习C#还是比较容易的,难点就是各个控件的学习,这个也不是一朝一日就能使用熟练的。今天记录的主要是模拟数据实现类似动态动态折线的效果:

先来张效果图片:

使用之前,你需要在解决方案资源管理器里面的“应用”一项右击,选择添加引用,找到ZedGraph.dll,如图:

然后就是使用了,直接放代码,注释在代码里面已经很详细了:

From1.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using ZedGraph;//引用ZedGraph控件

namespace ChartDynamic
{
    public partial class Form1 : Form
    {
        PointPairList list;//添加数据
        Random ran;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GraphPane mPane = zedGraphControl1.GraphPane;//获取索引到GraphPane面板上
            ran = new Random();//随机种子
            mPane.XAxis.Title.Text = "waveLength";//X轴标题
            mPane.YAxis.Title.Text = "A/D";//Y轴标题
            mPane.Title.Text = "NIRS";//标题
            //mPane.XAxis.Scale.MaxAuto = true;
            mPane.XAxis.Type = ZedGraph.AxisType.LinearAsOrdinal;//出现图表右侧出现空白的情况....
            list = new PointPairList();//数据点
            mPane.XAxis.CrossAuto = true;//容许x轴的自动放大或缩小
            for (int i = 0; i < 300; i++)
            {
                int x = i;
                int y = ran.Next(20);
                list.Add(x, y);

            }
            LineItem mCure = mPane.AddCurve("", list, Color.Blue, SymbolType.None);
            zedGraphControl1.AxisChange();//画到zedGraphControl1控件中,此句必加
        }
        //程序的定时任务,实现界面不断的刷新,频率可以在控件的属性里面设置
        private void timer1_Tick(object sender, EventArgs e)
        {
            list.Clear();//清楚原来数据中的内容
            for (int i = 0; i < 300; i++)
            {
                int x = i;
                int y = ran.Next(20);
                list.Add(x, y);
            }
            zedGraphControl1.AxisChange();
            zedGraphControl1.Refresh();//重新刷新
        }
    }

}

 

posted @ 2015-11-23 20:52  tomi_mint  阅读(11864)  评论(2编辑  收藏  举报