需要获得具有%值的饼图

本文关键字: | 更新日期: 2023-09-27 18:06:43

我的ASPX部分

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

我的后台代码如下:

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

但是我得到了以下例外:"Chart1的DataSourceID必须是IDataSource类型控件的ID。无法找到ID为SqlDataSource1的控件。"

需要获得具有%值的饼图

从您的aspx页面的Chart中删除DataSourceID="SqlDataSource1"元素,它可能不存在,并且您不需要它,因为您试图以编程方式填充数据到图表

相关文章:
  • 没有找到相关文章