当我重新加载 div 时,Google 折线图不会重新加载

发布于 2024-11-08 21:51:45 字数 1861 浏览 0 评论 0原文

我在使用 Google 折线图 API 时遇到问题。当我重新加载 div 时,折线图不会再次加载。

这是折线图的代码

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Year');
        data.addColumn('number', 'Sales');

        data.addRows(4);


         <?php 
            //gets line chart data
            echo $join;?>


        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 320, height: 250, title: ''});
  }

</script>

<h3>Sales History</h3>
    <table border="5" style="border-color: #3b6aa8;" >
        <tr>
            <td>
                <div id="chart_div" style="border: 1;border-color: green;"></div>
            </td>
        </tr>
</table>

用于重新加载 div 的代码

<script type="text/javascript">
$(document).ready(function() {


$('#sliderform').submit(function() {

    bodyContent = $.ajax({
          url: "dashboardreload.php",
          global: false,
          type: "POST",
          data: ({year : $("#yearval").val(), month: $("#monthval").val(),day : $("#dayval").val(),
          year1 : $("#year1val").val(), month1: $("#month1val").val(),day1 : $("#day1val").val()
                    }),
          dataType: "html",
          async:false,
          success: function(msg){
           //alert('test');
          }
       }
    ).responseText;

    $('#dash').html(bodyContent);
    //confirm(bodyContent);
    return false;


    });

});
</script>

I am having issue with Google line chart API. When I reload the div, the line chart does not load again.

This is the code for line chart

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Year');
        data.addColumn('number', 'Sales');

        data.addRows(4);


         <?php 
            //gets line chart data
            echo $join;?>


        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 320, height: 250, title: ''});
  }

</script>

<h3>Sales History</h3>
    <table border="5" style="border-color: #3b6aa8;" >
        <tr>
            <td>
                <div id="chart_div" style="border: 1;border-color: green;"></div>
            </td>
        </tr>
</table>

code used to reload the div

<script type="text/javascript">
$(document).ready(function() {


$('#sliderform').submit(function() {

    bodyContent = $.ajax({
          url: "dashboardreload.php",
          global: false,
          type: "POST",
          data: ({year : $("#yearval").val(), month: $("#monthval").val(),day : $("#dayval").val(),
          year1 : $("#year1val").val(), month1: $("#month1val").val(),day1 : $("#day1val").val()
                    }),
          dataType: "html",
          async:false,
          success: function(msg){
           //alert('test');
          }
       }
    ).responseText;

    $('#dash').html(bodyContent);
    //confirm(bodyContent);
    return false;


    });

});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-11-15 21:51:46

所以我假设代码

$('#dash').html(bodyContent);

正在尝试将 div 重新插入页面
并且您正在从服务器获取新版本,

#dash 元素在您的代码中不可见,但我是
假设它是父标签。

这段代码试图

bodyContent = $.ajax({
      url: "dashboardreload.php",
      global: false,
      type: "POST",
      data: ({
         year : $("#yearval").val(), month: $("#monthval").val(),day : $("#dayval").val(),
      year1 : $("#year1val").val(), month1: $("#month1val").val(),day1 : $("#day1val").val()
                }),
      dataType: "html",
      async:false,
      success: function(msg){
       //alert('test');
      }
   }
).responseText;

首先在 javascript 中重新获取页面,您定义一个这样的变量,

var bodyContent = .....

否则您将遇到一些麻烦,否则在全局范围内

,其次 ajax 调用是异步的,您拥有的成功方法是什么
在 rdy 时为您提供服务器的响应

success: function(bodyContent){
   $('#dash').html(bodyContent);
}

我希望这有帮助

so im going to asume that the code

$('#dash').html(bodyContent);

is trying to reinsert the div into the page
and you are fetching a new version from the server

the #dash element is not visible in your code here but i am
assuming its a parent tag.

and this code is trying to refetch the page

bodyContent = $.ajax({
      url: "dashboardreload.php",
      global: false,
      type: "POST",
      data: ({
         year : $("#yearval").val(), month: $("#monthval").val(),day : $("#dayval").val(),
      year1 : $("#year1val").val(), month1: $("#month1val").val(),day1 : $("#day1val").val()
                }),
      dataType: "html",
      async:false,
      success: function(msg){
       //alert('test');
      }
   }
).responseText;

first of all in javascript you define a variable like this

var bodyContent = .....

you will run in to some truble otherwise with global scope

and secondly ajax calls are async the success method you have there is what
gives you the response from the server when its rdy

success: function(bodyContent){
   $('#dash').html(bodyContent);
}

i hope this helps

三月梨花 2024-11-15 21:51:45

所以这可能是问题所在,

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);

即使您正在重新加载此内容,页面仍然加载了这些引用
并且第二次可能不会调用drawChart,因为
谷歌脚本没有加载任何新内容

来解决这个问题,您可能想在加载新内容后自己调用drawChart函数,

success: function(msg){
   drawChart();
 }

也许这会有所帮助。格

so this might be the problem

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);

even tho you are reloading this content the page still has these references loaded
and there might not ever be a call to drawChart on the second time around because
the google script isn't loading any new content

to get around this you might want to call the drawChart function yourself after having loaded the new content

success: function(msg){
   drawChart();
 }

maybe this will help. gl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文