Populate group data to a particular series on Highcharts

邮差的信 提交于 2019-12-20 04:22:22

问题


Trying to group tow columns and and populate it as a particular series on highcharts. my code not grouping columns, And showing all data as a single series.

$query = $db->Prepare("SELECT class, SUM(marks), DATE(date_column) as 
dates FROM classes GROUP BY class,DATE(date_column)");
$query->execute();
while ( $row = $query->fetch(PDO:: FETCH_ASSOC)) {
    $date = $row['dates'];
    $dates[] = $row['dates'];
    $class[] = $row['class'];
    $marks[] = $row['SUM(marks)'];

    $output[] = array($date,(int)$marks);

 }

echo json_encode(array('date'=>$dates,'marks'=>$marks, 'name' => $class)); 

JS

<script>
  $(document).ready(function () {
    $(function() {   
    $.getJSON('data.php', function(data) {
        // Create the chart
       Highcharts.chart('container', {
          title: {
          text: 'class Marks'
          },

            xAxis: {
                categories: data.dates
            },

        series : [{
            "name" : data.name,
            "data": data.marks
        }],

      });
   });
 });
});
</script>

Table

Expected output in JSFiddle

data response

date: ["2019-02-07", "2019-02-09", "2019-02-12", "2019-02-08", "2019-02-12", 
"2019-02-13", "2019-03-13",…]
marks: ["45", "166", "78", "64", "64", "627", "87", "352"]
name: ["claas 1", "claas 1", "claas 1", "class 2", "class 2", "class 2", "class 3", "class 3"]

回答1:


By seeing your fiddle the expected output you want for HighCharts is as follow:

1: Category Data:

  • It should be an array of dates.
  • Make sure you remove duplicates and order them in ascending/descending order whichever you want, to see a continuous graph.
"categories":["2019-02-07", "2019-02-08", "2019-02-09", "2019-02-12", "2019-02-13", "2019-02-14"]

2: Series Data:

  • It would be an array of object, where each object contains two properties name and data.
  • Data should have n no of values if your categories array has n values and each corresponds to same index.
  • As we don't have data for each date for each class, we would add 0 there.

So data would look like

"series":[
      {
         "name":"class 1",
         "data":[45,0,166,78,0,0]
      },
      {
         "name":"class 2",
         "data":[0,64,0,64,627,0]
      },
      {
         "name":"class 3",
         "data":[0,0,0,0,87,352]
      }
   ]

Final Fiddle which can be achieved by PHP using below code:

$arrDates = [];
$arrClass = [];
$arrData  = [];

while ( $row = $query->fetch(PDO:: FETCH_ASSOC)) {
  $arrDates[] = $row['dates'];
  $arrClass[] = $row['class'];
  $arrData[$row['class'] . ':' . $row['dates']] = $row['marks']; // to identify for which date & class this marks belong to, we can directly search for index.
}

$arrDates = array_unique($arrDates);
sort($arrDates);
$arrClass = array_unique($arrClass);

// Create series data
$arrSeriesData = [];
foreach($arrClass as $strClass){
  $tempArr = ['name' =>  $strClass];
  foreach($arrDates as $strDate){
      $tempArr['data'][] = isset($arrData[$strClass . ':' . $strDate]) ? intval($arrData[$strClass . ':' . $strDate]) : 0;
  }

  $arrSeriesData[] = $tempArr;
}


$response = ['categories' => $arrDates, 'series' => $arrSeriesData];

echo json_encode($response);

Output:
{"categories":["2019-02-07","2019-02-08","2019-02-09","2019-02-12","2019-02-13","2019-02-14"],"series":[{"name":"class 1","data":[45,0,166,78,0,0]},{"name":"class 2","data":[0,64,0,64,627,0]},{"name":"class 3","data":[0,0,0,0,87,352]}]}

Update your javascript code to reflect the above

$(document).ready(function() {
    $(function() {
        $.getJSON('data.php', function(data) {
            // Create the chart
            Highcharts.chart('container', {
                title: {
                    text: 'class Marks'
                },

                xAxis: {
                    categories: data.categories
                },
                series: data.series,

            });
        });
    });
});


来源:https://stackoverflow.com/questions/57867138/populate-group-data-to-a-particular-series-on-highcharts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!