Nhuren
Nhuren

Reputation: 513

Dynamically add chart control in asp.net

I need to add the chart dynamically at the runtime.Actually multiple charts.As for example: i have to fetch the records of 14 weeks from database and show the records in the chart of each week.(i.e., 14 charts).But number of week may varies according to the users and the charts. So, how can i overcome this issue?

I am thankful for any idea regarding this.

Chart Chart1 = new Chart();

          Chart1.Series.Add(new Series());


          Chart1.ChartAreas.Add(new ChartArea());
          Chart1.ChartAreas[0].Area3DStyle.Enable3D = false;

           Chart1.Series[0].YValueMembers = "Value";
           Chart1.Series.Add(new Series());
           Chart1.Series[1].YValueMembers = "AnotherValue";
           Chart1.DataSource = lsttest;
           Chart1.DataBind();

           Chart1.Series[0].Color = Color.Blue;
           Chart1.Series[1].Color = Color.DarkGreen;

           Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Count";
           Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Status";
           Chart1.Series[0].IsValueShownAsLabel = true;
           Chart1.Series[1].IsValueShownAsLabel = true;

        Chart1.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageType.Jpeg;

        Chart1.Width = new System.Web.UI.WebControls.Unit(300, System.Web.UI.WebControls.UnitType.Pixel);
        Chart1.Height = new System.Web.UI.WebControls.Unit(200, System.Web.UI.WebControls.UnitType.Pixel);

Upvotes: 1

Views: 6440

Answers (2)

daniel
daniel

Reputation: 9

Use PlaceHolder and append the chart control to it.

for(int i=1;i<=n;i++)
{
     Chart1.ID="Chart "+i;
     Chart chart1 = new Chart();
     createChart(dt.Tables[0],chart1); // function to create charts
     chartPlaceHolder.Controls.Add(chart1);
}


private void createChart(DataTable dt, Chart chart1)
{
          Chart1.Series.Add(new Series());

          Chart1.ChartAreas.Add(new ChartArea());
          Chart1.ChartAreas[0].Area3DStyle.Enable3D = false;

           Chart1.Series[0].YValueMembers = "Value";
           Chart1.Series.Add(new Series());
           Chart1.Series[1].YValueMembers = "AnotherValue";
           Chart1.DataSource = dt;
           Chart1.DataBind();

           Chart1.Series[0].Color = Color.Blue;
           Chart1.Series[1].Color = Color.DarkGreen;

           Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Count";
           Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Status";
           Chart1.Series[0].IsValueShownAsLabel = true;
           Chart1.Series[1].IsValueShownAsLabel = true;

    Chart1.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageType.Jpeg;
    
    Chart1.Width = new System.Web.UI.WebControls.Unit(300, System.Web.UI.WebControls.UnitType.Pixel);
    Chart1.Height = new System.Web.UI.WebControls.Unit(200, System.Web.UI.WebControls.UnitType.Pixel);

}

Upvotes: -1

Siddarth Amudhu
Siddarth Amudhu

Reputation: 53

Your code is correct, all you need to do is put it in a loop and make it runs on any number of times you want. And when creating a new chart, use the loop counter.

So after this line add the loop:

Chart Chart1 = new Chart();
for(int i=1;i<=n;i++)
{
     Chart1.ID="Chart "+i;
}

Upvotes: 3

Related Questions