如何在高线图中输入具有x轴和y轴值的系列数据? [英] How to feed series data that has x axis and y axis values in line highcharts?
问题描述
我必须在折线图中显示实验的一些结果.我让它适用于静态数据,但我想使其动态化.
I have to show some results of an experiment in a line chart. I got it working for static data, but I want to make it dynamic.
所以我得到了:
我有一个折线图:
var options = {
chart: {
renderTo: 'container',
type: 'line',
},
series: [{}]
}
我想要这样的东西:我正在努力制作这些数据:
I am struggling to make those data:
data: [
[0, 29.9],
[1, 71.5],
[3, 106.4]
]
在php端
并将其发送给jquery,并提供给我的折线图. 我知道JSON编码,但是首先应该如何在php中创建数组? 以及如何编码并将其提供给jquery并提供折线图?
in php side and send it jquery, and feed to my line chart. I am aware of JSON encoding, but how should I create and array in php in first place? And how should I encode it and give it to jquery and feed my line chart?
到目前为止,此方法可以处理静态数据:
This is working so far with static data:
var arr = [[0, 15], [10, 50], [20, 56.5], [30, 46.5], [40, 22.1],
[50, 2.5], [60, 27.7], [70, 55.7], [80, 76.5]];
options.series[0].name = 'Occlusion';
options.series[0].data = arr;
var l = new Highcharts.Chart(options);
});
但是我希望arr来自php.
But I want arr to come from php.
先谢谢了.
推荐答案
如果要创建这种格式,则需要首先对其进行重组.考虑以下示例:
If you want to create such format, you need to restructure them first. Consider this example:
<?php
// lets say you have a raw data like this
$php_values = array(
array(0, 15),
array(10, 50),
array(20, 56.5),
array(30, 46.5),
array(40, 22.1),
array(50, 2.5),
array(60, 27.7),
array(70, 55.7),
array(80, 76.5),
);
$temp = array();
// format each array
foreach($php_values as $key => $value) {
$temp[] = '['.implode(',', $value).']';
}
// then format the whole array
$formatted_values = '['.implode(',', $temp).']';
?>
<script type="text/javascript">
var values = <?php echo $formatted_values; ?>;
console.log(values);
</script>
正如@SebastianBochan指出的那样,
$formatted_values = json_encode($php_array)
的工作方式相同(可能是最好,最干净的方式)
as @SebastianBochan pointed out,
$formatted_values = json_encode($php_array)
works the same way (probably the best and cleanest way)
这篇关于如何在高线图中输入具有x轴和y轴值的系列数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!