0

I have a google map where different cities are marked with a bar chart over them displaying the percentage of foods sold at a particular city/town.

On each of this city I need a bar chart showing these statistics. The "bar chart icon" should be slightly bigger so as to be visible. Each Bar chart should be drawn based on the real numbers related to that particular city.

Currently I am trying to "convert" the drawn chart to a png image "on the fly" and then planning to overlay that image onto google maps. But the code is not working.

1)Is it possible to make this approach work? My ultimate objective is to overlay real bar chart on a google map. Is there any other way to achieve this functionality. 2) The converted png image needs to have a transparent background.

Thanks. The code is:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Adding a Custom Overlay</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
         #chart_div {
        display: none;
  height:100px;
  width: 100px;
  background-color: #00f; 
  margin: 0px; 
  padding: 0px;
      }
    </style>
 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBGCabKYKRUI0-5zvdIcA_mRMImMecdNKM"></script>
 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script>
      
         // Load the Visualization API and the corechart package.
      google.charts.load('current', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.charts.setOnLoadCallback(initMap);
   // This example creates a custom overlay called USGSOverlay, containing
      // a U.S. Geological Survey (USGS) image of the relevant area on the map.

      // Set the custom overlay object's prototype to a new instance
      // of OverlayView. In effect, this will subclass the overlay class therefore
      // it's simpler to load the API synchronously, using
      // google.maps.event.addDomListener().
      // Note that we set the prototype to an instance, rather than the
      // parent class itself, because we do not wish to modify the parent class.

   
      var overlay;
      USGSOverlay.prototype = new google.maps.OverlayView();

      // Initialize the map and the custom overlay.

      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 11,
          center: {lat: 62.323907, lng: -150.109291},
          mapTypeId: 'satellite'
        });

        var bounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(62.281819, -150.287132),
            new google.maps.LatLng(62.400471, -150.005608));

        // The photograph is courtesy of the U.S. Geological Survey.
        var srcImage = 'https://developers.google.com/maps/documentation/' +
            'javascript/examples/full/images/talkeetna.png';

         // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
          ['Mushrooms', 45],
          ['Onions', 9],
          ['Olives', 11],
          ['Zucchini', 15],
          ['Pepperoni', 20]
        ]);

        // Set chart options
        var options = {'title':'How Much Pizza I Ate Last Night',
                       'width':"100px",
                       'height':"100px"};

 /*       // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      //  chart.draw(data, options);
    
 */

  var chart_div = document.getElementById('chart_div');
      var chart = new google.visualization.ColumnChart(chart_div);
   
     google.visualization.events.addListener(chart, 'ready', function () {
     chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
        console.log(chart_div.innerHTML);
  console.log( chart.getImageURI());
       // The custom USGSOverlay object contains the USGS image,
        // the bounds of the image, and a reference to the map.
       // overlay = new USGSOverlay(bounds, chart.getImageURI(), map);
    overlay = new USGSOverlay(bounds, chart.getImageURI(), map);
      });
  
     chart.draw(data, options);
      }

 
      /** @constructor */
      function USGSOverlay(bounds, image, map) {

        // Initialize all properties.
        this.bounds_ = bounds;
        this.image_ = image;
        this.map_ = map;

        // Define a property to hold the image's div. We'll
        // actually create this div upon receipt of the onAdd()
        // method so we'll leave it null for now.
        this.div_ = null;

        // Explicitly call setMap on this overlay.
        this.setMap(map);
      }

      /**
       * onAdd is called when the map's panes are ready and the overlay has been
       * added to the map.
       */
      USGSOverlay.prototype.onAdd = function() {

        var div = document.createElement('div');
        div.style.borderStyle = 'none';
        div.style.borderWidth = '0px';
        div.style.position = 'absolute';

        // Create the img element and attach it to the div.
        var img = document.createElement('img');
        img.src = this.image_;
        img.style.width = '100%';
        img.style.height = '100%';
        img.style.position = 'absolute';
        div.appendChild(img);

        this.div_ = div;

        // Add the element to the "overlayLayer" pane.
        var panes = this.getPanes();
        panes.overlayLayer.appendChild(div);
      };

      USGSOverlay.prototype.draw = function() {

        // We use the south-west and north-east
        // coordinates of the overlay to peg it to the correct position and size.
        // To do this, we need to retrieve the projection from the overlay.
        var overlayProjection = this.getProjection();

        // Retrieve the south-west and north-east coordinates of this overlay
        // in LatLngs and convert them to pixel coordinates.
        // We'll use these coordinates to resize the div.
        var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
        var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());

        // Resize the image's div to fit the indicated dimensions.
        var div = this.div_;
        div.style.left = sw.x + 'px';
        div.style.top = ne.y + 'px';
        div.style.width = (ne.x - sw.x) + 'px';
        div.style.height = (sw.y - ne.y) + 'px';
      };

      // The onRemove() method will be called automatically from the API if
      // we ever set the overlay's map property to 'null'.
      USGSOverlay.prototype.onRemove = function() {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
      };

      google.maps.event.addDomListener(window, 'load', initMap);
    </script>
  </head>
  <body>
    <div id="chart_div"></div>
 
 <div id="map"></div>
 
  </body>
</html>
nihal
  • 357
  • 1
  • 3
  • 18
  • Can you put it together as a jsfiddle or codepen. It would be easy to debug and fix. – Farrukh Subhani Apr 26 '17 at 10:33
  • ok. I will try to put this on jsfiddle. – nihal Apr 27 '17 at 08:03
  • I am now able to convert the chart to an image, overlay the image onto google maps. However, There are issues that i need to solve. 1) I need to be able to resize this image\div so that it appears more like an icon. I am not able to change the div dimensions by changing css height, width 2) The image's white background need to be transparent. How to convert this image to a transparent image. Any help will be greatly appreciated. – – nihal Apr 27 '17 at 10:01
  • The resize code is now working fine. The only part left is "Making the background Transparent". – nihal Apr 27 '17 at 11:38
  • I need "chart.getImageURI()" to create an image that has transparent background. Currently I am getting an image that has white background. Any help/sample code in this regard will be much appreciated. – nihal Apr 27 '17 at 14:54

1 Answers1

0

Please see this for your solution to transparency on google charts Google Charts (JS) - is there a way of using a transparent background on a chart?

// In Chart options include {'backgroundColor': 'transparent'}

The http://jsfiddle.net/farrukhsubhani/d2ruuhgu/ link is something that has been forked from one of the answers to make sure I can refer back to it. Credit still goes to original answer and authors.

Community
  • 1
  • 1
Farrukh Subhani
  • 2,018
  • 1
  • 17
  • 25
  • Farrukh, Thanks so much mate! Adding backgroundColor 'transparent' line to the chart options did the trick. Now the image that is generated has a transparent background. – nihal Apr 27 '17 at 14:59