我已经在这几个小时了,我不知道出了什么问题。它显示正确的图形但我不能悬停在任何点上。我已经读完了我的var_dump(),我的所有记录都是有序的。我在某些时候错误地改变了日期吗?
我的图表是什么样的(哪个是正确的):http://screencloud.net/v/qrSS
我得到的错误:“Highcharts错误#15:www.highcharts.com/errors/15”
Highcharts期望对数据进行排序
当您尝试创建数据未按升序X顺序排序的折线图或股票图表时,会发生这种情况。出于性能原因,Highcharts不对数据进行排序,而是要求实施者预先对数据进行排序。
我的控制台:http://screencloud.net/v/qWHo
我的高级代码
$.post('/card/read_graph_json', inputs, function(data) {
json = jQuery.parseJSON(data);
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'spline'
},
global: {
useUTC: false
},
plotOptions: {
series: {
animation: false
}
},
title: {
text: '{{ $card->name }}'
},
subtitle: {
text: null
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%b',
year: '%b'
}
},
yAxis: {
title: {
text: null
},
},
tooltip: {
formatter: function() {
var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
date = new Date(this.x);
return '<strong>'+ this.series.name +'</strong><br/>'+ monthNames[date.getMonth()] + '. ' + date.getDay() + ', ' + date.getFullYear() +': '+ this.y;
}
},
series: [json]
});
});
我创建JSON的php
public function contact_records($contact_id, $number_past_entries = 6, $reverse = false)
{
// Pull 15 records from db and order them by date created "Y-m-d H:i:s"
return $this->has_many('Record')
->where_deleted(0)
->where_contact_id($contact_id)
->order_by('created_at', 'desc')
->take($number_past_entries)
->get();
}
public function post_read_graph_json()
{
$contact_id = Input::get('contact_id');
$tracker = Tracker::find(Input::get('tracker_id'));
$card = Card::find(Input::get('card_id'));
$serie = array();
if ($tracker != null && $card != null) {
Cookie::forever("default_graph_tracker_id", $tracker->id);
$data = array();
// This is where my records come from
$records = $card->contact_records($contact_id, 15);
// Create the serie
foreach ($records as $record) {
$data[] = array(
strtotime($record->created_at) * 1000,
floatval($record->read_tracker_value($tracker->id)->value)
);
}
$serie = array(
'name' => $tracker->name,
'data' => $data
);
}
return json_encode($serie);
}
答案 0 :(得分:1)
我使用了这个功能
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
更新了我的代码的一部分
$data = array();
foreach ($records as $record) {
$data[] = array(
'time' => strtotime($record->created_at) * 1000,
'value' => floatval($record->read_tracker_value($tracker->id)->value)
);
}
aasort($data, 'time');
$new_data = array();
foreach ($data as $record) {
$new_data[] = array(
$record['time'],
$record['value']
);
}
$serie = array(
'name' => $tracker->name,
'data' => $new_data
);
ta da!