1. 程式人生 > >ZedGraph圖表控制元件 X軸顯示時間 C#

ZedGraph圖表控制元件 X軸顯示時間 C#

  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();
        }