在一个页面上显示多个Google图表C#

时间:2016-05-02 23:10:14

标签: javascript c# asp.net visual-studio

我有以下C#从存储过程中提取数据,但result_chart图表不会呈现。但是,如果我删除BindChartResult和与之关联的类(GetChartDataResultpublic 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; } } ),它会呈现正常,但我需要同时显示。我不确定我在哪里犯了错误。任何帮助将不胜感激。

C#

<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>

ASP.NET

fname = input("Enter Filename: ")
infile = open(fname, "r")
data = infile.readlines()

mylist = {}
for i in data:
    if int(i) in mylist:
        mylist[int(i)] += 1
    else:
        mylist[int(i)] = 1

print my list

output example = {8: 2, 9: 2, 7: 5}

1 个答案:

答案 0 :(得分:0)

BindChartResult中找到我的问题的解决方案我需要将ltScripts.Text = strScript.ToString();更改为Literal1.Text = strScript.ToString();以对应我的aspx文件中的Literal控件。