使用wordpress和mysql数据库从php创建google图表

mwkjh3gx  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(289)

我正在尝试使用php将google图表从mysql数据库插入wordpress网站。在我的数据源下面:

php mysql谷歌图表示例:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
      google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable(<?php echo $chartDataInJson; ?>);

        var options = {
          chart: {
            title: 'Company Performance',
            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
          }
        };

        var chart = new google.charts.Bar(document.getElementById('columnchart_material'));

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

我想填充数组$chartdatainjson,如下所示:

<?php
  $chartData = array(
        array('created_date', 'AAA', 'BBB', 'CCC'),
        array('2018-09-07', 9, 7, 4),
        array('2018-09-08', 10, 6, 12),
        array('2018-09-09', 11, 5, 3)
    );

$chartDataInJson = json_encode($chartData);
?>

请了解如何用php从数据库中动态加载这些值。我试过这个:

<?php
  global $wpdb;
  $result = "SELECT * FROM stat GROUP BY created_date";
  $chartData = array();
  foreach ($result as $row)  {
     //code
  }
  $chartDataInJson = json_encode($chartData);
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题