gpt4 book ai didi

javascript - 按月份显示数据

转载 作者:行者123 更新时间:2023-11-29 05:17:17 26 4
gpt4 key购买 nike

我想在饼图中显示员工数据。我能够使用 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"); ?>

`

最佳答案

您可以在代码中尝试使用 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);

关于javascript - 按月份显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com