Custom Label Show Percentage on Highcharts - highcharts

I want to customize non stacked column labels on highchats .
Instead of this
I want to show the labels like this
What i already tried is make another series that shows the percentage.but it's not showing like the expected results.
Is it possible to do in Highcharts ?
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script>
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Show Percentage'
},
subtitle: {
text: ''
},
xAxis: {
categories: [
'Jan',
'Feb'
],
crosshair: true
},
yAxis: {
min: 0,
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 0
},
series : {
dataLabels : {
enabled :true
}
}
},
series: [
{
name: 'A',
data: [100, 80]
}, {
name: 'B',
data: [50, 40]
}]
});
</script>

You can enable the dataLabels only for the first series and use the render event to position the labels correctly:
chart: {
type: 'column',
events: {
render: function() {
var points = this.series[0].points,
shapeArgs,
y;
points.forEach(function(point, i) {
shapeArgs = point.shapeArgs;
if (point.y > this.series[1].points[i].y) {
y = shapeArgs.y;
} else {
y = this.series[1].points[i].shapeArgs.y;
}
y -= point.dataLabel.height;
point.dataLabel.attr({
x: shapeArgs.x + shapeArgs.width - point.dataLabel.width / 2,
y: y
});
}, this);
}
}
},
Live demo: http://jsfiddle.net/BlackLabel/rqp5nb0a/
API Reference:
https://api.highcharts.com/class-reference/Highcharts.SVGElement#attr
https://api.highcharts.com/highcharts/chart.events.render

Related

Why do I get 'Cannot read property 'isValid' of undefined' when using 400 data points?

I am trying to visualise more than 400 data points using highcharts timeline, but I'm getting Cannot read property 'isValid' of undefined
Fiddle: https://jsfiddle.net/0fv2kxqb/
const test = [];
for (let i = 0; i < 400; i++) {
test.push({
x: Date.UTC(1900 + i, 9, 4),
name: 'test'
});
}
Highcharts.chart('container', {
chart: {
zoomType: 'x',
type: 'timeline'
},
xAxis: {
type: 'datetime',
visible: false
},
yAxis: {
gridLineWidth: 1,
title: null,
labels: {
enabled: false
}
},
legend: {
enabled: false
},
title: {
text: 'Timeline of Space Exploration'
},
subtitle: {
text: 'Info source: www.wikipedia.org'
},
tooltip: {
style: {
width: 300
}
},
series: [{
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'{point.x:%d %b %Y}</span><br/>{point.label}'
},
marker: {
symbol: 'circle'
},
data: test
}]
});
#container {
min-width: 400px;
max-width: 600px;
margin: 0 auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/timeline.js"></script>
<div id="container"></div>
It works fine with ~300 data points
Does it not work with large amounts of data points?
That problem is a bug and it is reported here: https://github.com/highcharts/highcharts/issues/11116
Possible workarounds:
set cropThreshold bigger than data length: https://jsfiddle.net/BlackLabel/rtkj5xd7/
enable getExtremesFromAll: https://jsfiddle.net/BlackLabel/rtkj5xd7/2/

HighCharts: Compare the series data with each other and show percentage

I am working with highcharts and I have to display the series data in form of the percentage. considering first series data as total value and for another calculate its percentage with respect to total
e.g
series: [{
name: 'Total value',
data: [50,100,50,100,50,]
}, {
name: 'Value 1',
data: [5,20,10,62,31,]
}]
I have these values for series data and need to calculate the percentage for value1 field which will be (5*100)/50=20% (50 is consider as total value)
same for all values. I have tried many ways but it considers total value as 50+5, i.e the total of both data fields respectively.
Can anyone help me to solve this problem? I have uploaded the highchart at link
Highcharts.chart('container', {
chart: {type: 'area' },
xAxis: { categories: ['A','B','C','D','E',] },
yAxis: { min: 0,
allowDecimals: false,
title: { text: ' Count'}, },
tooltip: {
headerFormat: '<b>{point.x}</b> ',
pointFormat: '{series.name}: {point.y}<br> ({point.percentage:.1f}%)'
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0,stacking: 'normal',
dataLabels: {
enabled: true,},},
series: {
cursor: 'pointer',}},
series: [{
name: 'Total value',
data: [50,100,50,100,50,]
}, {
name: 'Value 1',
data: [5,20,10,62,31,]}]});
You can achieve what you are after by using a pointFormatter in the tooltip, like this:
pointFormatter: function() {
return this.series.name + ": " + this.y + "<br/>(" + this.y * 100 / this.series.chart.series[0].data[this.x].y + "%)"
}
Additionaly, if you move the tooltip formatting inside the series, you will only have the tooltip display for the series you want in that way, and not for the total series. Like this:
series: [{
name: 'Total value',
data: [50, 100, 50, 100, 50, ]
}, {
name: 'Value 1',
data: [5, 20, 10, 62, 31, ],
tooltip: {
headerFormat: '<b>{point.x}</b> ',
pointFormatter: function() {
return this.series.name + ": " + this.y + "<br/>(" + this.y * 100 / this.series.chart.series[0].data[this.x].y + "%)"
}
}
}]
Highcharts.chart('container', {
chart: {
type: 'area'
},
xAxis: {
categories: ['A', 'B', 'C', 'D', 'E', ]
},
yAxis: {
min: 0,
allowDecimals: false,
title: {
text: ' Count'
},
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0,
stacking: 'normal',
dataLabels: {
enabled: true,
},
},
series: {
cursor: 'pointer',
}
},
series: [{
name: 'Total value',
data: [50, 100, 50, 100, 50, ]
}, {
name: 'Value 1',
data: [5, 20, 10, 62, 31, ],
tooltip: {
headerFormat: '<b>{point.x}</b> ',
pointFormat: '{series.name}: {point.y}<br> ({point.percentage:.1f}%)',
pointFormatter: function() {
return this.series.name + ": " + this.y + "<br/>(" + this.y * 100 / this.series.chart.series[0].data[this.x].y + "%)"
}
}
}]
});
#container {
min-width: 310px;
max-width: 800px;
margin: 0 auto
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sunburst.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
Working example: https://jsfiddle.net/ewolden/eudhkgp3/

How get data name in Highcharts pie chart legend instead of "Slice" using array of values?

I am creating a pie chart in Highcharts with a legend. When I specify the series as an array of values (my preferred method), the legend displays the string "Slice" for each value. If I instead specify the series as a list of objects with named values, I correctly see the data names. Can I see the data names if I specify the series as an array of values?
Here is the code that doesn't work but that I would like to use:
<html>
<head>
<title>HighCharts pie</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script>
Highcharts.setOptions({
lang: {
decimalPoint: '.',
thousandsSep: ','
}
});
$(function () {
$('#container').highcharts({
chart: {
type: 'pie',
height: '500',
width: '750',
borderColor: '#EBBA95',
borderWidth: 2
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
showInLegend: true,
dataLabels: {
enabled: true,
format: '{point.y:,.0f}'
}
}
},
title: {
text: 'Rental Amounts for Paramount Directors'
},
legend: {
enabled: true
},
xAxis: {
categories: ["BADHAM, J", "COPPOLA, F", "GUILERMAN, J", "HILLER, A",
"SPIELBERG, S"],
},
series: [{
name: 'Directors',
data: [74100000, 30673000, 36915000, 50000000, 90434000],
}]
});
});
</script>
</head>
<body>
<div id="container" style="width:600px; height:400px;"></div>
</body>
</html>
Here is the code that does work when I specify the series as a list of objects with named values:
<html>
<head>
<title>HighCharts pie</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script>
Highcharts.setOptions({
lang: {
decimalPoint: '.',
thousandsSep: ','
}
});
$(function () {
$('#container').highcharts({
chart: {
type: 'pie',
height: '500',
width: '750',
borderColor: '#EBBA95',
borderWidth: 2
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
showInLegend: true,
dataLabels: {
enabled: true,
format: '{point.y:,.0f}'
}
}
},
title: {
text: 'Rental Amounts for Paramount Directors'
},
legend: {
enabled: true
},
series: [{
name: "Directors",
slicedOffset: 20,
data: [{
name: "BADHAM, J",
y: 74100000
}, {
name: "COPPOLA, F",
y: 30673000
}, {
name: "GUILERMAN, J",
y: 36915000
}, {
name: "HILLER, A",
y: 50000000
}, {
name: "SPIELBERG, S",
y: 90434000
}]
}]
});
});
</script>
</head>
<body>
<div id="container" style="width:600px; height:400px;"></div>
</body>
</html>
You can do this with tooltip.formatter and legend.labelFormatter and accessing the highchart options.
For the tooltip text:
tooltip: {
formatter: function() {
var sliceIndex = this.point.index;
var sliceName = this.series.chart.axes[0].categories[sliceIndex];
return 'The value for <b>' + sliceName +
'</b> is <b>' + this.y + '</b>';
}
},
And for the legend:
legend: {
enabled: true,
labelFormatter: function() {
var legendIndex = this.index;
var legendName = this.series.chart.axes[0].categories[legendIndex];
return legendName;
}
},
Live demo.
as of 5 years later, do this:
legend: {
enabled: true,
labelFormatter: function() {
let legendName = this.series.chart.axes[0].categories[this.x];
return legendName;}}

highcharts tooltip wrong date

I had made one highchart in that tooltip is shows date and time in format but it is showing wrong date and time.
Please go through the code below.
HTML Code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Javascript Code
var maxval="94";
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
credits: {
enabled: false
},
chart: {
type: 'column',
renderTo: 'container',
},
title: {
text: 'Weekly Traffic'
},
xAxis: {
type: 'datetime',
labels: {
format: '{value:%d-%b-%Y}',
rotation:-45,
},
},
yAxis: {
labels:{enabled: false},
title: {
text: ''
},
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+': '+Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x) +'<br>'+ Highcharts.numberFormat((this.y /maxval ) * 100) + '%';
}
},
plotOptions: {
line: {
enableMouseTracking: false
},
series:{
pointStart: 1444242600000,
pointInterval: 86400000,
shadow:false,
dataLabels:{
enabled:true,
formatter:function()
{
var pcnt = (this.y /maxval ) * 100;
return Highcharts.numberFormat(pcnt) + '%';
}
}
}
},
series: [{
name: 'Firefox',
data: [10,56,32,12,64,13,38],
},{
name: 'Chrome',
data: [52,59,10,60,94,3,8],
},{
name: 'Edge',
data: [22,56,20,35,14,73,38],
},{
name: 'Opera',
data: [30,36,80,65,44,53,81],
},{
name: 'Safari',
data: [40,16,50,77,34,33,36],
}],
});
});
});
The working fiddle is given
here.
you need to set utc false in global option of highcharts.
Highcharts.setOptions({
global: {
useUTC: false
}
});
see Updated fiddle here

onhover add effects to all unselected bar in highcharts

My requirement is to turn out other column bar graph in highcharts to dim color when we hover over one. I looked for this on community but found no specific answer. my code is as below:
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<apex:includeScript value="{!URLFOR($Resource.ios, 'jquery-1.9.1.js')}"/>
<script>
var lowerIndex;
var upperIndex;
if({!maxBarVisible}==0)
{
lowerIndex = 0;
upperIndex = 0;
}
else
{
lowerIndex = {!maxBarVisible} - ({!maxBarVisible}-1);
upperIndex = {!maxBarVisible};
}
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column',
marginRight: 50,
marginBottom: 30
},
title: {
text:''
},
credits: {
enabled: false
},
legend: {
enabled: false
},
scrollbar:{
enabled:{!showSlider}
},
xAxis: {
min:lowerIndex,
max:upperIndex,
categories: [{!x_axisMonths}]
},
yAxis: {
min: 0,
title: {
text: 'MRR($)'
}
},
colors: [
'rgb(24, 150, 212)',
'rgb(116, 187, 59)',
],
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true,
tip: 'bottomLeft'
},
series: [{!y_axisData}]
});
});
});
</script>
Any suggestions?

Resources