0
我有以下C#從存儲過程中提取數據,但fee_chart
圖表不顯示。但是,如果我刪除result_chart
及其關聯的類(BindChartResult
和GetChartDataResult
),它呈現很好,但我需要同時顯示。我不確定我犯了什麼錯誤。任何幫助將不勝感激。在一個頁面上顯示多個Google圖表C#
C#
public partial class feestatistics: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
string query = "SELECT DISTINCT year.year_title, year.year_id FROM year";
DataTable dt = GetData(query);
ddlyear.DataSource = dt;
ddlyear.DataTextField = "year_title";
ddlyear.DataValueField = "year_id";
ddlyear.DataBind();
ddlyear.Items.Insert(0, new ListItem("All", "All"));
ddlyear.Items.Insert(0, new ListItem(" - Select -", ""));
}
}
private void BindChartFees() {
DataTable dsChartData = new DataTable();
StringBuilder strScript = new StringBuilder();
try {
dsChartData = GetChartDataFees();
strScript.Append(@ "<script type='text/javascript'>
google.load('visualization', '1', {
packages: ['corechart']
}); < /script>
< script type = 'text/javascript' >
function drawChart() {
var data = google.visualization.arrayToDataTable([
['status', 'total'], ");
foreach(DataRow row in dsChartData.Rows) {
strScript.Append("['" + row["status"] + "'," + row["total"] + "],");
}
strScript.Remove(strScript.Length - 1, 1);
strScript.Append("]);");
strScript.Append(@ " var options = {
title: 'Fee Statistics',
is3D: false,
};
");
strScript.Append(@ "var chart = new google.visualization.PieChart(document.getElementById('fee_chart'));
chart.draw(data, options);
}
google.setOnLoadCallback(drawChart);
");
strScript.Append(" </script>");
ltScripts.Text = strScript.ToString();
} catch {}
}
private void BindChartResult() {
DataTable dsChartData = new DataTable();
StringBuilder strScript = new StringBuilder();
try {
dsChartData = GetChartDataResult();
strScript.Append(@ "<script type='text/javascript'>
google.load('visualization', '1', {
packages: ['corechart']
}); < /script>
< script type = 'text/javascript' >
function drawChart() {
var data = google.visualization.arrayToDataTable([
['status', 'total'], ");
foreach(DataRow row in dsChartData.Rows) {
strScript.Append("['" + row["status"] + "'," + row["total"] + "],");
}
strScript.Remove(strScript.Length - 1, 1);
strScript.Append("]);");
strScript.Append(@ " var options = {
title: 'Pass/Fail Statistics',
is3D: false,
};
");
strScript.Append(@ "var chart = new google.visualization.PieChart(document.getElementById('result_chart'));
chart.draw(data, options);
}
google.setOnLoadCallback(drawChart);
");
strScript.Append(" </script>");
ltScripts.Text = strScript.ToString();
} catch {}
}
private DataTable GetChartDataFees() {
if (ddlyear.SelectedValue == "All") {
DataSet dsData = new DataSet();
try {
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter sqlCmd = new SqlDataAdapter("GetFeeStatistics", sqlCon);
sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlCon.Open();
sqlCmd.Fill(dsData);
sqlCon.Close();
} catch {
throw;
}
return dsData.Tables[0];
} else {
DataSet dsData = new DataSet();
try {
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter sqlCmd = new SqlDataAdapter("GetFeeStatisticsYear", sqlCon);
sqlCmd.SelectCommand.Parameters.AddWithValue("@year", ddlyear.SelectedValue);
sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlCon.Open();
sqlCmd.Fill(dsData);
sqlCon.Close();
} catch {
throw;
}
return dsData.Tables[0];
}
}
private DataTable GetChartDataResult() {
if (ddlyear.SelectedValue == "All") {
DataSet dsData = new DataSet();
try {
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter sqlCmd = new SqlDataAdapter("GetResultStatistics", sqlCon);
sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlCon.Open();
sqlCmd.Fill(dsData);
sqlCon.Close();
} catch {
throw;
}
return dsData.Tables[0];
} else {
DataSet dsData = new DataSet();
try {
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter sqlCmd = new SqlDataAdapter("GetResultStatisticsYear", sqlCon);
sqlCmd.SelectCommand.Parameters.AddWithValue("@year", ddlyear.SelectedValue);
sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlCon.Open();
sqlCmd.Fill(dsData);
sqlCon.Close();
} catch {
throw;
}
return dsData.Tables[0];
}
}
protected void ddlyear_SelectedIndexChanged(object sender, EventArgs e) {
//call BindChartFees
BindChartFees();
//call BindChartResult
BindChartResult();
}
private static DataTable GetData(string query) {
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(query);
String constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
}
ASP.NET
<div class="row">
<div class="col-12">
<div class="panel">
<asp:DropDownList ID="ddlyear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlyear_SelectedIndexChanged"></asp:DropDownList>
</div>
<div>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<div class="panel">
<div>
<asp:Literal ID="ltScripts" runat="server"></asp:Literal>
<div id="fee_chart" style="width: auto; height: 500px;">
</div>
</div>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<div class="panel">
<div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<div id="result_chart" style="width: auto; height: 500px;">
</div>
</div>
</div>
</div>
</div>
@DespeiL一個使用'fee_chart'不是嗎?這行代碼? 'strScript.Append(@「var chart = new google.visualization.PieChart(document.getElementById('fee_chart')); chart.draw(data,options);' – mcclosa