1

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);
3
  • ya, but i was able to load only one chart at runtime.Only the latest one. I just want to know the appropriate way for doing this. Commented Jul 26, 2012 at 3:59
  • 1
    Well, thats a problem with your code then. Show the code. Did you look to create multiple chart controls? They have to be created dynamically. You can't assign to just one statically created control. Commented Jul 26, 2012 at 4:00
  • My code is as follows.I think it's not appropriate for my requiement. Commented Jul 26, 2012 at 4:28

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

-1

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

}

1 Comment

You used same chart object Chart1 in createChart() but the new chart object chart1. So, it will add empty chart to placeholder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.