问题
I have been at this for hours and I have no idea what's wrong. It displays the correct graph but I can't hover on any of the points. I've read through my var_dump() and all my records are in order. Am I converting the dates wrong at some point?
What my graph looks like (Which is correct): http://screencloud.net/v/qrSS
The error I'm getting: "Highcharts error #15: www.highcharts.com/errors/15"
Highcharts expects data to be sorted
This happens when you are trying to create a line series or a stock chart where the data is not sorted in ascending X order. For performance reasons, Highcharts does not sort the data, instead it is required that the implementer pre-sorts the data.
My console: http://screencloud.net/v/qWHo
My highcharts code
$.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]
});
});
My php that creates the JSON
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);
}
回答1:
I used this function
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;
}
Updated tis part of my code
$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!
来源:https://stackoverflow.com/questions/14306296/highcharts-returning-error-15-but-the-graph-is-correct