0

My ASPX part

<div runat="server" id="divMain">
    <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
        <Series>
            <asp:Series Name="Series1" ChartType="Pie">
            <Points>

                //I want this part to come from code-behind

                //<asp:DataPoint AxisLabel="1 Star"  YValues="100" />
                //<asp:DataPoint AxisLabel="2 Star" YValues="123" />
                //<asp:DataPoint AxisLabel="3 Star" YValues="155" />
                //<asp:DataPoint AxisLabel="4 Star" YValues="245" />

            </Points>
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">

            </asp:ChartArea>
        </ChartAreas>
        <Legends>
            <asp:Legend Title="Test abc" />
        </Legends>
    </asp:Chart>
  </div>

My code-behind follows down:

protected void Page_Load(object sender, EventArgs e)
{
    double[] yValues = { 10, 27.5, 7, 12, 45.5 };
    string[] xNames = { "Mike", "John", "William", "George", "Alex" };

    Series series1 = new  Series("pie");
    series1.ChartType = SeriesChartType.Pie;
    series1.BorderWidth = 3;
    series1.ShadowOffset = 2;
    Chart1.Series[0].Points.DataBindXY(xNames, yValues);
    divMain.Controls.Add(Chart1);
}

But I am getting the following exception: "The DataSourceID of Chart1 must be the ID of a control of type IDataSource. A control with ID SqlDataSource1 could not be found."

amhed
  • 3,649
  • 2
  • 31
  • 56
Rakesh
  • 87
  • 1
  • 3
  • 9

1 Answers1

1

Remove DataSourceID="SqlDataSource1" element from the Chart in your aspx page, it probably does not exist, and you don't need it as you try to fill the data to the chart programatically.

Axarydax
  • 16,353
  • 21
  • 92
  • 151
  • yes its working but the things i want to bind it in percentage like 20%, 45% like that instead of 20, 45.24 etc... could be helpful if you can. Thanks – Rakesh Apr 01 '13 at 13:24
  • You need to format the labels. Here's an article that might help: http://blogs.msdn.com/b/alexgor/archive/2008/11/11/microsoft-chart-control-how-to-using-keywords.aspx – amhed Apr 01 '13 at 14:14