markmarkoh / datamaps

Customizable SVG map visualizations for the web in a single Javascript file using D3.js
http://datamaps.github.io
MIT License
3.78k stars 1.01k forks source link

Only one map instance being displayed #242

Open vishal-kony opened 9 years ago

vishal-kony commented 9 years ago

I have 2 <div> (one remains hidden unless I click a button). The 2 <div> are getting populated with the map however, one of the map has its <svg> tag with 0 width and height (please see the image)

2015-11-12_1914

why is this happening? I have tried swapping the two <div> but then I can see world map but not the US map. Any resolution to this?

markmarkoh commented 9 years ago

@vishal-kony Datamaps tries to maximize it's size basic on it's container <div>. If that <div> is not visible, the width/height will be 0.

To offset this you can specify a height and width when you create the map, like:

var map = new Datamap({
  element: document.getElementById('world_map'), 
  height: 500,
  width: 500
})

Can you give that a shot and see if the tabs work better?

vishal-kony commented 9 years ago

@markmarkoh Thanks for a quick reply. I can now see the maps properly on the tabs, But upon window re-size, The map that is currently active gets re-sized but, upon switching the tab, I cant see the other map. Is it because i have a common map re-size for both the maps? Can you confirm on that?

vishal-kony commented 8 years ago

Here's my map generating code. Common for both the US and world map. As mentioned before the re-size here re-sizes only the map that is currently active. The other map disappears on resize

var map = new Datamap({
        scope: scope,
        element: document.getElementById(div_id),
        height: 250,
        width: document.getElementById('usa_map').offsetWidth,
        responsive: true,
        fills: {
            'low': '#80CBC4',
            'medium': '#26A69A',
            'high': '#00897B',
            defaultFill: '#E0F2F1'
        },
        data: goodToGoData,
        geographyConfig: {
            highlightBorderColor: '#ff0027',
            popupTemplate: function (geography, data) {
                var initial = 0;
                if (data != null) {
                    initial = data.value;
                }
                return '<div class="hoverinfo"><strong>' + geography.properties.name + '</strong><br>' + 'Sessions : ' + initial + '</div>';
            },
            highlightBorderWidth: 2
        }
    });

    window.addEventListener('resize', function () {
        map.resize();
    });