Insert Links into Google Charts api data?

后端 未结 6 1193
日久生厌
日久生厌 2020-12-10 13:08

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

Link

The code I have been playing with is this:

6条回答
  •  天命终不由人
    2020-12-10 13:56

    Since the SVG-embedding route is (understandably) too hairy for you to want to muck with, let's try a completely different approach. Assuming that you have the flexibility to alter your functional specification a bit, such that the bars are clickable, not the labels, then here's a much simpler solution that will work.

    Look for the alert in this snippet, that's the part that you will customize to do the redirect.

    function drawVisualization() {
      // Create and populate the data table.
      var rawData = [
        ['Year', 'Austria'],
        ['2003',  1336060],
        ['2004',  1538156],
        ['2005',  1576579],
        ['2006',  1600652],
        ['2007',  1968113],
        ['2008',  1901067]
      ];
      var data = google.visualization.arrayToDataTable(rawData);
    
      // Create and draw the visualization.
      var chart = new google.visualization.BarChart(document.getElementById('visualization'));
      chart.draw(data,
               {title:"Yearly Coffee Consumption by Country",
                width:600, height:400,
                vAxis: {title: "Year"},
                hAxis: {title: "Cups"}}
          );
      var handler = function(e) {
        var sel = chart.getSelection();
        sel = sel[0];
        if (sel && sel['row'] && sel['column']) {
          var year = rawData[sel['row'] + 1][0];
          alert(year); // This where you'd construct the URL for this row, and redirect to it.
        }
      }
      google.visualization.events.addListener(chart, 'select', handler);
    }
    

提交回复
热议问题