ZedGraph实时柱图(winform)

    最近得到一个需求要做一个实时票数显示的程序,要求有一个动态显示选手票数的柱图.本想用.net的GDI+AJAX来做,可是搞了一个上午,感觉很麻烦,至CSDN上面问看有没有什么好的方法,告诉我说可以用ZedGraph,于是谷歌了一下,从sourceforge上把它down下来,开始研究...

    建一个winform项目,将ZedGraph.dll添加到bin里,这样就可以调用它的方法了.打开winform设计界面,从工具箱上拖一个zedGraphControl控件进来(注:这里须要你先将zedgraph的控件都添加进来的哦).我们先声时一个静态的全局变量和一个全局方法:

private static int num = 1;

public double[] getArrvalues()
{
            double[] arr ={ 100, 135, 115, 125, 75, 120 };

            arr[0] += num;
            arr[1] += num * 2;
            arr[2] += num * 3;
            arr[3] += num * 4;
            arr[4] += num * 5;
            arr[5] += num * 6;
            num++;
            return arr;
 }

然后把下面这段代码加到Form_Load里面:

            this.zedGraphControl1.GraphPane.Title.Text = "快乐男声实时投票显示";
            this.zedGraphControl1.GraphPane.XAxis.Title.Text = "选手";
            this.zedGraphControl1.GraphPane.YAxis.Title.Text = "票数";
            this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;

            double[] arrValues = getArrvalues();
            string[] xtitles = { "王栎鑫", "吉杰", "魏晨", "苏醒", "张杰", "陈楚生" };

            BarItem myBar = this.zedGraphControl1.GraphPane.AddBar("票数", null, arrValues, Color.Red);
            myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
            myBar.Bar.Border.Color = Color.Transparent;
            this.zedGraphControl1.GraphPane.XAxis.Scale.TextLabels = xtitles;
            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 10f;
            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.Blue;
            this.zedGraphControl1.AxisChange();
            this.zedGraphControl1.GraphPane.XAxis.Type = AxisType.Text;

运行,这时你可以看到静态的柱图显示.

那如何动态显示数据呢?在Winform里面,我们可用Timer控件,也可以用多线程,这里我用的是Timer控件.

从工具箱里面拖一个Tmier控件到Form窗体,然后将其Enable置为true,在其timer1_Tick事件里面加入下面代码(注:这里大家一般是实时读取数据库作为柱图的数据源):

            double[] arrValues = getArrvalues();
            // Shift the text items up by 5 user scale units above the bars
            string[] xtitles = { "王栎鑫", "吉杰", "魏晨", "苏醒", "张杰", "陈楚生" };
            const float shift = 5;

            this.zedGraphControl1.GraphPane.YAxis.Scale.MaxAuto = false;
            this.zedGraphControl1.GraphPane.YAxis.Scale.Max = 10000;
            this.zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;
            this.zedGraphControl1.GraphPane.YAxis.Scale.MinorStep = 1;
            this.zedGraphControl1.GraphPane.YAxis.Color = Color.Transparent;
            this.zedGraphControl1.GraphPane.Chart.Fill = new Fill(Color.White, Color.Green, 45.0F);
            this.zedGraphControl1.GraphPane.YAxis.IsVisible = false;
            for (int i = 0; i < arrValues.Length; i++)
            {

                // format the label string to have 1 decimal place
                string lab = arrValues[i].ToString();
                // create the text item (assumes the x axis is ordinal or text)
                // for negative bars, the label appears just above the zero value
                TextObj text = new TextObj(lab, (float)(i + 1), (float)(arrValues[i] < 0 ? 0.0 : arrValues[i]) + shift);
                // tell Zedgraph to use user scale units for locating the TextObj
                text.Location.CoordinateFrame = CoordType.AxisXYScale;
                // AlignH the left-center of the text to the specified point
                text.Location.AlignH = AlignH.Center;
                text.Location.AlignV = AlignV.Center;

                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                // rotate the text 90 degrees
                text.FontSpec.Angle = 0;

                // add the TextObj to the list
                this.zedGraphControl1.GraphPane.GraphObjList.Add(text);
            }
            BarItem myBar = this.zedGraphControl1.GraphPane.AddBar(null, null, arrValues, Color.Red);

            myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
            myBar.Bar.Border.Color = Color.Transparent;

            this.zedGraphControl1.GraphPane.XAxis.Scale.TextLabels = xtitles;
            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 4.5f;
            this.zedGraphControl1.GraphPane.BarSettings.Type = BarType.Overlay;
            this.zedGraphControl1.GraphPane.XAxis.Type = AxisType.Text;
            int num = 0;
            num = this.zedGraphControl1.GraphPane.GraphObjList.Count;
            this.zedGraphControl1.GraphPane.GraphObjList.RemoveRange(0, num - 6);

            this.zedGraphControl1.AxisChange();

            this.zedGraphControl1.Refresh();

运行后,你就可以看到动态显示的柱图了,是不是很简单?

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值