Highcharts and json format - highcharts

I have this code for a query in a file called querytojson.php:
if(!$query = #pg_query("SELECT AVG(\"UploadSpeed\") AS \"UploadSpeed\",
AVG(\"DownloadSpeed\") AS \"DownloadSpeed\",
AVG(\"Latency\") AS \"Latency\",
AVG(\"Jitter\") AS \"Jitter\",
AVG(\"PacketLoss\") AS \"PacketLoss\" FROM \"ZipPerformance\" "))
die("<br>Errore nella query: " . pg_last_error($query));
while($row = pg_fetch_assoc($query)){
// aggiungo all'array
$risultati[] = $row;
}
// stampo a video i risultati formattati secondo la sintassi di JSON
echo json_encode($risultati);
the json data is in this format:
[{"UploadSpeed":"0.342153197182936","DownloadSpeed":"4.35602301750153","Latency":"110.290067528565","Jitter":"0.0333323723888251","PacketLoss":"0.164373075044556"}]
Now I'd want to create a graph with Highcharts library like this, the file is called index1.html:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'column'
},
title: {
text: 'HOBBIT'
},
tooltip: {
},
labels: {
html: 'index.html'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Velocità di connessione'
}
},
series: []
};
})
I'd want to pass json data directly to index.html.

I've done somthing similar to this. The way i did it was I created the chart in a sperate js file and just called the chart as a function passing in the JSON object as a variable. This is an example of one of my scripts in use on my site. In your case the labels and values are the same variable so the loop iteration is different, but the idea is still the same.
var chart;
function pieChart(id, values, labels, animate){
if (animate === undefined ){
animate = true;
}
var arrays = new Array();
for (i=0;i<values.length;i++) {
arrays[i] = [labels[i], values[i]];
}
chart = new Highcharts.Chart({
chart: {
renderTo: id,
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
credits: {
enabled: false
},
title: {
text: 'Event Occurrence',
style: {
color: '#000000'
}
},
tooltip: {
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ this.y +' %';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ this.y +' %';
}
}
},
series: {
animation: animate
}
},
series: [{
type: 'pie',
name: 'Probability',
data: arrays
}]
});
}

Related

Adding thousands separator for custom formatted highcharts tooltip

I am using highcharts to in my app and want to add tooltip with thousand separator like I did it in data labels. I used custom formatting for tooltip, so what should I change to achieve this
tooltip options in highcharts
tooltip: {
formatter: function () {
return (this.x + ':' + this.y);
},
shared: true,
useHTML: true
},
JS FIDDLE
use thousand separator in lang
$(function () {
Highcharts.setOptions({
lang: {
decimalPoint: '.',
thousandsSep: ','
}
});
and in tooltip- formatter use like
tooltip: {
pointFormat: '<b>{point.x} :</b>' + 'Count: <b>{point.y:,.0f}</b>',
shared: true,
useHTML: true
}
Updated fiddle with separator without decimal point
Just following Nishith Kant Chaturvedi's answer, and since there is no jsfiddle example available, here you can see how to implement that answer.
Highcharts.setOptions({
lang: {
decimalPoint: '.',
thousandsSep: ','
}
});
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: ''
},
xAxis: {
categories: ['Salary']
},
yAxis: {
title: {
text: ''
},
stackLabels: {
enabled: true,
format: '{total:,.2f} $us'
},
labels: {
format: "{value:,.2f} $us",
}
},
legend: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
borderColor: '#CCC',
borderWidth: 1,
},
tooltip: {
headerFormat: '<b>{point.x}</b><br/>',
pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
format: '{point.y:,.2f} $us',
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
}
}
},
series: [{
type: 'column',
name: 'Tomas',
data: [60000]
}, {
type: 'column',
name: 'Amy',
data: [18000]
}, {
type: 'column',
name: 'Jenny',
data: [85000]
}]
});
http://jsfiddle.net/zrc5skLy/
Use Highcharts.numberFormat() in point.x or point.y
example:
tooltip: {
enabled: true,
split: true,
shared: true,
formatter: function () {
// The first returned item is the header, subsequent items are the points
return [Highcharts.dateFormat("%Y-%m-%d %H:%M:%S", this.x)].concat(
this.points
? this.points.map(function (point) {
return (
point.series.name +
": " +
// numberFormat(data, decimal)
Highcharts.numberFormat(point.y, 2)
);
})
: []
);
},
},

HighCharts - increase bar height and reduce bar gap

Trying to increase bar height to accommodate series name inside bar and trying to reduce default gap between series group. The section "series: []" is, I assume for the JSON return function to populate. Tried many examples from all the generous JSFiddle examples to no avail. I'm a noob so please be patient. Any ideas? So far I have: http://jsfiddle.net/PeterHanekom/7ue5bkm8/1/
var options = {
chart: {
renderTo: 'container',
type: 'bar'
},
colors: ['#00B9B9', '#527CED', '#A7D36B', '#B9B900', '#0097FF', '#400040', '#EA4D00', '#336699', '#8080C0', '#ADA870'],
title: {
text: 'Cluster Totals',
x: -20 //center
},
subtitle: {
text: 'Dept - Branch',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
min: 0,
max: 100,
title: {
text: 'Percentage'
},
labels: {
overflow: 'justify'
}
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
align: 'center',
borderWidth: 0
},
plotOptions: {
bar: {
stacking: 'normal',
pointWidth: 20,
pointPadding: 0,
cursor: 'pointer',
point: {
events: {
click: function() {
alert('later drilldown events');
}
}
dataLabels: {
enabled: true,
inside:true,
useHTML: true,
align: 'left',
color: 'white',
style: {
fontWeight: 'bold'
},
verticalAlign: 'middle',
formatter: function () {
if (this.series.name)
return '<span style="color:black; height: 25px;">' + this.series.name + ' = ' + this.y + '</span>';
else return '';
}
}
}
},
series: []
}
$.getJSON("./services/get360Pivot.php?s_Search=" + sOperatorSearch, 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];
chart = new Highcharts.Chart(options);
});
I think you options you need are groupPadding and pointWidth. e.g.
groupPadding:0.1,
pointWidth:20,
http://jsfiddle.net/s3t2pL94/

Customizing the series data in pie charts with a dynamic json series (HighCharts)

I am fairly new to Highcharts. I have a pie chart that is being rendered fine with dynamic data (JSON String) . But I want to customize certain slices to be Sliced: true and Selected: True. I am not sure how to do it. Please find my code as below,
<div id="container_${page_id}" style="min-width: 310px; height: 400px; margin: 0 auto;"></div>
var series = [];
var total = 0;
$.each(data, function(index, value) {
series.push([value.statisticData+'_'+value.sizes,parseInt(value.sizes)]);
console.log("statisticData",value.statisticData);
console.log("value.sizes",value.sizes);
total += parseInt(value.count);
});
var chartSessionStatus = new Highcharts.Chart({
chart: {
renderTo: $("#container_"+testSessionPageId).get(0),
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
exporting: {
enabled: true,
buttons: {
contextButton: {
menuItems: [{
text: 'Statistics',
}]
}}
},
tooltip: {
formatter: function() {
return this.point.name.split('_')[0] + ': <b>'+this.y+'</b>';
}
},
title: {
text: '<h4 style="font-style: bold">Tenant Statistics</h4>'
},
legend: {
labelFormatter: function() {
return this.name.split('_')[0] + ' (' + this.y+')';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
distance: 0,
useHTML: true,
formatter: function() {
if( this.y > 0)
return '<b>'+this.point.name.split('_')[0] +'('+ this.y +')</b>';
}
},
showInLegend: true,
size: 150
}
},
series: [{
type: 'pie',
name: 'Tenant Statistics',
data: series
}]
});
});
Main point of concern, in the series , I have the option for data as "data : series" but I would like to customize certain slices in this series. But since the data is dynamic I am not sure how to use only certain parts of the json string
The Json that i receive is in the following format :
[{"statisticData":"Tests Created","sizes":"3"},{"statisticData":"Students","sizes":"2"},{"statisticData":"Users","sizes":"7"},{"statisticData":"Schools","sizes":"10"}]
Any Help is greatly appreciated !!! Thanks in advance.
After getting json, you can parse it / add slice parameter and then use in highcharts. So instead of pushing :series.push([value.statisticData+'_'+value.sizes,parseInt(value.sizes)]);
you can push something like:
series.push({
name: value.statisticData+'_'+value.sizes,
y: parseInt(value.sizes),
sliced: true,
selected: true
});

Retrieve series name and data with getjson

I'm trying to retrieve from a JSON formatted file this series name and data
[
{ "name":"gfs00", "data": [ [1359676800000, 30], [1359687600000, 32]]
},
{ "name":"gfs06", "data": [ [1359676800000, 28], [1359687600000, 29]]
}
]
Here's the highchart code:
var data_temp;
$.getJSON('dati/performance_gfs/precipitazioni/pioggia_live_data.php',
{ run: runs_gfs} ,function(data_temp){
for (var i = 0; i <data_temp.length ;i++){
chart.addSeries({
data:data_temp,
});
}
});
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container_temperatura_windchill_heatindex',
defaultSeriesType: 'line',
zoomType: 'x',
},
title: {
text: 'Ensemble'
},
xAxis: {
type: 'datetime'
},
yAxis: [{ // Primary yAxis
id:0,
labels: {
formatter: function() {
return this.value +'°C';
},
style: {
color: '#89A54E'
}
},
title: {
text: 'Temperature',
style: {
color: '#89A54E'
}
}
}],
plotOptions: {
line: {
lineWidth: 1,
states: {
hover: {
lineWidth: 2
}
},
marker: {
enabled: false,
states: {
hover: {
enabled: true,
symbol: 'circle',
radius: 5,
lineWidth: 1
}
}
},
pointInterval: 24 * 3600000, // one hour
pointStart: Date.UTC(2012, 9, 6, 0, 0, 0)
}
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%d-%m-%Y %H:%M', this.x)+'<br/>'+
this.y;
}
},
series: []
});
});
}
As can be seen I don't know how to assign the series names and data from the JSON file which is correct what I need is something like http://jsfiddle.net/vXdxv/1/
Try the following:
for (var i = 0; i <data_temp.length ;i++){
chart.addSeries(data_temp[i], false);
});
chart.redraw();

Updating basic column high chart

I am having a problem updating my basic column high chart.
My json array that is returned from the server is:
[{"name":"positive","data":[18,35,32,38]},{"name":"negative","data":[0,14,65,121]}]
I am only using the "data" arrays ^ i.e. jsonArr[0].data and jsonArr[1].data
I have the data set up correctly, however the chart is not depicting these values. Any ideas?
Here is my javascript:
$(document).ready(function() {
var twitterChart;
posArr = [];
negArr = [];
function renderGraph()
{
$.getJSON("FrontController", function(jsonArr, statusTxt, xhr){
console.log("Data returned : " + data);*/
posArr = jsonArr[0].data;
negArr = jsonArr[1].data;
console.log("posArr: " + posArr);
console.log("negArr: " + negArr);
twitterChart.series[0].data = posArr;
twitterChart.series[1].data = negArr;
});
setTimeout(renderGraph, 3000)
}
twitterChart = new Highcharts.Chart({
chart: {
renderTo: 'container1',
type: 'column'
},
title: {
text: ''
},
subtitle: {
text: ':'
},
xAxis: {
categories: [
'Column 1',
'Column 2',
'Column 3',
'Column 4'
]
},
yAxis: {
min: 0,
title: {
text: 'Testing"'
}
},
legend: {
layout: 'vertical',
backgroundColor: '#FFFFFF',
align: 'left',
verticalAlign: 'top',
x: 100,
y: 70,
floating: true,
shadow: true
},
tooltip: {
formatter: function() {
return ''+
this.x +': '+ this.y +' tweets';
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Positive',
data: posArr
}, {
name: 'Negative',
data: negArr
}]
});
renderGraph();
});
</script>
To update series data use twitterChart.series[0].setData(array); see: http://api.highcharts.com/highcharts#Series.setData()

Resources