javascript - 使用 Google-charts api 中的饼图切片将切片点击导航到不同的页面

标签 javascript jquery html api google-visualization

![enter image description here][1]


<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

// Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.
        data.addRows([
          ['Mushrooms', 3],
          ['Onions', 1],
          ['Olives', 1],
          ['Zucchini', 1],
          ['Pepperoni', 2]
        ]);

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

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

        google.visualization.events.addListener(chart, 'select', selectHandler);

        var label1 = data.pieSliceText('label');

        // The selection handler.
        // Loop through all items in the selection and concatenate
        // a single message from all of them.
    function selectHandler() {

    window.location.href = 'http://www.google.com?search='&label1;   
    // I want that on clickof slice , i must be able to navigate to a different page.                                  /    // with the clicked slice ID passed as a Querystring.
}

      }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>

请参阅此问题图表。我不能张贴图表:
Google Pie Charts Label

我必须获取饼图中单击的切片的 ID 并使用其 ID 并将其与 url 连接以使其可重定向。使用点击切片的 id 作为查询字符串。请帮忙。

最佳答案

您需要从DataTable中获取相关数据:

function selectHandler() {
    var selection = chart.getSelection();
    if (selection.length) {
        var pieSliceLabel = data.getValue(selection[0].row, 0);
        window.location.href = 'http://www.google.com?search=' + pieSliceLabel;
    }
}

关于javascript - 使用 Google-charts api 中的饼图切片将切片点击导航到不同的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22150329/

相关文章:

html - Django SelectDateWidget 内联选择( Bootstrap ?)

javascript - 加快了缓慢的 jQuery Mobile 页面转换 - 为什么?

javascript - 使用 jQuery 和变量的问题

javascript - 为什么我的 .hoverIntent 动画背景位置不起作用?

javascript - 如何在javascript中调用另一个窗口的函数?

jquery - 如果文档高于视口(viewport)则执行函数

javascript - 自定义溢出产生的水平滚动条 :auto

javascript - 类型 'csrf_token' 上不存在属性 'Object'

javascript - 取一个数字并将其转换为数字数组

javascript - 即使在 stopPropagation 之后,嵌套元素也会触发父事件