I like to set dynamically the value of the minPointLengh for high chart. But the below mentioned code is not working. can you please help me to get it done.
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
var minPointLength;
if(this.y > 0)
minPointLength = 3,
else
minPointLength = 0,
series: {
minPointLength: minPointLength,
}
},
series: [{
data: [4, 0, 1, 5]
}]
});
});
I like to set dynamically the value of the minPointLengh for high chart. But the below mentioned code is not working. can you please help me to get it done.
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
var minPointLength;
if(this.y > 0)
minPointLength = 3,
else
minPointLength = 0,
series: {
minPointLength: minPointLength,
}
},
series: [{
data: [4, 0, 1, 5]
}]
});
});
My suggestion: change any 0
values to null
values.
The minPointLength
will not affect any points with null y
values.
Update code to:
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
minPointLength: 3
}
},
series: [{
data: [4, null, 1, 5]
}]
});
});
Example:
Additional reason that your code wouldn't work: you are trying to set the minPointLength
property directly within the plotOptions
object - it needs to be inside one of the series selector objects (ie 'series: {}
', 'column: {}
', etc).
The solution shared by @jlbriggs works fine, but it also removes data from tooltips.
I took similar approach, but instead of making them null, I simply converted 0
values to -1
and set min
in yAxis
as 1
. And also showed 0
for values <0
in tooltip.
This is not possible. The attribute minPointLength
applies to an entire series and cannot be changed for individual points.
If you want to correctly show when values are positive and when they are 0, it may be helpful to show data labels so those columns are clearly marked (see this example at: https://jsfiddle/brightmatrix/1spxcsmd/).
Also, anything you want to do dynamically within Highcharts code needs to be in functions called by either events or the formatter
function. You can't insert variables or code directly into the chart options. One of the reasons your original code wasn't working was due to your code in plotOptions
.
Problem with existing solutions:
Solution:
We add extra bar height in series data and remove(subtract) that from tooltip data.
tooltip: formatter(data){}
).buildGraph(){
series:""
}
formatter(data){
, you'll find extraBarHeight for each bar. Subtract that amount from the tooltip data (that you probably will be fetching from points property which is automatically updated according to series data)Now calculating extraBarHeight is tricky.
Iterate through all dataPoints in series data and find the highest
one, maxUnit
extraBarHeight = 10% of maxUnit (You can change percentage)
Compare each data point in series. We are doing this to avoid small units to bee larger than big units and leave the zero bars as it is.
if(dataPoint< extraBarHeight && dataPoint !== 0)
dataPoint = dataPoint + extraBarHeight;
It is a long and plicated solution but this is the only possible way at least for now.
You cannot use code inside object.
Try to do this:
var minPointLength;
if(this.y > 0)
minPointLength = 3,
else
minPointLength = 0,
};
series: {
minPointLength : minPointLength
}