Multiple Series underneath and get clicked y- value and x-value - highcharts

Someone had a problem here: Highcharts.js - Event when any point is clicked_
They found a good solution. i have the same problem, but i have multiple y-series underneath. how can i detect which series was the correct one to be clicked?
example here:
// create the chart
Highcharts.chart('container', {
chart: {
type: 'line',
events: {
click: function (event) {
var label = this.renderer.label(
'x: ' + Highcharts.numberFormat(event.xAxis[0].value, 2) + ', y: ' + Highcharts.numberFormat(event.yAxis[0].value, 2),
event.xAxis[0].axis.toPixels(event.xAxis[0].value),
event.yAxis[0].axis.toPixels(event.yAxis[0].value)
)
.attr({
fill: Highcharts.getOptions().colors[0],
padding: 10,
r: 5,
zIndex: 8
})
.css({
color: '#FFFFFF'
})
.add();
setTimeout(function () {
label.fadeOut();
}, 1000);
}
}
},
yAxis: [{ min: 1, max: 3,
height: '40%'},{
height: '60%', top: '40%'}],
series: [{
yAxis: 0,
data: [2,2,2,2,2,2,2,2]
},
{
yAxis: 1,
data: [2.9, 3.5, 106.4, 129.2, 1.0, 4.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});

You can find the proper yAxis by comparing min and max with event values. For example:
chart: {
events: {
click: function(event) {
const yAxis = event.yAxis.find(axis => (
axis.axis.max >= axis.value && axis.axis.min <= axis.value
));
if (!yAxis) {
return;
}
const label = this.renderer.label(
'x: ' + Highcharts.numberFormat(event.xAxis[0].value, 2) + ', y: ' + Highcharts.numberFormat(yAxis.value, 2),
event.xAxis[0].axis.toPixels(event.xAxis[0].value),
yAxis.axis.toPixels(yAxis.value)
)
.attr({
fill: Highcharts.getOptions().colors[0],
padding: 10,
r: 5,
zIndex: 8
})
.css({
color: '#FFFFFF'
})
.add();
setTimeout(function() {
label.fadeOut();
}, 1000);
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/yfq9t2jd/
API Reference: https://api.highcharts.com/highcharts/yAxis

Related

Highcharts.js - Event when any point is clicked

There is an example here https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/series-point-events-click/ of alerting when a marker on the series is clicked.
Is there any way to alert when any x,y point on the chart is clicked?
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click: function () {
alert('Category: ' + this.category + ', value: ' + this.y);
}
}
}
}
},
You need the click event from the chart object. https://api.highcharts.com/highcharts/chart.events.click
Highcharts.chart('container', {
chart: {
events: {
click: function (event) {
var label = this.renderer.label(
'x: ' + Highcharts.numberFormat(event.xAxis[0].value, 2) + ', y: ' + Highcharts.numberFormat(event.yAxis[0].value, 2),
event.xAxis[0].axis.toPixels(event.xAxis[0].value),
event.yAxis[0].axis.toPixels(event.yAxis[0].value)
)
.attr({
fill: Highcharts.getOptions().colors[0],
padding: 10,
r: 5,
zIndex: 8
})
.css({
color: '#FFFFFF'
})
.add();
setTimeout(function () {
label.fadeOut();
}, 1000);
}
}
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/chart/events-click/

HighCharts: Annotation on X axis?

A picture is better than words:
Annotation just above or below the X Axis
How can I obtain the y coordinate of these annotations?
Here is the code so far, with a dummy y:
var chart = Highcharts.chart('container', {
series: [{
data: [29.9, 71.5, 106.4, -129.2, -144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
var LineVal = 5.5;
// the button action
$button = $('#button');
$button.click(function() {
chart.xAxis[0].addPlotLine({
value: LineVal,
color: 'red',
width: 2,
id: 'plot-line-1'
});
chart.addAnnotation({
labels: [{
point: {
x: LineVal,
xAxis: 0,
y: 0,
yAxis:0
},
text: LineVal.toFixed(2),
backgroundColor: 'black'
}],
});
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/annotations.js"></script>
<div id="container" style="height: 400px"></div>
<button id="button" class="autocompare">Add plot line</button>
jsfiddle
I think that a better solution will be using the chart.renderer tool to render a label which could be dynamically placed.
Demo: http://jsfiddle.net/BlackLabel/0e4vrytc/
let label = chart.renderer.label(LineVal, 0, 0, 'callout', 17.5, -50)
.css({
color: '#FFFFFF'
})
.attr({
fill: 'rgba(0, 0, 0, 0.75)',
padding: 8,
r: 5,
zIndex: 6
})
.add();
label.translate(chart.xAxis[0].toPixels(LineVal) - label.getBBox().width / 2, chart.plotHeight + chart.plotTop)
API: https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#label

Highchart: Background color of Axis

I saw a similar question here.
In reply its written that we can have background color by making a rectangle.
My question is how I can get x and Y position of all the ticks on axis. I will need it because I want background color only on alternate dates.
Thanks
Here's a quick example that shades between the 2nd and 4th ticks:
var tickStart = 1;
var tickEnd = 3;
$(function () {
var rect = null;
function drawRect(chart){
if (rect){
rect.element.remove();
}
var xAxis = chart.xAxis[0];
var pixStart = xAxis.toPixels (xAxis.tickPositions[tickStart], false);
var pixEnd = xAxis.toPixels (xAxis.tickPositions[tickEnd], false);
rect = chart.renderer.rect(pixStart, chart.chartHeight - xAxis.bottom, pixEnd - pixStart , 25, 00)
.attr({
'stroke-width': 0,
stroke: '#888888',
fill: '#888888',
zIndex: 3
})
.add();
}
$('#container').highcharts({
chart: {
events: {
load: function() {
drawRect(this);
},
redraw: function(){
drawRect(this);
}
}
},
xAxis: {
},
series: [{
animation: false,
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
Fiddle here.
Did you try to use labels.formatter with background and useHTML flag? Something like this: http://jsfiddle.net/AeV7h/
xAxis: {
categories: ['Foo', 'Bar', 'Foobar'],
labels: {
useHTML: true,
formatter: function() {
return '<span class="hc-label">' + this.value + '</span>';
}
}
},
And CSS:
.hc-label {
background-color: red;
padding: 0px 5px;
color: yellow;
}

datatable inside Highcharts-Pie chart

I want to display the datatable inside Pie chart using Highcharts.I can display the table outside but on exporting the image, datatable is not exported.
So my aim is to display datatable inside the Pie chart.
Refering to this.I was able to partly acheive it.demo
But how can i give the column headers and borders to it?
$(function () {
Highcharts.drawTable = function() {
// user options
var tableTop = 200,
colWidth = 60,
tableLeft = 50,
rowHeight = 20,
cellPadding = 2.5,
valueDecimals = 1,
valueSuffix = '';
// internal variables
var chart = this,
series = chart.series,
renderer = chart.renderer,
cellLeft = tableLeft;
// draw category labels
$.each(series, function(serie_index, serie) {
renderer.text(
serie.name,
cellLeft + cellPadding,
tableTop + (serie_index + 1) * rowHeight - cellPadding
)
.css({
fontWeight: 'bold'
})
.add();
});
$.each(series[0].data, function(i) {
renderer.text(
series[0].data[i].name,
cellLeft + colWidth - cellPadding,
tableTop + (i + 2) * rowHeight - cellPadding
)
.attr({
align: 'right'
})
.add();
});
$.each(series[0].data, function(i) {
renderer.text(
Highcharts.numberFormat(series[0].data[i].y, valueDecimals) + valueSuffix,
150,
tableTop + (i + 2) * rowHeight - cellPadding
)
.attr({
align: 'left'
})
.add();
});
}
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
events: {
load: Highcharts.drawTable
},
height: 600,
width: 800,
marginBottom: 250
},
title: {
text: 'Browser market shares at a specific website, 2010'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
}]
});
});
For the column headers, I don't know. Depends on what you want to do.
As for the borders, since that's SVG, you could draw the borders using RECT and PATH. Take a look at this fork of your demo for some crude borders and maybe you can improve on it.

Dual x-axis on same line with Highcharts. Possible?

Can we create multiple x-axis chart like this using Highcharts?
If yes, can someone please provide some pointers?
There are three year data displayed. i.e. 2010,2011, 2012
https://www.adr.com/DRDetails/CorporateActions?cusip=055622104
The older answers didn't run for me in JSFiddle.
Here is a working example, if it helps anyone:
http://jsfiddle.net/kPqKW/
$(function () {
$('#container').highcharts({
chart: {
renderTo: 'container'
},
xAxis: [{
type: 'datetime'
}, {
type: 'datetime',
opposite: true
}],
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
pointStart: Date.UTC(2010, 0, 1),
pointInterval: 24 * 3600 * 1000 // one day
}, {
data: [176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0],
pointStart: Date.UTC(2010, 0, 10),
pointInterval: 24 * 3600 * 1000, // one day
xAxis: 1
}]
});
});
Using highstocks (highcharts lesser known sibling), you can do something like what you're looking for. Check out this fiddle
$(function() {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function(data) {
// split the data set into ohlc and volume
var ohlc = [],
volume = [],
dataLength = data.length;
for (i = 0; i < dataLength; i++) {
ohlc.push([
data[i][0], // the date
data[i][1], // open
data[i][2], // high
data[i][3], // low
data[i][4] // close
]);
volume.push([
data[i][0], // the date
data[i][5] // the volume
])
}
// set the allowed units for data grouping
var groupingUnits = [[
'week', // unit name
[1] // allowed multiples
], [
'month',
[1, 2, 3, 4, 6]
]];
// create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Historical'
},
yAxis: [{
title: {
text: 'OHLC'
},
height: 200,
lineWidth: 2
}, {
title: {
text: 'Volume'
},
top: 300,
height: 100,
offset: 0,
lineWidth: 2
}],
series: [{
type: 'area',
name: 'AAPL',
data: ohlc,
dataGrouping: {
units: groupingUnits
}
}, {
type: 'area',
name: 'Volume',
data: volume,
yAxis: 1,
dataGrouping: {
units: groupingUnits
}
}]
});
});
});
You can simply use Highcharts grouped categories plugin, take a look: https://github.com/blacklabel/grouped_categories
It seems possible, but in different view. one axis at the top and other one is at the bottom. You've set opposite to true as we do for y - axis. Have a look at this question in
Highcharts forum
<div id="container" style="height: 400px; width: 500px"></div>
<script type="text/javascript">
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
xAxis: [{
type: 'datetime',
},{
type: 'datetime',
opposite: true
}],
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
pointStart: Date.UTC(2010, 0, 1),
pointInterval: 24 * 3600 * 1000 // one day
},{
data: [176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0],
pointStart: Date.UTC(2010, 0, 10),
pointInterval: 24 * 3600 * 1000, // one day
xAxis: 1
}]
});
</script>
for working example, look at this jsfilddle
you may go through this fiddle https://jsfiddle.net/ajaytripathi10/z8mrwhxz/. Hope this will help.
$(function () {
$('#container').highcharts({
title: {
text: 'Shared tooltip chart'
},
xAxis: [{
type: 'datetime',
id: 'x1'
}, {
type: 'datetime',
id: 'x2',
opposite: true
}],
yAxis: {
min: 0
},
tooltip: {
shared: true,
useHTML: true,
formatter: function () {
var tooltip = '';
var i, len;
tooltip += '<small>Apple</small>';
tooltip += '<table>';
for (i = 0, len = 4; i < len; i++) {
if(this.points[i] != undefined){
if(this.points[i].series.name.indexOf('Apple') >= 0){
tooltip += '<tr>';
tooltip += '<td style="color: ' + this.points[i].series.color + '">\u25CF</td>';
tooltip += '<td>' + Highcharts.dateFormat('%Y-%m-%d', this.points[i].x) + ':</td>';
tooltip += '<td style="text-align: right"><b>' + this.points[i].y + '</b></td>';
tooltip += '</tr>';
}
}
}
tooltip += '</table>';
tooltip += '<small>Mango</small>';
tooltip += '<table>';
for (i = 0, len = 4; i < len; i++) {
if(this.points[i] != undefined){
if(this.points[i].series.name.indexOf('Mango') >= 0){
tooltip += '<tr>';
tooltip += '<td style="color: ' + this.points[i].series.color + '">\u25CF</td>';
tooltip += '<td>' + Highcharts.dateFormat('%Y-%m-%d', this.points[i].x) + ':</td>';
tooltip += '<td style="text-align: right"><b>' + this.points[i].y + '</b></td>';
tooltip += '</tr>';
}
}
}
tooltip += '</table>';
return tooltip;
}
},
series: [{
name: 'Apple',
id: 'series1',
xAxis: 'x1',
color: 'rgba(255, 0, 0, 1.0)',
data: [5, 3, 4, 7, 6, 1, 0],
pointInterval: 1000 * 60 * 60 * 24,
pointStart: Date.UTC(2015, 2, 10)
}, {
name: 'Apple New',
id: 'series2',
//linkedTo: 'series1',
xAxis: 'x2',
color: 'rgba(255, 180, 180, 1.0)',
data: [4, 5, 5, 6, 1, 3, 4],
pointInterval: 1000 * 60 * 60 * 24,
pointStart: Date.UTC(2015, 2, 17)
},{
name: 'Mango',
id: 'series3',
xAxis: 'x1',
color: 'rgba(255, 0, 0, 1.0)',
data: [15, 13, 14, 17, 16, 11, 10],
pointInterval: 1000 * 60 * 60 * 24,
pointStart: Date.UTC(2015, 2, 10)
}, {
name: 'Mango New',
id: 'series4',
//linkedTo: 'series1',
xAxis: 'x2',
color: 'rgba(255, 180, 180, 1.0)',
data: [14, 15, 15, 16, 11, 13, 14],
pointInterval: 1000 * 60 * 60 * 24,
pointStart: Date.UTC(2015, 2, 17)
}]
});
});
Complete code for making horizontal and vertical line in column,piechart highcharts
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div id="container" class="col-md-4" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<div class="col-md-8">
<div id="container2" class="col-md-6" style="min-width: 310px; height: 400px; margin: 0 auto" class="col-md-6"></div><div id="container3" class="col-md-6" style="min-width: 310px; height: 400px; margin: 0 auto" class="col-md-6" class="col-md-6">dddd</div>
</div>
<script>
Highcharts.chart('container', {
chart: {
type: 'column',
},
title: {
text: 'sample charts for both horizontal and vertical line'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
gridLineWidth: 1,
minPadding: 2,
maxPadding: 2,
maxZoom: 6 ,
categories: ['Jan', 'Feb', 'Apr', 'May', 'Jun',
'Dec']
},
yAxis: {
title: {
text: 'Temperature'
},
labels: {
formatter: function () {
return '$'+this.value;
}
},
},
tooltip: {
crosshairs: true,
shared: true
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1
}
}
},
series: [{
// name: '',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5]
}]
});
Highcharts.chart('container2', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares in January, 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Chrome',
y: 61.41,
sliced: true,
selected: true
}, {
name: 'Internet Explorer',
y: 11.84
}, {
name: 'Firefox',
y: 10.85
}, {
name: 'Edge',
y: 4.67
}, {
name: 'Safari',
y: 4.18
}, {
name: 'Sogou Explorer',
y: 1.64
}, {
name: 'Opera',
y: 1.6
}, {
name: 'QQ',
y: 1.2
}, {
name: 'Other',
y: 2.61
}]
}]
});
Highcharts.chart('container3', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares in January, 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Chrome',
y: 61.41,
sliced: true,
selected: true
}, {
name: 'Internet Explorer',
y: 11.84
}, {
name: 'Firefox',
y: 10.85
}, {
name: 'Edge',
y: 4.67
}, {
name: 'Safari',
y: 4.18
}, {
name: 'Sogou Explorer',
y: 1.64
}, {
name: 'Opera',
y: 1.6
}, {
name: 'QQ',
y: 1.2
}, {
name: 'Other',
y: 2.61
}]
}]
});
</script>
</body>
</html>

Resources