Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi, i tried to create new chart and add this Chart Chart1 = new Chart(); into my Cs file. Then I add the series and chartarea to create the chart dynamically, but strangely, when the codes is executed, no chart is shown. If I do not add that statement in and define Chart ID in Aspx file like this: ( <asp:Chart ID="Chart1" runat="server"> ) then try to create chart dynamically in Cs file, it works. But i want to create new chart every time and do not want to depend on defining Chart ID to produce chart dynamically.

This is my codes of when defining Chart ID in Aspx file to produce chart dynamically, chart can be produced: (it works)
ASP.NET
//Aspx file:

<asp:scriptmanager ID="ScriptManager1" runat="server">
            </asp:scriptmanager>
             
        </div>
        <asp:updatepanel ID="UpdatePanel1" runat="server">
            <contenttemplate>

 <asp:Chart ID="Chart1" runat="server">

                </asp:Chart>

  <asp:placeholder ID="PlaceHolder1" runat="server"></asp:placeholder>
        </contenttemplate>
        </asp:updatepanel>

//Cs file:
            Chart1.DataSource = tg;
            Chart1.Width = 600;
            Chart1.Height = 350;

            Chart1.Series.Add(new Series());
            Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
            List<object> lst = tg.AsEnumerable().ToList<object>();

            foreach (DataRow row in tg.Rows)
                Chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["Min"], row["Max"], row["Avg"], row["Percentile25"], row["Percentile50"], row["Percentile75"] });

            if(tg.Rows.Count>0)
            {
                string title = (tg.Rows[2]["Process"].ToString());
            }

            //create chartareas
            ChartArea ca = new ChartArea();
            ca.Name = "ChartArea1";
            ca.AxisX = new Axis();
            ca.AxisY = new Axis();
            Chart1.ChartAreas.Add(ca);

            //databind
            Chart1.DataBind();
            Chart1.Visible = true;

This is my codes when I tried to create new chart by adding in this: Chart Chart1 = new Chart() in Cs file and didn't define ChartID in Aspx file; (does not work)
ASP.NET
//Aspx file:
 </div>
        <asp:updatepanel ID="UpdatePanel1" runat="server">
            <contenttemplate>

<asp:Chart runat="server">

                </asp:Chart>
                </br>

        <asp:placeholder ID="PlaceHolder1" runat="server"></asp:placeholder>
        </contenttemplate>
        </asp:updatepanel>

//Cs file:
            Chart Chart1 = new Chart();
            Chart1.DataSource = tg;
            Chart1.Width = 600;
            Chart1.Height = 350;

            Chart1.Series.Add(new Series());
            Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
            List<object> lst = tg.AsEnumerable().ToList<object>();

            foreach (DataRow row in tg.Rows)
                Chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["Min"], row["Max"], row["Avg"], row["Percentile25"], row["Percentile50"], row["Percentile75"] });

            if(tg.Rows.Count>0)
            {
                string title = (tg.Rows[2]["Process"].ToString());
            }

            //create chartareas
            ChartArea ca = new ChartArea();
            ca.Name = "ChartArea1";
            ca.AxisX = new Axis();
            ca.AxisY = new Axis();
            Chart1.ChartAreas.Add(ca);

            //databind
            Chart1.DataBind();
            Chart1.Visible = true;


Question: How to add in this statement: Chart Chart1 = new Chart(); into Cs file to create new chart every time during execution instead of depending on defining Chart ID in Aspx file (<asp:Chart ID="Chart1" runat="server">) to create the chart dynamically?

I really need help on this. Appreciate if someone can help me on this, thanks a lot!!
Posted
Updated 28-Sep-15 10:34am
v5
Comments
ZurdoDev 28-Sep-15 16:23pm    
I don't follow, but if you don't want chart in your aspx page then you need to somewhere add the dynamic chart to the page. For example, have a div on your page with id of divChart. Then you could do divChart.Controls.Add(Chart1);
j snooze 28-Sep-15 17:09pm    
I second RyanDev's comment. nowhere in the code that you posted did you actually tell the server side to put the chart on the page. I see you have a placeholder which is fairly common for adding dynamic controls to. You need to add the control to that if thats where it goes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900