show/hide series vanishes styling of datalable - highcharts

In my chart I am combining a class together with datalables of each series.
At some point I am hiding datalables of one series. But If now I show/hide any other series, the hided datalables appears.
I made a jsfiddle example. Do the following steps to see my meaning:
1. click on button 1 to hide datalables on series1
2. Hide series 2
Now the datalables on series 1 shows up. How can I stop this behavior?
jsfiddle link: http://jsfiddle.net/jmogexfj/2/
$(function () {
button1();
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
useHTML: true,
formatter: function () {
return '<div class="label_'+this.series._i+'">' + this.y + '</div>';
}
}
}
},
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]
},
{
data: [10.9, 50.5, 60.4, 100.2, 104.0, 106.0, 120.6, 108.5, 106.4, 104.1, 90.6, 50.4]
}]
});
});
function button1(){
$("#b1").click(function(){
$(".label_0").toggle();
});
}

That's how Highcharts library works. When you update chart (like hiding by legend, resizing window browser etc.), elements are redrawn.
To hide dataLabels, use series.update():
$('#container').highcharts().series[0].update({
dataLabels: {
enabled: false
}
});
Demo: http://jsfiddle.net/jmogexfj/4/
If you really need to use classes, then use chart.events.redraw to show/hide dataLabel on each redraw: http://jsfiddle.net/jmogexfj/5/
Highcharts example:
chart: {
events: {
redraw: function () {
var showOrHide = display ? 'show' : 'hide';
$(".label_0")[showOrHide]();
}
}
},
Click button:
function button1() {
$("#b1").click(function () {
display = !display;
$(".label_0").toggle();
});
}

Related

How can I apply different background color to entire tooltip for different series in highcharts

I would to give different backgroundcolor to entire tooltip for different series. I checked highcharts api document but their is no background color option in series.tooltip = {}.
Can you please suggest some way or alternative for this?
I checked this question - Changing backgroundcolor of tooltip for a specific data point in Highcharts
My question is similar to this one.
ex. I want to apply red color to a series through formatter and not want to honor yellow background color given in tooltip options.
please check this fiddle - http://jsfiddle.net/eoqdcxn4/1/
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
tooltip: {
useHTML:true,
backgroundColor:"yellow",
formatter: function() {
console.log(this);
if(this.point.customBck)
return '<div style="background-color:red;">The value for <b>'+ this.x + '</b> is <b>'+ this.y +'</b></div>';
else
return '<div>The value for <b>'+ this.x + '</b> is <b>'+ this.y +'</b></div>';
}
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [{
y:29.9,
customBck: true
}, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
Thanks in advance.
You can change tooltip background color in the refresh event depending on the hovered series, for example:
(function(H) {
H.addEvent(H.Tooltip, 'refresh', function(e) {
var series = this.chart.series.find(function(s) {
return s.isHovered;
});
this.label.attr({
fill: series.options.tooltipColor
});
});
}(Highcharts));
$(function() {
var chart = new Highcharts.Chart({
...,
plotOptions: {
series: {
events: {
mouseOver: function() {
this.isHovered = true;
},
mouseOut: function() {
this.isHovered = false;
}
}
}
},
series: [{
tooltipColor: 'red',
...
}, {
tooltipColor: 'yellow',
...
}]
});
});
Live demo: http://jsfiddle.net/BlackLabel/ojqews35/
API Reference: https://api.highcharts.com/highcharts/plotOptions.series.events
Docs: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts

Value or Logic Dependent Data Label format in Highcharts

Is there a solution in Highcharts to format a Label depending on the value?
I know there are color zones option for the dependent formatting of columns based on the value of the data. For example, if the value is bigger than 10 the column turns red, else remains the base color.
I need to get very similar function on the dataLabels. If the value is bigger than 10 turn the label background to red. If this is possible do exist a solution to dynamically get this specific value from the data series, or only can be/ should be a static setting?
Values bigger than 100 should have red dataLabel background color, and the rest (smaller) green background color:
https://jsfiddle.net/saboarpad/6bow3mx1/3/
Highcharts.chart('container', {
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
backgroundColor:'red',
color:'white'
}
}
},
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]
}]
});
You can edit data labels style after they have been generated:
chart: {
events: {
load: function() {
var points = this.series[0].points;
points.forEach(function(point) {
if (point.y > 100) {
point.dataLabel.text.css({
'color': 'white',
'background-color': 'red'
});
}
});
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/h64gjL2t/
Also, you can individually format dataLabel for each point: https://jsfiddle.net/BlackLabel/4k8p6mfg/
series: [{
data: [{
x: 0,
y: 29.9,
dataLabels: {
backgroundColor: 'red',
color: 'white'
}
},
71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4
]
}]
API: https://api.highcharts.com/highcharts/series.line.data.dataLabels
I find a similar solution, but not clear the use of the code.
In the dataLabels nod there is this "formatter" function which defines the color code based on the this.y value, so here We can do some compare of data: which one is bigger or smaller, and the function returns the color code and concatenates into a HTML span elements inline style.
However, everything is work until is use the plain "color" tag, but this is for the color of the fonts. I needed to change the a background as well. So I changed the color tag to:
1. Highcharts own tag: "backgroundColor" -- nothing happens
2. HMTL back ground color tag: "background-color" -- nothing happens
:( Im feel kind of lost.
Highcharts.chart('container', {
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
color: 'blue',
formatter: function() {
var color = this.y > 100 ? 'red': 'green'
return '<span style="color: ' + color + '">' + this.y + '</span>';
}
}
}
},
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]
}]
});

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/

Can not exporting renderer shapes added in callback function in highcharts / highstock

I want to dynamically added several primitive shapes like text and images into highcharts / highstock.
Please see this fiddle.
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
spacingBottom: 50
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
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]
}]
}, function(chart) { // on complete
$("#add").click(function(){
chart.renderer.image('http://highcharts.com/demo/gfx/sun.png', 100, 100, 30, 30)
.add();
});
});
});
After click button "Add Image", there is a image will be added to chart. Unfortunately, those dynamically added elements can not be exported to images.
Is there any possible solution to fix this?
You can use chart.exportChart() function to achieve that: http://jsfiddle.net/B6s7V/38/
The same solution as here.
$("#export").click(function () {
chart.exportChart(null, {
chart: {
events: {
load: function () {
this.renderer.image('http://highcharts.com/demo/gfx/sun.png', 100, 100, 30, 30).add();
}
}
}
});
});
When you export a chart, the chart SVG is regenerated from the original .Chart call. If you want to add things after the chart has drawn, do it in the charts:events:load callback.
chart: {
renderTo: 'container',
spacingBottom: 50,
events: {
load: function(){
this.renderer.image('http://highcharts.com/demo/gfx/sun.png', 100, 100, 30, 30).add();
}
}
},
See updated fiddle here.

How to add custom exporting button in highCharts?

We are using highcharts in our application and I want to add an button next to print and export button. I do have working example in jsfiddle
var chart1;
$(document).ready(function () {
// Add Custom button to highchart
chart1 = new Highcharts.Chart({
chart: {
renderTo: 'container1',
shadow: true
},
title: { text: 'Sales' },
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: { title: { text: '$'} },
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]
}],
exporting: {
buttons: {
'exportTo': {
_id: 'exportTo',
symbol: 'diamond',
x: -62,
symbolFill: '#B5C9DF',
hoverSymbolFill: '#779ABF',
onclick: function () {
showDialog(this);
}
}
}
}
});
var showDialog = function (selectedChart) {
var modal = $("#myModal").modal();
$('#btnSaveImage').click(function () {
saveChartInNas(selectedChart);
modal.modal('hide');
});
};
});
We have lots of charts, on click of the chart I want to display a modal window and do some process on save button. Instead of creating the modal and exporting property settings for all charts in all the different pages I am wondering is it possible to some kind of plugin or add on ? If so, can someone show me how to achieve this.
I do not want to add exporting logic in all the pages.
Thank you
exporting: {
filename: 'event-id-metadata-graph',
buttons: {
contextButton: {
menuItems: [{
text: 'Download PDF',
onclick: function () {
this.exportChart({
type: 'application/pdf'
});
}
}, {
text: 'Print',
onclick: function () {
alert('Launch Print Table function')
},
separator: false
}]
}
}
},
I think you are asking whether there is a way to set this up as some sort of default, instad of editing every chart definition ?
You should take a look at Highcharts.setOptions() http://api.highcharts.com/highcharts#Highcharts.setOptions()
This allows you to set up defaults for all charts.

Resources