![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/