使用ASP.NET CHART CONTROL添加动态图表,c#

时间:2008-12-02 19:03:21

标签: c# asp.net

我想在网页中添加动态图表。就像这样......

我从用户那里得到开始和结束日期,并为开始和结束日期之间的每个日期绘制单独的图表。

我从sql数据库获取数据并将其与图表绑定如下:

   SqlConnection UsageLogConn = new   
          SqlConnection(ConfigurationManager.ConnectionStrings["UsageConn"].ConnectionString);
                UsageLogConn.Open();//open connection

                string sql = "SELECT v.interval,dateadd(mi,(v.interval-1)*2,'" + startdate + " 00:00:00') as 'intervaltime',COUNT(Datediff(minute,'" + startdate + " 00:00:00',d.DateTime)/2) AS Total  FROM usage_internet_intervals v left outer join (select * from Usage_Internet where " + name + "  LIKE ('%" + value + "%') and DateTime BETWEEN '" + startdate + " 00:00:00' AND '" + enddate + " 23:59:59') d on v.interval = Datediff(minute,'" + startdate + " 00:00:00',d.DateTime)/2 GROUP BY v.interval,Datediff(minute,'" + startdate + " 00:00:00',d.DateTime)/2 ORDER BY Interval";

                SqlCommand cmd = new SqlCommand(sql, UsageLogConn);
                SqlDataAdapter mySQLadapter = new SqlDataAdapter(cmd);

                Chart1.DataSource = cmd;

                // set series members names for the X and Y values 
                Chart1.Series["Series 1"].XValueMember = "intervaltime";
                Chart1.Series["Series 1"].YValueMembers = "Total";
                UsageLogConn.Close();
                // data bind to the selected data source
                Chart1.DataBind();


                cmd.Dispose();

上面的代码只为一个日期添加了一个图表,我在设计视图中添加了'chart1',而没有创建动态。但我想在运行时向网页添加更多动态图表。

任何人都可以帮我吗?

我正在使用VS 2008,ASP.NET 3.5 图表lib是:using System.Web.UI.DataVisualization.Charting;

3 个答案:

答案 0 :(得分:4)

好的,所以我可能过度了,但我试着让这个非常有活力。是的,列表名称有点奇怪,但我用了另一个例子来构建它。

 protected void Page_Load(object sender, EventArgs e)
 {
   Bench[] benchList;
   FoodIntake[] foodIntakeList;
   Panel panelChartHolder;

   panelChartHolder = new Panel();
   Controls.Add(panelChartHolder);

   benchList = Bench.GetAll();
   AddNewCharts(benchList, panelChartHolder, 
     GetBenchXValue, GetBenchYValue);

   foodIntakeList = FoodIntake.GetAll();
   AddNewCharts(foodIntakeList, panelChartHolder, 
     GetFoodIntakeXValue, GetFoodIntakeYValue);
  }

好的,这第一部分很简单。创建一个面板来保存您要添加的图表,获取您希望由图表表示的列表(对于此示例,它们恰好与Linq一起使用到Sql)并调用该方法来创建图表。

  private void AddNewCharts<T>(T[] listToAdd, Panel panelToAddTo, 
     Func<T, DateTime> xMethod, Func<T, Int32> yMethod)
  {

    ChartArea mainArea;
    Chart mainChart;
    Series mainSeries;

    mainChart = new Chart();
    mainSeries = new Series("MainSeries");

    for (Int32 loopCounter = 0; loopCounter < listToAdd.Length; loopCounter++)
    {
      mainSeries.Points.AddXY(xMethod(listToAdd[loopCounter]), 
        yMethod(listToAdd[loopCounter]));
    }

    mainChart.Series.Add(mainSeries);
    mainArea = new ChartArea("MainArea");
    mainChart.ChartAreas.Add(mainArea);

    panelToAddTo.Controls.Add(mainChart);
  }

正如您所看到的,我刚刚创建了一个新图表,为其添加了一个系列,并为其添加了一个ChartArea。下一部分几乎只是循环遍历集合并将其中的每个项目添加到列表本身。它使用传入的委托方法(Func)来获取X和Y值。

最后一部分保存了四个方法,负责从两个列表中获取X和Y值。基本上我这样做是为了让图表创建方法尽可能通用。可能有点矫枉过正。

  private DateTime GetBenchXValue(Bench currentBench)
  {
    return currentBench.DateLifted;
  }

  private Int32 GetBenchYValue(Bench currentBench)
  {
    return currentBench.BenchAmount;
  }

  private DateTime GetFoodIntakeXValue(FoodIntake currentIntake)
  {
    return currentIntake.DateEaten;
  }

  private Int32 GetFoodIntakeYValue(FoodIntake currentIntake)
  {
    return currentIntake.Calories;
  }

所以当你运行它时,你会得到两张图并排。请注意,它们将非常简单,因为有数百万种不同的属性可以设置以改善外观。希望这是你要求的。

  using System;
  using System.Web.UI.DataVisualization.Charting;
  using System.Web.UI.WebControls;

答案 1 :(得分:2)

protected void Page_Load(object sender, EventArgs e)
    {
        MySqlConnection UsageLogConn = new MySqlConnection("Server=localhost;UID=root;Password=;database=productactivation");
        UsageLogConn.Open();//open connection

        string sql = "select * from sales";
        DataSet ds = new DataSet();
        MySqlCommand cmd = new MySqlCommand(sql, UsageLogConn);
        MySqlDataAdapter mySQLadapter = new MySqlDataAdapter(cmd);
        mySQLadapter.Fill(ds);
        Chart1.DataSource = ds;

        // set series members names for the X and Y values 
        Chart1.Series["Series1"].XValueMember = "title_id";
        Chart1.Series["Series1"].YValueMembers = "qty";
        Chart1.Series["Series2"].XValueMember = "title_id";
        Chart1.Series["Series2"].YValueMembers = "qty";
        UsageLogConn.Close();
        // data bind to the selected data source
        Chart1.DataBind();


        cmd.Dispose();

    }

答案 2 :(得分:0)

我更新了.NET 4.0的MS图表示例,并添加了另外两个项目 - ChartsWithMVC和ChartsWithoutWebForms。您可能会发现我的示例代码很有用,因为我使用asp.net图表控件有一个非常基本的动态图表系统实现:

http://develocity.blogspot.com/2010/04/aspnet-chart-controls-without-web-forms.html

希望它有所帮助。