将组数据填充到Highcharts上的特定系列 [英] Populate group data to a particular series on Highcharts

查看:75
本文介绍了将组数据填充到Highcharts上的特定系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试对拖曳列进行分组,并将其填充为高图上的特定系列.我的代码没有对列进行分组,而是将所有数据显示为一个系列.

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>

JSFiddle中的预期输出

数据响应

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"]

推荐答案

通过摆弄您想要的HighCharts预期输出,如下所示:

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

1:类别数据:

  • 应该是日期数组.
  • 请确保删除重复项,并按照需要的升序/降序对其进行排序,以查看连续的图形.
"categories":["2019-02-07", "2019-02-08", "2019-02-09", "2019-02-12", "2019-02-13", "2019-02-14"]

2:系列数据:

  • 它将是一个对象数组,其中每个对象包含两个属性namedata.
  • 如果您的categories数组具有n值,并且每个值对应于相同的索引,则
  • 数据应没有n个值.
  • 由于我们没有每个班级每个日期的数据,因此我们在此处添加0.
  • 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.

所以数据看起来像

"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]
      }
   ]

最终小提琴可以通过PHP使用以下代码来实现:

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]}]}

更新您的JavaScript代码以反映以上内容

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,

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

这篇关于将组数据填充到Highcharts上的特定系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆