How to auto-format dates in custom tooltip formatter? - highcharts

Highcharts does a great job auto-formatting dates both on the x-axis and in tooltips. Unfortunately I need a custom function to format my y-values, and /plotOptions/line/tooltip/pointFormat accepts only a format string and not a function, so I have to set /tooltip/formatter instead. How do I get hold of the formatted date (e.g. Oct'13 or 20. Oct) as shown on the x axis? I don't seem to have access to point.key from there, only the raw millis value.
http://jsfiddle.net/9Fke4/

You can use dateFormat()
tooltip: {
formatter: function() {
return '<b>' + Highcharts.dateFormat('%b\' %d',this.x) + ':</b> ' + this.y;
}
},
http://jsfiddle.net/9Fke4/1/

FWIW, this was answered in a Highcharts issue on Github
formatter: function() {
var tooltip = this.series.chart.tooltip,
item = this.point.getLabelConfig(),
header = this.series.chart.tooltip.tooltipFooterHeaderFormatter(item);
return header + this.y;
}
Corresponding fiddle:
http://jsfiddle.net/9Fke4/12/
If you are using a shared series:
tooltip: {
formatter: function (tooltip) {
var header,
s = [];
$.each(this.points, function(i, point) {
if(header == null) {
var config = point.point.getLabelConfig();
header = tooltip.tooltipFooterHeaderFormatter(config);
}
s.push(point.y);
});
return header + s.reverse().join('<br>');
},
shared: true
}

conver the raw milliseconds to a date object using
var currentDate = new Date (tome in milliseconds)
in the tootip/formatter function you have defined.
this will give you good control over the date. you can use currentDate.getMonth(), getDate(), getDay(), etc methods to get the information you want from that date.
build a string with the above info and return.
I hope this will help you.

The solution I ended up with is to pre-format the y-values and store them as part of the series data; the pre-formatted values can then be referenced from the tooltip headerFormat and pointFormat, where they can be used along with {point.key}, which contains the auto-formatted date (and which is not available when providing a custom tooltip formatter):
http://jsfiddle.net/uG3sv/

Related

Highchart bubble-chart : Show Y-Axis value as numeric range

I have one Bubble Highchart and need to display Y-Axis price value as price range like '1-10', 11-20', '21-30'
But I am not able to achieve the same.
How can I achieve this?
Hello ppotaczek, Thank you very much for your quick response. I am trying achieve the bubble chart with below screen-shot.
Please refer this screen-shot
Thanks
You can simply use formatter function:
yAxis: {
labels: {
tickInterval: 25,
formatter: function() {
if (this.value > 0) {
return this.value - 24 + ' - ' + (this.value);
}
return this.value;
}
},
}
Live demo: https://jsfiddle.net/BlackLabel/3rqdn5pj/
API Reference:
https://api.highcharts.com/highcharts/yAxis.tickInterval
https://api.highcharts.com/highcharts/yAxis.labels.formatter

Highcharts rangeSelector: is there a formatter function callback for the inputData fields?

I'm using Highcharts and need to chart data with quarterly frequency using a format like 'yyyy Q#'.
I found how to format the periods in the tooltip (using a JS formatter function) and would now like to use the same format in the rangeSelector 'From' and 'To' input fields.
From the documentation I see I could use 'inputDataFormat', but that has the standard PHP strftime formatter function, which does not cover my requirement.
Any idea appreciated!
Massimo
It is possible to extend Highcharts format. You need to define Highcharts.dateFormats object:
Highcharts.dateFormats = {
Q: function(timestamp) {
var date = new Date(timestamp),
y = date.getFullYear(),
m = date.getMonth();
return y + '.Q' + (Math.floor(m / 3) + 1);
}
};
And then you can use 'Q' to format timestamp.
rangeSelector: {
inputEnabled: true,
inputDateFormat: '%Q'
},
example: http://jsfiddle.net/twcu0djj/

Pie chart legend shows empty values

Using dot net highcharts and the label formatter to only display certain items in the legend, it displays values that have been returned as ''.
.SetLegend(new Legend { Enabled = true, LabelFormatter = "function() { if (this.y >= 5) { return this.name; } else { return ''; } }" })
You can disable default legend and create your own as HTML. Then you can control which point should be or not displayed.
Example: http://jsfiddle.net/N3KAC/1/
$legend = $('#customLegend');
$.each(chart.series[0].data, function (j, data) {
$legend.append('<div class="item"><div class="symbol" style="background-color:'+data.color+'"></div><div class="serieName" id="">' + data.name + '</div></div>');
});
$('#customLegend .item').click(function(){
var inx = $(this).index(),
point = chart.series[0].data[inx];
if(point.visible)
point.setVisible(false);
else
point.setVisible(true);
});
The formatter only formats the text that gets displayed - it does not determine whether or not there is a legend entry for a series.
What you need is the showInLegend property, and you will need to run your check on the series object, not on the legend.
Reference:
http://api.highcharts.com/highcharts#plotOptions.series.showInLegend

Showing tooltip with two or more stack columns

When I select a column, I want to show a tool-tip with only the values for the specific stack selected, but I'm showing all values for the two stacks (2012 and 2013).
Can I configure the tool-tip to show only one stack value?
If I cannot do it, how can I show the stack label into the tool-tip(2012 or 2013)?
I tried to use point.series.stack, but I'm getting a undefined value. Is it possible?
Code to show a tooltip over a stacked chart:
tooltip: {
formatter: function() {
var s = '<b>'+ this.x +'</b>';
$.each(this.points, function(i, point) {
s += '<br/>'+ point.series.name +': '+point.series.stack+':'+ point.y +'m';
});
return s;
},
shared : true
}
The complete code
Thanks for your help.
Hi again
I followed your advice in order to show only the values for the same stack.
I have this formatter code:
tooltip: {
formatter: function() {
var tip = '<b>'+ this.x ;
var stackSelected = this.point.series.options.stack;
tip += '/'+stackSelected+'</b>';
$.each(this.series.chart.series, function(i, s) {
if(s.options.stack == stackSelected){
tip += '<br/><br/>';
tip += '<b>'+s.name+' : </b>'+s.yAxis;
}
});
return tip;
},
shared : false
}
But I have problems in order to get the value for each serie.
Could you help me on this please?
Thanks in advance
I forgot the complete code: http://jsfiddle.net/Kqumw/4/
I can get that i want
Thanks for your tips.
This is my tooltip code:
tooltip: {
formatter: function() {
var tip = '<b>'+ this.x ;
var stackSelected = this.point.series.options.stack;
var categorySelected = this.point.category;
tip += '/'+stackSelected+'</b>';
tip += '<br/><br/>';
var index = 0;
$.each(this.series.chart.series, function(i, s) {
if(s.options.stack == stackSelected){
$.each(s.data, function(j, point){
if(point.category == categorySelected)
tip += '<b>'+s.name+' : </b>'+point.y+'<br>';
});
}
});
return tip;
},
shared : false
}
This is the complete code http://jsfiddle.net/Kqumw/5/
It's not possible to show different tooltip for the same category when shared is set for tooltip. If you want to show stack in tooltip, use point.series.options.stack, see: http://jsfiddle.net/Kqumw/1/
If you want to show only one stack (full stack for hovered point), then disable shared (shared: false) and find on your own corresponding points for the same category and the same stack (by comparing category index/name from points and stack id from series).

HighCharts - number format with $ in its short form

I'm using dotnet.highcharts to generate a chart.
If I do not set any label formatting on the y-axis labels, this is what is displayed.
This is good.
So 200M = 200,000,000
And it looks like this is done automatically.
If I wanted to put a $ in front of the value and I use:
function() { return '$' + Highcharts.numberFormat(this.value, 0) ; }
Then the label will now display $200,000,000.
How would i get the format to display it with the short form with the dollar sign like $200M ?
thanks.
Unfortunately in this case, you have to either take the pre formatted label, or rebuild the level of abbreviation you want in the formatter yourself.
In this case, you could something like
yAxis: {
labels: {
formatter: function() {
return '$'+this.value / 1000000 + 'M';
}
}
},
According to this stackoverflow post, you can call the original formatter with:
this.axis.defaultLabelFormatter.call(this);
Put this code before call the chart
Highcharts.setOptions({
lang: {
thousandsSep: ','
}
});

Resources