Highcharts Donut Chart text in center change on hover - highcharts

I'm looking for a way to have text in the center of a donut chart change on hover. When there is no hover state, the center will display the total amount (here I am representing vacation days, so it may be 15 vacation days). When hovering, the text in the center needs to match the segment hovered over (available, requested, and taken vacation days), but I cannot figure out how to change the text in the center based on hover. Does anyone have any ideas? Any help is greatly appreciated!!
http://jsfiddle.net/NVX3S/146/
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container1" style="min-width: 300px; height: 300px; margin: 0 auto"></div>
$(function () {
var colors = ['#8d62a0', '#ceb3d8', '#d5dddd'];
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container1',
type: 'pie',
height: 250,
width: 250,
borderRadius: 0
},
credits: {
enabled: false
},
title: false,
tooltip: false,
plotOptions: {
pie: {
borderWidth: 6,
startAngle: 90,
innerSize: '55%',
size: '100%',
shadow: true,
// {
// color: '#000000',
// offsetX: 0,
// offsetY: 2,
// opacity: 0.7,
// width: 3
// },
dataLabels: false,
stickyTracking: false,
states: {
hover: {
enabled: true
}
}
}
},
series: [{
data: [
{y:65, color: colors[0]},
{y:15, color: colors[1]},
{y:20, color: colors[2]}
]
// data: [
// ['Firefox', 44.2],
// ['IE7', 26.6],
// ['IE6', 20],
// ['Chrome', 3.1],
// ['Other', 5.4]
// ]
}]
},
function(chart) { // on complete
var xpos = '50%';
var ypos = '53%';
var circleradius = 102;
// Render the text
chart.renderer.text('126.5', 103, 127).css({
width: circleradius*2,
color: '#4572A7',
fontSize: '16px',
textAlign: 'center'
}).attr({
// why doesn't zIndex get the text in front of the chart?
zIndex: 999
}).add();
});
});

Use points.events.mouseOver/mouseOut, for example: http://jsfiddle.net/NVX3S/147/
Code:
point: {
events: {
mouseOver: function(){
this.series.chart.innerText.attr({text: this.y});
},
mouseOut: function(){
this.series.chart.innerText.attr({text: 112});
}
}
}
Where innerText is just custom property, created like this:
chart.innerText = chart.renderer.text('112', 112, 125).css({ ... }).add();

Related

How can I find x and y coordinates of a point in a pie?

How can I get the X and Y coordinates of a point?
I would like to place a badge at the arc line, right between the two points in the pie chart. At the moment I add circle and text using the chart renderer.
$(function() {
$('#chart').highcharts({
chart: {
type: 'pie',
options3d: {
enabled: false,
alpha: 0
},
height: 470
},
colors: ['#EF3F3E', '#757575'],
title: {
style: {
fontSize: '90px',
align: 'center',
color: '#EF3F3E'
},
verticalAlign: 'middle'
},
tooltip: {
enabled: false
},
plotOptions: {
pie: {
innerSize: 420,
dataLabels: {
enabled: false
},
shadow: false,
center: ['50%', '50%'],
borderWidth: 0
}
},
series: [{
name: 'Delivered amount',
data: [56, 44]
}]
}, function(chart) { // on complete
chart.setTitle({
color: "#333333",
});
chart.renderer.circle(650, 420, 50).attr({
fill: '#3A3A3B',
'stroke-width': 0,
zIndex: 3
}).add();
chart.renderer.circle(650, 420, 40).attr({
fill: '#EF3F3E',
'stroke-width': 0,
zIndex: 4
}).add();
chart.customText = chart.renderer.text('30', 635, 430)
.css({
color: 'black',
font: '28px Arial, sans-serif',
'z-index': '5'
}).attr({
zIndex: 5
}).add();
});
});
https://jsfiddle.net/yq2s5ng1/5/
Is there a better way to display this badge? For example using markers insted of rendering custom elements?
EDIT:
In case anyone is interested, here is how I solved this:
function getCoordinates() {
var x;
var y;
var h = chart.series[0].data[0].shapeArgs.x;
var k = chart.series[0].data[0].shapeArgs.y;
var r = chart.series[0].data[0].shapeArgs.innerR;
//var theta = chart.series[0].data[0].angle;
//var theta = chart.series[0].data[0].series.endAngleRad;
var theta = chart.series[0].data[0].shapeArgs.end;
x = h + r * Math.cos(theta);
y = k + r * Math.sin(theta);
return { x: x, y: y };
}
This post helped me to find the Cartesian coordinates: https://math.stackexchange.com/questions/475917/how-to-find-position-of-a-point-based-on-known-angle-radius-and-center-of-rotat

Highcharts Progress Bar Chart

Is it possible to create a progress chart in Highcharts like this:
https://0.s3.envato.com/files/84221450/screenshots/weblator_responsive_charts_7_bootstrap.jpg
I believe a bar chart can be customized to create this. But is it possible to change the styling so that no axes are showing and the bar labels are positioned above the bars rather than preceding them?
Possible to recreate but it is not so dynamic( in style ). So you have to adjust css according to data series in chart.
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'bar',
marginBottom: 120
},
legend: {
enabled: false
},
colors: ['#173c64'],
xAxis: {
categories: ['option 1', 'option 2', 'option 3', 'option 4 ', 'option 5 '],
labels: {
align: 'left',
x: 5,
y: -20, /* to be adjusted according to number of bars*/
style: {
fontSize: "1rem",
color: '#000'
}
},
lineWidth: 0,
gridLineWidth: 0,
lineColor: 'transparent',
minorTickLength: 0,
tickLength: 0,
title: {
enabled: false
}
},
yAxis: {
lineWidth: 0,
gridLineWidth: 0,
lineColor: 'transparent',
labels: {
enabled: false
},
minorTickLength: 0,
tickLength: 0,
title: {
enabled: false
}
},
plotOptions: {
bar: {
stacking: "normal",
//groupPadding: 0, //add here
//pointPadding: 0, //add here,
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
}
}
},
title: {
margin: 0,
useHTML: true,
text: "Test",
style: {
"color": "#333333",
"fontSize": "1.5rem",
"fontWeight": "bold"
}
},
series: [{
data: [{
y: 100,
color: '#99ddff'
}, {
y: 10,
color: '#ff8c1a'
}, {
y: 20,
color: '#ff471a'
}, {
y: 60,
color: '#c299ff'
}, {
y: 10,
color: '#99ddff'
}]
}]
})
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

Label Y-Axis stops in Highcharts solid gauges

I have a solid gauge that looks like this: https://jsfiddle.net/fb5Ltxbt/
These are the requirements I've achieved thus far:
Gauge range of 0 to 50
If value < 40, show in red, else in green
Display major ticks
This is what I want to add if it's possible:
Something to indicate Y-Axis stops as defined in the code
In other words, what I want is to ADD something like the "250" indicator from the following image:
It doesn't have to be an additional number, just something, anything, that marks each stop.
JSFiddle source code below:
var gaugeOptions = {
chart: {
type: 'solidgauge',
backgroundColor: null
},
title: null,
pane: {
center: ['35%', '75%'],
size: '100%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: 'white',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
// the value axis
yAxis: {
// Add something to indicate each of these stops
stops: [
[0.8, 'red'],
[1.0, 'green']
],
lineWidth: 0,
tickInterval: 10, // Keep the labels for these ticks
minorTickInterval: 2.5,
title: null,
labels: {
y: -5,
style: {
'color': 'black',
'fontWeight': 'bold'
}
}
}
};
var chart = Highcharts.chart('chartContainer', Highcharts.merge(gaugeOptions, {
yAxis: {
min: 0,
max: 50,
title: null
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'ABC',
data: [30],
dataLabels: {
formatter: function() {
return this.point.y;
},
y: -20,
style: {
textShadow: false,
'color': 'black',
'fontSize': '2em'
},
borderWidth: 0
},
tooltip: {
valueSuffix: ' km/h'
}
}]
}));
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/solid-gauge.js"></script>
<div style="width: 1024px; height: 768px; margin: 0;">
<div id="chartContainer"></div>
</div>
I have seen how to make specific labels outside gauge chart but it requires all normal ticks to be removed. I'd don't want them to be removed if possible but please say so if what I want is not possible too, thanks!
You can an additional yAxis to achieve additional ticks. The additional axis should be linked to the main axis. Then, it has the same scale as the main axis and you gain control over additional, independent ticks - you define specific ticks via tickPositions.
{
linkedTo: 0,
lineWidth: 0,
minorTickLength: 0,
tickPositions: [35],
tickLength: 75,
labels: {
x: 30,
y: -30,
style: {
fontSize: '25px'
}
}
}]
Live example and output
https://jsfiddle.net/ym2tvzy7/
If you want ticks with different color and styles, you can add more axes.
https://jsfiddle.net/ym2tvzy7/1/
If i understand your requirements correctly
May be this will help not exactly as your requirement anyway as an alternative
You can also try :-
plotBands: [{
from: 39.5,
to: 40.5,
color: '#55BF3B' // green
}]
var gaugeOptions = {
chart: {
type: 'solidgauge',
backgroundColor: null
},
title: null,
pane: {
center: ['35%', '75%'],
size: '100%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: 'white',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
// the value axis
yAxis: {
stops: [
[0.8, 'red'],
[1.0, 'green']
],
lineWidth: 0,
tickInterval: 10,
minorTickInterval: 2.5,
title: null,
labels: {
y: -5,
style: {
'color': 'black',
'fontWeight': 'bold'
}
}
}
};
var chart = Highcharts.chart('chartContainer', Highcharts.merge(gaugeOptions, {
yAxis: {
min: 0,
max: 50,
title: null,
plotBands: [{
from: 0,
to: 40,
color: '#55BF3B' // green
}]
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'ABC',
data: [31],
dataLabels: {
formatter: function() {
return this.point.y;
},
y: -20,
style: {
textShadow: false,
'color': 'black',
'fontSize': '2em'
},
borderWidth: 0
},
tooltip: {
valueSuffix: ' km/h'
}
}]
}));
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/solid-gauge.js"></script>
<div style="width: 1024px; height: 768px; margin: 0;">
<div id="chartContainer"></div>
</div>

Highcharts pie tooltip cuts off

I am using Highcharts to generate a pie chart. It is generating it fine but the problem is that I have a fixed area to display the chart and the tooltip has large amount of text.
The tooltip is too large to fit inside the chart div, how can I fix this?
$(function () {
var chart;
$(document).ready(function () {
chart = new Highcharts.Chart({
chart: {
renderTo: 'ER_inpatient_stay',
plotBackgroundColor: null,
plotBorderWidth: null,
marginTop: 0,
plotShadow: false,
backgroundColor: 'none',
},
legend: {
layout: 'vertical',
align: 'left',
borderColor: '#fff',
itemMarginTop: 30,
verticalAlign: 'top',
x: 70,
y: 0
},
title: {
text: ''
},
tooltip: {
userHTML: true,
style: {
padding: 10,
width: 250,
height: 60,
},
formatter: function () {
return this.point.residents;
},
},
exporting: {
enabled: false
},
credits: {
enabled: false
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
formatter: function () {
return this.point.y;
},
color: 'white',
distance: -10
},
showInLegend: true,
tooltip: {}
}
},
series: [{
type: 'pie',
size: 80,
name: '',
data: [{
'name': 'E/R Visits',
'y': 1,
'residents': "fMonroe Monroe",
'color': '#FA3D19'
}, {
'name': 'Inpatient Stay',
'y': 21,
'residents': "fGinanni Ginanni, fJobs Jobs, fMonroe Monroe, fDickerson Dickerson, fBrown Brown, fHerter Herter, fDavidson Davidson, fFernbach Fernbach, fGentry Gentry, fJones Jones, fKostic Kostic, fnewresident lnewresident, fLogger Logger, fMaxwell Maxwell, fMcGuire McGuire, fMiller Miller, fO'Farrell O'Farrell, fProgram Program, fProgram2 Program2, frer rer",
'color': 'orange'
}]
}]
});
});
})
Fiddle: http://jsfiddle.net/faridu86/syrF6/
Please take look at the workaournd with using CSS
.highcharts-container {
overflow: visible !important;
}
.tooltip {
position: relative;
z-index: 50;
border: 2px solid rgb(0, 108, 169);
border-radius: 5px;
background-color: #ffffff;
padding: 5px;
font-size: 9pt;
}
http://jsfiddle.net/G4NLn/8/
There's the solution that worked for me, inspired by Sebastian's one :
.highcharts-container,
.highcharts-container svg {
overflow: visible !important;
}
Only takes care of the overflow though, you still need to clip the content of the tooltip.
Issue has been resolved for me, by changing the svg and highchart-container size dynamically as following
$('.highcharts-series-group').mouseover(function(){
setTimeout(function() {
$('.highcharts-tooltip').css({'height': 'auto !important'});
var tspans = $('tspan').length;
var svg_height = 150;
if( (tspans * 16 ) > 150 ){
svg_height = tspans * 16;
}
$('.highcharts-container').css({'height': svg_height+'px'});
$('svg').height(svg_height);
},100);
});
fiddle : http://jsfiddle.net/faridu86/syrF6/2/
I used the answer bellow but its not working for me. The problem for me is the tooltip is coming normally but when the mouse is out the tooltip doesn't dissapear. Here is my solution and it works fine for me. Hope that it will help
$('.highcharts-series-group').mouseenter(function(){
$('.highcharts-tooltip').css({'height': 'auto !important'});
var tspans = $('tspan').length;
var svg_height = 150;
if( (tspans * 16 ) > 150 ){
svg_height = tspans * 16;
}
$('.highcharts-container').css({'height': svg_height+'px'});
$('svg').height(svg_height);
defect_chart.redraw()
});
$('.highcharts-series-group').mouseleave(function(){
defect_chart.tooltip.hide()
});

Highcharts donut - Center text

I got a donut chart which I want to output text inside when hovering the different parts in the chart. I'm almost done, but I can't seem to get the text to center correctly.
JSFiddle: http://jsfiddle.net/K5dhu/
Code:
$(function() {
var colors = ['#8d62a0', '#ceb3d8', '#d5dddd'];
var chart = new Highcharts.Chart({
chart: {
renderTo: 'DivGraphCategories',
type: 'pie',
height: 250,
width: 250,
borderRadius: 0
},
credits: {
enabled: false
},
title: false,
plotOptions: {
pie: {
borderWidth: 0,
startAngle: 90,
innerSize: '70%',
size: '100%',
shadow: false,
// {
// color: '#000000',
// offsetX: 0,
// offsetY: 2,
// opacity: 0.7,
// width: 3
// },
dataLabels: false,
stickyTracking: false,
states: {
hover: {
enabled: false
}
},
point: {
events: {
mouseOver: function(){
this.series.chart.innerText.attr({text: this.y});
},
mouseOut: function(){
this.series.chart.innerText.attr({text: "Mouseout value"});
}
}
}
}
},
series: [{
data: [
{y: 6926, name: 'hopp1'},
{y: 1370, name: 'hopp2'},
{y: 1250, name: 'hopp3'},
{y: 10000, name: 'hopp4'},
{y: 1062, name: 'hopp5'},
{y: 6376, name: 'hopp6'},
{y: 2514, name: 'hopp7'},
{y: 349, name: 'hopp8'}
]
// data: [
// ['Firefox', 44.2],
// ['IE7', 26.6],
// ['IE6', 20],
// ['Chrome', 3.1],
// ['Other', 5.4]
// ]
}]
},
function(chart) { // on complete
var xpos = '50%';
var ypos = '53%';
var circleradius = 102;
// Render the text
chart.innerText = chart.renderer.text('Start value', 112, 125).css({
width: circleradius*2,
color: '#4572A7',
fontSize: '16px',
textAlign: 'center'
}).attr({
// why doesn't zIndex get the text in front of the chart?
zIndex: 999
}).add();
});
});
As you can see, the text is aligned left and positioned at 112, 125 at start. Is there any way to set the position each time based on how long the text is? Or any property that sets this text to center, no matter whats in there.. The text is set on hover within the mouseOver function.
Well, this might not be the perfect definition of an answer to this question. But it's a way of solving the problem in another way, which gives far more customizability.
Create a div or something outside the graph area and position it with css exactly at the center of where the donut chart will appear.
Example:
<div class="graph_center">
<h5 id="GraphCenterName"></h5>
<h3 id="GraphCenterValue"></h3
</div>
Then you would just use the mouseover and mouseout events like this:
point: {
events: {
mouseOver: function(){
$('#GraphCenterName').text(this.name);
$('#GraphCenterValue').text(this.y);
},
mouseOut: function(){
$('#GraphCenterName').text('Whatever default text you want');
$('#GraphCenterValue').text('Whatever default value you want"');
}
}
}

Resources