显示每月mysql记录的图表

时间:2020-09-10 18:00:03

标签: mysql pdo charts

我希望显示数据库中我的记录的html5 js月度图表,其中列状态=“销售”(计数等于销售的状态总数)并且日期为当前时间戳。

SELECT * FROM products WHERE status = 'Sale' ORDER BY MONTH(date), id DESC

类似此页面https://phppot.com/demo/creating-dynamic-data-graph-using-php-and-chart-js/的东西,但我希望显示的是像2020年9月这样的月份,而不是2020年10月,然后是计数。

到目前为止我尝试过什么,但是从数据库中什么也没得到

<script type="text/javascript">
      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});
      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);
      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {
        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows
        ([
 <?php
            $sql        = "SELECT * FROM products WHERE status = 'Sale' ORDER BY MONTH(date)";
            $result     = $db->query($sql);
            if ($result->rowCount > 0) {
                            while ($row = $result->fetch()) {
                                $a[] = "['".$row["status"]."']";
                }
                echo implode(', ', $a);
}
?>

        ]);

        // Set chart options
        var options = {'title':'Monthly  sales chart',
                       'width':900,
                       'height':500};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

我不知道如何做到这一点,如果有人可以给我看,我将不胜感激?

致谢,非常感谢

0 个答案:

没有答案