Highcharts Pie charts get the selected pie id - highcharts

How do I get Highcharts Pie charts get the selected pie id ?
I have a data array
var data= { name: series[i], y: Data[i],id:categorydata[i] };
Render the chart
new Highcharts.Chart({ ....
....
series: [{
type: 'pie',
name: 'Category',
data: data
}]
});
How do I get the id of the selected pie.
I am doing this in
plotOptions: {
series: {
animation: false,
events:{
click: function (event) {
var point = this;
//How do I Access the id???????
alert('value: ' + this.series);
}
}
},

You want the event handling on the point config, not the series. Each wedge is a point in a single series:
var data = [{ name: 'One', y: 10, id: 0 },{ name: 'Two', y: 10, id: 1 }];
// some other code here...
series:[
{
"data": data,
type: 'pie',
animation: false,
point:{
events:{
click: function (event) {
alert(this.x + " " + this.y);
}
}
}
}
],
Fiddle here.
Full running code:
var chart;
point = null;
$(document).ready(function () {
var data = [{ name: 'One', y: 10, id: 0 },{ name: 'Two', y: 10, id: 1 }];
chart = new Highcharts.Chart(
{
series:[
{
"data": data,
type: 'pie',
animation: false,
point:{
events:{
click: function (event) {
alert(this.id);
}
}
}
}
],
"chart":{
"renderTo":"container"
},
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="width: 320px; height: 200px"></div>

Related

How to sort order highchart pie jasperstudio by slice value min to max

I am working on jasper but i don't want to sort the data in the query as many other element of the report using the same query.
So i would like to sort it on the pie chart itself as example on this fiddle http://jsfiddle.net/highcharts/3bDMe/1/. How can it be done without button click? I meant as the chart load it automatically sort by slice value ascending.
$(function () {
$(document).ready(function () {
// Build the chart
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Browser market shares at a specific website, 2010'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage}%</b>',
percentageDecimals: 1
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Firefox', 45.0],
['IE', 6.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 88.5],
['Opera', 26.2],
['Others', 30.7]
]
}]
});
$('#sort').click(function() {
chart.series[0].data.sort(function(a, b) {
return b.y - a.y;
});
var newData = {};
for (var i = 0; i < chart.series[0].data.length; i++) {
newData.x = i;
newData.y = chart.series[0].data[i].y;
newData.color = Highcharts.getOptions().colors[i];
chart.series[0].data[i].update(newData, false);
// Workaround:
chart.legend.colorizeItem(chart.series[0].data[i], chart.series[0].data[i].visible);
}
chart.redraw({ duration: 2000 });
});
});
});
In the load event you can create a new data array with sorted values and use setData method to apply changes:
chart: {
...,
events: {
load: function() {
var data = this.series[0].data,
newData = [];
data.forEach(function(point) {
newData.push({
y: point.y,
name: point.name
})
});
newData.sort(function(a, b) {
return a.y - b.y;
});
this.series[0].setData(newData);
}
}
}
Live demo: http://jsfiddle.net/BlackLabel/6vzd8ak7/
API Reference: https://api.highcharts.com/class-reference/Highcharts.Series#setData

How can I set navigator's start&end in Highstock candlestick chart?

I want to set navigator's position and range programmably in Highstock candlestick chart. By default, navigator's end(left) side indicate end of data series. And also width of navigator is seems like fixed some number of data.
In this picture, I want to set navigator to yellow position. How can I do this?
You need to set the xAxis min and max, to show a specific area on chart load. Can be done like this:
xAxis: {
min: 1330764400000,
max: 1330774400000
},
$.getJSON('https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/large-dataset.json', function (data) {
// Create a timer
var start = +new Date();
// Create the chart
Highcharts.stockChart('container', {
chart: {
events: {
load: function () {
if (!window.TestController) {
this.setTitle(null, {
text: 'Built chart in ' + (new Date() - start) + 'ms'
});
}
}
},
zoomType: 'x'
},
rangeSelector: {
buttons: [{
type: 'day',
count: 3,
text: '3d'
}, {
type: 'week',
count: 1,
text: '1w'
}, {
type: 'month',
count: 1,
text: '1m'
}, {
type: 'month',
count: 6,
text: '6m'
}, {
type: 'year',
count: 1,
text: '1y'
}, {
type: 'all',
text: 'All'
}],
selected: 3
},
yAxis: {
title: {
text: 'Temperature (°C)'
}
},
xAxis: {
min: 1330764400000,
max: 1330774400000
},
title: {
text: 'Hourly temperatures in Vik i Sogn, Norway, 2009-2017'
},
subtitle: {
text: 'Built chart in ...' // dummy text to reserve space for dynamic subtitle
},
series: [{
name: 'Temperature',
data: data.data,
pointStart: data.pointStart,
pointInterval: data.pointInterval,
tooltip: {
valueDecimals: 1,
valueSuffix: '°C'
}
}]
});
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
Working JSFiddle example: https://jsfiddle.net/ewolden/axrce6j3/2/
If you want to show a specific area after the chart has been drawn, you can use the setExtremes function like this:
chart.xAxis[0].setExtremes(1330764400000, 1330774400000, true);
API Reference: Axis.setExtremes()
$.getJSON('https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/large-dataset.json', function (data) {
// Create a timer
var start = +new Date();
// Create the chart
Highcharts.stockChart('container', {
chart: {
events: {
load: function () {
if (!window.TestController) {
this.setTitle(null, {
text: 'Built chart in ' + (new Date() - start) + 'ms'
});
}
}
},
zoomType: 'x'
},
rangeSelector: {
buttons: [{
type: 'day',
count: 3,
text: '3d'
}, {
type: 'week',
count: 1,
text: '1w'
}, {
type: 'month',
count: 1,
text: '1m'
}, {
type: 'month',
count: 6,
text: '6m'
}, {
type: 'year',
count: 1,
text: '1y'
}, {
type: 'all',
text: 'All'
}],
selected: 3
},
yAxis: {
title: {
text: 'Temperature (°C)'
}
},
xAxis: {
min: 1330764400000,
max: 1330774400000
},
title: {
text: 'Hourly temperatures in Vik i Sogn, Norway, 2009-2017'
},
subtitle: {
text: 'Built chart in ...' // dummy text to reserve space for dynamic subtitle
},
series: [{
name: 'Temperature',
data: data.data,
pointStart: data.pointStart,
pointInterval: data.pointInterval,
tooltip: {
valueDecimals: 1,
valueSuffix: '°C'
}
}]
});
});
$('#setExtremeButton').click(function() {
$('#container').highcharts().xAxis[0].setExtremes(1330774400000,1350974400000, true)
})
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
<button id="setExtremeButton">
Set new min/max
</button>
Working example: https://jsfiddle.net/ewolden/axrce6j3/12/

Highcharts - synchronized-charts crosshair line and circle point display

I changed the official chart, but the crosshair not my expected.
DEMO : Official synchronized-charts
What I changed :
Add xAxis.categories as my custom xAxis labels
Change series[0].fillOpacity 0.3 to 1
Use my custom json data
CODE :
```javascript
//$.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=activity.json&callback=?', function (activity) {
var json = {
xData: ["1/1", "1/2", "1/3", "1/4", "1/5", "1/6", "1/7", "1/8", "1/9", "1/10"],
datasets: [{
name: "Num of dog",
data: [1,2,3,4,5,1,2,3,4,5],
unit: "dogs",
type: "area",
valueDecimals: 0
},{
name: "Num of cat",
data: [1,2,3,4,5,1,2,3,4,5],
unit: "cats",
type: "area",
valueDecimals: 0
}]
};
//$.each(activity.datasets, function (i, dataset) {
$.each( json.datasets, function (i, dataset) {
// Add X values
dataset.data = Highcharts.map(dataset.data, function (val, j) {
//return [activity.xData[j], val];
return [json.xData[j], val];
});
$('<div class="chart">')
.appendTo('#container')
.highcharts({
...,
xAxis: {
crosshair: true,
events: {
setExtremes: syncExtremes
},
categories: ["1/1", "1/2", "1/3", "1/4", "1/5", "1/6", "1/7", "1/8", "1/9", "1/10"],
//labels: {
//format: '{value} km'
//}
},
...,
series: [{
...,
fillOpacity: 1,
//fillOpacity: 0.3,
...
});
```
DEMO : My synchronized-charts
What I need:
Display crosshair line, like Official synchronized-charts
Don't show circle point, like Official synchronized-charts
Show circle point When mouse hover, like Official synchronized-charts
Crosshair line put to front
Does anyone know to accomplish this?
Thank you!
For Don't show circle point, like Official synchronized-charts. Added
plotOptions: {
series: {
marker: {
enabled: false
}
pointPlacement: 'on'
}
},
For Crosshair line put to front. Updated xAxis
xAxis: {
categories: json.xData,
tickmarkPlacement: 'on',
crosshair: {
width: 2,
zIndex: 3
},
events: {
setExtremes: syncExtremes
},
},
/*
The purpose of this demo is to demonstrate how multiple charts on the same page can be linked
through DOM and Highcharts events and API methods. It takes a standard Highcharts config with a
small variation for each data set, and a mouse/touch event handler to bind the charts together.
*/
/**
* In order to synchronize tooltips and crosshairs, override the
* built-in events with handlers defined on the parent element.
*/
$('#container').bind('mousemove touchmove touchstart', function(e) {
var chart,
point,
i,
event;
for (i = 0; i < Highcharts.charts.length; i = i + 1) {
chart = Highcharts.charts[i];
event = chart.pointer.normalize(e.originalEvent); // Find coordinates within the chart
point = chart.series[0].searchPoint(event, true); // Get the hovered point
if (point) {
point.highlight(e);
}
}
});
/**
* Override the reset function, we don't need to hide the tooltips and crosshairs.
*/
Highcharts.Pointer.prototype.reset = function() {
return undefined;
};
/**
* Highlight a point by showing tooltip, setting hover state and draw crosshair
*/
Highcharts.Point.prototype.highlight = function(event) {
this.onMouseOver(); // Show the hover marker
this.series.chart.tooltip.refresh(this); // Show the tooltip
this.series.chart.xAxis[0].drawCrosshair(event, this); // Show the crosshair
};
/**
* Synchronize zooming through the setExtremes event handler.
*/
function syncExtremes(e) {
var thisChart = this.chart;
if (e.trigger !== 'syncExtremes') { // Prevent feedback loop
Highcharts.each(Highcharts.charts, function(chart) {
if (chart !== thisChart) {
if (chart.xAxis[0].setExtremes) { // It is null while updating
chart.xAxis[0].setExtremes(e.min, e.max, undefined, false, {
trigger: 'syncExtremes'
});
}
}
});
}
}
// Get the data. The contents of the data file can be viewed at
// https://github.com/highcharts/highcharts/blob/master/samples/data/activity.json
var json = {
xData: ["1/1", "1/2", "1/3", "1/4", "1/5", "1/6", "1/7", "1/8", "1/9", "1/10"],
datasets: [{
name: "Num of dog",
data: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
unit: "dogs",
type: "area",
valueDecimals: 0
}, {
name: "Num of cat",
data: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
unit: "cats",
type: "area",
valueDecimals: 0
}]
}
//$.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=activity.json&callback=?', function(activity) {
$.each(json.datasets, function(i, dataset) {
// Add X values
dataset.data = Highcharts.map(dataset.data, function(val, j) {
return [json.xData[j], val];
});
$('<div class="chart">')
.appendTo('#container')
.highcharts({
chart: {
marginLeft: 40, // Keep all charts left aligned
spacingTop: 20,
spacingBottom: 20
},
title: {
text: dataset.name,
align: 'left',
margin: 0,
x: 30
},
credits: {
enabled: false
},
legend: {
enabled: false
},
xAxis: {
categories: json.xData,
tickmarkPlacement: 'on',
crosshair: {
width: 2,
zIndex: 3
},
events: {
setExtremes: syncExtremes
},
},
yAxis: {
title: {
text: null
},
zIndex: 1000
},
plotOptions: {
series: {
marker: {
enabled: false
},
pointPlacement: 'on'
}
},
tooltip: {
positioner: function() {
return {
x: this.chart.chartWidth - this.label.width, // right aligned
y: 10 // align to title
};
},
borderWidth: 0,
backgroundColor: 'none',
pointFormat: '{point.y}',
headerFormat: '',
shadow: false,
style: {
fontSize: '18px'
},
valueDecimals: dataset.valueDecimals
},
series: [{
data: dataset.data,
name: dataset.name,
type: dataset.type,
color: Highcharts.getOptions().colors[i],
fillOpacity: 1,
tooltip: {
valueSuffix: ' ' + dataset.unit
}
}]
});
});
//});
.chart {
min-width: 320px;
max-width: 800px;
height: 220px;
margin: 0 auto;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
Fiddle demo

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

Highcharts Combo graphs with common legend click

I have a combo graphs, with Pie and Bar Graph, now my problem is that I want pie chart and bar graph both controlled from the same legend, as status are the same... source example created a JS fiddle any help would be much appreciated.
http://jsfiddle.net/TV8f4/
$(document).ready(function () {
var Loveralldata = [0,1,1,0,0,5];
var LDNOKdata = [['1032',11],['1040',0]];
var LDOKONOKdata = [['1032',1],['1040',0]];
var LDOKOOKdata = [['1032',1],['1040',0]];
var LTBDdata = [['1032',1],['1040',0]];
var LNAdata = [['1032',1],['1040',0]];
var LNUAdata = [['1032',4],['1040',8]];
var LCatData = ['Delhi HO','Regional Offices'];
$('#location').highcharts({
chart: {
//events: {
// click: function (event) {
// alert('hide');
// }
//}
},
credits: {
enabled: false
},
title: {
text: 'Location chart'
},
xAxis: {
categories: LCatData
},
yAxis: {
maxPadding: 1.5
},
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click: function() {
alert(this.options.name);
}
}
}
}
},
tooltip: {
formatter: function () {
var s;
if (this.point.name) { // the pie chart
s = '' +
this.series.name + ': ' + this.y ;
} else {
s = '' +
this.category + ' ' + this.x + ': ' + this.y;
}
return s;
}
},
colors: ['#367A01', '#00D700', '#FFD700', '#D9D9D9', '#F4FA58', '#757873'],
labels: {
items: [{
style: {
left: '40px',
top: '8px',
color: 'black'
}
}]
},
series: [{
type: 'column',
name: 'Adequate and Effective',
data: LDOKOOKdata
}
, {
type: 'column',
name: 'New Remediation Plans',
data: LDOKONOKdata
}, {
type: 'column',
name: 'New Controls',
data: LDNOKdata
},
{
type: 'column',
name: 'Not Updated/Approved ',
data: LNUAdata
},
{
type: 'column',
name: 'NA',
data: LNAdata
}
, {
type: 'column',
name: 'To Be Deleted',
data: LTBDdata
}
, {
type: 'pie',
name: 'Overall Status',
data: [['Adequate and Effective', Loveralldata[2]], ['New Remediation Plans', Loveralldata[1]], ['New Controls', Loveralldata[0]], ['Not Updated/Approved', Loveralldata[5]], ['NA', Loveralldata[3]], ['To Be Deleted', Loveralldata[4]]],
center: [100, 50],
size: 150,
showInLegend: false,
dataLabels: {
enabled: false
}
}]
});
});
You can use legendItemClick event and then hide element with series index in pie chart like here http://jsfiddle.net/TV8f4/3/
legendItemClick:function(){
var index = this.index,
chart = this.chart,
series = chart.series,
len = series.length,
pieSerie = series[len-1];
pieSerie.data[index].setVisible();
}

Resources