簡體   English   中英

如何從C#ASP.NET Web表單背后的代碼獲取圖表值

[英]How to get values of chart from code behind c# asp.net web form

如何從C#ASP.NET Web表單背后的代碼獲取圖表值,以下是我的腳本

<script type="text/javascript" 
src="https://www.gstatic.com/charts/loader.js"></script>
<script type = "text/javascript">
     google.charts.load('current', {packages: ['corechart']});     
  </script>
 <script language = "JavaScript">
     function drawChart() {
        // Define the chart to be drawn.
        var data = google.visualization.arrayToDataTable([
           ['Home Type', 'Total', 'Pending', 'Closed'],
           ['one', 20, 10, 10],
           ['two',  15, 6, 8],
           ['three',  5, 1, 4],
           ['four',  5, 1, 8],
           ['five',  5, 1, 4],
           ['six',  20, 10, 10],
           ['seven',  15, 6, 8],
           ['eight',  5, 1, 3],
           ['nine',  5, 1, 8],
           ['ten',  5, 1, 12],
        ]);

        var options = {colors: ['#257ec2', '#f1565a', '#2ab691'], legend: { position: 'top', maxLines: 3 },chartArea:{
left:5,
top: 20,
width: '100%',
height: '370',
 }};  

        // Instantiate and draw the chart.
        var chart = new google.visualization.ColumnChart(document.getElementById('container'));

        chart.draw(data, options);
     }
     google.charts.setOnLoadCallback(drawChart);
  </script>

下面是我的HTML

<div id = "container" style = "height: 490px;"></div>

我不知道從C#背后的代碼獲得動態效果

我無法對其進行測試,但您不必將<script/>放入aspx中,而必須使用ClientScriptManager.RegisterStartupScript將js塊注入aspx.cs中。

像這樣:

protected void Page_Load(object sender, EventArgs e)
{
    regiterAdsScript();  
}

public void regiterAdsScript()
{
    // Define the name and type of the client script on the page.
    String csName = "ButtonClickScript";
    Type csType = this.GetType();
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;
    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(csType, csName))
    {
        StringBuilder csText = new StringBuilder();
        csText.AppendLine("<script type=\"text/javascript\"> ");
        csText.AppendLine("     google.charts.load('current', {packages: ['corechart']});");
        csText.AppendLine("</script>");
        csText.AppendLine("<script language =  <\"JavaScript\">");
        csText.AppendLine("     function drawChart() {");
        csText.AppendLine("        // Define the chart to be drawn.");
        csText.AppendLine("        var data = google.visualization.arrayToDataTable([");
        csText.AppendLine("           ['Home Type', 'Total', 'Pending', 'Closed'],");
        csText.AppendLine("           ['one', 20, 10, 10],");
        csText.AppendLine("           ['two',  15, 6, 8],");
        csText.AppendLine("           ['three',  5, 1, 4],");
        csText.AppendLine("           ['four',  5, 1, 8],");
        csText.AppendLine("           ['five',  5, 1, 4],");
        csText.AppendLine("           ['six',  20, 10, 10],");
        csText.AppendLine("           ['seven',  15, 6, 8],");
        csText.AppendLine("           ['eight',  5, 1, 3],");
        csText.AppendLine("           ['nine',  5, 1, 8],");
        csText.AppendLine("           ['ten',  5, 1, 12],");
        csText.AppendLine("        ]);");
        csText.AppendLine("        var options = {colors: ['#257ec2', '#f1565a', '#2ab691'], legend: { position: 'top', maxLines: 3 },chartArea:{left:5,top: 20,width: '100%',height: '370', }};");
        csText.AppendLine("        // Instantiate and draw the chart.");
        csText.AppendLine("        var chart = new google.visualization.ColumnChart(document.getElementById('container'));");
        csText.AppendLine("        chart.draw(data, options);");
        csText.AppendLine("     }");
        csText.AppendLine("     google.charts.setOnLoadCallback(drawChart);");
        csText.AppendLine("</script>");

        //cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
        cs.RegisterStartupScript(csType, csName, csText.ToString());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM