我想在饼图中显示员工数据。我能够使用jQuery和php制作饼图。但需求是在一个页面中有12个按钮。姓名1月至12月。
当用户点击feb时,会向用户显示feb的饼图。点击dec dec饼图。向用户显示。
在我的数据库中有一个date
列,其中存储日期,如2015-06-23
我无法获得SQL查询的逻辑,这是我的代码,但需要改进。
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<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
$servername = "localhost";
$username = "root";
$dbname = "dat_database";
$password = "";
$conn = new mysqli($servername, $username, $password, $dbname);
$EMP_ID = 'MMUM254';
$sql = "SELECT typeof_task,hourspend from emp_dat_info where EMP_ID='" . $EMP_ID. "' ORDER BY date DESC LIMIT 10 " ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$a[] = "['" . $row["typeof_task"] . "', " . $row["hourspend"] . "]";
}
echo implode(', ', $a);
}
?>
]);
// Set chart options
var options = {'title':'How Much Hours You Spend',
'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>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
<?php echo date("Y"); ?>
`
答案 0 :(得分:1)
您可以在代码中尝试使用month
子句来获取月份数据
$sql = "SELECT typeof_task,hourspend
FROM emp_dat_info
WHERE EMP_ID='" . $EMP_ID. "'
ORDER BY MONTH(date) DESC LIMIT 10 " ;
$result = $conn->query($sql);
答案 1 :(得分:1)
如果要获取月份的全名,可以在查询中尝试MONTHNAME子句。在MONTHNAME子句中传递日期。
$sql = "SELECT typeof_task,hourspend
FROM emp_dat_info
WHERE EMP_ID='" . $EMP_ID. "'
ORDER BY MONTHNAME(date) DESC LIMIT 10 " ;
$result = $conn->query($sql);
有关详情,请访问以下链接:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month
答案 2 :(得分:1)
尝试使用日期
的MONTH$sql = "SELECT typeof_task,hourspend
FROM emp_dat_info
WHERE EMP_ID='" . $EMP_ID. "'
ORDER BY MONTH(date) DESC LIMIT 10 " ;
$result= $conn->query($sql);