简体   繁体   English

Google图表未显示

[英]Google Chart not showing

Please can some identify why are these charts not showing. 请确定一些为什么未显示这些图表。 This code was working with another project. 该代码正在与另一个项目一起使用。 I used the same code in a new project i have only added the master page however now the charts are not showing. 我在一个新项目中使用了相同的代码,但我只添加了母版页,但是现在未显示图表。

I can only see a blank background. 我只能看到空白的背景。

HTML 的HTML

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
         google.load("visualization", "1", { packages: ["corechart"] });
         google.load('visualization', '1', {packages: ['table']});
         google.load('visualization', '1.0', {'packages':['controls']});
         google.setOnLoadCallback(drawChart);
         function drawChart() {

            var dataValues = JSON.parse(document.getElementById("ChartData").value);
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Locality');
            data.addColumn('number', 'Frequency');

            for (var i = 0; i < dataValues.length; i++) {
                data.addRow([dataValues[i].location,dataValues[i].frequency]);
            }

             // Create a dashboard.
            var dashboard = new google.visualization.Dashboard(
            document.getElementById('dashboard_div'));

            // Create a range slider, passing some options
            var donutRangeSlider = new google.visualization.ControlWrapper({
            'controlType': 'NumberRangeFilter',
            'containerId': 'filter_div',
            'options': {
            'filterColumnLabel': 'Frequency'
            }
            });

            var pieoptions = { 'title': 'Pie Chart Test',
            'width': 900,
            'height': 500,
            backgroundColor: 'transparent',
            is3D: true
            };

            var columnoptions = {
            title: 'Column Chart Test',
            hAxis: {title: 'Locality', titleTextStyle: {color: 'black'}} 
             };

            // Create a pie chart, passing some options
            var pieChart = new google.visualization.ChartWrapper({
            'chartType': 'PieChart',
            'containerId': 'chart_div',
            'options': {
            'width': 300,
            'height': 300,
            'pieSliceText': 'value',
            'legend': 'right'
            }
            }); 

            var chart =  new google.visualization.PieChart(document.getElementById('piechart'));
            chart.draw(data, pieoptions);

            var chart =  new google.visualization.ColumnChart(document.getElementById('columnchart'));
            chart.draw(data, columnoptions);

            var table = new google.visualization.Table(document.getElementById('table_div'));
            table.draw(data, {showRowNumber: true});

            dashboard.bind(donutRangeSlider, pieChart);

            // Draw the dashboard.
            dashboard.draw(data);
        }
    </script> 
 </asp:Content>

  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

        <asp:Button id="b1" Text="Draw Charts" onclick="Button1_Click" runat="server" />
        <asp:HiddenField runat="server" ID="ChartData" />
        <div id="piechart" style="width: 900px; height: 500px;"></div>
        <div id="columnchart" style="width: 900px; height: 500px;"></div>
        <div id="table_div" style="width: 400px; height: 400px"></div>
        <!--Div that will hold the dashboard-->
        <div id="dashboard_div">
        <!--Divs that will hold each control and chart-->
        <div id="filter_div"></div>
        <div id="chart_div"></div>
        </div>
    </asp:Content>


<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

</asp:Content> 

Code Behind 背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //This is a method for testing, it is called from the button and then calls a method from the Utility class.
    public List<Items> getChartData()
    {
        List<Items> dataList = new List<Items>();
        dataList.Add(new Items("A", 110));
        dataList.Add(new Items("B", 120));
        dataList.Add(new Items("C", 30));
        dataList.Add(new Items("D", 150));
        dataList.Add(new Items("E", 210));
        dataList.Add(new Items("F", 310));
        JavaScriptSerializer jss = new JavaScriptSerializer();

        this.ChartData.Value = jss.Serialize(dataList);

        return dataList;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        getChartData();
    }
}

public class Items
{
    public string location = "";
    public int frequency = 0;

    public Items(string location, int frequency)
    {
        this.location = location;
        this.frequency = frequency;
    }
}

Help with examples is really appreciated. 非常感谢提供示例的帮助。

Try to view source on the client-side. 尝试在客户端上查看源。 Asp.net prepends ids with the names of their Asp.net container, so when you run on client side, you JSON.Parse (and possible other fields) don't necessarily have the id you think they do. Asp.net在ID前面加上它们的Asp.net容器的名称,因此当您在客户端运行时,您的JSON.Parse(以及其他可能的字段)不一定具有您认为的ID。

Check out this blog post for a detailed description. 查看此博客文章以获取详细描述。 You probably need to change something like this: 您可能需要更改以下内容:

<asp:HiddenField runat="server" ID="ChartData" ClientIDMode="Predictable" />

I don't know for sure if this is you issue, but doing a simple "View Source" in your web browser and checking the ID's of the html elements should tell you if this is the problem. 我不确定这是否是您的问题,但是在Web浏览器中执行一个简单的“查看源代码”并检查html元素的ID应该可以告诉您这是否是问题。

My previous answer was my first instinct based on past trouble with ASP.Net. 我以前的回答是我过去基于ASP.Net遇到的麻烦的第一个本能。 Upon looking further into your code, I think you may be misusing the dashboard. 在进一步研究代码后,我认为您可能正在滥用仪表板。

  • Make sure any charts that are not bound to a control are outside of the dashboard's div, but that the divs for the chart control and chart are inside it. 确保未绑定到控件的所有图表都在仪表板的div之外,但图表控件和图表的div在其中。
  • If you have a chart bound to a control using the dashboard, you should not be calling chart.draw() for that chart. 如果你有一个图表使用的仪表盘绑定到控件,你应该调用chart.draw()为该图。 The dashboard will handle drawing of that chart for you. 仪表板将为您处理该图表的绘制。
  • You may need to declare your dashboard-bound chart as a google.visualization.ChartWrapper instead of a google.visualization.PieChart , but I'm not certain of this one. 可能需要将仪表板绑定的图表声明为google.visualization.ChartWrapper而不是google.visualization.PieChart ,但是我不确定这一点。

I would recommend scrapping the dashboard for the moment, and testing the whole thing without it. 我建议暂时取消仪表板,然后在没有仪表板的情况下进行测试。 Dashboards a little more complicated than your regular charts. 仪表板比常规图表要复杂一些。

Also, as a side note, you can load all your google libraries with one call by adding the additional packages to the same request. 另外,请注意,您可以通过将其他软件包添加到同一请求中,一次调用即可加载所有Google库。

See https://developers.google.com/chart/interactive/docs/gallery/controls#using_controls_and_dashboards for more information on how to setup a chart and control with a dashboard. 请参阅https://developers.google.com/chart/interactive/docs/gallery/controls#using_controls_and_dashboards了解有关如何设置图表和使用仪表板进行控制的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM