Need to call an API with min and max X values on Click of rangeselector Buttons - highcharts

I want to make an API call on Click of RangeSelector Buttons in High stock. I am aware of the fact that there is a click event in RangeSelector but the problem is the event passed to that does not contain the min and max values of the graph. And I need these two values for making an API call. I am also aware that we can use afterSetExtremes here but the problem with that is it is triggered multiple times even when the button is not clicked. Below are some part of my code. Can anybody tell how can I get Xmin and Xmax in the click event?
const zoomButtons = [
{
type: 'minute',
count: 5,
text: '5min',
events:{
click: onClickOfRangeSelector
}
}]
function onClickOfRangeSelector(e) {
console.log(this);
console.log(e);// need to make an API call here
}

You can refer to a chart variable, please check the below code:
var chart = Highcharts.stockChart('container', {
...,
rangeSelector: {
buttons: [{
type: 'minute',
count: 5,
text: '5min',
events: {
click: function(a, b, c) {
var min = chart.xAxis[0].max - this._range,
max = chart.xAxis[0].max;
console.log(min, max);
}
}
}]
}
});
Live demo: http://jsfiddle.net/BlackLabel/x1gc38nd/
API Reference: https://api.highcharts.com/highstock/rangeSelector.buttons.events

Related

Highcharts: how to dynamically set up max value of yAxis, based on the displayed data?

I have two charts here where I was using static data for the candles. Let's consider the second Y-axis, where the max value for the few given candles is 215.
In the code I provided, the max value for this y Axes are set as:
yAxis: [{
...
}, {
max: 215*4
}],
From where we can see that I am hardcoding the value for the max as 215*4. What I need to do is, to be able to dynamically get the current value and to multiply it by 4.
I tried it like:
yAxis: [{
...
}, {
max: this.getExtremes().dataMax * 4
}],
But seems is not working and seems that this.getExtremes() can be used in the events only. So is there any way how can I accomplish to set up max value depending on the current displayed max value?
I also tried the following (but it still does not work).
render: function() {
let yAxisExtremesVolumeMax = this.yAxis[1].getExtremes().dataMax;
this.yAxis[1].update({
max: yAxisExtremesVolumeMax * 4
});
},
Anyway, I am not sure that I want to check it this way, since the render event can be called too many times, so I prefer other alternatives.
Try to use this solution with the load instead the render. Doing update in the render callback creates the infinity loop. Code:
chart: {
events: {
load() {
let chart = this,
max = chart.series[1].dataMax;
chart.yAxis[1].update({
max: max * 4
})
}
}
},
Demo: https://jsfiddle.net/BlackLabel/c6qjnf5z/
Try to use this solution...
load: function () {
let { min, max } = this.yAxis[1].getExtremes();
max = max * 4;
this.yAxis[1].setExtremes(min, max);
}

Highcharts - use navigator as a seperate time selector

I have more than one chart in one page, all of them uses same data but in different chart types.
I would like to change all of their time with only one navigator on the top. like using it as a datepicker input, how can i achieve that?
Use afterSetExtremes event callback function and apply the same extremes on the rest charts:
xAxis: {
events: {
afterSetExtremes: function(e) {
var min = e.userMin,
max = e.userMax;
chart1.xAxis[0].setExtremes(min, max, true, false);
chart2.xAxis[0].setExtremes(min, max, true, false);
}
}
}
Live demo: http://jsfiddle.net/BlackLabel/8gc9qwsp/
API Reference:
https://api.highcharts.com/class-reference/Highcharts.Axis#setExtremes
https://api.highcharts.com/highstock/xAxis.events.afterSetExtremes

Is it possible to define custom technical indicators, that takes 2 series as input?

I have a highstocks chart that shows trends of non stock data because it has some good features that our product needs.
one of these features is custom technical indicators, espcially between 2 different trends showing.
I know how to define maually an indicator, but it seems I can't use more then 1 series with it, while I want to create indicators for a veraity of differneces between 2 or more series.
I can't find a way to do this, so I'm wondering if it is at all possible
Average between two series can be simply added as another separate series. You can use for example load event to calculate it and chart.addSeries() method to add it.
Code:
chart: {
zoomType: 'xy',
events: {
load: function() {
var chart = this,
dataLen = chart.series[0].points.length,
newSeries = {
id: "avg",
name: "average",
data: []
},
series1 = chart.series[0],
series2 = chart.series[1],
i;
for (i = 0; i < dataLen; i++) {
newSeries.data.push({
x: series1.points[i].x,
y: (series2.points[i].y + series1.points[i].y) / 2
})
}
chart.addSeries(newSeries);
}
}
}
Demo:
https://jsfiddle.net/BlackLabel/15sa38qb/
API:
https://api.highcharts.com/highcharts/chart.events.load
https://api.highcharts.com/class-reference/Highcharts.Chart#addSeries

Highcharts / Highstock scroll default position

I have a HighStock charge with a scrollbar very similar to this one
http://jsfiddle.net/gh/get/jquery/3.1.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/navigator-disabled/
Highcharts.stockChart('container', {
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Stock Price'
},
navigator: {
enabled: false
},
series: [{
name: 'AAPL Stock Price',
data: data,
tooltip: {
valueDecimals: 2
}
}]
});
I want the default scroll position to be somewhere in the middle of the chart though, is there any way to do this?
You can do this with xAxis.setExtremes on the chart.events.load call. A simple example (using arbitrary 1 year range):
chart: {
events: {
load: function() {
this.xAxis[0].setExtremes(
Date.UTC(2013, 0, 1),
Date.UTC(2013, 11, 31)
);
}
}
},
Example jsFiddle.
If you need to determine what your "middle" is it can depend on your data source where you could pre-calculate it or you could do it also on the chart.events.load call.
For fun I added the mid-point calculations. This uses moment.js because it is much easier than rolling your own time methods.
minX = data[0][0];
maxX = data[data.length - 1][0];
midX = minX + ((maxX - minX) / 2);
var midDate = new Date(midX);
var startDate = moment(midDate);
var endDateMoment = moment(midDate);
startDate.subtract(1.5, 'months');
endDateMoment.add(1.5, 'months');
Note that I am addinng/subtracting 1.5 months to make your 3 month range selector valid. The moment.js library rounds those up to 2 months. You get the idea though. So your setExtremes becomes:
this.xAxis[0].setExtremes(
startDate.valueOf(), //.valueOf() in this context converts to javascript time
endDateMoment.valueOf()
);
And here is live jsFiddle.

How to add current price line on hover?

I found a plugin that is able to show current price line on highstock candlestick chart, but it only displays the latest data.
I have tried to find a way to display the horizontal price line when user hovers over the data.
I thought it might be related to tooltip event, but I have no idea how to do it.
Could someone give me a hint? Thanks,
tooltop:{
formatter: function () {
}
},
http://jsfiddle.net/RolandBanguiran/nf7ne/
There is a couple of ways to achieve that. Problem is a little missing description (do you want to display current price on hover, or actually hovered value?).
Anyway, the easiest way would be to enable crosshairs: demo.
Another way is to add/remove plotLine on mouseOver event for series.point, demo and code:
point: {
events: {
mouseOver: function() {
var chart = this.series.chart;
chart.yAxis[0].removePlotLine("tooltip-line");
chart.yAxis[0].addPlotLine({
width: 2,
color: "black",
id: "tooltip-line",
value: this.y
});
}
}
}
If you want to hide that line, use mouseOut event and simply remove plotLine like above.
Third option bases on the above one - in case you want to display current price instead of hovered one. In such case, change value for the plotLine, demo and code:
point: {
events: {
mouseOver: function() {
var chart = this.series.chart;
chart.yAxis[0].removePlotLine("tooltip-line");
chart.yAxis[0].addPlotLine({
width: 2,
color: "black",
id: "tooltip-line",
value: this.series.yData[this.series.yData.length - 1][0]
});
},
mouseOut: function() {
this.series.yAxis.removePlotLine("tooltip-line");
}
}
}
Extra tip:
Check out more option for plotLines in the API (like dash style, or label).

Resources