ZedGraph图表控件 X轴显示时间 C#

这段代码展示了如何在C#中利用ZedGraph控件创建一个带有时间轴的图表。首先,清除了现有控件,然后设置了图表的标题和轴标签。接着,从dataGridView3中提取数据,格式化日期并存储到数组中。通过设置X轴类型为Text,可以显示日期。最后,添加曲线和符号,并填充颜色,使图表更具视觉效果。通过对X轴Scale.TextLabels的设置,实现了时间数据在X轴上的显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  private void CreateGraph(ZedGraphControl zgc)
        {
            //zgc.Controls.Clear();
            GraphPane myPane = zgc.GraphPane;

            //添加新的控件前先将面板上已经存在的控件清除。

            myPane.CurveList.Clear();
            myPane.GraphObjList.Clear();              // Set the titles and axis labels
            myPane.Title.Text = "指数趋势";
            myPane.XAxis.Title.Text = "序号";
            myPane.YAxis.Title.Text = "指数";

            DateTime dt;
            String[] szx = new String[dataGridView3.RowCount];//X轴的日期数组
            Double[] szy = new Double[dataGridView3.RowCount];//Y轴的数据数组

            for (int i = 0; i < dataGridView3.RowCount; i++)
            {
                //list.Add(i, Convert.ToDouble(dataGridView3[2, i].Value.ToString()));
                dt = (DateTime)dataGridView3[3,i].Value;
                szx[i] = dt.ToString("MM-dd hh:mm:ss");//按照02-29 18:00:00显示
                szy[i] = Convert.ToDouble(dataGridView3[2, i].Value.ToString());
            }

            myPane.XAxis.Type = AxisType.Text;
           
            // Generate a blue curve with circle symbols, and "My Curve 2" in the legend

            //添加X轴、Y轴的数据到面板上,但是由于AddCurve方法没有匹配的参数,所以第二个X轴想显示         //时间数据时,要先设置为null,然后在下面单独添加。
            LineItem myCurve = myPane.AddCurve("指数", null, szy, Color.Blue, SymbolType.Circle);

            myPane.XAxis.Scale.TextLabels = szx;//添加日期到X轴

            // Fill the area under the curve with a white-red gradient at 45 degrees
            myCurve.Line.Fill = new Fill(Color.White, Color.White, 45F);
            // Make the symbols opaque by filling them with white
            myCurve.Symbol.Fill = new Fill(Color.White);


            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);

            // Calculate the Axis Scale Ranges
            zgc.AxisChange();
            zgc.Refresh();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值