Highcharts : barchart - highcharts

I have a barchart with many years in the x-axis. I want to select the years. Here we have the opportunity to do this in 2 ways: either with a click-and-drag, either with sliders.
I have two problems.
The first concerns the click-and-drag: When I select the extreme years, then appear two years, and not only those selected. For example, if I select "1960" then appears 1960 and 1961. Same if I select 2016 shows 2015 and 2016.
The second problem concerns the sliders. If both sliders are to the same value, then there is a problem. For example, if my two sliders are on 1998, the graph shows the years 2015 and 2016. If they are both on 1966, the graph displays the years 1994 and 1995.
Here is my code HTML :
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
<div style="margin: 20px 0px 0px 60px">
<!--
The "oninput" attribute is automatically showing the value of the slider on load and whenever the user changes the value.
Since we are using a category x-axis, the values are between 0 and 12. For this example, I'm adding your base year (2004)
to the output value so it shows a label that's meaningful to the user. To expand this example to more years, set your max value
to the appropriate value and the base year to wherever you plan to start your chart's data.
-->
<form oninput="output1.value=parseInt(slider1.value)+1960;output2.value=parseInt(slider2.value)+1960">
<input type="range" name="slider1" class="mySlider" min="0" max="56" value="0" />
<output name="output1" id="output1" for="slider1" style="font-size:10px;padding-top:0px;text-align:left">1960</output>
<br />
<input type="range" name="slider2" class="mySlider" min="0" max="56" value="56" />
<output name="output2" id="output2" for="slider2" style="font-size:10px;padding-top:0px;text-align:right">2016</output>
</form>
</div>
Here is my code JavaScript :
$(function () {
$('#container').highcharts({
chart: {
type: 'column',
zoomType: 'x'
},
colors:[
'#d8d826'
],
legend:{
enabled:false
},
title:{
style:{
fontSize:'0px'
}
},
subtitle:{
style:{
fontSize:'0px'
}
},
xAxis: {
// NOTE: There is an interesting bug here where not all labels will be shown when the chart is redrawn.
// I'm not certain why this is occuring, and I've tried different methods to no avail. I'll check with Highcharts.
categories: ['1960','1961','1962','1963','1964','1965','1966','1967','1968','1969','1970','1971','1972','1973','1974','1975','1976','1977','1978','1979','1980','1981','1982','1983','1984','1985','1986','1987','1988','1989','1990','1991','1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'],
tickmarkPlacement: 'on', tickInterval: 1,
minRange: 1 // set this to allow up to one year to be viewed
},
yAxis: {
min: 15,
title: {
text: 'Number',
style:{
fontSize:'0px'
}
}
},
tooltip: {
shared: false,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'data by year',
data: [49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,50]
}]
});
// on change handler for both sliders
$('.mySlider').bind('change', function(e) {
e.preventDefault();
var chart = $('#container').highcharts();
// use setExtremes to set the x-axis ranges based on the values in the sliders
chart.xAxis[0].setExtremes($('input[name="slider1"]').val(), $('input[name="slider2"]').val());
});
});
Look at in : https://jsfiddle.net/uvat8u05/13/
How to solve these two problems please?
Thank you

Highcharts are expecting int values for setExtremes() method. So you should get the value of sliders;
$('input[name="slider1"]').val();
Then parse this value to int;
parseInt($('input[name="slider1"]').val());
Here I fixed it for you: jsFiddle

Related

Highcharts Highstock How do I change the label on top of tooltip for OHLC series data?

Problem
Run the snippet below in Firefox browser and the top tooltip label is "open". How does one replace the top tooltip label "open" with the new label "Adjusted"?
This is the csv data in the original snippet code:
<pre id="csv" style="display: none">
date,open,high,low,close
2006-05-20, 63.26, 64.88, 62.82, 64.51
2006-05-22, 63.87, 63.99, 62.77, 63.38
</pre>
Forced Solution
In the code snippet I can force the desired result by replacing the CSV header "open" with the name "Adjust" as shown below:
<pre id="csv" style="display: none">
date,Adjust,high,low,close
2006-05-20, 63.26, 64.88, 62.82, 64.51
2006-05-22, 63.87, 63.99, 62.77, 63.38
</pre>
However I prefer to change the name in the tooltip rather than to change the header in the CSV so I do not regard this as an approved solution.
Snippet
<html>
<head>
<title>
AAPL Combined OHLC and Moving Average
</title>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
</head>
<body>
<div id="chart-container" style="width: 100%; height: 100%; margin: 0 auto"></div>
<pre id="csv" style="display: none">
date,open,high,low,close
2006-05-20, 63.26, 64.88, 62.82, 64.51
2006-05-22, 63.87, 63.99, 62.77, 63.38
2006-05-23, 64.86, 65.19, 63.00, 63.15
2006-05-24, 62.99, 63.65, 61.56, 63.34
2006-05-25, 64.26, 64.45, 63.29, 64.33
2006-05-26, 64.31, 64.56, 63.14, 63.55
2006-05-30, 63.29, 63.30, 61.22, 61.22
2006-05-31, 61.76, 61.79, 58.69, 59.77
</pre>
<script type="text/javascript">
Highcharts.stockChart('chart-container', {
rangeSelector: {
selected: 2
},
title: {
text: 'AAPL Stock Price'
},
plotOptions: {
series: {
turboThreshold: 0
},
ohlc: {
color: 'black',
tooltip: {
// pointFormat: 'Adjusted'
}
}
},
data: {
csv: document.getElementById('csv').innerHTML,
seriesMapping: [{
x: 0,
open: 1,
high: 2,
low: 3,
close: 4
}]
},
series: [{
type: 'ohlc', // bars
visible: true,
// set color in plotOptions
tooltip: {
// pointFormat: 'Adjusted'
}
}]
});
</script>
</body>
</html>
IF I uncomment either or both pointFormat statements (in the tooltip options shown above) THEN the Open, High, Low, and Close data no longer display in the tooltip pop-up. Instead only the word "Adjusted" appears in the tooltip.
Set name for the series, disable firstRowAsNames property and set startRow to 1 in data options:
data: {
firstRowAsNames: false,
startRow: 1,
...
},
series: [{
name: 'Adjust',
...
}]
Live demo: http://jsfiddle.net/BlackLabel/fkh0oa2s/
API Reference:
https://api.highcharts.com/highstock/data
https://api.highcharts.com/highstock/series.ohlc.data

Slider on barchart

With Highcharts I realized a barchart. This one show the number of documents per year.
To select one or more dates, use the slider located below the graph.
I managed to put it in place.
However, I can not link it to the chart.
Here, if you put the first cursor on 2000 and the second on 2003, he would have the graph should shows only the dates 2000, 2001, 2002, and 2003.
Can you help me to do this please ?
Here is my HTML/PHP code :
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<div id="container"></div>
<div style="margin: 20px 0px 0px 60px">
<!--
The "oninput" attribute is automatically showing the value of the slider on load and whenever the user changes the value.
Since we are using a category x-axis, the values are between 0 and 12. For this example, I'm adding your base year (2004)
to the output value so it shows a label that's meaningful to the user. To expand this example to more years, set your max value
to the appropriate value and the base year to wherever you plan to start your chart's data.
-->
<script>
$( function() {
$( "#slider-range" ).slider({
range: true,
min: 1960,
max: 2016,
values: [ 1960, 2016 ],
slide: function( event, ui ) {
$( "#amount" ).val( ui.values[ 0 ] + " - " + ui.values[ 1 ] );
}
});
$( "#amount" ).val( $( "#slider-range" ).slider( "values", 0 ) +
" - " + $( "#slider-range" ).slider( "values", 1 ) );
} );
</script>
<p>
<label for="amount">Year(s):</label>
<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
</p>
<div id="slider-range"></div>
</div>
And here is my JS code :
$(function () {
$('#container').highcharts({
chart: {
type: 'column',
zoomType: 'x'
},
colors:[
'#d8d826'
],
legend:{
enabled:false
},
title:{
style:{
fontSize:'0px'
}
},
subtitle:{
style:{
fontSize:'0px'
}
},
xAxis: {
// NOTE: There is an interesting bug here where not all labels will be shown when the chart is redrawn.
// I'm not certain why this is occuring, and I've tried different methods to no avail. I'll check with Highcharts.
categories: ['1960','1961','1962','1963','1964','1965','1966','1967','1968','1969','1970','1971','1972','1973','1974','1975','1976','1977','1978','1979','1980','1981','1982','1983','1984','1985','1986','1987','1988','1989','1990','1991','1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'],
tickmarkPlacement: 'on', tickInterval: 1,
minRange: 1 // set this to allow up to one year to be viewed
},
yAxis: {
min: 15,
title: {
text: 'Number',
style:{
fontSize:'0px'
}
}
},
tooltip: {
shared: false,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'data by year',
data: [49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4,49.9,83.6,48.9,69.1,50]
}]
});
});
You could see the result on : https://jsfiddle.net/uvat8u05/20/
The simple solution for your problem is to add jQuery responsible for your slider inside load event callback function of your chart, and use setExtremes for setting new extremes on slide:
http://api.highcharts.com/highcharts#Axis.setExtremes
function(chart) {
$("#slider-range").slider({
range: true,
min: 1960,
max: 2016,
values: [1960, 2016],
slide: function(event, ui) {
$("#amount").val(ui.values[0] + " - " + ui.values[1]);
chart.xAxis[0].setExtremes(ui.values[0] - 1960, ui.values[1] - 1960)
}
});
$("#amount").val($("#slider-range").slider("values", 0) +
" - " + $("#slider-range").slider("values", 1));
}
Here you can find live example how it can work: https://jsfiddle.net/uvat8u05/22/
Regards,

Hover on areas not on point in Highchart-polygon

I have one chart and i want to include hover on area but i found it works only on point only.
series: [{
name: 'Target',
type: 'polygon',
data: [[153, 42], [149, 46], [149, 55], [152, 60]],
color: Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0.5).get(),
enableMouseTracking: true
}],
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x} cm, {point.y} kg'
}
http://jsfiddle.net/onhfLqdm/3/
As area is bounded by points so how can i hover area instead of points?
Update On hover on each polygon asker wants to show data coming from json.To do so in a div out of container please view this fiddle
In Tooltip One more option to show some info coming from json ,tooltip can be used.Put your data using some name like "someText" (as in my fiddle )and get it in formatter function of tooltip using
this.options.someText
See this fiddle for data in tooltiip
Old Answer:
plotOptions: {
series: {
events: {
mouseOver: function () {
$("#polygon").html('Moused over Event')
.css('color', 'green');
},
mouseOut: function () {
$("#polygon").html('Moused out Event')
.css('color', 'red');
}
}
}
}
Fiddle link is here

Highstock bug: navigator doesn't work when startOnTick/endOnTick set to true on datetime xAxis

When startOnTick and/or endOnTick are set to true for a datetime xAxis, dragging the navigator left and right expands it until it fills up the whole range of data.
See:
http://jsfiddle.net/L3t4s/2/
Duplicate code below:
<div id="container" style="height: 400px; min-width: 600px"></div>
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<script type="text/javascript" src="http://www.highcharts.com/samples/data/usdeur.js"></script>
$(function() {
$('#container').highcharts('StockChart', {
chart: {
plotBorderWidth: 1
},
rangeSelector: {
selected: 4
},
xAxis: {
startOnTick: true,
endOnTick: true
},
series: [{
name: 'USD to EUR',
data: usdeur
}]
});
});
It's caused by real time redraws while dragging navigator. To prevent this set liveRedraw: false, see: http://jsfiddle.net/L3t4s/4/

Chart rendering issue when hiding all series via clicking on legends

Steps to reproduce.
We have a chart that display 4 series of data, and we have the corresponding legends with each series.
THe initial chart is loaded with 1 years worth of data.
We then remove all data-series from the chart by clicking on the 4 legends
We then change the Zoom level of the chart - e.g. going from a 6 month zoom, to a 3 month zoom. (NOTE: We change Zoom with no data-series being displayed).
We then re-enable the data-series by clicking on the legends.
The chart does not redraw correctly. To get the chart to redraw we have to reload the entire page.
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 500px; min-width: 600px"> </div>
$(function() {
var seriesOptions = [],
yAxisOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL', 'GOOG'],
colors = Highcharts.getOptions().colors;
$.each(names, function(i, name) {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename='+ name.toLowerCase() +'-c.json&callback=?', function(data) {
seriesOptions[i] = {
name: name,
data: data
};
// As we're loading the data asynchronously, we don't know what order it will arrive. So
// we keep a counter and create the chart when all the data is loaded.
seriesCounter++;
if (seriesCounter == names.length) {
createChart();
}
});
});
// create the chart when all data is loaded
function createChart() {
chart = new Highcharts.StockChart({
chart: {
renderTo: 'container'
},
navigator: {
enabled: false
},
legend: {
enabled: true
},
rangeSelector: {
selected: 4
},
scrollbar: {
enabled: false
},
yAxis: {
labels: {
formatter: function() {
return (this.value > 0 ? '+' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},
plotOptions: {
series: {
compare: 'percent'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2
},
series: seriesOptions
});
}
});
JsFiddle Example
It looks like a bug, so I've reported it to our devs here: https://github.com/highslide-software/highcharts.com/issues/1568
A simple workaround :
After some trial and error, I found out that if you do not disable the navigator, the above bug is not exhibited. (Navigator is enabled by default.)
Comment the line as shown below:
navigator: {
//enabled: false
}
Js Fiddle : http://jsfiddle.net/msjaiswal/FDXBu/1/
Certainly this is a bug in Highcharts but we can live with this above simple workaround.
This issue has been resolved in version 1.3.2 of HighStock.

Resources