highcharts Gauge live data with ajax - highcharts

I have used highcharts library in my project and i have to update Gauge every 1 second with data creating by PHP file .
The highcharts Gauge Example have some code to make it trigger every 1 second , but i cant found some thing like This Link to get data from PHP file with Ajax Technic .
Anyone have any idea to implement this scenario and apply Ajax to this chart ?

You not found example because it is generic, see how it can be done with $.post():
http://jsfiddle.net/oceog/zpwdp/
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'gauge',
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
plotShadow: false
},
title: {
text: 'Speedometer'
},
pane: {
startAngle: -150,
endAngle: 150,
background: [{
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#FFF'],
[1, '#333']
]
},
borderWidth: 0,
outerRadius: '109%'
}, {
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#333'],
[1, '#FFF']
]
},
borderWidth: 1,
outerRadius: '107%'
}, {
// default background
}, {
backgroundColor: '#DDD',
borderWidth: 0,
outerRadius: '105%',
innerRadius: '103%'
}]
},
// the value axis
yAxis: {
min: 0,
max: 200,
minorTickInterval: 'auto',
minorTickWidth: 1,
minorTickLength: 10,
minorTickPosition: 'inside',
minorTickColor: '#666',
tickPixelInterval: 30,
tickWidth: 2,
tickPosition: 'inside',
tickLength: 10,
tickColor: '#666',
labels: {
step: 2,
rotation: 'auto'
},
title: {
text: 'km/h'
},
plotBands: [{
from: 0,
to: 120,
color: '#55BF3B' // green
}, {
from: 120,
to: 160,
color: '#DDDF0D' // yellow
}, {
from: 160,
to: 200,
color: '#DF5353' // red
}]
},
series: [{
name: 'Speed',
data: [80],
tooltip: {
valueSuffix: ' km/h'
}
}]
},
// Add some life
function (chart) {
setInterval(function () {
$.post('/echo/json/',{
json: JSON.stringify({
//This is just a sample data, see comments after code to real world usage
inc: Math.round((Math.random() - 0.5) * 20)
})},
function(data) {
var point = chart.series[0].points[0],
newVal,
inc = data.inc;
newVal = point.y + inc;
if (newVal < 0 || newVal > 200) {
newVal = point.y - inc;
}
point.update(newVal);
},"json");
}, 3000);
});
});​
In real world you use something like
$.post('new_data.php',{last_time: last_time},function(data) {
//set point here
},"json")
in new_data.php you do something like
$data=get_data($last_time);
echo json_encode($data);
exit;

Related

Update chart border color with plotband color in angular gauge

I have a highchart for gauge as below :
$('#div1').highcharts({
chart: {
type: 'gauge',
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
plotShadow: false,
borderColor:'#EBBA95',
borderWidth: 2
},
title: {
text: 'demo'
},
credits: {
enabled: false
},
pane: {
startAngle: -150,
endAngle: 150,
background: [{
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#FFF'],
[1, '#333']
]
},
borderWidth: 0,
outerRadius: '109%'
}, {
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#333'],
[1, '#FFF']
]
},
borderWidth: 1,
outerRadius: '107%'
}, {
// default background
}, {
backgroundColor: '#DDD',
borderWidth: 0,
outerRadius: '105%',
innerRadius: '103%'
}]
},
// the value axis
yAxis: {
min: 0,
max: 100,
minorTickInterval: '',
tickPixelInterval: 15,
tickWidth: 2,
tickPosition: 'inside',
tickLength: 10,
tickColor: '#666',
labels: {
step: 2,
rotation: 'auto'
},
title: {
text: ''
},
plotBands: [{
from: 0,
to: 50,
color: '#55BF3B' // green
}, {
from: 50,
to: 80,
color: '#DDDF0D' // yellow
}, {
from: 80,
to: 100,
color: '#DF5353' // red
}]
},
series: [{
name: 'series1',
data: [100],
tooltip: {
valueSuffix: ''
}
}]
});
Now I want to update the border color of a chart with series data plot band color. As here we have data as 100 and its plot band color is red ,the chart border color should be red. How can I get plot band color ?
Initially it will be different color . I want to update the color depending on the plot band color for the tool tip of the gauge . In the above image it is 186 so the color shoulb be red.If it falls under 0-120 category then the border color should be green.
put proper red color in chat options
Fiddle
chart: {
renderTo: 'container',
type: 'gauge',
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
plotShadow: false,
borderColor:'#DF5353', /*red color*/
borderWidth: 2
},
Update
After getting what OP wants
using events load option you can get changed border color based on date value in series
Update fiddle link
Test it by changing the data value in series
events: {
load: function() {
var series_data = this.series[0].data; //this is series data
for (var i = 0; i < series_data.length; i++) {
if (series_data[i].y >= 0 && series_data[i].y <= 120) {
this.update({
chart: {
borderColor: "#55BF3B"
},
});
}
if (series_data[i].y > 120 && series_data[i].y <= 160) {
this.update({
chart: {
borderColor: "#DDDF0D"
},
});
}
if (series_data[i].y > 160 && series_data[i].y <= 200) {
this.update({
chart: {
borderColor: "#DF5353"
},
});
}
}
}
}

Highcharts Gauge Chart with text labels

i have a gauge charts with defined tick positions and want to show some text labels there. e. g.:
-300 => Text 1
-200 => Text 2
-100 => Text 3
...
Is there a way to do that? Here's a fiddle of the chart.
jsfiddle.net/r8d3jnx6/
$('#container').highcharts({
chart: {
type: 'gauge',
alignTicks: false,
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
plotShadow: false
},
title: {
text: null
},
tooltip: {
enabled: false
},
pane: {
startAngle: -70,
endAngle: 70,
background: null
},
plotOptions: {
gauge: {
dial: {
baseWidth: 2,
baseLength: '100%',
radius: '100%',
rearLength: 0
},
pivot: {
radius: 5
},
dataLabels: {
enabled: false
}
}
},
yAxis: [ {
min: -300,
max: 500,
tickPositions: [-300, -200, -100, 0, 100, 200, 300, 400, 500],
lineColor: '#933',
lineWidth: 0,
minorTickPosition: 'outside',
tickLength: 0,
minorTickLength: 0,
labels: {
distance: 12,
//rotation: 'auto'
},
//categories: ['Grunder'],
offset: 0,
plotBands: [{
from: -300,
to: 2,
thickness: 15,
color: '#55BF3B'
}, {
from: 0,
to: 202,
thickness: 15,
color: {
linearGradient: {x1: 0, x2: 1, y1: 0, y2: 0},
stops: [
[0, '#55BF3B'], //green
[1, '#DDDF0D'] //yellow
]
}
}, {
from: 200,
to: 500,
thickness: 15,
color: {
linearGradient: {x1: 0, x2: 1, y1: 0, y2: 0},
stops: [
[0, '#DDDF0D'], //yellow
[1, '#ff0000'] //red
]
}
}]
}, {
min: -200,
max: 200,
lineColor: '#339',
tickColor: '#339',
minorTickColor: '#339',
offset: -100,
lineWidth: 2,
labels: {
enabled: false
},
tickLength: 5,
minorTickLength: 5,
endOnTick: false
}],
series: [{
data: [250]
}]
});
You can use a yAxis.labels.formatter function to check what the label value is, and return a text of your choice depending on that value.
To take your example, it could look like this:
yAxis: {
labels: {
formatter: function() {
if(this.value == -300) return '-300 => Text 1';
if(this.value == -200) return '-200 => Text 2';
if(this.value == -100) return '-100 => Text 3';
return this.value;
}
}
See this updated JSFiddle of how it works. As you can see you'll have to face the potential issue of labels overlapping the gauge, but you can fix that with distance, rotation and some other attributes (see API), depending on how your labels end up looking.

How to refresh pane background when update date on gauge highcharts?

I have a problem when I update the gauge chart.
I add some life from my chart using this code:
$(function () {
$.getJSON('my.php', function(data) {
$('.container').highcharts({
chart: {
type: 'gauge',
borderWidth: 0,
width: 170,
height: 200,
marginTop: 0,
},
title: {
text: 'title',align: 'center',x: 0,y: 8,floating: true
},
pane: [{
startAngle: -120,
endAngle: 120,
background: [
{
backgroundColor: {linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },stops: [[0, '#FFF'],[1, '#333']]},borderWidth: 0,outerRadius: '109%'
},
{
backgroundColor: {linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },stops: [[0, '#333'],[1, '#FFF']]},borderWidth: 1,outerRadius: '107%'
},
{
backgroundColor: mybackgroundColor(data,100,120),
},
{
backgroundColor: '#DDD',borderWidth: 0,outerRadius: '105%',innerRadius: '103%'
}
]
}],
yAxis: {
min: 0,
max: 250,
minorTickWidth: 1,
minorTickLength: 10,
minorTickPosition: 'inside',
minorTickColor: '#666',
tickPixelInterval: 30,
tickWidth: 2,tickLength: 10,
tickColor: '#666',
labels: {
step: 2,
rotation: 'auto'
},
title: {
text:'km/h',
y : 85
},
plotBands: [
{
from: 0,
to: 100,
color: '#DDDF0D' /*yellow*/
},
{
from: 100,
to: 120,
color: '#55BF3B' /*green*/
},
{
from: 120,
to: 250,
color: '#DDDF0D' /*yellow*/
},
{
from: 30,
to: 35,
color: '#DF5353' /*red*/
}
]
},
series: [
{
name: 'speed',
data: data,
dataLabels: {
enabled: true,
style: {
fontWeight:'bold',
fontSize: '12px'
}
}
}
]
},
// Add some life
function (chart) {
if (!chart.renderer.forExport) {
setInterval(function () {
chart.series[0].color = "#000";
var point = chart.series[0].points[0];
$.getJSON('mydata.php', function(data) {
point.update(data);
});
}, 3000);
};
});
});
});
my function mybackgroundColor(data,100,120) define the pane backgroundColor, it depends the value of data.
When I update point ussing this part of code:
// Add some life
function (chart) {
if (!chart.renderer.forExport) {
setInterval(function () {
chart.series[0].color = "#000";
var point = chart.series[0].points[0];
$.getJSON('mydata.php', function(data) {
point.update(data);
});
}, 3000);
};
});
The wild was updated, but the background didn't.
I try to do this, but it doesn't work:
this.chart.plotBackground.attr({
fill: '#fff'
})
;
Does anybody has an idea?
It's pane background, not a plotBackground. Anyway, in the fact pane background is stored as one of plotBands. So simple you can update it's color:
chart.yAxis[0].plotLinesAndBands[2].svgElem.attr({
fill: 'green'
});
It's [2], because I want to update red plotBand which is third element from background options in a pane.
And some demo: http://jsfiddle.net/Yrygy/190/

Using the VU Guage: add the numeric value below needle

In the Highcharts samples, I am seeing that the speedometer has a numeric indicator below the needle pivot (http://jsfiddle.net/radi8/kbvC3/497/). I am going to use the VU gauge but would like to add this numeric indicator to this gauge view. Playing with the fiddle (http://jsfiddle.net/radi8/Z2s77/1/), I cannot seem to get this element to display.
Any suggestions?
My VU from jsFiddle
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'gauge',
plotBorderWidth: 1,
plotBackgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#FFF4C6'],
[0.3, '#FFFFFF'],
[1, '#FFF4C6']
]
},
plotBackgroundImage: null,
height: 200
},
title: {
text: 'Efficiencies'
},
pane: [{
startAngle: -45,
endAngle: 45,
background: null,
center: ['25%', '145%'],
size: 300
}, {
startAngle: -45,
endAngle: 45,
background: null,
center: ['75%', '145%'],
size: 300
}],
yAxis: [{
min: 0,
max: 110,
minorTickPosition: 'outside',
tickPosition: 'outside',
labels: {
rotation: 'auto',
distance: 20
},
plotBands: [{
from: 0,
to: 70,
color: '#DF5353', // red
innerRadius: '100%',
outerRadius: '105%'
}, {
from: 70,
to: 95,
color: '#DDDF0D', // yellow
innerRadius: '100%',
outerRadius: '105%'
}, {
from: 95,
to: 110,
color: '#55BF3B', // green
innerRadius: '100%',
outerRadius: '105%'
}],
pane: 0,
title: {
text: 'OEE %<br/><span style="font-size:8px">Machine 001</span>',
y: -40
}
}, {
min: 0,
max: 110,
minorTickPosition: 'outside',
tickPosition: 'outside',
labels: {
rotation: 'auto',
distance: 20
},
plotBands: [{
from: 0,
to: 70,
color: '#DF5353', // red
innerRadius: '100%',
outerRadius: '105%'
}, {
from: 70,
to: 95,
color: '#DDDF0D', // yellow
innerRadius: '100%',
outerRadius: '105%'
}, {
from: 95,
to: 110,
color: '#55BF3B', // green
innerRadius: '100%',
outerRadius: '105%'
}],
pane: 1,
title: {
text: 'Cycle Eff %<br/><span style="font-size:8px">Machine 001</span>',
y: -40
}
}],
plotOptions: {
gauge: {
dataLabels: {
enabled: false
},
dial: {
radius: '100%'
}
}
},
series: [{
data: [80],
yAxis: 0
}, {
data: [80],
yAxis: 1
}]
},
// Let the music play
function(chart) {
setInterval(function() {
var left = chart.series[0].points[0],
right = chart.series[1].points[0],
leftVal,
//inc = (Math.random() - 0.5) * 30;
inc1 = Math.random() * (30) + 70;
inc2 = Math.random() * (30) + 70;
leftVal = left.y + inc1;
rightVal = right.y + inc2; // / 3;
if (leftVal < 0 || leftVal > 110) {
//leftVal = left.y - inc;
leftVal = 110 - inc1;
}
if (rightVal < 0 || rightVal > 110) {
rightVal = 110 - inc2;
}
left.update(leftVal,false);
right.update(rightVal);//, false);
chart.redraw();
}, 1500);
});
});
You have to enable and position dataLabels (now you have dataLabels: {enabled: false}).
See: http://api.highcharts.com/highcharts#plotOptions.gauge.dataLabels

How can you change the "label" (valueSuffix) on a highcharts gauge after the gauge has been created?

How can you change the "label" (valueSuffix) on a highcharts gauge after the gauge has been created?
something like this:
chart.series[0].remove();
chart.addSeries({
name: 'Speed',
data: [Math.random() * 200],
tooltip: {
valueSuffix: parseInt(Math.random() * 200, 16)
}
});
http://jsfiddle.net/noahpeters/d36Se/1/
Yes it is possible by using html for yAxis title. Then you can use jquery or js to change div content.
http://jsfiddle.net/d36Se/2/
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'gauge',
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
plotShadow: false
},
title: {
text: 'Speedometer'
},
pane: {
startAngle: -150,
endAngle: 150,
background: [{
backgroundColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, '#FFF'],
[1, '#333']
]
},
borderWidth: 0,
outerRadius: '109%'
}, {
backgroundColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, '#333'],
[1, '#FFF']
]
},
borderWidth: 1,
outerRadius: '107%'
}, {
// default background
}, {
backgroundColor: '#DDD',
borderWidth: 0,
outerRadius: '105%',
innerRadius: '103%'
}]
},
// the value axis
yAxis: {
min: 0,
max: 200,
minorTickInterval: 'auto',
minorTickWidth: 1,
minorTickLength: 10,
minorTickPosition: 'inside',
minorTickColor: '#666',
tickPixelInterval: 30,
tickWidth: 2,
tickPosition: 'inside',
tickLength: 10,
tickColor: '#666',
labels: {
step: 2,
rotation: 'auto'
},
title: {
useHTML:true, text: '<div id="tooltipTitle">km/h</div>'
},
plotBands: [{
from: 0,
to: 120,
color: '#55BF3B' // green
}, {
from: 120,
to: 160,
color: '#DDDF0D' // yellow
}, {
from: 160,
to: 200,
color: '#DF5353' // red
}]
},
tooltip: {
formatter: function(){
return 'aaa';
}
//valueSuffix: ' km/h'
},
series: [{
name: 'Speed',
data: [80]
}]
},
// Add some life
function (chart) {
setInterval(function () {
chart.series[0].remove();
chart.addSeries({
name: 'Speed',
data: [Math.random() * 200]
});
$('#tooltipTitle').html('aaaa');
}, 3000);
});

Resources