How to place tooltip above point in highcharts x-range? - highcharts

I have created highcharts x-range chart with multiple series in one category. Tooltips are displaying above the middle of the category. I'd like it to display above each x-range point. How can I do it?
I've tried to override tooltip positioner. But the argument point contains values plotX and plotY like it is placed on the middle of y value and I can't calculate its real position.
this.tooltipPositioner = function(labelWidth, labelHeight, point){
return {
x: point.plotX,
y: point.plotY
};
};
Live Demo: https://jsfiddle.net/levra/mh7uj93r/

You can use tooltip.positioner but instead of the argument point, you can use hovered point object where you will find the correct point plot values. To get hovered point object use plotOptions.series.point.events.mouseOver callback. Check the code and demo posted below:
HTML:
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/xrange.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
JS:
var hoveredPoint;
var chart = Highcharts.chart('container', {
chart: {
type: 'xrange'
},
title: {
text: 'Highcharts X-range'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: ''
},
categories: ['Prototyping', 'Development', 'Testing'],
reversed: true
},
tooltip: {
positioner: function(labelWidth, labelHeight) {
var x = hoveredPoint.shapeArgs.x,
y = hoveredPoint.shapeArgs.y,
width = hoveredPoint.shapeArgs.width,
tooltipOffsetTop = -10;
return {
x: x + chart.plotLeft + width / 2 - labelWidth / 2,
y: y + chart.plotTop - labelHeight + tooltipOffsetTop
}
}
},
plotOptions: {
xrange: {
point: {
events: {
mouseOver: function() {
hoveredPoint = this;
},
mouseOut: function() {
hoveredPoint = null;
}
}
}
}
},
series: [{
name: 'Project 1',
// pointPadding: 0,
groupPadding: 0,
borderColor: 'gray',
pointWidth: 20,
data: [{
x: Date.UTC(2014, 10, 21),
x2: Date.UTC(2014, 11, 2),
y: 0,
partialFill: 0.25
}, {
x: Date.UTC(2014, 11, 2),
x2: Date.UTC(2014, 11, 5),
y: 1
}, {
x: Date.UTC(2014, 11, 8),
x2: Date.UTC(2014, 11, 9),
y: 2
}, {
x: Date.UTC(2014, 11, 9),
x2: Date.UTC(2014, 11, 19),
y: 1
}, {
x: Date.UTC(2014, 11, 10),
x2: Date.UTC(2014, 11, 23),
y: 2
}],
dataLabels: {
enabled: true
}
},
{
name: 'Project 2',
// pointPadding: 0,
// groupPadding: 0,
borderColor: 'gray',
pointWidth: 20,
data: [{
x: Date.UTC(2014, 10, 23),
x2: Date.UTC(2014, 11, 2),
y: 0,
partialFill: 0.25
}],
dataLabels: {
enabled: true
}
}
]
});
Demo:
https://jsfiddle.net/BlackLabel/51gybnew/1/

Related

how can i scroll my gnatt highcharts using mouse wheel

https://jsfiddle.net/cavrzkwg/1/
here is the link for the gnatt highcharts i want to make it scrollable using mousewheel not the main scrollbar but the inbuilt scrollbar of highcharts
i tried the setextremes ec=vents but no luck
here is the link for the gnatt highcharts i want to make it scrollable using mousewheel not the main scrollbar but the inbuilt scrollbar of highcharts
i tried the setextremes ec=vents but no luck
Highcharts.ganttChart('container', {
title: {
text: 'Gantt Chart with Progress Indicators'
},
scrollbar: {
enabled: true
},
yAxis: {
min: 0,
max: 3,
scrollbar:{
enabled : true,
showFull: false
}
},
xAxis: {
min: Date.UTC(2014, 10, 17),
max: Date.UTC(2014, 10, 30),
grid: {
enabled: true,
borderColor: 'rgba(0,0,0,0.3)',
borderWidth: 1
}
},
legend: {
enabled: true
},
series: [{
name: 'Project 1',
data: [{
name: 'Start prototype',
start: Date.UTC(2014, 10, 18),
end: Date.UTC(2014, 10, 25),
completed: 0.25
},{
name: 'Start',
start: Date.UTC(2014, 9, 18),
end: Date.UTC(2014, 10, 25),
completed: 0.25
}, {
name: 'Test prototype',
start: Date.UTC(2014, 10, 27),
end: Date.UTC(2014, 10, 29)
}, {
name: 'Develop',
start: Date.UTC(2014, 10, 20),
end: Date.UTC(2014, 10, 25),
completed: {
amount: 0.12,
fill: '#fa0'
}
}, {
name: 'Run acceptance tests',
start: Date.UTC(2014, 10, 23),
end: Date.UTC(2014, 10, 26)
}]
},
{
name: 'Project 2',
data: [{
name: 'Start prototype2',
start: Date.UTC(2014, 10, 18),
end: Date.UTC(2014, 10, 25),
completed: 0.25
}, {
name: 'Test prototype2',
start: Date.UTC(2014, 10, 27),
end: Date.UTC(2014, 10, 29)
}]
}]
});
Try this custom functionality, which should allow scrolling the chart using the mouse wheel.
(function(H) {
//internal functions
function stopEvent(e) {
if (e) {
if (e.preventDefault) {
e.preventDefault();
}
if (e.stopPropagation) {
e.stopPropagation();
}
e.cancelBubble = true;
}
}
//the wrap
H.wrap(H.Chart.prototype, 'render', function(proceed) {
var chart = this,
mapNavigation = chart.options.mapNavigation;
proceed.call(chart);
// Add the mousewheel event
H.addEvent(chart.container, document.onmousewheel === undefined ? 'DOMMouseScroll' : 'mousewheel', function(event) {
var delta, extr, step, newMin, newMax, axis = chart.yAxis[0];
e = chart.pointer.normalize(event);
// Firefox uses e.detail, WebKit and IE uses wheelDelta
delta = e.detail || -(e.wheelDelta / 120);
delta = delta < 0 ? 1 : -1;
if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
extr = axis.getExtremes();
step = (extr.max - extr.min) / 20 * delta;
newMin = extr.min + step;
newMax = extr.max + step;
console.log('max', extr.max)
console.log('min', extr.min)
if (newMin > axis.dataMin && newMax < axis.dataMax) {
axis.setExtremes(newMin, newMax, true, false);
}
}
stopEvent(event); // Issue #5011, returning false from non-jQuery event does not prevent default
return false;
});
});
}(Highcharts));
Demo: https://jsfiddle.net/BlackLabel/9jL68mk4/

How to show multiple tooltip in xrange highcarts?

I'm trying to show some tooltip on a particular time on the xrange Highcharts on chart render , but there is no existing feature for that, is there any possibilities to show a tooltip like below
From this example I found that annotation can be used to show some message, I tried to utilize this.
function annotateChart({
text,
value
}) {
const annons = this.annons;
const label = this.renderer.label(text, 0, 0).attr({
stroke: 'red',
'stroke-width': 2,
align: 'center',
zIndex: 99,
class: 'newText'
}).add();
annons.push({
label,
value
});
}
function redrawAnnons() {
const xAxis = this.xAxis[0];
const yAxis = this.yAxis[0];
this.annons.forEach(ann => {
ann.label.animate({
x: xAxis.toPixels(ann.value),
y: yAxis.toPixels(-2.0)
});
});
}
Highcharts.chart('container', {
chart: {
type: 'xrange',
marginTop: '55',
events: {
load: function() {
this.annons = [];
[{
text: ['min', 'time', 'data', 'bluetooth'],
value: Date.UTC(2014, 11, 2)
}, {
text: 'max',
value: Date.UTC(2014, 11, 21, 5)
}].forEach(obj => {
annotateChart.call(this, obj);
});
redrawAnnons.call(this)
},
redraw: redrawAnnons
}
},
title: {
text: ''
},
xAxis: {
type: 'datetime',
minPadding: 0,
opposite: true
},
yAxis: {
title: {
text: ''
},
categories: ['Prototyping', 'Development', 'Testing'],
reversed: true,
lineColor: '#2c2d39',
lineWidth: 1
},
series: [{
name: ' ',
borderColor: 'gray',
pointWidth: 20,
data: [{
x: Date.UTC(2014, 10, 21),
x2: Date.UTC(2014, 11, 2),
y: 0,
partialFill: 0.25
}, {
x: Date.UTC(2014, 11, 2),
x2: Date.UTC(2014, 11, 5),
y: 1
}, {
x: Date.UTC(2014, 11, 8),
x2: Date.UTC(2014, 11, 9),
y: 2
}, {
x: Date.UTC(2014, 11, 9),
x2: Date.UTC(2014, 11, 19),
y: 1
}, {
x: Date.UTC(2014, 11, 10),
x2: Date.UTC(2014, 11, 23),
y: 2
}],
dataLabels: {
enabled: false
}
}]
});
#container {
min-width: 300px;
max-width: 800px;
height: 190px;
margin: 1em auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/xrange.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
I'm not sure whether this is the correct approach, if there is any other way to achieve this, also the tooltip is shown on the chart top margin
marginTop: '55'
Tooltip With more that 2 data is affecting the highchart responsiveness.

Highcharts afterAnimate in combination with xAxis max

We are developing some charts using Highcharts.
And we are using the series.events.afterAnimate in combination with an xAxis.max value.
But when we have for example points from Januari until September, but with an xAxis.max value until December the afterAnimate function is called when it reached December we guess..
But we would like have a callback when all the valid points are drawn, and not we it reaches its max value...
is this possible?
That happens because the clipPath width is by default equal width of the plot area. To change it you can overwrite Highcharts.Series.animate method and set clipPath width equal width of the particular series.
EDIT:
Also, note that when the width of the clipPath is shorter and the duration of the animation is the same the animation will be much slower. To make it look better the time has to be shortened - it can be calculated depending on series width and updated in chart load event.
Check demo and code posted below:
Code:
(function(H) {
H.Series.prototype.animate = function(init) {
var series = this,
chart = series.chart,
clipRect,
animation = H.animObject(series.options.animation),
sharedClipKey;
// Initialize the animation. Set up the clipping rectangle.
if (init) {
series.setClip(animation);
// Run the animation
} else {
sharedClipKey = this.sharedClipKey;
clipRect = chart[sharedClipKey];
if (clipRect) {
clipRect.animate({
width: series.group.getBBox().width,
x: 0
}, animation);
}
if (chart[sharedClipKey + 'm']) {
chart[sharedClipKey + 'm'].animate({
width: chart.plotSizeX + 99,
x: 0
}, animation);
}
// Delete this function to allow it only once
series.animate = null;
}
}
})(Highcharts);
Highcharts.setOptions({
chart: {
events: {
load: function() {
var chart = this,
series = chart.series[0],
seriesWidth = series.graph.getBBox().width,
duration = (seriesWidth / chart.plotWidth) * series.options.animation.duration;
chart.series[0].update({
animation: {
duration: duration
}
});
}
}
},
plotOptions: {
series: {
animation: {
duration: 2000
}
}
}
});
Highcharts.chart('container', {
title: {
text: 'Chart with half data but with max data'
},
subtitle: {
text: 'A label should appear when all points are drawn'
},
xAxis: {
type: 'datetime',
min: Date.UTC(2019, 0, 1),
max: Date.UTC(2019, 11, 31),
tickInterval: 2592000000, // month
},
plotOptions: {
series: {
events: {
afterAnimate: function() {
this.chart.renderer.label(this.name + ' has appeared', 100, 70)
.attr({
padding: 10,
fill: Highcharts.getOptions().colors[0]
})
.css({
color: 'white'
})
.add();
}
}
}
},
series: [{
data: [{
x: Date.UTC(2019, 0, 1),
y: 29.9
},
{
x: Date.UTC(2019, 1, 1),
y: 139.9
},
{
x: Date.UTC(2019, 2, 1),
y: 59.9
},
{
x: Date.UTC(2019, 3, 1),
y: 19.9
},
]
}]
});
Highcharts.chart('container2', {
title: {
text: 'Chart with full data'
},
subtitle: {
text: 'A label should appear on the plot area after animate'
},
xAxis: {
type: 'datetime',
min: Date.UTC(2019, 0, 1),
max: Date.UTC(2019, 11, 31),
tickInterval: 2592000000, // month
},
plotOptions: {
series: {
events: {
afterAnimate: function() {
this.chart.renderer.label(this.name + ' has appeared', 100, 70)
.attr({
padding: 10,
fill: Highcharts.getOptions().colors[0]
})
.css({
color: 'white'
})
.add();
}
}
}
},
series: [{
data: [{
x: Date.UTC(2019, 0, 1),
y: 29.9
},
{
x: Date.UTC(2019, 1, 1),
y: 139.9
},
{
x: Date.UTC(2019, 2, 1),
y: 59.9
},
{
x: Date.UTC(2019, 3, 1),
y: 19.9
},
{
x: Date.UTC(2019, 4, 1),
y: 29.9
},
{
x: Date.UTC(2019, 5, 1),
y: 139.9
},
{
x: Date.UTC(2019, 6, 1),
y: 59.9
},
{
x: Date.UTC(2019, 7, 1),
y: 19.9
},
{
x: Date.UTC(2019, 8, 1),
y: 29.9
},
{
x: Date.UTC(2019, 9, 1),
y: 139.9
},
{
x: Date.UTC(2019, 10, 1),
y: 59.9
},
{
x: Date.UTC(2019, 11, 1),
y: 19.9
},
]
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px"></div>
<div id="container2" style="height: 400px"></div>
Demo:
https://jsfiddle.net/BlackLabel/gj7eqkv8/
API reference:
https://api.highcharts.com/highcharts/chart.events.load

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/

highcharts Gauge live data with ajax

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;

Resources