日期:2014-05-20  浏览次数:20771 次

C#新手求解 ZedGraph实现曲线问题
想做一个温度曲线,数据库有了,但是怎么实现把数据库中的值取出来变成曲线呢?

C# code


private void InitializeComponent()
    {
       this.ZedGraphWeb1.RenderGraph += new ZedGraphWebControlEventHandler(this.OnRenderGraph1);
       this.Load += new System.EventHandler(this.Page_Load);
     }


private void OnRenderGraph1(ZedGraphWeb zgw, Graphics g, MasterPane masterPane)
    {
        GraphPane myPane = masterPane[0];

        myPane.YAxis.Scale.Max = 60;
        myPane.YAxis.Scale.Min = 0;

        myPane.XAxis.MajorGrid.IsVisible = true;
        myPane.YAxis.MajorGrid.IsVisible = true;
        myPane.XAxis.MajorGrid.Color = Color.LightGray;
        myPane.YAxis.MajorGrid.Color = Color.LightGray;
        myPane.XAxis.MajorGrid.DashOff = 0;
        myPane.YAxis.MajorGrid.DashOff = 0;

        myPane.YAxis.Scale.BaseTic = -40;
   
        myPane.XAxis.Scale.MajorStep = 10;
        myPane.XAxis.Scale.MinorStep = 2.5;
    
        myPane.YAxis.Scale.MajorStep = 10;
        myPane.YAxis.Scale.MinorStep = 2.5;

        myPane.YAxis.Scale.IsVisible = true;

        myPane.Title.Text = "温度曲线图";
        myPane.XAxis.Title.Text = "时间";
        myPane.YAxis.Title.Text = "温度";

        DataSourcePointList dspl = new DataSourcePointList();

        dspl.DataSource = GetData().Tables[0];
 
        dspl.XDataMember = "Timestamp";

        dspl.YDataMember = "Air_Temperature";

        dspl.ZDataMember = null;

        LineItem myCurve2 = myPane.AddCurve("温度℃", dspl, Color.LightGray, SymbolType.Plus);

        myPane.XAxis.Type = AxisType.Date;
        myPane.XAxis.CrossAuto = true;

        myPane.Chart.Fill = new Fill(Color.Black, Color.Black, 45F);
        masterPane.AxisChange(g);
   }


private void BindData()
    {
        SqlConnection sqlcon = new SqlConnection();
        try
        {
            sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand();
            sqlcom.Connection = sqlcon;
            sqlcom.CommandText = "select top 10  Air_Temperature as 气温 from Weather  order by Timestamp desc"; 
            sqlcom.CommandType = CommandType.Text;
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
            ds = new DataSet();
            sqlda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            sqlcon.Close();
        }
    }


protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
         BindData();
        }
        InitializeComponent();
    }      




------解决方案--------------------

程序似乎没问题。
OnRenderGraph1()方法能触发吗?可以放在intializecomponet中调用一下。