使用PHP的Mysql数据集中的Google API折线图

PHP to arrange result set for google API I am having a problem with the fetched dataset arrangement for the google API charts. I want to plot line on google line chart but I cant seems to get the data in correct format.

I have 4 departments: Dispatch, Sales, Support , Calibration (To be used as labels)

X Axis: month : January, February, March, April etc

value: value, 50, 35, 65, 120 etc

mysql database result set :

    Department     Month      Value
    Sale        February       50
    Sale        March          35
    Sale        April          65
    Sale        May            120
    Dispatch   February       85
    Dispatch   March          23
    Dispatch   April          45
    Dispatch   May            33
    .... etc

And this is repeated for all four departments.

I want PHP to format data result so this looks like this:

    ['Month',   'Sales',  'Dispatch',   'Support', 'Calibration'],
    ['February', '50',     '85',         '15',      '53'],
    ['March',    35'',     '23',         '12',      '55'],

etc

$sql_line_chart = "SELECT d.department as department, MONTHNAME(date_created) AS month, 
                   COALESCE(SUM(CASE WHEN c.cat_id IN (5,6,7,8,9,10,11,12,13,15) THEN time_spent END), 0) as value
                    FROM master AS m 
                     INNER JOIN category AS c ON c.cat_id = m.cat_id 
                      INNER JOIN department AS d ON d.dept_id = m.dept_id 
                      WHERE 
                      AND 
                      date_created > CURRENT_DATE - INTERVAL DAYOFYEAR(CURRENT_DATE) - 1 DAY 
                     AND  
                    date_created < CURRENT_DATE - INTERVAL DAYOFYEAR(CURRENT_DATE) - 1 DAY + INTERVAL 1 YEAR 
                   GROUP BY d.department, month 
                  ORDER by d.department ASC, m.date_created ASC"; 


        <div class="col-lg-6"> 
           <script type="text/javascript"> 

            google.charts.setOnLoadCallback(lineChart); 

      function lineChart() { 
        var data = google.visualization.arrayToDataTable([ 


       ['Month', 'Dispatch', 'Sales', 'Support', 'Calibration'], 

             $result = query($sql_line_chart); 
             confirm_result($result); 

                while ($row = fetch_array($result)) { 

                $down_time_spent            = $row['value']; 
                $month                      = $row['month']; 


                echo "['{$month}' " . "," ."{$value}],"; 

      } ?> 

        ]); 

        var options = { 
          title: 'Performance Trace', 
          curveType: 'none', 
          legend: { position: 'bottom' } 
        }; 

        var chart = new       google.visualization.LineChart(document.getElementById('line_chart')); 

          chart.draw(data, options); 
      } 
       </script> 

          <div id="line_chart" style="width: 1200px; height: 500px"></div> 



    </div>

This does not work, How do I get the data arranged so it a plot for all 4 departments?

Any help would be appreciated.

Thanks.