按月将 JSON 中的值相加以获得 Fusion 图表

Add together values from JSON by month for a Fusion Charts graph

我正在使用 MySQL、PHP 和 Fusion Charts 创建一个折线图来跟踪 2016 年每个月开具发票的金额。我 table 我用于此图表的是 InvoiceAmount(小数类型)和 InvoiceDate(DateTime 类型)。

我的目标是让图表的 X 轴是 InvoiceDate 中的按月(一月、二月、三月等)日期,Y 轴是 InvoiceAmount 中的美元金额。我首先将 MySQL 数据转换为 JSON 格式,以便 Fusion Charts 可读:

//the SQL query to be executed
$query = "SELECT DATE_FORMAT(InvoiceDate, '%M') AS InvoiceMonth, InvoiceAmount FROM Estimates WHERE InvoiceDate IS NOT NULL AND YEAR(InvoiceDate) = 2016 AND InvoiceAmount IS NOT NULL AND InvoiceAmount > 0";

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
//Converting the results into an associative array
while($row = $result->fetch_assoc()) {
  $jsonArrayItem = array();
  $jsonArrayItem['label'] = $row['InvoiceMonth'];
  $jsonArrayItem['value'] = $row['InvoiceAmount'];
  //append the above created object into the main array.
  array_push($jsonArray, $jsonArrayItem);
  }
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function.
echo json_encode($jsonArray, JSON_PRETTY_PRINT);

这样输出 JSON:

[
{
    "label": "January",
    "value": "11361.00"
},
{
    "label": "December",
    "value": "1164.40"
},
{
    "label": "February",
    "value": "166.80"
},
{
    "label": "July",
    "value": "5088.00"
},
{
    "label": "January",
    "value": "214.50"
},
{
    "label": "June",
    "value": "620.40"
},
{
    "label": "July",
    "value": "5250.00"
},
{
    "label": "March",
    "value": "3425.00"
},
{
    "label": "January",
    "value": "3790.00"
},
{
    "label": "February",
    "value": "1909.80"
},
{
    "label": "January",
    "value": "1780.00"
},
{
    "label": "January",
    "value": "3060.00"
},
{
    "label": "January",
    "value": "2680.00"
},
{
    "label": "February",
    "value": "604.80"
}
]

等等

如您所见,有几个值附加了相同的月份。目前,折线图将这些 label/value 对中的每一对都视为一个不同的实例,而我需要的是将特定月份的所有值加在一起以得出一月、二月、三月的月度总值等

我可以在 PHP 或 MySQL 中使用一个函数来检查哪个月份附加到该值,然后将同一月份的所有值加在一起吗?请让我知道解决此问题的最佳方法。感谢您的帮助。

您可以使用 GROUP BYSUM 让 SQL 语句每月仅 return 一条记录:

SELECT   DATE_FORMAT(InvoiceDate, '%M') AS InvoiceMonth, 
         SUM(InvoiceAmount) AS InvoiceAmount
FROM     Estimates 
WHERE    YEAR(InvoiceDate) = 2016 
     AND InvoiceAmount > 0
GROUP BY DATE_FORMAT(InvoiceDate, '%M')

请注意,从长远来看,硬编码年份的查询不可维护。

我建议显示过去 12 个月。最后一列给出了"year-month",比如201508。它用于对结果进行排序:

SELECT   DATE_FORMAT(InvoiceDate, '%M') AS InvoiceMonth, 
         SUM(InvoiceAmount) AS InvoiceAmount,
         EXTRACT(YEAR_MONTH FROM InvoiceDate) As InvoiceYearMonth
FROM     Estimates 
WHERE    EXTRACT(YEAR_MONTH FROM InvoiceDate) >= EXTRACT(YEAR_MONTH FROM CURDATE())-100 
     AND InvoiceAmount > 0
GROUP BY DATE_FORMAT(InvoiceDate, '%M')
ORDER BY InvoiceYearMonth