将数据实现为图表真的是我的新手。昨天我正在处理饼图,并完成了它的工作。现在,我偶然发现了一个条形图,该条形图每月我要制作两个条形图。我看过几个示例条形图。在我的数据库中,我有两个表用于两种类型的用户(Stylist和Client)。当我注册为某种用户类型时,我的表中将出现一个date_created
列,指示帐户的创建。至于所有用户表。其格式为2019-02-16
,因此为yy-mm--dd。我正在计划获取中间月份和年份月份,并且在我的图表中(每个月2bars,有一个来自网络的示例代码)将用于每个表。先生,您能帮我吗?我真的不明白如何实现这一目标。
这是我的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>
答案 0 :(得分:0)
在查询中使用Date_format函数可以获得月份和年份
DATE_FORMAT($date,'%d-%b-%Y');
一个月
DATE_FORMAT($date,'%b');