首页 > 解决方案 > 如何将日期与创建日期分开并放入图表?

问题描述

我对将我的数据实现为图表真的很陌生。昨天我正在处理饼图并完成了我的工作。现在,我偶然发现了一个条形图,它每月有两个条形图,我想制作。我看过几个示例条形图。在我的数据库中,我有两个表用于两种类型的用户(造型师和客户)。当我注册到某个用户类型时,我的表中会有一date_created列指示帐户的创建。至于所有用户表。它的格式是2019-02-16yy-mm--dd。我的计划是获取中间的月份和年份,并且在我的图表中(每月 2 条,有来自网络的示例代码)将用于每个表。各位大佬能帮我解决一下吗?我真的不明白如何实现这一点。

这是我的 php 文件。

<?php $connect = mysqli_connect("xxx","xxx","xxx","xxx");  

//THIS IS THE FIRST BAR for the graph, it needs to be replaced with my data
$dataPoints1 = array(
        array("label"=> "2010", "y"=> 36.12),
        array("label"=> "2011", "y"=> 34.87),
        array("label"=> "2012", "y"=> 40.30),
        array("label"=> "2013", "y"=> 35.30),
        array("label"=> "2014", "y"=> 39.50),
        array("label"=> "2015", "y"=> 50.82),
        array("label"=> "2016", "y"=> 74.70)
    ); 
    //THIS IS THE SECOND BAR in the graph and also needs my data
    $dataPoints2 = array(
        array("label"=> "2010", "y"=> 64.61),
        array("label"=> "2011", "y"=> 70.55),
        array("label"=> "2012", "y"=> 72.50),
        array("label"=> "2013", "y"=> 81.30),
        array("label"=> "2014", "y"=> 63.60),
        array("label"=> "2015", "y"=> 69.38),
        array("label"=> "2016", "y"=> 98.70)
 );


 <!DOCTYPE html>
 <html lang="en">
 <head>
<script>
        window.onload = function () {

        var chart = new CanvasJS.Chart("chartContainer", {
            animationEnabled: true,
            theme: "light2",
            title:{
                text: "Users registered every month"
            },
            legend:{
                cursor: "pointer",
                verticalAlign: "center",
                horizontalAlign: "right",
                itemclick: toggleDataSeries
            },
            data: [{
                type: "column",
                name: "Real Trees",
                indexLabel: "{y}",
                yValueFormatString: "$#0.##",
                showInLegend: true,
                dataPoints: <?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?>
            },{
                type: "column",
                name: "Artificial Trees",
                indexLabel: "{y}",
                yValueFormatString: "$#0.##",
                showInLegend: true,
                dataPoints: <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>
            }]
        });
        chart.render();

        function toggleDataSeries(e){
            if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                e.dataSeries.visible = false;
            }
            else{
                e.dataSeries.visible = true;
            }
            chart.render();
        }

        }
        </script>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Admin</title>
      <div  id="piechart" style="width: 625px; height: 400px; border: 3px solid black; border-radius: 25px; margin-bottom: 20px; padding: 10px;"></div>
  </body>
</html>

标签: phpdatabase

解决方案


在查询中使用 Date_format 函数,您可以获得月份和年份

DATE_FORMAT($date,'%d-%b-%Y');

一个月

DATE_FORMAT($date,'%b');

推荐阅读