How to format Highstock range slider date and time formatting - highcharts

Highstock has a time range selector below the main graph that contains dates and times. For example 10. Oct in this example: https://www.highcharts.com/demo/stock/intraday-area
How can I format those, eg to say '10/10' instead?

As I understand you want to change the navigator date format, under the chart.
You can add "navigator" to your chart options like this;
navigator: {
xAxis: {
labels: {
formatter: function() {
var d = new Date(this.value),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [day,month,year].join('/');
}
}
}
}
You can find full code snippet below and change formatter function.
Highcharts.getJSON('https://cdn.jsdelivr.net/gh/highcharts/highcharts#v7.0.0/samples/data/new-intraday.json', function (data) {
// create the chart
Highcharts.stockChart('container', {
title: {
text: 'AAPL stock price by minute'
},
subtitle: {
text: 'Using ordinal X axis'
},
xAxis: {
gapGridLineWidth: 0
},
navigator: {
xAxis: {
labels: {
formatter: function() { // You can change the function according to your needs.
var d = new Date(this.value);
var month = '' + (d.getMonth() + 1);
var day = '' + d.getDate();
var year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [day,month].join('/');
}
}
}
},
rangeSelector: {
buttons: [{
type: 'hour',
count: 1,
text: '1h'
}, {
type: 'day',
count: 1,
text: '1D'
}],
selected: 1,
inputEnabled: false
},
series: [{
name: 'AAPL',
type: 'area',
data: data,
gapSize: 5,
tooltip: {
valueDecimals: 2
},
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
threshold: null
}]
});
});
<div id="container" style="height: 400px; min-width: 310px"></div>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/data.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/modules/export-data.js"></script>

Related

How to set a different value on opposite y-axis in bar-negative high charts

I wanted to have different values on the left(negative) side as 1k ,2k,3k etc and different values on the right(positive) side of y-axis like 100,200,300 etc...
I tried different approaches by having conditions in labels formatter function
// Data gathered from http://populationpyramid.net/germany/2015/
// Age categories
var categories = [
'48-51', '51-54', '57-60', '60-63',
'63-66'
],
oppositecategories = [];
Highcharts.chart('container', {
chart: {
type: 'bar'
},
title: {
text: 'Headcount and Hires Age Distribution'
},
subtitle: {
text: 'Lorem Ipsum'
},
xAxis: [{
categories: categories,
reversed: false,
labels: {
step: 1
}
}, { // mirror axis on right side
opposite: true,
reversed: false,
categories: oppositecategories,
linkedTo: 0,
labels: {
step: 1
}
}],
yAxis: [{
title: {
text: null
},
labels: {
formatter: function() {
return Math.abs(this.value) + 'K';
},
y: 10
}
},
{
title: {
text: null
},
opposite: false,
reversed: false,
labels: {
step: 100,
y: 100
}
}
],
plotOptions: {
series: {
stacking: 'normal'
}
},
tooltip: {
formatter: function() {
return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' +
'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y * 1000), 0);
}
},
series: [{
name: 'Current HC',
data: [-2.132, -1.387, -1.121, -1.479, -1.239]
}, {
name: 'Hires(4Q)',
data: [
1.17, 1.72, 1.36, 1.03, 1.21
]
}]
});
I expect different values on negative side and different values on positive side
You can create two separate yAxis for the series:
yAxis: [{
max: 0,
offset: 0,
width: '50%'
}, {
min: 0,
offset: 0,
left: '50%',
width: '50%'
}]
Live demo: http://jsfiddle.net/BlackLabel/Lgqyc4k8/
Using yAxis.labels.formatter you can have this result :
yAxis: [{
title: {
text: null
},
tickInterval:0.1,
labels: {
formatter: function() {
if(this.value < 0 && this.value % 0.5 === 0){ // Negative value only on .5 k
return Math.abs(this.value) + 'K';
} else if(this.value > 0){ // Positive values
return this.value * 1000;
} else if (this.value === 0) {
return 0;
}
},
y: 10
}
},
Fiddle

Fixing the decimals on y axis to 5 places

I am trying to solve the following three problems
Decimal Places on Y Axis (all Axis) - should be constant 5
Decimal Places on Tooltip - should be constant 5
Adding a Suffix to all points on the tooltip
I need to keep the values shown on the on Y Axis fixed to 5 decimal places ( say 0.96724 or 1.55400 ) as received from the data feed.
I have forked and created a new jsfiddle to highlight the problem. This shows only upto two digits.
From various examples, I tried to create a tooltip, but it did not worked.
Any help or pointers would be highly appreciated. Created a jsfiddle below
Regards
Amit
http://jsfiddle.net/amonga141/tgz469a5/2/
// Some styling options for yAxis
Highcharts.theme = {
yAxis: {
gridLineWidth: 1,
gridLineColor: "rgb(240, 240, 240)",
minorGridLineColor: 'rgba(255, 255, 255, 0.07)',
lineColor: "rgb(241, 141, 5)",
lineWidth: 1,
tickColor: "rgb(241, 141, 5)",
tickWidth: 1,
showEmpty: false,
labels: {
style: {
color: 'rgb(102, 102, 102)',
fontWeight: 'bold'
}
},
title: {
style: {
color: 'rgb(229, 64, 40)',
font: "bold 12px Lucida Grande, Lucida Sans Unicode, Verdana, Arial, Helvetica, sans-serif"
}
}
}
};
// Apply the styling options
Highcharts.setOptions(Highcharts.theme);
Highcharts.setOptions({
global: {
useUTC: false
}
});
// Data generator helper
function data(start, end, samples, min, max) {
var temp = [];
var range = ~~ ((end - start) / samples);
for (var i = 1; i < samples; i++) {
temp.push([start.getTime() + range * i, min + Math.random() * (max - min)]);
}
return temp;
}
//
var tooltipSuffix ='<br> TTIP Sufix:Suffix';
// Create a timer
var now = new Date();
var start = new Date(now - 60000);
var example_data = [{
name: 'Signal 1',
data: data(start, now, 20, 1.55478, 1.55481),
yAxis: 0
}, {
name: "Signal 2",
data: data(start, now, 20, 1.32419, 1.13253),
yAxis: 1
}, {
name: "Signal 3",
data: data(start, now, 20, 0.97456, 0.97545),
yAxis: 2
}];
//////////////////////
// Chart definition //
//////////////////////
var chart = $("#chart").highcharts({
legend: {
enabled: true
},
chart: {
animation: Highcharts.svg,
events: {
load: function () {
// set up the updating of the chart each second
var chart = this,
series = chart.series;
setInterval(function () {
var x = new Date(); // current time
series[0].addPoint(data(x, x, 2, 1.55678, 1.59133)[0], false, true);
series[1].addPoint(data(x, x, 2, 1.33456, 1.39921)[0], false, true);
series[2].addPoint(data(x, x, 2, 0.95989, 0.99092)[0], false, true);
chart.redraw();
}, 2000);
}
}
},
yAxis: [{
opposite: false,
showRects: true,
offset: 40,
title: {
text: 'Scale 1'
}
}, {
opposite: true,
showRects: true,
offset: 90,
title: {
text: 'Scale 2'
}
}, {
opposite: false,
showRects: true,
offset: 140,
title: {
text: 'Scale 3'
}
}],
xAxis: {
type: 'datetime'
},
// tooltip: {
// xDateFormat: '%A, %b %e, %H:%M:%S', // Shows only [Day, Mon dd, hh:mi:ss]
// shared: true,
// pointFormat: ' <span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> <br/>'+tooltipSuffix,
// valueDecimals: 5,
// crosshairs: true
// } ,
series: example_data
});
<div id="chart"></div>
See Working fiddle with your code here
For labels use following in your options of yAxis.
labels: {
format: '{value:.5f}',
}
for tooltip see the formatter function below
tooltip: {
shared: true,
pointFormat: ' <span style="color:{series.color}">{series.name}</span>: <b>{point.y:.5f}</b>'+tooltipSuffix+' <br/>',
}

Highchart xaxis data decrease when shift

I am creating a chart and populating a random data every 5 minute. I set the xAxis data range to 5 hour and when start with initial data, it's width is full in chart but when continuously add point, the data width is decreasing. How do I do to make it not decrease the series width?
I have the following code:
$(function () {
var current_time = (new Date()).getTime()+300000;
var now_time = (new Date()).getTime();
var zone_name = ['A','B','C'];
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg,
marginRight: 10,
events: {
load: function() {
var chartsss = $('#container').highcharts();
setInterval(function() {
for(var i=0;i<zone_name.length;i++)
{
var seriessx = chartsss.series[i];
seriessx.addPoint([current_time, Math.random()], false, true);
seriessx.addPoint([current_time, Math.random()], false, true);
}
chartsss.redraw();
current_time += 300000;
}, 1000);
}
}
},
plotOptions: {
series: {
marker: {
enabled: false
}
},
spline: {
lineWidth: 2,
states: {
hover: {
lineWidth:3
}
},
marker: {
enabled: false,
states: {
hover: {
enabled: false,
symbol: 'circle',
fillColor: '#ff0000',
radius: 4,
lineWidth: 1
}
}
}
},
},
title: {
text: 'Daily Zone'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
day: '%e of %b',
minute: '%H:%M',
hour: '%H:%M'
},
tickPosition: 'inside',
tickInterval: 3600 * 1000,
minRange:5 * 3600 * 1000,
maxRange:5 * 3600 * 1000
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%H:%M', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: true
},
exporting: {
enabled: false
},
series: (function(){
var seriesx = [];
for(var i=0;i<zone_name.length;i++)
{
var datax = [];
var time = (new Date()).getTime();
now_time=current_time;
for(var j = -59 ;j <= 0;j++)
{
datax.push({
x: now_time,
y: Math.random()
});
now_time+=300000;
}
seriesx.push({
name:zone_name[i],
data: datax
});
}
current_time=now_time;
return seriesx;
})()
});
});
});
JsFiddle Link: http://jsfiddle.net/jedipalm/YLFhD/4/
Image when started chart: http://i.stack.imgur.com/rE8m3.jpg
Image when continuously add point in few second :http://i.stack.imgur.com/J8GtA.jpg
sorry for my bad English.
Why you are adding twice the same point? Highcharts doesn't support two points in one series with the same x-value. Removed one of addPoint() and works fine: http://jsfiddle.net/YLFhD/8/
try following code:
for(var j = -120 ;j <= 0;j++)
{
datax.push({
x: now_time,
y: Math.random()
});
now_time+=300000;
}
http://jsfiddle.net/YLFhD/6/

Highcharts drilldown from a series

I have a chart that displays series data by year.
eg 1011, 1112, 1213, 1415, and shows an actual and target for each year.
If the user clicks on a column, the chart drills down into that dataset, however I'd like to be able to show Actual and Target next to each other, rather than a single column...
JSFiddle: http://jsfiddle.net/MuydK/
Any help appreciated, am stumped on this as tried several methods.
I believe I understand how to set up the data into a series set rather than just a dataset, but don't know what changes I need to make to SetChart etc...
Also the Series titles disappear once I've drilled in and out once, any ideas...?
Many Thanks for you help.
var chart;
$(document).ready(function () {
var colors = Highcharts.getOptions().colors,
categories1 = ['1011', '1112', '1213', '1415'],
name1 = 'Actual',
data1 = [ ...
I updated your code with simple data, I think this is what you want,This is just a demo you can update code according to you.
JSFiddle
Here is code:
$(function () {
var chart;
$(document).ready(function () {
var colors = Highcharts.getOptions().colors,
categories1 = ['1011', '1112', '1213', '1415'],
name1 = 'Actual',
data1 = [
{
y: 1674,
color: colors[0],
drilldown: {
name: '1011 Actual',
categories: ['BS', 'B', 'IT', 'C'],
data: [3, 32, 54, 50],
color: colors[0],
name1: '1011 Target',
data1: [0, 31, 50, 60],
color1:colors[1]
}
}
]; var colors = Highcharts.getOptions().colors,
categories2 = ['1011', '1112', '1213', '1415'],
name2 = 'Target',
data2 = [
{
y: 1633,
color: colors[1],
drilldown: {
name: '1011 Actual',
categories: ['BS', 'B', 'IT', 'C'],
data: [3, 32, 54, 50],
color: colors[0],
name1: '1011 Target',
data1: [0, 31, 50, 60],
color1:colors[1]
}
}
]; function setChart(name, categories, data, color) {
console.log(name, categories, data, color);
chart.xAxis[0].setCategories(categories);
while (chart.series.length > 0) {
chart.series[0].remove(true);
}
for (var i = 0; i < data.length; i++) {
chart.addSeries({
name: name[i],
data: data[i],
color: color[i]
});
}
}
chart = new Highcharts.Chart({
chart: {
renderTo: 'con1',
type: 'column'
},
title: {
text: 'Learner Responsive 16-18'
},
subtitle: {
text: 'Click the columns to view breakdown by department. Click again to view by Academic Year.'
},
xAxis: {
categories: categories1
, labels: {rotation:-90, align:'right'}
},
yAxis: {
title: {
text: 'Learner Responsive 16-18'
}
},
plotOptions: {
column: {
cursor: 'pointer',
point: {
events: {
click: function () {
var drilldown = this.drilldown;
if (drilldown) { // drill down
setChart([drilldown.name,drilldown.name1], drilldown.categories, [drilldown.data, drilldown.data1], [drilldown.color,drilldown.color1]);
} else { // restore
setChart(name, categories1, [data1, data2], 'white');
}
}
}
},
dataLabels: {
enabled: true,
color: colors[0],
style: {
fontWeight: 'bold'
},
formatter: function () {
return this.y; // +'%';
}
}
}
},
tooltip: {
formatter: function () {
var point = this.point,
series = point.series,
s = 'Learner Responsive 16-18' + '<br/>' + this.x + ' ' + series.name + ' is <b>' + this.y + '</b><br/>';
if (point.drilldown) {
s += 'Click to view <b>' + point.category + ' ' + series.name + ' </b>' + ' by department';
} else {
s += 'Click to return to view by academic year.';
}
return s;
}
},
series: [{
name: name1,
data: data1,
color: colors[0]
},{
name: name2,
data: data2,
color: colors[1]
}],
exporting: {
enabled: false
}
},
function (chart) {
console.log(chart);
});
});
});

Change Color of Volume Columns (High/Low) in HighCharts

I've got a simple chart showing candlesticks with volume columns underneath: http://jsfiddle.net/T83Xy/
Basically, I want to use black and red for the columns depending on whether it closes higher than the open or not. I've seen some samples by pushing Y: data, color: '#000000' as the parameter. The problem is I'm pushing a date and a volume number. I attempted to push X: date, Y: data, color: '#000000' but it's throwing errors and not giving me the expected result.
At first, you need to set series.turboThreshold to 0 if you have a big amount of points. This will disable the input data format check.
Then, to apply column colors depending on candles, I suggest you this piece of code:
Highcharts.seriesTypes.column.prototype.pointAttribs = (function(func) {
return function(point, state) {
var attribs = func.apply(this, arguments);
var candleSeries = this.chart.series[0]; // Probably you'll need to change the index
var candlePoint = candleSeries.points.filter(function(p) { return p.index == point.index; })[0];
var color = (candlePoint.open < candlePoint.close) ? '#FF0000' : '#000000'; // Replace with your colors
attribs.fill = state == 'hover' ? Highcharts.Color(color).brighten(0.3).get() : color;
return attribs;
};
}(Highcharts.seriesTypes.column.prototype.pointAttribs));
Be careful as this code will affect ALL of your charts that currently on page. But you can easily add some conditions to run this only on specific chart. Here is a default Highstock demo with the code above.
This worked perfectly for me:
$(function () {
jQuery.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function (data) {
// split the data set into ohlc and volume
var volumeColor = '';
var ohlc = [],
volume = [],
dataLength = data.length,
// set the allowed units for data grouping
groupingUnits = [[
'week', // unit name
[1] // allowed multiples
], [
'month',
[1, 2, 3, 4, 6]
]],
i = 0;
for (i; i < dataLength; i += 1) {
ohlc.push([
data[i][0], // the date
data[i][1], // open
data[i][2], // high
data[i][3], // low
data[i][4] // close
]);
if (i==0) {
volumeColor = '#CCCCCC';
} else {
if (data[i][1] >= data[i-1][1]) {
volumeColor = '#006633';
} else {
volumeColor = '#CC0033';
}
}
volume.push({
x: data[i][0], // the date
y: data[i][5],
color: volumeColor
});
}
// create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Historical'
},
yAxis: [{
labels: {
align: 'right',
x: -3
},
title: {
text: 'OHLC'
},
height: '60%',
lineWidth: 2
}, {
labels: {
align: 'right',
x: -3
},
title: {
text: 'Volume'
},
top: '65%',
height: '35%',
offset: 0,
lineWidth: 2
}],
series: [{
type: 'candlestick',
name: 'AAPL',
data: ohlc,
dataGrouping: {
units: groupingUnits
}
}, {
type: 'column',
name: 'Volume',
data: volume,
yAxis: 1,
turboThreshold: Number.MAX_VALUE,
dataGrouping: {
enabled: false,
units: groupingUnits
}
}]
});
});
});
<script src="https://code.jquery.com/jquery-3.3.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>
The current (HighCharts 7+) solution for this, which doesn't require overriding any methods, is to simply set the color attribute for the volume point according to the comparison between the current candlestick point's open and its close: green if <, red if >, and yellow if equal.
Here is a minimal example.
// Return a color matching the candle by comparing the open (1) and close (4)
function volumeBarColor(point) {
if (point[1] < point[4])
return 'green';
if (point[1] > point[4])
return 'red';
return 'yellow';
}
Highcharts.getJSON('https://www.highcharts.com/samples/data/aapl-ohlcv.json', data => {
// Split the data set into ohlc and volume
const ohlc = [],
volume = [];
for (let i = 0; i < data.length; i += 1) {
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({
x: data[i][0], // the date
y: data[i][5], // the volume
color: volumeBarColor(data[i]),
});
}
// Create the chart
Highcharts.stockChart('container', {
title: {
text: 'AAPL Historical'
},
yAxis: [{
labels: {
align: 'right',
x: -3
},
height: '60%',
}, {
labels: {
align: 'right',
x: -3
},
top: '65%',
height: '35%',
offset: 0,
}],
series: [{
type: 'candlestick',
name: 'AAPL',
data: ohlc,
}, {
type: 'column',
name: 'Volume',
data: volume,
yAxis: 1,
}]
});
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/data.js"></script>
<div id="container"></div>

Resources