我正在制作饼图&从数据表中获取值。这是我的代码:
.aspx代码
<asp:Chart ID="Chart1" runat="server" Width="500px">
<Series>
<asp:Series Name="Series1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
.cs代码
Chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
Chart1.Series["Series1"]["DrawingStyle"] = "Emboss";
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
Chart1.Series["Series1"].IsValueShownAsLabel = true;
DataTable dt = new DataTable();
DataColumn dc;
con.ConnectionString = ConfigurationManager.ConnectionStrings["mySQLConnection"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("Select Name, COUNT(code) AS quantity from vTable GROUP BY Name", con1);
var dr = cmd.ExecuteReader();
dt.Load(dr);
Chart1.DataSource = dt;
Chart1.Series["Series1"].XValueMember = "Name";
Chart1.Series["Series1"].YValueMembers = "quantity";
Chart1.DataBind();
我想在饼图上显示两个标签及其值。 SQL查询中的Name列应显示为label&amp;数量作为价值。怎么可能?
答案 0 :(得分:1)
我相信您正在寻找DataPointCollection.DataBindXY(IEnumerable, IEnumerable[])
,MSDN。
示例:
string[] Names = new string[] { "Item1", "Item2", "Item3" };
double[] Values = new double[] { 0.1, 0.2, 0.3 };
chart1.Series[0].Points.DataBindXY(Names, Values);
希望能让你朝着正确的方向前进!