Make the legend disappear in Highcharts - highcharts

I am using Highcharts and I am trying to make the legend to disappear and appear using a button, to save some screen real-estate.
I tried everything I could think of and all I managed to do is to make the SVG of the legend to hide and show using chart.legend.[group, nav, container].hide() but the white space which the legend takes never goes away. I even tried deleting the legend DOM element but the chart wouldn't resize (even calls to chart.reflow() don't help.
See here: http://jsfiddle.net/zbzzn/q83h5g4z/1/
Is there any way to make the legend disappear and reflow the chart so that the legend white space goes away?

Here's my sketch for a toggle method that sits directly on the Legend prototype. It avoids modifying the container width and height.
Highcharts.Legend.prototype.toggle = function () {
if (this.display) {
this.group.hide();
} else {
this.group.show();
}
this.display = !this.display;
this.chart.isDirtyBox = true;
this.chart.redraw();
};
Live demo at http://jsfiddle.net/highcharts/3Bh7b/76/

I managed to do a hack around it that works:
http://jsfiddle.net/q83h5g4z/4/
I really hope there is another way to do it without the hack.
if (!isVisible) {
originalLegendHeight = legend.legendHeight;
legend.options.maxHeight = -1; //0 doesn't work :( because someone did "if (maxHeight)" instead of "if (maxHeight === null)"
$('#container').height($('#container').height()-legend.legendHeight);
chart.redraw();
chart.reflow();
} else {
legend.options.maxHeight = null;
$('#container').height($('#container').height()+originalLegendHeight);
chart.redraw();
chart.reflow();
}

See this solution:
http://jsfiddle.net/3Bh7b/73/
var legend = chart.legend;
if(legend.display) {
legend.group.hide();
legend.display = false;
} else {
legend.group.show();
legend.display = true;
}

The final solution I reached is (where legendVisible determines the visibility of the legend) :
var legendElement = $(chart.container).find('.highcharts-legend');
if (legendElement.length > 0) {
if (chart.legend.options.enabled && legendVisible) {
legendElement.show();
chart.legend.options.maxHeight = chart.legend.options.originalMaxHeight || null;
chart.reflow();
} else {
legendElement.hide();
chart.legend.options.originalMaxHeight = chart.legend.options.maxHeight;
chart.legend.options.maxHeight = -1;
chart.reflow();
}
}

Related

How do I hide specific legend items?

I am trying to hide specific legend items on my graph (http://api.highcharts.com/highcharts/legend). If I loop through the chart.legend.allItems and try to change a specific items visible property it does not effect the legend at all.
$.each(chart.legend.allItems, function() {
this.visible = false;
});
How can I hide a specific legend item on my graph ?
I think this is being made out to be more complex than it needs to be.
You can set showInLegend to false in your config options for the series.
If you need to do it programmatically, you can use series.update() to accomplish it dynamically.
Reference:
http://api.highcharts.com/highcharts/plotOptions.series.showInLegend
http://api.highcharts.com/highcharts/Series.update
I found other solution in this question
var item = chart.series[1];
//hide serie in the graph
item.hide();
item.options.showInLegend = false;
item.legendItem = null;
chart.legend.destroyItem(item);
chart.legend.render();
EDIT:
Other solution for last version of highcharts:
$('#container').highcharts().series[1].update({ showInLegend: false });
You can do that with the load event - Highcharts Doc
chart: {
events: {
load: function() {
var myChart = this;
$.each(myChart.series, function(index, serie) {
if(index === 2 ) { // hide serie
serie.hide();
}
});
}
}
},
Here a fiddle
if you want to hide some labels you can add some css class to hide them according to some special feature of the series. I pass an example in which the series is hidden with empty data and also hide the label:
for (var i = 0; i < chart.series.length; i++) {
if (chart.series[i].dataMax === 0) {
chart.series[i].hide();
}
}
$("#charContent").find('.highcharts-legend-item-hidden').each(function () {
$(this).addClass('hidden');
});
In the css:
.highcharts-legend-item-hidden.hidden {
display: none;
}
Maybe it's not a very clean solution but it works for me

Two markers at the same position

2 markers at the same position how not display the tooltip of the hidden marker? When I pass over the visible markers.
map.on('pointermove', function(e) {
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
if (layer == layer_1) {
return feature;
}
});
map.getTarget().style.cursor = feature ? 'pointer' : '';
tooltip.style.display = feature ? '' : 'none';
if (feature){
overlay.setPosition(e.coordinate);
tooltip.innerHTML = 'marker hidden';
}
});
Can't just comment or I would since I only want to ask you: did you try using setStyle() instead to change the style? That usually forces the change event. I'm asking that because I had a similar issue here to hide/show features.

Titanium iOS table rows are clickable but shouldn't be

I am working on a Titanium Appcelerator project, a tabbed application with tables. These tables contain a gradient background with a label and an input field. The problem is that on iOS when the row is tapped in the label area, the background color of the row fades to black and then fades back in. This is an old app that I'm updating for the latest iOS devices and Titanium version. It didn't behave this way before, but I have no idea where the change might be. Here are some tidbits where these UI elements are created.
function createRow(obj){
var row;
if (Titanium.Platform.name == 'iPhone OS') {
obj.selectionStyle = Ti.UI.iPhone.TableViewCellSelectionStyle.NONE;
obj.height = 40;
obj.width = 200;
obj.touchEnabled = false;
row = Titanium.UI.createTableViewRow(obj);
} else if (Titanium.Platform.name == 'android'){
obj.touchEnabled = true;
obj.width = Titanium.Platform.displayCaps.platformWidth-8;
if(Titanium.Platform.displayCaps.platformWidth > 320){
obj.height= getAndroidNumbers(40,70);
} else {
obj.height= 40;
}
row = Titanium.UI.createView(obj);
var row2 = Titanium.UI.createView(obj);
row.add(row2);
row2.addEventListener('click',function(e){
Ti.UI.Android.hideSoftKeyboard();
});
row2.addEventListener('swipe',function(e){
Ti.UI.Android.hideSoftKeyboard();
});
row2.addEventListener('touchmove',function(e){
Ti.UI.Android.hideSoftKeyboard();
});
}
return row;
}
Any ideas would be greatly appreciated.
I think what you are talking about is the highlight option of the tableview.
on iPhone to set the highlight (or disable it) you would use the selectionStyle of the tableview. so to disable it you would do:
selectionStyle: Ti.UI.iPhone.TableViewCellSelectionStyle.NONE
on Android try this:
tableView :{
backgroundColor:'transparent',
separatorStyle:'none',
selectionStyle:'none',
separatorColor : 'transparent',
}

How do I stop my fixed navigation from moving like this when the virtual keyboard opens in Mobile Safari?

I understand that mobile safari has a lot of bugs around fixed elements, but for the most part I've managed to get my layout working correctly until I added a much needed text input to the fixed navigation at the bottom. Now when the user focuses on the text input element and the virtual keyboard appears, my navigation, which is otherwise always fixed at the bottom of the page, jumps up to a really strange spot in the middle of the page.
I'd add some of my code to this post, but I wouldn't be sure where to start. That navigation is fixed at the bottom and positioned to the left and bottom 0, and 100% width. From there, I don't know what's going on, I can only assume it's a mobile safari bug.
It also appears to lose it's position fixed and become relative, only while the text input element is focused on and the virtual keyboard is open.
http://dansajin.com/2012/12/07/fix-position-fixed/ this is one of the solutions proposed. Seems worth a shot.
In short: set fixed elements to position:absolute when any input is focused and reset them when that element is blurred
.header {
position: fixed;
}
.footer {
position: fixed;
}
.fixfixed .header,
.fixfixed .footer {
position: absolute;
}
and
if ('ontouchstart' in window) {
/* cache dom references */
var $body = $('body');
/* bind events */
$(document)
.on('focus', 'input', function() {
$body.addClass('fixfixed');
})
.on('blur', 'input', function() {
$body.removeClass('fixfixed');
});
}
The solutions on the top are some ways to go and fix the problem, but I think adding extra css class or using moderniz we are complicating things.If you want a more simple solution, here is a non-modernizr non-extra-css but pure jquery solution and work on every device and browsers I use this fix on all my projects
if ('ontouchstart' in window) {
$(document).on('focus', 'textarea,input,select', function() {
$('.navbar.navbar-fixed-top').css('position', 'absolute');
}).on('blur', 'textarea,input,select', function() {
$('.navbar.navbar-fixed-top').css('position', '');
});
}
I had a similar problem, but I found a workaround by adding the following css class to the body element on input focus and then removing it again on unfocus:
.u-oh {
overflow: hidden;
height: 100%;
width: 100%;
position: fixed;
}
Taking from what sylowgreen did, the key is to fix the body on entering the input. Thus:
$("#myInput").on("focus", function () {
$("body").css("position", "fixed");
});
$("#myInput").on("blur", function () {
$("body").css("position", "static");
});
Add javascript like this:
$(function() {
var $body;
if ('ontouchstart' in window) {
$body = $("body");
document.addEventListener('focusin', function() {
return $body.addClass("fixfixed");
});
return document.addEventListener('focusout', function() {
$body.removeClass("fixfixed");
return setTimeout(function() {
return $(window).scrollLeft(0);
}, 20);
});
}
});
and add class like this:
.fixfixed header{
position: absolute;
}
you can reference this article: http://dansajin.com/2012/12/07/fix-position-fixed/
I really like the solution above. I packaged it up into a little jQuery plugin so I could:
Set which parent gets the class
Set which elements this applies to (don't forget "textarea" and "select").
Set what the parent class name is
Allow it to be chained
Allow it to be used multiple times
Code example:
$.fn.mobileFix = function (options) {
var $parent = $(this),
$fixedElements = $(options.fixedElements);
$(document)
.on('focus', options.inputElements, function(e) {
$parent.addClass(options.addClass);
})
.on('blur', options.inputElements, function(e) {
$parent.removeClass(options.addClass);
// Fix for some scenarios where you need to start scrolling
setTimeout(function() {
$(document).scrollTop($(document).scrollTop())
}, 1);
});
return this; // Allowing chaining
};
// Only on touch devices
if (Modernizr.touch) {
$("body").mobileFix({ // Pass parent to apply to
inputElements: "input,textarea,select", // Pass activation child elements
addClass: "fixfixed" // Pass class name
});
}
I use this jQuery script:
var focus = 0;
var yourInput = $(".yourInputClass");
yourInput.focusin(function(){
if(!focus) {
yourInput.blur();
$("html, body").scrollTop($(document).height());
focus = 1;
}
if(focus) {
yourInput.focus();
focus = 0;
}
});
Works perfectly for me.
The focusin and focusout events seem to be better suited to this problem than the focus and blur events since the former bubble up to the root element. See this answer on SO.
Personally I use AngularJS, so I implemented it like this:
$window.document.body.addEventListener('focusin', function(event) {
var element = event.target;
var tagName = element.tagName.toLowerCase();
if(!$rootScope.inputOverlay && (tagName === 'input' || tagName === 'textarea' || tagName === 'select')) {
$rootScope.$apply(function() {
$rootScope.inputOverlay = true;
});
}
});
$window.document.body.addEventListener('focusout', function() {
if($rootScope.inputOverlay) {
$rootScope.$apply(function() {
$rootScope.inputOverlay = false;
});
}
});
Note: I am conditionally running this script if this is mobile Safari.
I put an ng-class attribute on my navbar:
<div class="navbar navbar-default navbar-fixed-top" ng-class="{'navbar-absolute': inputOverlay}">
using the following CSS:
.navbar-absolute {
position: absolute !important;
}
You can read more about focusin here and focusout here.
Test this one. It works. I just test it.
$(document).on('focus','input', function() {
setTimeout(function() {
$('#footer1').css('position', 'absolute');
$('#header1').css('position', 'absolute');
}, 0);
});
$(document).on('blur','input', function() {
setTimeout(function() {
$('#footer1').css('position', 'fixed');
$('#header1').css('position', 'fixed');
}, 800);
});
None of these solutions worked for me because my DOM is complicated and I have dynamic infinite scroll pages, so I had to create my own.
Background: I am using a fixed header and an element further down that sticks below it once the user scrolls that far down. This element has a search input field. In addition, I have dynamic pages added during forward and backwards scroll.
Problem: In iOS, anytime the user clicked on the input in the fixed element, the browser would scroll all the way to the top of the page. This not only caused undesired behavior, it also triggered my dynamic page add at the top of the page.
Expected Solution: No scroll in iOS (none at all) when the user clicks on the input in the sticky element.
Solution:
/*Returns a function, that, as long as it continues to be invoked, will not
be triggered. The function will be called after it stops being called for
N milliseconds. If `immediate` is passed, trigger the function on the
leading edge, instead of the trailing.*/
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
function is_iOS() {
var iDevices = [
'iPad Simulator',
'iPhone Simulator',
'iPod Simulator',
'iPad',
'iPhone',
'iPod'
];
while (iDevices.length) {
if (navigator.platform === iDevices.pop()) { return true; }
}
return false;
}
$(document).on("scrollstop", debounce(function () {
//console.log("Stopped scrolling!");
if (is_iOS()) {
var yScrollPos = $(document).scrollTop();
if (yScrollPos > 200) { //200 here to offset my fixed header (50px) and top banner (150px)
$('#searchBarDiv').css('position', 'absolute');
$('#searchBarDiv').css('top', yScrollPos + 50 + 'px'); //50 for fixed header
}
else {
$('#searchBarDiv').css('position', 'inherit');
}
}
},250,true));
$(document).on("scrollstart", debounce(function () {
//console.log("Started scrolling!");
if (is_iOS()) {
var yScrollPos = $(document).scrollTop();
if (yScrollPos > 200) { //200 here to offset my fixed header (50px) and top banner (150px)
$('#searchBarDiv').css('position', 'fixed');
$('#searchBarDiv').css('width', '100%');
$('#searchBarDiv').css('top', '50px'); //50 for fixed header
}
}
},250,true));
Requirements: JQuery mobile is required for the startsroll and stopscroll functions to work.
Debounce is included to smooth out any lag created by the sticky element.
Tested in iOS10.
I wasn't having any luck with the solution proposed by Dan Sajin. Perhaps the bug has changed since he wrote that blog post, but on iOS 7.1, the bug will always surface when the position is changed back to fixed after the input is blurred, even if you delay until the software keyboard is hidden completely. The solution I came to involves waiting for a touchstart event rather than the blur event since the fixed element always snaps back into proper position when the page is scrolled.
if (Modernizr.touch) {
var $el, focused;
$el = $('body');
focused = false;
$(document).on('focus', 'input, textarea, select', function() {
focused = true;
$el.addClass('u-fixedFix');
}).on('touchstart', 'input, textarea, select', function() {
// always execute this function after the `focus` handler:
setTimeout(function() {
if (focused) {
return $el.removeClass('u-fixedFix');
}
}, 1);
});
}
HTH

Issue with ios hover event

I am currently having issues making iPad work with a :hover event. To clear up what i mean i have uploaded part of the website at http://playing.everythingcreative.co.uk and I have 3 images, that when hovered over a div disappears to show text underneath but this does not work on the iPad at all. I tried:
ontouchstart="touchStart(event);"
But i don't know enough on how it works to use it right.
Any help would be great.
I figured it out anyway using the example on iOS automatic hover fix? and changing:
if(navigator.platform == "iPad") {
to:
if ("ontouchstart" in document.documentElement) {
The final code:
$(document).ready(function() {
if ("ontouchstart" in document.documentElement) {
$("div").each(function() { // have to use an `each` here - either a jQuery `each` or a `for(...)` loop
var onClick; // this will be a function
var firstClick = function() {
onClick = secondClick;
return false;
};
var secondClick = function() {
onClick = firstClick;
return true;
};
onClick = firstClick;
$(this).click(function() {
return onClick();
});
});
}
});

Resources