我正在尝试显示与我的数据库连接的图表。但它会产生错误。错误是"数据点插入错误。此数据系列只能设置1个Y值。"请帮我。 代码如下:
public partial class chart3 : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(@"connectionString");
cmd = new SqlCommand("Select * from GraphChart",con);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
Chart1.DataSource = dt;
Chart1.DataBind();
}
}
来源:
<div>
<asp:Chart ID="Chart1" runat="server" CssClass="auto-style1" Width="423px">
<Series>
<asp:Series Name="Series1" XValueMember="Name"></asp:Series>
<asp:Series Name="Series2" YValueMembers="Age"></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Title="Employee Data"></asp:Legend>
</Legends>
</asp:Chart>
</div>
答案 0 :(得分:2)
以下给出的代码正常运行。谢谢大家。
public partial class chart3 : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(@"connectionString");
cmd = new SqlCommand("Select * from GraphChart",con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
DataView source = new DataView(ds.Tables[0]);
Chart1.DataSource = source;
Chart1.Series[0].XValueMember = "Name";
Chart1.Series[0].YValueMembers = "Age";
Chart1.DataBind();
}
}
答案 1 :(得分:0)
您可能正在设置标签而不是真实数据。
也许此链接可以提供帮助:ASP.NET MVC 3 MSChart Error: Only 1 Y values can be set for this data series