如何从 asp.net c# 中的 2 个字符串值生成饼图
How to generate a pie chart from 2 string values in asp.net c#
我有 2 列 ID 和 Severity,我的数据库中的数据类型均为字符串 table,Severity 列具有高、中和低值。我添加了一个图表控件并使用 sql 数据源指定了它的数据,但我无法获得输出,因为 Y 坐标值应该是整数类型。我需要生成如下所示的图表,每个严重级别都有一个百分比值:
代码:
private void GetChartData()
{
string cs = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ID, Severity FROM AMD", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
Chart1.Series[0].XValueMember = "Severity";
Chart1.Series[0].YValueMembers = "ID";
Chart1.Series[0].ChartArea = "ChartArea1";
Chart1.DataSource = dt;
Chart1.DataBind();
int high = 0, med = 0, low = 0;
string[] x = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
//y[i] = dt.Rows[i][1].ToString();
if (dt.Rows[i][1].ToString().ToLower().Contains("high"))
{
high++;
}
else if (dt.Rows[i][1].ToString().ToLower().Contains("medium"))
{
med++;
}
else if (dt.Rows[i][1].ToString().ToLower().Contains("low"))
{
low++;
}
Chart1.Series[1].Points.DataBindXY(x, high);
}
}
我该如何实现这一目标?请指导...提前致谢...
如果不能尝试这些,我猜:
//SqlCommand cmd = new SqlCommand("SELECT ID, Severity FROM AMD", con);
SqlCommand cmd = new SqlCommand("SELECT Severity, Count(*) AS ItemCount FROM AMD GROUP BY Severity", con);
然后是
Chart1.Series[0].XValueMember = "Severity";
Chart1.Series[0].YValueMembers = "ItemCount";
将其粘贴到设计页面中。
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
title: 'CHART',
width: 400,
height: 400,
bar: { groupWidth: "95%" },
legend: { position: "none" },
isStacked: true
};
$.ajax({
type: "POST",
url: "Chart.aspx/GetChartData", //Chart.aspx is the page name.
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
debugger;
var data = google.visualization.arrayToDataTable(r.d);
//var chart = new google.visualization.BarChart($("#chart")[0]); //***BarChart***
var chart = new google.visualization.PieChart($("#chart")[0]); //***PieChart***
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
<div id="chart" style="width: 500px; height: 500px;"> //this is the div which shows the chart.
</div>
将此粘贴到您的 C# 页面中。
using System.Web.Services;
[WebMethod]
public static List<object> GetChartData()
{
string query = "select Share,Value from Table";
string constr = ConfigurationManager.ConnectionStrings["dbcn"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Share", "Value"
});
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["Share"], sdr["Value"]
});
}
}
con.Close();
return chartData;
}
}
}
我有 2 列 ID 和 Severity,我的数据库中的数据类型均为字符串 table,Severity 列具有高、中和低值。我添加了一个图表控件并使用 sql 数据源指定了它的数据,但我无法获得输出,因为 Y 坐标值应该是整数类型。我需要生成如下所示的图表,每个严重级别都有一个百分比值:
代码:
private void GetChartData()
{
string cs = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ID, Severity FROM AMD", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
Chart1.Series[0].XValueMember = "Severity";
Chart1.Series[0].YValueMembers = "ID";
Chart1.Series[0].ChartArea = "ChartArea1";
Chart1.DataSource = dt;
Chart1.DataBind();
int high = 0, med = 0, low = 0;
string[] x = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
//y[i] = dt.Rows[i][1].ToString();
if (dt.Rows[i][1].ToString().ToLower().Contains("high"))
{
high++;
}
else if (dt.Rows[i][1].ToString().ToLower().Contains("medium"))
{
med++;
}
else if (dt.Rows[i][1].ToString().ToLower().Contains("low"))
{
low++;
}
Chart1.Series[1].Points.DataBindXY(x, high);
}
}
我该如何实现这一目标?请指导...提前致谢...
如果不能尝试这些,我猜:
//SqlCommand cmd = new SqlCommand("SELECT ID, Severity FROM AMD", con);
SqlCommand cmd = new SqlCommand("SELECT Severity, Count(*) AS ItemCount FROM AMD GROUP BY Severity", con);
然后是
Chart1.Series[0].XValueMember = "Severity";
Chart1.Series[0].YValueMembers = "ItemCount";
将其粘贴到设计页面中。
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
title: 'CHART',
width: 400,
height: 400,
bar: { groupWidth: "95%" },
legend: { position: "none" },
isStacked: true
};
$.ajax({
type: "POST",
url: "Chart.aspx/GetChartData", //Chart.aspx is the page name.
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
debugger;
var data = google.visualization.arrayToDataTable(r.d);
//var chart = new google.visualization.BarChart($("#chart")[0]); //***BarChart***
var chart = new google.visualization.PieChart($("#chart")[0]); //***PieChart***
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
<div id="chart" style="width: 500px; height: 500px;"> //this is the div which shows the chart.
</div>
将此粘贴到您的 C# 页面中。
using System.Web.Services;
[WebMethod]
public static List<object> GetChartData()
{
string query = "select Share,Value from Table";
string constr = ConfigurationManager.ConnectionStrings["dbcn"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Share", "Value"
});
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["Share"], sdr["Value"]
});
}
}
con.Close();
return chartData;
}
}
}