Highcharts calculated max value not working with 2+ Y axis - highcharts

I have been stuck on this for a while now and despite several attempts and a lot of research I have yet to find the solution.
I am building charts with two Y axis. Everything works just fine except that the highcharts computed max Y value is always much higher than it should be. Resulting in a zoomed out graph.
I've replicated the problem here : http://jsfiddle.net/9vmtz3nf/
I cannot hardcode any max value as it will always depend on the data coming in from the DB and there is a very wide margin of values possible.
So basically, how can I tweak the options and parameters to make sure the graph max value is set to the closest tick (150/600 in the jsfiddle example) ?
Thank you in advance
$(function () {
Highcharts.chart('container', {
chart: {
type: 'line'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: [{
title: {
text: 'Primary Axis'
},
}, {
title: {
text: 'Secondary Axis'
},
opposite: true
}],
plotOptions: {
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 124.0, 126.0, 135.6, 111.5, 116.4, 114.1, 95.6, 54.4]
}, {
data: [129.9, 271.5, 306.4, 29.2, 544.0, 376.0, 435.6, 348.5, 216.4, 294.1, 35.6, 354.4],
yAxis: 1
}]
});
});

This is a result of the chart trying to find a common denominator. Even if you do hard code a max value, the chart will behave this way.
Setting the alignTicks property to false will fix this.
chart: {
alignTicks: false
}
Fiddle:
http://jsfiddle.net/jlbriggs/9vmtz3nf/2/
Reference:
http://api.highcharts.com/highcharts/chart.alignTicks

Related

Highcharts - Axis minPadding and maxPadding not working

I was adding some padding to my chart because I want to control the space between the actual chart and the container, and I encountered a problem.
Look at mi first fiddle, all seems legal but the padding isn't being applied:
https://jsfiddle.net/9r2Lqmpj/
Highcharts.chart('container', {
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
minPadding: 0.4,
maxPadding: 0.4
},
yAxis: {
startOnTick: false,
minPadding: 0.2
},
series: [{
data: [129.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
But if I duplicate the "xAxis" key, this time without the categories array, which seems highly wrong, for no reason it works:
https://jsfiddle.net/v0rj1xkg/
Highcharts.chart('container', {
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
minPadding: 0.4,
maxPadding: 0.4
},
xAxis: {
minPadding: 0.4,
maxPadding: 0.4
},
yAxis: {
startOnTick: false,
minPadding: 0.2
},
series: [{
data: [129.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
I wasn't expecting at all that this worked, in fact, I discovered it by mistake. Why is it working only when I repeat the xAxis key definition?
Properties minPadding and maxPadding don't work with category axis type. Use min and max options instead.
Highcharts.chart('container', {
xAxis: {...},
...
}, function() {
var xAxis = this.xAxis[0];
this.update({
xAxis: [{
min: xAxis.min + 0.2,
max: xAxis.max - 0.2
}]
}, true, true, false);
});
Live demo: https://jsfiddle.net/BlackLabel/96s30khd/
Well Agustin, not much of a discovery rather than a common logic there in your second scenario. I am not really sure what you're trying to achieve, but having xAxis defined twice is not necessary. Just remove the first one to get the same result, so it looks like your second one is overwriting the first one. Have a look at your fiddle here https://jsfiddle.net/xcmq869s/ which looks the same to me. Also it might be the case that the padding doesn't work with categorical data, so it works fine when no categories are specified. U can certainly alter the padding with removed categories.
On the other note, if you want to keep your x-axis labels and add only outer spacing, try using the chart.spacing config option as in the fiddle here https://jsfiddle.net/xcmq869s/1/.

How to position a Highcharts title on the right of the chart?

I'm checking through the documentation and don't really see a way to do this.
What I'd like to do is position the chart title to the right of the chart, rotated 90 degrees and centered vertically.
So something like this (used Photoshop to represent rotation):
I know I can set the x, and y position like this:
title: {
text: 'Chart title',
align: 'right',
y: 140,
x: 50,
rotation: 90 //this does not work
}
But that doesn't really give me what I want. Is there a setting that I'm not finding to position the title on the right, and rotate it 90 degrees?
Here is a fiddle I was messing with for the x, y axis positioning:
http://jsfiddle.net/c2tb1p5b/1/
You can do this using a combination of x, y placement on the title along with CSS - you will need to mess around with the exact values but something like this should work:
Highcharts.chart('container', {
chart: {
type: 'bar'
},
title: {
text: 'Chart title',
align: 'right',
y:-480,
x:-280
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
labels: {
step: 1
}
},
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]
}]
});
CSS:
.highcharts-title {
transform: rotate(90deg);
}
http://jsfiddle.net/c2tb1p5b/2/

How to indicate null value but don't connect it in Highcharts Line graph?

I want to indicate presence of null value in the graph.
I thought of replacing null with 0 but then, it would connect with rest of the real data and will draw incorrect graph. Is it possible to not connect zero value with rest of data points?
Alternatively, is it possible to indicate null value using plotband over it like shown in the image?
jsfiddle
var categoryText= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Highcharts.stockChart('container',{
chart: {
defaultSeriesType: 'spline',
renderTo: 'container'
},
xAxis: {
labels: {
formatter: function() {
return categoryText[this.value];
}
}
},
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
series: [{
data: [29.9, null, 106.4, 129.2, 144.0, 176.0, null, 148.5, 216.4, 194.1, 95.6, 54.4],
}]
});
If you have data declared above, then you can try this to include plot bands only for the nulls. I tried adjusting the offset of each tick to the middle of month but it seems a little inaccurate. Please adjust to your liking. Check out the fiddle.
plotBands: data.map(function(item, index) {
return { color: 'orange', from: index-0.5, to: index+0.5, data: item }
}).filter(function(item) { return item.data === null})
},
http://jsfiddle.net/95dLon47/3/

Highcharts: how display tooltip, when point = null?

When we hover on may In example, we don't see tooltip for this month, because data is null. Can I set the settings to see the tooltip when data is null?
Well as far as I know there is no generic option for that since highcharts ignores null values from showing.
On the other hand, we can replace the null points with "fake" ones, that have an average value between the 2 closest points (this will cause the chart flow remain the same), and with a custom property isNull which can be used as a flag later.
After doing that, we can use a formatter function for the tooltip, and manipulate the tooltip the way we want, for example displaying only the series name when a point isNull.
$(function () {
$('#container').highcharts({
title: {
text: 'The line is connected from April to Juni, despite the null value in May'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
connectNulls: true
}
},
tooltip: {
formatter: function(){
if(this.point.isNull == undefined)
return this.series.name+"<br>"+"<b>value:</b>"+this.y;
return this.series.name;
}
},
series: [{
data: [null, 71.5, 106.4, 129.2, null, 176.0, 135.6, null, 216.4, 194.1, 95.6, 54.4]
}]
}, function(chart){
var series = chart.series[0];
var data = series.data;
$.each(data,function(i){
var point = data[i];
var prevPoint = data[i-1] != undefined ? data[i-1].y : 0;
var nextPoint = data[i+1] != undefined ? data[i+1].y : 0;
if(point.y == null){
series.addPoint({x:point.x,y:(prevPoint+nextPoint)/2, isNull:true});
}
});
});
});
http://jsfiddle.net/zb5ksxtc/1/
I hope this is what you meant.
UPDATE
We can also do something not less hacky... (I guess a little hacking could work fine in this unusuall case)
What I did here was creating a "fake" scatter series, with points located where null values from the real series (used same average logic).
The series has hidden markers, and has a unique name.
After that, I used a shared tooltip and a formatter to display the right tooltip.
I used the name attribute to determine what series is focused:
$(function () {
$('#container').highcharts({
title: {
text: 'The line is connected from April to Juni, despite the null value in May'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
connectNulls: true,
},
},
tooltip: {
shared:true,
formatter: function(){
var points = this.points;
var seriesName = points[0].series.name;
if(seriesName == 'fake')
return "Null tooltip";
return seriesName+"<br>value: <b>"+this.y+"</b>";
}
},
series: [{
type:'areaspline',
data: [null, 71.5, 106.4, 129.2, null, 176.0, 135.6, null, 216.4, 194.1, 95.6, 54.4]
},
{
type:'spline',
lineWidth:0,
name:'fake',
showInLegend:false,
marker:{
symbol:'circle',
radius:0,
}
}
]
}, function(chart){
var series = chart.series[0];
var fake_series = chart.series[1];
$.each(series.data, function(i){
var point = this;
var nextPoint = series.data[i+1] != undefined ? series.data[i+1].y : 0;
var prevPoint = series.data[i-1] != undefined ? series.data[i-1].y : 0;
if(series.data[i].y == null)
fake_series.addPoint([series.data[i].x, (nextPoint+prevPoint)/2]);
});
});
});
and the fiddle ofcourse: http://jsfiddle.net/zb5ksxtc/8/

Moving highchart to the left when there is a Y-axis title

I'm trying to put Y-Axis title on top right of the Y-Axis, right below the chart title. However, when I insert the axis title, the whole graph moves to the right. I tried to used spacingLeft to set the position of the chart, but nothing changes. Is there a way to keep the position of the chart even if I add the axis title?
Here is my code:
$(function () {
$('#container').highcharts({
chart: {
marginBottom: 80,
spacingTop: 20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
title: {
text: 'Chart title',
y: -10
},
yAxis: {
title: {
text: 'aaaaaaaaaaaaaaaaaaa',
align: 'high',
rotation: 0,
x: 100,
y:-20
},
labels: {
align: 'high',
x: 0,
y: -5
}
},
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]
}]
});
});
jsfiddle: http://jsfiddle.net/8m7rc/1/
Min Sun Song, you can use offset property to reduce that gap.
offset: -120,
I've updated you fiddle here
Hope this is what you are looking for.
You can set marginLeft property for a chart. See demo.

Resources