使用PHP为HighCharts正确格式化JSON

时间:2016-11-18 13:52:34

标签: javascript php json highcharts

大家好!我正在使用HighCharts创建一个柱状图,可以向下钻取到堆积的柱状栏中。我发现的例子是工作,现在我只需要以正确的格式获取我的数据。我没有得到{[的正确..

我正在使用的示例是:http://jsfiddle.net/49q18Lp3/21/

我在Laravel 5.2应用程序中创建JSON文件并将它们加载到JavaScript中。我在控制器中创建这些JSON文件,该控制器从mongo驱动程序获取数据。我将添加部分代码:

$drilldown_data = array();
$data_drilldown_file = array();

foreach ($id_array as $char) {        
    $cursor = $collection->find(array("_id" => new MongoId($char)));
    foreach ($cursor as $document) {
        if (is_array($document['comments'])) {
            foreach ($document['comments'] as $comments) {
                $data_filtered = array();
                array_push($data_filtered, array('filtered on language', $comments['nr_filtered_on_language']));
                array_push($data_filtered, array('filtered on non relevant', $comments['nr_filtered_on_relevant']));
                array_push($data_filtered, array('filtered on spam', $comments['nr_filtered_on_spam']));
                array_push($data_filtered, array('filtered on etc', $comments['nr_filtered_on_etc']));


                $array_4 = array("name" => $document['project_id'],
                        "data" => $data_filtered
                    );
                array_push($drilldown_data, $array_4);
            }
        }
    }
}

array_push($data_drilldown_file, json_encode($drilldown_data));
$results = File::put('/path/to/json.json', $data_drilldown_file);
// Exception caught!
if ($results === false) {
    die("Error writing to file");
}
// Replace the "][" with ",", needed for multiple selected projects!
$oldMessage = "][";
$deletedFormat = ",";
$str = file_get_contents('/path/to/json.json');
$str = str_replace("$oldMessage", "$deletedFormat", $str);
$str = substr($str, 1, -1);
// Replaces the old content with the new!
file_put_contents('/path/to/json.json', $str);

这会产生以下JSON格式:

{
    "name": 1,
    "data": [
        ["filtered on language", 223],
        ["filtered on non relevant", 1985],
        ["filtered on spam", 2356],
        ["filtered on etc", 2035]
    ]
}, {
    "name": 1,
    "data": [
        ["filtered on language", 223],
        ["filtered on non relevant", 1985],
        ["filtered on spam", 2356],
        ["filtered on etc", 2035]
    ]
}, {
    "name": 1,
    "data": [
        ["filtered on language", 223],
        ["filtered on non relevant", 1985],
        ["filtered on spam", 2356],
        ["filtered on etc", 2035]
    ]
}, {
    "name": 67,
    "data": [
        ["filtered on language", 223],
        ["filtered on non relevant", 1985],
        ["filtered on spam", 2356],
        ["filtered on etc", 2035]
    ]
}, {
    "name": 67,
    "data": [
        ["filtered on language", 223],
        ["filtered on non relevant", 1985],
        ["filtered on spam", 2356],
        ["filtered on etc", 2035]
    ]
}, {
    "name": 67,
    "data": [
        ["filtered on language", 223],
        ["filtered on non relevant", 1985],
        ["filtered on spam", 2356],
        ["filtered on etc", 2035]
    ]
}

但格式必须如下:

{
                                'Animals': {
                                    name: 'Animals',
                                    data: [
                                        ['Cows', 2],
                                        ['Sheep', 3]
                                    ]
                                },
                                    'Animals2': {
                                    name: 'Animals',
                                    color: Highcharts.getOptions().colors[1],
                                    data: [
                                        ['Cows', 22],
                                        ['Sheep', 13]
                                    ]
                                },
                                    'Fruits': {
                                    name: 'Fruits',
                                    data: [
                                        ['Apples', 5],
                                        ['Oranges', 7],
                                        ['Bananas', 2]
                                    ]
                                },
                                    'Fruits2': {
                                    name: 'Fruits',
                                    color: 'red',
                                    data: [
                                        ['Apples', 15],
                                        ['Oranges', 17],
                                        ['Bananas', 22]
                                    ]
                                },
                                    'Cars': {
                                    name: 'Cars',
                                    data: [
                                        ['Toyota', 1],
                                        ['Volkswagen', 2],
                                        ['Opel', 5]
                                    ]
                                },
                                    'Cars2': {
                                    name: 'Cars',
                                    color: '#bada55',
                                    data: [
                                        ['Toyota', 11],
                                        ['Volkswagen', 21],
                                        ['Opel', 15]
                                    ]
                                }
                            }

有人可以帮我解决这个问题吗?

0 个答案:

没有答案