Add image to google chart - Javascript Google Chart

Javascript examples for Google Chart:Chart Configuration

Description

Add image to google chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Area Chart Example</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script> 
      <div id="chart_div" style="width: 900px; height: 500px;"></div> 
      <script type="text/javascript">
      google.setOnLoadCallback(drawChart);
      function drawChart() {/*w w  w .  j a  v a 2 s. com*/
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2013',  1000,      400],
          ['2014',  1170,      460],
          ['2015',  660,       1120],
          ['2016',  1030,      540]
        ]);
        var options = {
          title: 'Company Performance',
          hAxis: {title: 'Year',  titleTextStyle: {color: '#333'}},
          vAxis: {minValue: 0}
        };
        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
          var g_collection = document.getElementsByTagName('g');
          var g_array = Array.prototype.slice.call( g_collection, 0 );
          var hAxis = g_array.filter(function(g){
              return g.logicalname && g.logicalname.indexOf("hAxis") > -1 && g.logicalname.indexOf("title") === -1
          });
          hAxis.forEach(function(g, index){
               var image = document.createElementNS("http://www.w3.org/2000/svg", "image");
               var x = g.childNodes[0].getAttribute('x');
               var y = g.childNodes[0].getAttribute('y');
               image.setAttributeNS(null, "x", x - 25);
               image.setAttributeNS(null, "y", y - 70);
               image.setAttributeNS("http://www.w3.org/1999/xlink", "href", "https://cdn3.iconfinder.com/data/icons/location-map/512/pin_marker_star-512.png");
               image.setAttributeNS(null, "height", "50px");
               image.setAttributeNS(null, "width", "50px");
               image.setAttributeNS(null, "style","cursor:pointer");
               image.addEventListener("mousedown", function(e){
                   console.log("mouse down on marker"+index);
               });
               g.appendChild(image);
          });
      }

      </script>  
   </body>
</html>

Related Tutorials