2016-05-02 13 views
0

fee_chartグラフはレンダリングされませんが、ストアドプロシージャからデータを取得する次のC#があります。しかし、result_chartとそれに関連するクラス(BindChartResultGetChartDataResult)を削除してもそれはうまく描画されますが、私は両方を表示する必要があります。私はどこでエラーが起きたのか分かりません。どんな助けでも大歓迎です。1つのページに複数の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> 
+0

@DespeiL 1つは 'fee_chart'を使用していますか?このコード行は? 'strScript.Append(@" var chart = new google.visualization.PieChart(document.getElementById( 'fee_chart')); chart.draw(data、options); ' – mcclosa

答えて

0

は私が私のaspxファイルでLiteral制御に対応してLiteral1.Text = strScript.ToString();ltScripts.Text = strScript.ToString();を変更するために必要なBindChartResultで私の問題への解決策を見つけました。

関連する問題