Highcharts列范围 - 如何呈现存储在PHP变量中的系列数据?

时间:2016-11-18 14:48:22

标签: php highcharts

尝试正确渲染Highcharts列范围表时遇到问题。我将所有必要的数据存储在PHP变量中。问题是我传递这些变量时无法获取表格。 如果我尝试传递直接数字数据,则会呈现表格,因此问题出在data字段中。

这是我的代码:

<?php
if (!isset($divId)) {
    $mt = microtime();
    $mt = explode(" ", $mt);
    $divId = "stacked_container_" . floor($mt[0] * 100000) . "_" . $mt[1];
}
?>
<div id="<?php echo $divId; ?>" class="col-mx-12"></div>

<?php
$title = $elements['title'];
$resources = $elements['series']['resources'];
$values = $elements['series']['values'];

$resourcesSeries = '';
$valuesMinSeries = '';
$valuesMaxSeries = '';
foreach ($resources as $key => $resource) {
    $resourcesSeries .= '"' . $resource . '",';
    $valuesMinSeries .=  $values[$key][0] . ',';
    $valuesMaxSeries .=  $values[$key][1] . ',';
}

$globalNav = $view['nav'];

// Transforming strings into array type
$valuesMin = str_split($valuesMinSeries);
$valuesMax = str_split($valuesMaxSeries);

$globalNav->addScriptJS("
        $(document).ready(function () {
             $('#" . $divId . "').highcharts({
        chart: {
            type: 'columnrange'
        },
        title: {
            text: '" . $title . "'
        },
        xAxis: {
            categories: [" . $resourcesSeries . "]
        },
        yAxis: {
            title: {
                text: 'range'
            }
        },
        series: [{
            data: [ 
                    [<?php echo $valuesMin[0]?> , <?php echo $valuesMax[0]?> ]
                   ]
            }]
        });

    });
    ");

?>

在此示例中,我尝试仅显示$valuesMin$valuesMax的第一个元素。 最终目标是渲染所有数据,因此我也需要实现foreach

2 个答案:

答案 0 :(得分:0)

您似乎正在复制resources数组,将其称为$resources变量和$values变量:

$resources = $elements['series']['resources'];
$values = $elements['series']['resources'];

我将假设应该是这样的:

$resources = $elements['series']['resources'];
$values = $elements['series']['values'];

答案 1 :(得分:0)

我找到了解决问题的方法。这是如何:

    //The result will be in the $data variable
    $data = '';
    foreach ($resources as $key => $resource) {
    $resourcesSeries .= '"' . $resource . '",';
    $valuesMinSeries .=  $values[$key][0] . ',';
    $valuesMaxSeries .=  $values[$key][1] . ',';
    $data .= "[".values[$key][0]. "," .$values[$key][1]. "],";
    }

series字段中:

series: [{
        data: [ " .$data. " ]
        }]

希望这会有所帮助!