按月将JSON中的值添加到Fusion Charts图表中

Liz*_*Liz 2 php mysql json fusioncharts

我使用MySQL,PHP和Fusion图表来创建一个折线图来跟踪2016年每个月的发票金额.我在表格中使用的两列是InvoiceAmount(十进制类型)和InvoiceDate( DateTime类型).

我的目标是让我的图表的X轴是来自InvoiceDate的月份(Jan,Feb,Mar等)的日期,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);
Run Code Online (Sandbox Code Playgroud)

这输出一个像这样的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"
}
]
Run Code Online (Sandbox Code Playgroud)

等等

正如您所看到的,有几个值附加了相同的月份.目前,线图将这些标签/值对中的每一个视为一个不同的实例,当我需要的是将特定月份的所有值加在一起以制作1月,2月,3月等的整体月度值时.

当前的折线图示例.

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

tri*_*cot 5

您可以使用GROUP BYSUM让SQL语句每月只返回一条记录:

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')
Run Code Online (Sandbox Code Playgroud)

请注意,从长远来看,具有硬编码年份的查询无法维护.

我建议显示过去12个月.最后一列给出了"年 - 月",如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
Run Code Online (Sandbox Code Playgroud)