javascript - 多个 Google 图表,只有一个可用

标签 javascript charts google-visualization

我想在一页上显示多个 Google 图表。我搜索了一种解决方案,并在此站点上找到了一个。轻松创建新图表的功能。现在它只适用于一张图表,当我添加第二张图表时,它们都不再工作了。 Json 和 div id 是正确的,当我只使用其中 1 个图表的代码时,两者都有效。知道这里的问题/解决方案是什么吗?

$(function() {
var jsonDataClicks = $.ajax({
    url: 'ajax/get-total-clicks.php',
    dataType: 'json',
    async: false
    }).responseText;

var jsonDataActiveProducts = $.ajax({
    url: 'ajax/get-total-active-products.php',
    dataType: 'json',
    async: false
    }).responseText;

google.charts.load('current', {'packages':['corechart']});

google.charts.setOnLoadCallback(function() {
    drawChart(jsonDataClicks, 'total_clicks');
});

google.charts.setOnLoadCallback(function() {
    drawChart(jsonDataActiveProducts, 'total_active_products');
});

function drawChart(dataInput, containerID) {
    var data = new google.visualization.DataTable(dataInput);
    var containerDiv = document.getElementById(containerID);

    var options = {
        vAxis: {
            gridlines: {
                color: '#ebebeb'
            }
        },
        animation: {
            startup: true,
            duration: 1000,
            easing: 'out'
        },
        hAxis: {
            slantedText: true,
            slantedTextAngle: 45,
            textStyle : {
                fontName: 'Lato',
                fontSize: 11,
                color: '#6f6f6f'
            }
        },
        vAxis: {
            textStyle: {
                fontName: 'Lato',
                fontSize: 11,
                color: '#6f6f6f'
            }
        },
        tooltip: {
            textStyle: {
                fontName: 'Lato',
                fontSize: 11,
                color: '#6f6f6f'
            }
        },
        width: 1160,
        height: 400,
        chartArea: {'width': '80%', 'height': '80%'},
        colors: ['#47ab8b'],
        legend: 'none'
    };

    var chart = new google.visualization.ColumnChart(containerDiv);

    chart.draw(data, options);
}

});

最佳答案

方法 --> google.charts.setOnLoadCallback -- 每个页面加载实际上应该只调用一次

相反,请将'callback'添加到google.charts.load语句

另外,强烈建议不要使用 async: false 进行 $.ajax 调用

您可以在$.ajax({}).done上绘制图表

推荐与以下类似的设置...

$(function() {
  google.charts.load('current', {
    'callback': function () {

      drawChart('ajax/get-total-clicks.php', 'total_clicks');
      drawChart('ajax/get-total-active-products.php', 'total_active_products');

      function drawChart(dataURL, containerID) {
        $.ajax({
          url: dataURL,
          dataType: 'json'
        }).done(function (dataInput) {
          var data = new google.visualization.DataTable(dataInput);
          var containerDiv = document.getElementById(containerID);

          var options = {
              vAxis: {
                  gridlines: {
                      color: '#ebebeb'
                  }
              },
              animation: {
                  startup: true,
                  duration: 1000,
                  easing: 'out'
              },
              hAxis: {
                  slantedText: true,
                  slantedTextAngle: 45,
                  textStyle : {
                      fontName: 'Lato',
                      fontSize: 11,
                      color: '#6f6f6f'
                  }
              },
              vAxis: {
                  textStyle: {
                      fontName: 'Lato',
                      fontSize: 11,
                      color: '#6f6f6f'
                  }
              },
              tooltip: {
                  textStyle: {
                      fontName: 'Lato',
                      fontSize: 11,
                      color: '#6f6f6f'
                  }
              },
              width: 1160,
              height: 400,
              chartArea: {'width': '80%', 'height': '80%'},
              colors: ['#47ab8b'],
              legend: 'none'
          };

          var chart = new google.visualization.ColumnChart(containerDiv);
          chart.draw(data, options);
        });
      }
    },
    'packages':['corechart']
  });
});

关于javascript - 多个 Google 图表,只有一个可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40682827/

相关文章:

javascript - 谷歌图表,在轴上设置最小范围

google-sheets - 谷歌表格中的多层嵌套饼图

javascript - 使用 jQuery AJAX 的 JSONP 回调函数

javascript - 背景图像放大使用-webkit?

php - 可创建负条形图并能够叠加第二个图表的图表库

javascript - 在谷歌柱形图中的条形图上显示类型名称

javascript - Highcharts:在旭日图中实现类似于饼图的分解部分行为

javascript - 通知小部件不会每隔两次打开一次

jquery - 在条形内显示值 - jQuery 绘制水平堆积条形图

php - 使用 php 和 mysql 的 GoogleCharts