How do I make an API call from clicking on a bar element in Highcharts? - highcharts

I need to make an API call when clicking on a bar element. For example, look at this jsfiddle.
https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/series-point-events-click-column/
See how the x axis has the 12 months? Let's say that when clicking on the bar for "April," I want to make an API call in order to get sales data for the month of April and display that on the graph. I can't find a way to do this, because this click function...
plotOptions: {
series: {
events: {
click: function (event) {
//code goes here
}
}
}
}
... can only access items inside of the chart. I need to make an outside call to the database when clicking on a bar. Anything I can do? Thanks.
I tried this
document.querySelector('.rect.highcharts-point').addEventListener('click', e => {
//code goes here
});
It didn't work at all which is confusing, because this method clearly works when referencing the chart as whole, but doesn't work for just a bar element as you can see it working for the entire chart in this jsfiddle.
https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/chart/events-container/

Three ways how you can achieve that:
From the callback to the built-in point click() event:
plotOptions: {
series: {
point: {
events: {
click() {
let point = this;
getPoint(point)
}
}
}
}
},
.
function getPoint(p) {
console.log('From the callback: ', p)
}
From the div container:
document.querySelector('.chart-container').addEventListener('click', e => {
if (e.point != undefined) {
console.log('From the container: ', e.point)
}
});
Looping through the rects:
function getPointLoop(p) {
p.addEventListener('click', e => {
console.log('From the loop:', e.target.point)
})
}
let points = Array.from(document.querySelectorAll('rect.highcharts-point'))
for (i = 0; i < points.length; i++) {
getPointLoop(points[i])
}
Demo:
https://jsfiddle.net/BlackLabel/9bos6eu8/

Related

Redraw Highcharts Organization Chart after collapse

I'm using Highcharts to create organization charts that where each node can be collapsed when clicked as in provided example : http://jsfiddle.net/vegaelce/83uktasc/
It works well but it would be better if it is possible to "redraw" the chart once a node is fold/unfold (to optimise the space left and realign the nodes).
I tried without success :
chart.redraw();
Have you any idea how to make this ?
Thanks in advance
You need to set new data to implement the required feature. Nodes are hidden on svg level and they are not ignored in the redraw.
function getData(to) {
const data = [
...
];
if (to) {
const filters = [to];
return data.filter(el => {
const matched = filters.find(filter => filter === el[0]);
if (matched) {
filters.push(el[1]);
}
return !matched;
});
}
return data;
}
Highcharts.chart('container', {
...,
plotOptions: {
series: {
point: {
events: {
click: function() {
if (this.linksFrom.length) {
this.series.setData(getData(this.title || this.name));
} else {
this.series.setData(getData());
}
}
}
}
}
}
});
Live demo: http://jsfiddle.net/BlackLabel/5njf0cwq/
API Reference: https://api.highcharts.com/class-reference/Highcharts.Series#setData

Drilldown in Sankey Diagram

I'm looking for example code on how to implement a drilldown in a sankey diagram.
I tried using the drilldown code from the column drilldown demo but it didn't work for sankey.
The intended functionality is very similar to a dendogram, i.e- click a node to hide reveal child nodes.
Example:
https://jsfiddle.net/y_tddel/d7jby2z1/5/
.
In this example each node in the second column will be clickable and have their own child nodes.The expanded view of Indo-Iranian node is shown as as an example.
Series.point.events can be used to trigger the event when the user clicks on any node, and update the series data, and adding a back button.
Link: https://api.highcharts.com/highcharts/series.sankey.point.events.click
Look at the following code sample:
Highcharts.chart('container', {
series: [{
point: {
events: {
click: function (e) {
console.log(e, 'e')
let series = this.series,
chart = series.chart;
if (this.isNode) {
if (isDrilldown[isDrilldown.length - 1] != this.name && drilldown_data.hasOwnProperty(this.name)) {
chart.backButton = chart.renderer.button('back', chart.plotLeft, 20, function () {
isDrilldown.pop();
d = JSON.parse(JSON.stringify(drilldown_data[isDrilldown[isDrilldown.length - 1]]));
series.update({data: d});
chart.backButton.destroy();
chart.title.show();
}).add()
isDrilldown.push(this.name);
chart.title.hide()
this.series.update({
data: JSON.parse(JSON.stringify(drilldown_data[this.name]))
})
}
}
}
}
},
keys: ['from', 'to', 'weight', 'drilldown'],
data: JSON.parse(JSON.stringify(plot_data)),
type: 'sankey'
}],
});
Demo: https://jsfiddle.net/deveshgoyal/1zcnuq5b/2/
Current drilldown module is not compatible with the sankey series. You will need to create your own drilldown logic. Below is a basic demo which shows how to update series on click event - which could be treated as an drilldown to other series.
Demo: https://jsfiddle.net/BlackLabel/hn19xga5/
events: {
click() {
let series = this.series,
chart = series.chart;
if (!isDrilldown) {
chart.backButton = chart.renderer.button('back', 20, 20, function() {
series.update({
data: initialData
})
chart.backButton.destroy();
isDrilldown = false;
}).add()
isDrilldown = true;
}
this.series.update({
data: drilldown1
})
}
}
API: https://api.highcharts.com/highcharts/series.sankey.data.events.click
API: https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#button
API: https://api.highcharts.com/class-reference/Highcharts.Series#update

Highcharts --> organisation chart --> toggle visibility of node

I would like to add toggle visibility on click to a node in a Highcharts Organisation chart (Highcharts).
Is there a built in support for this?
This feature is not available from regular Highcharts API, however I created an example/guideline how to implement it by yourself.
First, you need to hide points after initial load in the events.load callback:
Code:
events: {
load() {
//hide nodes after initial load
let chart = this,
nodes = chart.series[0].nodeLookup;
for (let i in nodes) {
if (nodes[i].column > 2) {
nodes[i].graphic.hide();
nodes[i].dataLabel.hide();
nodes[i].linksTo[0].graphic.hide();
nodes[i].visible = false;
}
}
}
}
API to load event: https://api.highcharts.com/highcharts/chart.events.load
Next, you need to implement logic to show wanted nodes after click event on point. You can improve this logic and code as an independent function which will be trigger inside the point.events.click callback.
Code:
events: {
click() {
//show nodes
let series = this.series,
nodes = series.nodeLookup;
for (let i in nodes) {
if (nodes[i].linksTo.length && nodes[i].linksTo[0].from === "CEO") {
if (nodes[i].visible) {
nodes[i].graphic.hide()
nodes[i].dataLabel.hide();
nodes[i].visible = false;
} else {
nodes[i].graphic.show()
nodes[i].dataLabel.show();
nodes[i].visible = true;
}
}
}
this.linksFrom.forEach(link => {
if (link.graphic.visibility == "visible") {
link.graphic.hide()
} else {
link.graphic.show()
}
})
}
}
API: https://api.highcharts.com/highcharts/series.organization.data.events.click
Demo: https://jsfiddle.net/BlackLabel/b5nxv6k9/ - click on the CEO point to see how it work.

How can I use data.table or data.csv and extra data for click event handling

I have a pie chart built using a data.table (HTML table). http://jsfiddle.net/m78Lwkqt/2/
I want to use a 3rd column of the table to drive a click to a particular URL.
series:[
{
type: 'pie',
animation: false,
point:{
events:{
click:
function (event)
{
// I want to get the 3rd column of the table's value
// that corresponds to this point.
alert(this.y);
}
}
}
}
]
Found an answer: http://jsfiddle.net/ryanreid22/m78Lwkqt/4/
The trick is to add a seriesMapping value (in this case I've called it 'accountCode') that can be used in the point.click handler.
data: {
table: 'smry_t_pb_outlay_by_account',
seriesMapping: [{
x: 0, // X values are pulled from column 0 by default
y: 1, // Y values are pulled from column 1 by default
accountCode: 2 // used in the point.click below
}]
},
events: {
click:
function (event) {
alert(this.accountCode);
}
}
You could use a jQuery selector to get all the tds from the third column, and when needed match this.x from this click event up against the array you got from the jQuery selector.
For example, getting the third column using a jQuery selector:
var thirdColumn = $("#smry_t_pb_outlay_by_account tr td:nth-child(3)");
And then using this array inside your point.events.click to get the data:
point: {
events: {
click: function (event) {
alert(thirdColumn[this.x].innerHTML);
}
}
}
See this JSFiddle demonstration of the code.

Remove data labels when ploting and exporting graphs

So, I have stacked bar chart and I want to hide data labels when they don't fit in the area. for Example in Category 8, not to have data label "4" at all.
This is a similar question bolow:
Highcharts stacked bar chart hide data labels not to overlap
But My problem is I also need to hide lables on exporting
I have done this code after generating graph to hide labales
$.each(this.chartObject.series,function(i,data_series){
if (data_series.visible){
$.each(data_series.data,function(j,data){
if(data.yBottom != null && data.plotY != null){
if(data.yBottom - data.plotY < 15){
if (typeof data.dataLabel != 'undefined'){
data.dataLabel.hide();
}
}
}
});
}
});
You need to move loop from callback, into chart / events / load.
chart: {
events: {
load: function () {
var chart = this;
$.each(chart.series, function (i, serie) {
$.each(serie.data, function (j, data) {
if (data.yBottom - data.plotY < 15) data.dataLabel.hide();
});
});
}
},
renderTo: 'chart',
defaultSeriesType: 'bar'
},
Example: http://jsfiddle.net/HA5xE/20

Resources