使用PHP(和Laravel)创建包含数据的高清线图

时间:2014-10-23 01:47:18

标签: php laravel-4 highcharts

我有一个看起来像的数据集:

month_name | intake_total | adoption_totals
January | 12 | 36
February | 4 | 12
March | 23 | 46
April | 45 | 89
May | 10 | 15
June | 15 | 20
July | 23 | 22
August | 23 | 45
September | 45 | 67
October  | 23 | 12
November | 45 | 100
December | 0 | 12

我正在尝试创建一个显示折线图的数组

我有:

$chartArray["chart"] = array("type" => "line");
$chartArray["title"] = array("text" => "Intakes vs. Adoptions");
$chartArray["credits"] = array("enabled" => false);
$chartArray["xAxis"] = array("categories" => array());
foreach ($results as $result)
{
$categoryArray[] = $result->month_name;
$chartArray["series"][] = array("name" => 'Intake Totals', "data" => array($result->intake_total));
$chartArray["series"][] = array("name" => 'Adoption Totals', "data" => array($result->adoption_total));
}
$chartArray["xAxis"] = array("categories" => $categoryArray);
$chartArray["yAxis"] = array("title" => array("text" => "Number of Pets"));
return $chartArray;

我知道我需要使用每种类型的所有行填充每个系列的数据,但我无法弄清楚如何做到这一点。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

想出来。也许这会帮助别人:

$chartArray["chart"] = array("type" => "line");
        $chartArray["title"] = array("text" => "Intakes vs. Adoptions");
        $chartArray["credits"] = array("enabled" => false);
        $chartArray["xAxis"] = array("categories" => array());
        foreach ($results as $result)
        {
            $categoryArray[] = $result->month_name;

        }

        $intaketotal = [];
        $adoptiontotal = [];
        foreach($results as $result){
            array_push($intaketotal, $result->intake_total);
            array_push($adoptiontotal, $result->adoption_total);
        }

        $chartArray["series"][] = array("name" => 'Intake Totals', "data" =>$intaketotal);
        $chartArray["series"][] = array("name" => 'Adoption Totals', "data" => $adoptiontotal);

        $chartArray["xAxis"] = array("categories" => $categoryArray);
        $chartArray["yAxis"] = array("title" => array("text" => "Number of Pets"));

        return $chartArray;