Search code examples
javascriptgoogle-visualizationcolumn-chart

How to add links in google chart stacked column


I have been playing around with Google charts quite a bit over in the google charts play ground here

This is the code that shows the image below

<script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
      </script>
      <script type = "text/javascript">
         google.charts.load('current', {packages: ['corechart']});     
      </script>


      <div id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
      </div>
      <script language = "JavaScript">
         function drawChart() {
            var data = google.visualization.arrayToDataTable([
        ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
         'Western', 'Literature', { role: 'annotation' } ],
        ['2010', 10, 24, 20, 32, 18, 5, ''],
        ['2020', 16, 22, 23, 30, 16, 9, ''],
        ['2030', 28, 19, 29, 30, 12, 13, '']
      ]);

            var options = {
                            title: "h",
                            width: 600,

                            legend: { position: "none" },
                            isStacked:true
                        };

            var chart = new google.visualization.BarChart(document.getElementById('container'));
            chart.draw(data, options);
         }
         google.charts.setOnLoadCallback(drawChart);
      </script>

Chart Image

How can add a link clickable to each data?


Solution

  • Solution

    Handling Click and link Redirection

    <script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
          </script>
          <script type = "text/javascript">
             google.charts.load('current', {packages: ['corechart']});     
          </script>
    
    
          <div id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
          </div>
          <script language = "JavaScript">
             function drawChart() {
                var data = google.visualization.arrayToDataTable([
            ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
             'Western', 'Literature', { role: 'annotation' } ],
            ['2010', 10, 24, 20, 32, 18, 5, ''],
            ['2020', 16, 22, 23, 30, 16, 9, ''],
            ['2030', 28, 19, 29, 30, 12, 13, '']
          ]);
    
                var options = {
                                title: "h",
                                width: 600,
    
                                legend: { position: "none" },
                                isStacked:true
                            };
    
                var chart = new google.visualization.BarChart(document.getElementById('container'));
                chart.draw(data, options);
    			google.visualization.events.addListener(chart, 'select', selectHandler);
    			function selectHandler(e) {
    				  var selection = chart.getSelection();
    				  if (selection.length == true && selection[0].row != null) {
    					console.log(selection[0])
    					   //alert('You selected ' +data.getValue(selection[0].row, selection[0].column));
    					   //window.open(data.getValue(selection[0].row, 0));
    					    window.open('https://www.google.com/search?q='+data.getColumnLabel(selection[0].column)+':'+data.getValue(selection[0].row, selection[0].column));
    				   } 
    				}
             }
             google.charts.setOnLoadCallback(drawChart);
          </script>