Highcharts: Using JSON to load xAxis data as 'datetime'? - php

I'm trying to get my xAxis in Highcharts to use the 'datetime' format instead of regular categories, but I'm not sure on how to do this. Please check the following picture to see what I mean:
http://i.stack.imgur.com/qCOb4.png
Output from data.php:
[
{"name":"datetime","data":[1439929080000,1439929140000,1439929200000, ...
{"name":"krypTemp","data":[17,17.3,17.2,17.3,17.2,17.2,17.2,17.2,17.2, ...
{"name":"vindTemp","data":[19,18.9,18.9,18.8,18.8,18.7,18.7,18.7,18.7, ...
{"name":"uteTemp","data":[15,15.5,15.5,15.4,15.4,15.4,15.3,15.4,15.3, ...
{"name":"krypFukt","data":[59,59,59,59,59,59,59,59,59,59,59,59,59,59, ...
{"name":"vindFukt","data":[49,49,50,50,50,50,50,50,50,50,50,50,50,50, ...
]
And current configuration for my html page:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'spline'
},
xAxis: {
type: 'datetime'
},
series: []
}
$.getJSON("data.php", function(json) {
options.xAxis.categories = json[0]['data'];
options.series[0] = json[1];
options.series[1] = json[2];
options.series[2] = json[3];
options.series[3] = json[4];
options.series[4] = json[5];
chart = new Highcharts.Chart(options);
});
});
I know that datetime and categories conflict with eachother but not how I am supposed to rewrite this "options.xAxis.categories = json[0]['data'];" to fit/be displayed as 'datetime'.
Any help on this would be much appreciated as it is the last step towards my goal :).

In case when you have a datetime type of axis, you cannot use categories, because this is different type.
Replace this line:
options.xAxis.categories = json[0]['data'];
with
options.xAxis.tickPositions = json[0]['data'];
In case when labels are printed as numbers, you can use a labels formatter and use Highcharts.datetime inside.

Related

Date not showing correct in High Chart x-xis

My JSON:
[{"date":"2016,07,01","coached":"0.00000000000000","non_coached":"1.07262272089762","delta":-1.0726227208976},{"date":"2016,06,24","coached":"0.00000000000000","non_coached":"0.64400560224090","delta":-0.6440056022409},{"date":"2016,06,17","coached":"1.13666666666667","non_coached":"0.41228898426323","delta":0.72437768240344},{"date":"2016,06,10","coached":"0.68866666666667","non_coached":"0.46050000000000","delta":0.22816666666667},{"date":"2016,06,03","coached":"0.68666666666667","non_coached":"0.19209103840683","delta":0.49457562825984},{"date":"2016,05,27","coached":"-0.03933333333333","non_coached":"-0.04332859174964","delta":0.00399525841631},{"date":"2016,05,20","coached":"0.01214285714286","non_coached":"-0.07948965517241","delta":0.09163251231527},{"date":"2016,05,13","coached":"0.30733333333333","non_coached":"-0.10541436464088","delta":0.41274769797421},{"date":"2016,05,06","coached":"0.20937500000000","non_coached":"0.00750704225352","delta":0.20186795774648},{"date":"2016,04,29","coached":"0.00133333333333","non_coached":"-0.10642559109875","delta":0.10775892443208},{"date":"2016,04,22","coached":"-0.25600000000000","non_coached":"-0.06736248236953","delta":-0.18863751763047},{"date":"2016,04,15","coached":"0.31375000000000","non_coached":"-0.05191489361702","delta":0.36566489361702},{"date":"2016,04,08","coached":"-0.28800000000000","non_coached":"-0.00908682634731","delta":-0.27891317365269},{"date":"2016,04,01","coached":"0.14466666666667","non_coached":"-0.01988522238164","delta":0.16455188904831},{"date":"2016,03,25","coached":"0.20000000000000","non_coached":"0.05398581560284","delta":0.14601418439716},{"date":"2015,12,25","coached":"0.01344827586207","non_coached":"-0.06062585034014","delta":0.07407412620221},{"date":"2015,12,18","coached":"0.26806451612903","non_coached":"-0.00484311050477","delta":0.2729076266338},{"date":"2015,12,11","coached":"0.25352941176471","non_coached":"-0.01641483516484","delta":0.26994424692955},{"date":"2015,12,04","coached":"0.15000000000000","non_coached":"0.00259002770083","delta":0.14740997229917},{"date":"2015,11,27","coached":"0.18500000000000","non_coached":"0.06243626062323","delta":0.12256373937677},{"date":"2015,11,20","coached":"0.20500000000000","non_coached":"-0.06820584144645","delta":0.27320584144645},{"date":"2015,11,13","coached":"0.28800000000000","non_coached":"0.14813559322034","delta":0.13986440677966},{"date":"2015,11,06","coached":"0.05097560975610","non_coached":"0.02680397727273","delta":0.02417163248337},{"date":"2015,10,30","coached":"0.25279069767442","non_coached":"0.13122392211405","delta":0.12156677556037},{"date":"2015,10,23","coached":"0.06536585365854","non_coached":"0.12291605301915","delta":-0.05755019936061},{"date":"2015,10,16","coached":"0.03487804878049","non_coached":"0.30552833078101","delta":-0.27065028200052},{"date":"2015,10,09","coached":"0.06000000000000","non_coached":"0.17649499284692","delta":-0.11649499284692},{"date":"2015,10,02","coached":"0.18186046511628","non_coached":"0.30160173160173","delta":-0.11974126648545},{"date":"2015,09,25","coached":"-0.04195121951220","non_coached":"0.32907246376812","delta":-0.37102368328032},{"date":"2015,09,18","coached":"0.17447368421053","non_coached":"0.26081447963801","delta":-0.08634079542748},{"date":"2015,09,11","coached":"-0.04069767441860","non_coached":"0.39883058470765","delta":-0.43952825912625},{"date":"2015,09,04","coached":"0.13073170731707","non_coached":"0.29857580398162","delta":-0.16784409666455},{"date":"2015,08,28","coached":"0.39302325581395","non_coached":"0.30868827160494","delta":0.08433498420901},{"date":"2015,08,21","coached":"0.13843137254902","non_coached":"0.25431578947368","delta":-0.11588441692466},{"date":"2015,08,14","coached":"0.18000000000000","non_coached":"0.21200328407225","delta":-0.03200328407225},{"date":"2015,08,07","coached":"0.37620000000000","non_coached":"0.18070000000000","delta":0.1955},{"date":"2015,07,31","coached":"0.35239130434783","non_coached":"0.17344208809135","delta":0.17894921625648},{"date":"2015,07,24","coached":"0.01840909090909","non_coached":"0.24440514469453","delta":-0.22599605378544},{"date":"2015,07,17","coached":"0.16108695652174","non_coached":"0.26153225806452","delta":-0.10044530154278},{"date":"2015,07,10","coached":"0.16166666666667","non_coached":"0.31383471074380","delta":-0.15216804407713},{"date":"2015,07,03","coached":"0.11671875000000","non_coached":"0.21704361873990","delta":-0.1003248687399},{"date":"2015,06,26","coached":"0.30638888888889","non_coached":"0.17638668779715","delta":0.13000220109174},{"date":"2015,06,19","coached":"0.37225352112676","non_coached":"0.24194221508828","delta":0.13031130603848},{"date":"2015,06,12","coached":"0.25636363636364","non_coached":"0.21017214397496","delta":0.04619149238868},{"date":"2015,06,05","coached":"0.17027027027027","non_coached":"0.21259375000000","delta":-0.04232347972973},{"date":"2015,05,29","coached":"-0.36263888888889","non_coached":"0.05622291021672","delta":-0.41886179910561},{"date":"2015,05,22","coached":"0.18888888888889","non_coached":"0.18983076923077","delta":-0.00094188034188},{"date":"2015,05,15","coached":"-0.05687500000000","non_coached":"0.12545592705167","delta":-0.18233092705167},{"date":"2015,05,08","coached":"0.20102564102564","non_coached":"0.15625570776256","delta":0.04476993326308},{"date":"2015,05,01","coached":"0.07525000000000","non_coached":"0.12190193164933","delta":-0.04665193164933},{"date":"2015,04,24","coached":"0.29610389610390","non_coached":"0.13772519083969","delta":0.15837870526421},{"date":"2015,04,17","coached":"0.01613333333333","non_coached":"0.03368916797488","delta":-0.01755583464155},{"date":"2015,04,10","coached":"0.21420289855072","non_coached":"0.05916018662519","delta":0.15504271192553},{"date":"2015,04,03","coached":"-0.01797297297297","non_coached":"-0.05907435508346","delta":0.04110138211049},{"date":"2015,03,27","coached":"-0.00767123287671","non_coached":"0.06102201257862","delta":-0.06869324545533}]
Just want to show the first 13 weeks of data.
This is my code to process the JSON:
var series = [{name:'Coached',data:[]},{name:'Non Coached',data:[],stack: 'yes'},{name:'Opportunity',data:[],stack: 'yes'}],
year, month, day;
for(var i=0; i<13;i++){
var d = response[i].date.split(",");
year = d[0];
month = d[1];
day = d[2];
var coached = parseFloat(response[i].coached);
var non_coached = parseFloat (response[i].non_coached);
var delta = parseFloat (response[i].delta);
series[0].data.push([Date.UTC(year,month,day),coached]);
series[1].data.push([Date.UTC(year,month,day),non_coached]);
series[2].data.push([Date.UTC(year,month,day),delta]);
alert(Date.UTC(year,month,day));
}
console.log(series);
if(series.length){
stacked_chart_eCo_prod.userOptions.series = series;
var chart = new Highcharts.Chart(stacked_chart_eCo_prod.userOptions);
console.log(chart.series[0].data[0].options);
console.log(chart.series[1].data[0].options);
console.log(chart.series[2].data[0].options);
}
This is the PHP code:
stacked_chart_eCo_prod = new Highcharts.Chart({
colors: ['#003399','#910000','#55BF3B'],
chart: {
renderTo: 'stacked_chart_eCo_prod',
type: 'column'
},
title:{
text:''
},
yAxis: {
format: '{value:.2f}',
title: {
text: 'Job Uplift'
}
},
xAxis: {
type: 'datetime',
labels: {
format: '{value:%Y-%m-%d}',
align: 'right',
rotation: -30
}
},
plotOptions: {
column: {
stacking: 'normal'
// keys:['x','y','stack']
}
},
series: []
});
The date shown on the x-axis is wrong.
I have looked at this example but no use.
Any ideas what am I missing here?
Here is the issue. I think Date.UTC method is not working properly?

Generating Issues in Export JSON Data to Pie with gradient fill in Highcharts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating one Pie with gradient fill Chart from Highcharts using json,php and mysql query.Debug through firebug console but no errors and also graph not display.I creating a static graph [link] http://jsfiddle.net/sunman/YKE2P/10/ like this i want to create this with dynamic with json and mysql.Here is my problem my json data not generate graph.Below is my code.My json data displays like :
[{"name":{"MOZILA":45.0}},{"name":{"IE":26.8}},{"name":{"CHROME":12.8}},{"name":{"OPERA":6.2}},{"name":{"OTHERS":9.2}}]
index.php:
$(function () {
// Radialize the colors
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
return {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
// Build the chart
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: ' Rate of a specific project'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f}',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
},
connectorColor: 'silver'
}
}
},
series: [{
type: 'pie',
name: 'Total without cost',
data:[]
}]
});
$.getJSON("data.php", function(json) {
//options.series.push(data);
options.series[0].data = json;
chart = new Highcharts.Chart(options);
});
});
data.php:
$sql="SELECT mozila,ie,chrome,opera,safari,torch FROM webmarketing";
$resultSql = mysql_query($sql);
$result = array();
while($rows=mysql_fetch_array($resultSql)) {
$arr['name'] = array('MOZILA' => $rows['mozila']);
$arr1['name']= array('IE' => $rows['ie']);
$arr2['name'] = array('CHROME' => $rows['chrome']);
$arr3['name']= array('OPERA' => $rows['opera']);
$arr4['name'] = array('OTHERS' => $rows['safari']+$rows['torch']);
}
array_push($result,$arr);
array_push($result,$arr1);
array_push($result,$arr2);
array_push($result,$arr3);
array_push($result,$arr4);
print json_encode($result, JSON_NUMERIC_CHECK);
So request you to please help where is my code is wrong. I want link this type of graph but with dynamic.I am unaware of json code . So Kindly request to you suggest me of question.
The problem is here:
$.getJSON("data.php", function(json) {
options.series.push(data);
chart = new Highcharts.Chart(options);
});
You are adding new series, which has some strange format.. I advice to use that instead:
$.getJSON("data.php", function(json) {
options.series[0].data = json;
chart = new Highcharts.Chart(options);
});

Creating dynamic Highcharts based on an array

To sort off cap what I'm doing.. I have a loop that creates a table from information pulled from my database. I've setup a superglobal variable inside the loop that assigns one of my table field values to the variable; that part works no problem.
The problem is when I try to call that variable inside the highcharts function, it just doesn't work. The charts don't show up.
$(document).ready(function() {
var $container = $('$global_var');
Highcharts.setOptions({
chart: {
backgroundColor: {
linearGradient: [0, 0, 500, 500],
stops: [
[0, 'rgb(255, 255, 255)'],
[1, 'rgb(240, 240, 255)']
]
},
borderWidth: 2,
plotBackgroundColor: 'rgba(255, 255, 255, .9)',
plotShadow: true,
plotBorderWidth: 1
}
});
var chart1 = new Highcharts.Chart({
chart: {
renderTo: $container,
},
xAxis: {
type: 'datetime'
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
pointStart: Date.UTC(2010, 0, 1),
pointInterval: 3600 * 1000 // one hour
}]
});
});
</script>
In the table that's created, I have a <div> inserted with a dynamic ID (same values as the array I'm attempting to use with the highcharts function), which also works. The only issue is that I can't seem to pass the variable to the renderTo part of the highcharts function.
Here's how I'm declaring my superglobal variable inside my table loop (again, works fine):
$GLOBALS['a'] = $row['Name'] . $temp_array;
TL;DR.. If anyone knows how to pass a variable to renderTo in the Highcharts function I'd really love to know how you do it. Hopefully this is enough info, but if not I'll gladly provide what is requested. Thanks!
In renderTo you need to add string not object as you return.
So it should be
var $container = 'global_var', //id of container, string
and in the chart
chart: {
renderTo: $container,
},
If you would like to combine js variable with string from php variable it should be something like:
var $container = '<?php echo $variable ?>',

How to graph jqplot using datetime as yaxis and category for xaxis

var bar_fan = ['8:30','10:30'];
var bar_ticks = ['a','b'];
$(document).ready(function(){
plot2 = $.jqplot('bar_all', [bar_fan], {
seriesDefaults: {
renderer:$.jqplot.BarRenderer,
pointLabels: { show: true },
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
ticks: bar_ticks,
},
},
legend: {
show: true,
labels: bar_labels,
placement: 'outside'
}
});
bar_fan = ['8:30','10:30']; wont work. cause its time format.
if use value like bar_fan = [1,2]; will work.
how can I use yaxis with datetime value?
I got his working simply by adding the following to the axes configuration:
yaxis: {
renderer: $.jqplot.DateAxisRenderer
}
Now because yours are just time values, the graph will assume the current date. You may then want to specify either a minimum date or explicitly supply both date and time information, e.g., '2013-03-01 8:30AM'

javascript php mysql flot database

everyone! I have issues with regard to my x-axis. I'm using the api of flot. In my case, I'm extracting data from mysql to plot the graph. Both axes requires data to be extracted from mysql. For example, mysql will extract a data for the Y axis, say 20. My x axis will extract data from mysql of that particular time.
I tried to rewrite the script a few times, but to no conclusion. I tried attempting to create a variable, options, but it didn't work. It works only if I place the x axis statement just below y. The whole idea of creating a new variable options because I wanted to do a loop for the x-axis to continouous collect data from mysql. In the below quote, I simulated it with selected arrays.
$(function () {
var graph = [];
var power = <?php echo json_encode($data);?>;
var time = <?php echo json_encode($times);?>;
var row = <?php echo json_encode($nrow);?>;
//alert(time.toSource());
for (var i = 1; i < row; i += 1) {
//var test = time[i];
//alert(test);
//graph.push([i, power[i]]);
var hhmm = time[i].split(":");
var hh = parseInt(hhmm[0]);
var mm = parseInt(hhmm[1])/60;
var tt = hh+mm;
//var tx = hh;
graph.push([tt, power[i]]);
}
var options = {
xaxis: { ticks:[[1,time[1]],[2,time[2]],[3,time[3]],[4,time[4]],[5,time[5]],[6,time[6]],[7,time[7]],[8,time[8]]]}
};
//alert(options.toSource());
var plot = $.plot($("#placeholder"),
[ { data: graph, label: "Power" } ], {
series: {
lines: { show: true },
points: { show: true }
},
grid: { hoverable: true, clickable: true },
yaxis: { min: 0, max: 25 },
//xaxes: [ { mode: 'time' } ]
//xaxis: { mode: "time"}
//xaxis: { mode: "time",timeformat:"%H/%M" }
//xaxis: { ticks:[[1,time[1]],[2,time[2]],[3,time[3]],[4,time[4]],[5,time[5]],[6,time[6]],[7,time[7]]]}
}), options);
I have changed the options function, but it doesn't work.
var options = {
series: {
lines: { show: true },
points: { show: true }
},
grid: { hoverable: true, clickable: true },
yaxis: { min: 0, max: 25 },
for (var i = 1; i < row; i += 1)
{
xaxis: { ticks:[[i,time[i]]}
};
};
If I've surmised the format you wish for xaxis correctly, you could try the following:
var ticks = [];
for (var i = 1; i < row; i += 1)
{
ticks.push ([i, time[i]]);
}
var xaxis = {
ticks: ticks,
// ... any additional properties...
};
This will create an object xaxis with the property ticks which in turn is an array of arrays where each sub-array has two elements, the counter starting with one and the value of the time array for each i. I suppose you'll want to use code similar to this to create options:
var options = {
series: { lines: { show: true },
points: { show: true } },
grid: { hoverable: true, clickable: true },
yaxis: { min: 0, max: 25 },
xaxis: xaxis
};
You cannot use the for() statement in the object initialization, you'll have to create the xaxis array before, and use the variable name when creating the options object.

Categories