Adding dynamic charts using ASP.NET CHART CONTROL, C #

I wanted to add dynamic charts to a web page. It happens like this ...

I get the start and end dates from the user and draw separate charts for each date during the start and end dates.

I get data from sql database and bind it to the diagram as follows:

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

In the above code, only one chart is added for one date, and I added "chart1" to create the view and its dynamics not created. But I wanted to add dynamic runtime diagrams to the web page.

Can anyone help me with this?

I am using VS 2008, ASP.NET 3.5 and the lib chart: using System.Web.UI.DataVisualization.Charting;

+5
3

, , , , . , , .

 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 to 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;
+4
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

MS .NET 4.0 - ChartsWithMVC ChartsWithoutWebForms. , asp.net:

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

, .

0

All Articles