highstocks x-axis time incorrect - php

I have stock market data plotted over highcharts:
My JS Code:
$(function() {
$.getJSON('data.php', function(data) {
console.log(data);
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector:{
enabled:false
},
navigator: {
enabled: true
},
useUTC: false,
xAxis: {
type: 'datetime',
labels: {
formatter: function () {
return Highcharts.dateFormat('%H:%M', this.value);
},
}
},
title : {
text : 'NSE Stock Price'
},
credits: {
enabled : false
},
series : [{
name : 'NSE Stock Prices',
data : data,
tooltip: {
valueDecimals: 2
}
}]
});
});
});
My JSON output (partial):
[["09:01:00",27966.96],["09:03:00",27934.64],["09:03:00",27934.64],["09:07:00",27952.66],["09:07:00",27952.66],["09:07:00",27952.66]]
I expect time from 9:00am to 4:00pm on x-axis but I am just not getting it.
I tried changing time to unix time changing date format useUTC to true but noting worked out.
Update:
Tried this format too,
[["2014-11-14 09:01:00",27966.96],["2014-11-14 09:03:00",27934.64],["2014-11-14 09:03:00",27934.64]]
Update 2
My PHP code,
$array_item[] = array(date('H:i:s',strtotime($price_date)), (float)$last_traded_price);
Update 3: (some of the latest values)
[["2014-11-17 09:00:00",28065.72],["2014-11-17 09:02:00",28048.8],["2014-11-17 09:04:00",28041.93],["2014-11-17 09:06:00",28029.58],["2014-11-17 09:08:00",28018.68],["2014-11-17 09:10:00",28018.68],["2014-11-17 09:12:00",28018.68],["2014-11-17 09:14:00",28018.68],["2014-11-17 09:16:00",27988.73],["2014-11-17 09:18:00",28001.06],["2014-11-17 09:20:00",28003.06],["2014-11-17 09:22:00",27988.44],["2014-11-17 09:24:00",28011.67],["2014-11-17 09:26:00",27988.56],["2014-11-17 09:28:00",27991.17],["2014-11-17 09:30:00",27983.98],["2014-11-17 09:32:00",27972.01],["2014-11-17 09:34:00",27980.87],["2014-11-17 09:36:00",27994.42],["2014-11-17 09:38:00",28007.38],["2014-11-17 09:40:00",28005.57],["2014-11-17 09:42:00",27986.39],["2014-11-17 09:44:00",28010.79],["2014-11-17 09:46:00",27998.44],["2014-11-17 09:48:00",28012.66]]
This is as per: $array_item[] = array(strtotime($price_date), (float)$last_traded_price);
[[1416195000,28065.72],[1416195120,28048.8],[1416195240,28041.93],[1416195360,28029.58],[1416195480,28018.68],[1416195600,28018.68],[1416195720,28018.68],[1416195840,28018.68],[1416195960,27988.73],[1416196080,28001.06],[1416196200,28003.06],[1416196320,27988.44],[1416196440,28011.67],[1416196560,27988.56],[1416196680,27991.17],[1416196800,27983.98],[1416196920,27972.01],[1416197040,27980.87],[1416197160,27994.42],[1416197280,28007.38],[1416197400,28005.57],[1416197520,27986.39],[1416197640,28010.79],[1416197760,27998.44],[1416197880,28012.66],[1416198000,28018.49],[1416198120,28010.15],[1416198240,28028.18],[1416198360,28035.61],[1416198480,28024.87],[1416198600,28018.95],[1416198720,28007.81],[1416198840,28001.57],[1416198960,28006.85],[1416199080,27997.68],[1416199200,27993.06]]
The output: Still not giving perfect timings although not giving 00:00 as before.
The graph is breaking in middle.
Update 3:
PHP Code,
while ($stocks_bse_price->fetch())
{
$array_item[] = array(strtotime($price_date)*1000, (float)$last_traded_price);
}
JSFidle link: http://jsfiddle.net/Ywr3L/38/
Update 4:
Just realized its going wrong if figures for entire day is given as an input to array.
I have stored entire days data http://pastebin.com/NVAD5EyG
Update 5:
This is what I did in MySql query:
DATE_FORMAT(price_date,'%d-%c-%Y %r') as price_date
And then in PHP
$price_date = $price_date.' UTC';
$array_item[] = array(strtotime($price_date)*1000, (float)$last_traded_price);
Which is giving me this:
http://jsfiddle.net/rof96xuo/1/
[[1416301200000,28177.51],[1416301320000,28180.46],[1416301440000,28181.03],[1416301560000,28198.98],[1416301680000,28209.03],[1416301800000,28209.03]]
new Date(1416301200000)
Tue Nov 18 2014 14:30:00 GMT+0530 (India Standard Time)

As #RaeenHashemi is saying in the comments, highcharts expects datetime data to be in JavaScript epoch times (number of milliseconds since 1970/01/01). You are giving it a string representation of your datetime.
It looks like you are generating JSON from PHP. If so, modify the PHP to convert the time to epoch. If you have a DateTime object in your PHP use getTimeStamp:
$date->getTimestamp() * 1000; // unix timestamp to javascript millisecond timestamp
If you have date time strings in your PHP, convert it to a DateTime first.

Related

How to set xData as date (Y-M) for Highcharts synchronized charts?

I want to plot the synchronized charts like this https://www.highcharts.com/demo/synchronized-charts.
The JSON data will come from SQL database and the xData (x-axis) looks like below which are in year-month format:
xData: [
"2004-04","2004-04","2004-04","2004-04","2004-04","2004-04","2004-04",
"2004-04","2004-04","2004-04","2004-04","2012-08","2004-04","2004-04","2004-04",
"2004-04","2004-04","2004-04","2004-04","2004-04","2004-04","2004-04","2004-04",
"2004-04","2004-04","2004-04","2004-04","2004-04","2013-12","2004-04","2004-04",
"2007-11","2007-11","2012-08","2005-05","2004-04","2004-04","2004-04","2004-04",
"2004-04","2004-04","2004-04","2016-04","2004-04","2012-08","2004-04","2004-04",
"2004-04","2004-04","2004-04","2004-04","2004-04","2005-05","2004-04","2004-04",
"2012-08","2016-04","2004-04","2004-04","2004-04","2004-04","2012-08","2004-04",
"2004-04","2004-04","2004-04","2012-08","2004-04","2004-04","2004-04","2004-04",
"2004-04","2006-02","2007-11","2004-04","2004-04","2004-04","2005-05","2004-04",
"2004-04","2005-05","2004-04","2004-04","2012-08","2012-08","2006-02","2005-05",
"2004-04","2016-04","2004-04","2004-04","2004-04","2006-02","2004-04","2004-04",
"2004-04","2004-04","2004-04","2005-05","2007-11"
]
So, how to plot those data as x-axis for synchronized charts?
you could simply use that one array as the labels for the xAxis:
xAxis: {
labels: {
format: '{value}'
}
},
but in order to plot on a timeline axis, you may need to convert these strings into integer values, which can be done with PHP: strtotime($date.'-01 00:00:00') ...hint: you need to multiply the result (in seconds) with 1000 for JavaScript timestamps (in milliseconds).
I added categories: "activity.xData" to to the chart
and set the labels format to "this.value"
xAxis: {
crosshair: true,
categories: activity.xData,
events: {
setExtremes: syncExtremes
},
labels: {
format: this.value
}
},

How can I sort my DataTables row by date?

I want to sort my column by date:
var table = $('#table').DataTable({
"order": [[0, "desc"]],
});
But here is my result:
29.06.17
27.06.17
26.06.17
22.08.17
18.10.17
15.09.17
What I would expect is this:
18.10.17
15.09.17
22.08.17
29.06.17
27.06.17
26.06.17
June, then August, then September and then October....
I tested also:
"columnDefs": [
{ "type": "date-dd.mm.yy", targets: 0 }
],
But this didn't change anything.
dataTables date type uses Data.parse() which only supports a limited set of date formats. European style dd.mm.yy is not parseable thus the dates is alpha sorted.
You can deal with data attributes, i.e adding a data-sort="10/18/17" to each column, but I think it is easier to create a small plugin that return valid dates :
$.extend( $.fn.dataTableExt.oSort, {
"jarla-date-pre": function(a) {
a = a.split('.');
return new Date(a[1]+'/'+a[0]+'/'+a[2])
}
});
Use it like this :
columnDefs: [
{ type: 'jarla-date', targets: 0 }
]
demo -> http://jsfiddle.net/vad94dcs/
You need to use the render function which allows you to both format the date for display and use the raw date value for sorting.
The following code uses the moment.js javascript library to format the date.
{
data: 'DateField',
render: function (data, type, row) {
// If display or filter data is requested, format the date
if (type === 'display' || type === 'filter') {
return (moment(data).format("ddd DD/MM/YYYY (HH:mm)"));
}
// Otherwise the data type requested (`type`) is type detection or
// sorting data, for which we want to use the raw date value, so just return
// that, unaltered
return data;
}
},
Link to source at the datatables forum, here.

Highstock charts fails in remote but no in local

I have a MongoDB database with Laravel in Backend, I get the data from mongo and I out in to the highcharts, in Local it looks good:
But in remote with the same code looks like this:
And the console shows me the next error:
(14) highstock.js:8 Highcharts error #15: www.highcharts.com/errors/15
(8) highstock.js:8 Highcharts error #15: www.highcharts.com/errors/15
It's the same code, the same version of mongodb the sambe version of PHP driver:S
EDIT:
The jQuery code that I have:
$('#historic-graph').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'Gráfico Histórico'
},
series: [{
name: 'Dato',
data: data,
tooltip: {
valueDecimals: 2
}
}]
});
And the Data that I recieve:
[[1455839004000,130],[1455839006000,130],[1455839008000,133],[1455839011000,134],[1455839013000,133],[1455839015000,132],[1455839017000,134],[1455839020000,130],[1455839022000,134],[1455839024000,133],[1455839027000,134],[1455839029000,131],[1455839031000,134],[1455839034000,132],[1455839036000,130],[1455839038000,132],[1455839040000,134],[1455839043000,132],[1455839045000,133],[1455839047000,130]]
[TIME, VALUE]
The time is strtotime created_at * 1000 with php.
EDIT 2:
PHP code that I return with data variable in jQuery after ajax request:
foreach ($alldata as $item) {
$date = new Carbon($item['created_at']);
$date = $date->addHours(Auth::user()->utc)->timestamp * 1000;
array_push($data, array($date, floatval(round($item['value'], 2))));
}
I add an hour for users UTC timezone.

Wrong Unix time stamp while updating events drag & drop in full calendar

The events fetched from the database displaying correctly in the full calendar.
When I try to drag and drop the events to another date, it gives wrong Unix timestamp.
For example)
The events is currently in November 16 2012, when I drag and drop it to 20 November 2012, it gives the Unix time stamp as 1353340800000 and the date after converting using strtotime() PHP function, the result is 1983-07-03.
Issue Fixed :
Check the updated code below.
$('#calendar').fullCalendar({
editable: true,
eventDrop: function(calEvent,dayDelta,minuteDelta,allDay,revetFunc) {
var stDate = $.fullCalendar.formatDate(calEvent.start, 'dd-MM-yyyyy');
$.post('event_update.php',{'allday':allDay, 'event':calEvent.id, 'start':stDate}, function(response){
if(response.length > 0){
alert(response);
revertFunc();
}
});
},
loading: function(bool) {
if (bool) $('#loading').show();
else $('#loading').hide();
}
});
You have to cut the last 3 zeros.

Highcharts Issue - Labelling Y-Axis Based Series Data

I have an issue regarding highcharts...
Consider the example relation:
Data
-----
Id(pk)
Date date
Data varchar(50)
This relation contains data. It is important to note that the date is non-contiguous...i.e. data isn't entered everyday. An example table may be
Data
-----
Id Date Data
1 2011-08-22 SomeData
2 2011-08-29 MoreData
I present users with an HTML/jQuery interface, where data can be pulled from the database and graphed using highcharts. This works well:
http://dev.speechlink.co.uk/David/fifthiteration/dbgrapher.php
I use the following jQuery to label the x-axis:
xAxis: {
type: 'datetime',
maxZoom: 14 * 24 * 3600000, // fourteen days
lineWidth: 1,
lineColor: '#999999',
title: {
text: 'Date'
}
}
And this is for the series:
series: [{
type: 'spline',
name: data.questionTitle,
pointInterval: 24 * 3600 * 1000,
pointStart: Date.UTC(data.year, data.month, data.day),
data: cdata,
lineColor: '#f6a828',
color: '#418ed6'
}]
Now this works fine bar ONE problem -> all points are plotted as if they are done on successive days...This is not the case...For example, if in my table I have data submitted on the 24th and then no data until the 29th....the data on the 29th is incorrectly placed as data submitted the 25th...
This is obviously to do with this series argument:
**pointInterval: 24 * 3600 * 1000,**
My data comes in like this:
[23rd, 24th, 25th, 29th]
Whereas it should probably be something like:
[23rd, 24th, 25th, null, null, null, 29th]
I am not sure if the above is correct syntax..thats is why I am here...how do you tell highcharts to skip a point, but keep the same pointintervals....
EDIT:
I see on highcharts you can pass data like so:
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
xAxis: {
type: 'datetime'
},
series: [{
data: [
[Date.UTC(2010, 0, 1), 29.9],
[Date.UTC(2010, 2, 1), 71.5],
[Date.UTC(2010, 3, 1), 106.4]
]
}]
});
Is there a way to pass a php array in the correct format to the series above (i.e. with a data and associated value)..
If you dont have data points for an interval then you can specify as null in the series. You can try [23rd, 24th, 25th, null, null, null, 29th] it will perfectly work.

Categories