我m trying to show gender percentage into a pie chart using count statement and placing it in dataset. I
已经检索了数据,但我不知道如何在XY中绑定它,因为所有数据都在一个dataTable变量中。
所以我的程序流程,
用户从下拉列表中选择表名
用户生成报告
x和y的ddl填充了上面报告中显示的列
用户从ddl中选择具有列名的x和y值
对于数据库,它只有id,name和gender。
如果只是从DB读取值,那么我已经设法使用dataView(),但为此我真的不知道该怎么做。
P / S。 C#和asp.net中的新功能。感谢所有帮助和高级TQVM。
第1部分代码 - 获取用户下拉列表并创建数据集
public void testGraph()
{
string ddlX = ddlTestGraphX.SelectedValue;
string ddlY = ddlTestGraphY.SelectedValue;
CodeDB db = new CodeDB();
db.Open();
//SET UP THE DATA TO PLOT
string userInput = DDLTableName.SelectedValue;
if (userInput == "tblparticipant")
{
DataSet dsX = db.getDataSet2(ddlX, userInput);
DataTable dataX = dsX.Tables[0];
DataSet dsY = db.getDataSetCount(ddlY, userInput);
DataTable dataY = dsY.Tables[0];
string trimmedUserInput = userInput.Remove(0, 3);
try
{
if (rbPieChart.Checked == true)
{
pieChart(trimmedUserInput, dataX, dataY);
}
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = "Check Your drop down list";
}
}
}
第二部分代码 - 显示饼图
public void pieChart(string recTrimmedUserInput, DataTable recDataX, DataTable recDataY)
{
//BIND THE DATA TO THE CHART
Chart1.Series.Add(new Series());
Title title = Chart1.Titles.Add("CHART " + recTrimmedUserInput.ToUpper());
Chart1.Series[0].Points.DataBindXY(recDataX.AsDataView(), recDataY.AsDataView());
//SET THE CHART TYPE TO BE PIE
Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
//Chart1.Series[0].Label = "#PERCENT{P2}";
Chart1.Series[0]["PieLabelStyle"] = "Outside";
Chart1.Series[0]["PieStartAngle"] = "-90";
//SET THE COLOR PALETTE FOR THE CHART TO BE A PRESET OF NONE
//DEFINE OUR OWN COLOR PALETTE FOR THE CHART
Chart1.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.None;
//Chart1.PaletteCustomColors = new Color[] { Color.Blue, Color.Red };
//Chart1.Series[0].Label = "#VALX , #VALY";
Chart1.Series[0].Label = "#PERCENT{P2}";
//SET THE IMAGE OUTPUT TYPE TO BE JPEG
Chart1.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageType.Jpeg;
//ADD A PLACE HOLDER CHART AREA TO THE CHART
//SET THE CHART AREA TO BE 3D
Chart1.ChartAreas.Add(new ChartArea());
Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
//ADD A PLACE HOLDER LEGEND TO THE CHART
//DISABLE THE LEGEND
Chart1.Legends.Add(new Legend());
Chart1.Series[0].LegendText = "#VALX";
Chart1.Legends[0].Enabled = true;
}
第3部分代码 - 获取数据集
public DataSet getDataSetCount(string receivedDDL, string receivedUserInput)
{
string query = "Select " + receivedDDL +", count(id) as count From "+ receivedUserInput+" Group by "+ receivedDDL;
if (con.State.ToString() == "Open")
{
SqlDataAdapter ad = new SqlDataAdapter(query, con);
DataSet ds = new DataSet("Table");
ad.Fill(ds);
return ds;
}
else
{
return null;
}
}