我有以下使用Google饼图的代码,但是饼图预先填充了数据,我希望获得用户输入,我的图像将通过HTML的一些基本文本形式,但我不确定如何更改默认活动“
['Work', 8],
['Friends', 2],
['Eat', 2],
['TV', 3],
['Gym', 2],
['Sleep', 7]"
是用户输入的内容,而不是如图所示的预设值。
<html>
<head>
<!--bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- jQuery (CDN)-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- google api of the pi chart -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<!-- Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 8],
['Friends', 2],
['Eat', 2],
['TV', 3],
['Gym', 2],
['Sleep', 7]
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Time Spent', 'width':700, 'height':500};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
//hide and show the pie chart
$(document).ready(function(){
$("#hide").click(function(){
$("#piechart").hide();
});
$("#show").click(function(){
$("#piechart").show();
});
});
</script>
</head>
<body>
<div class ="container-fluid">
<button id = "hide">hide the pie chart</button>
<button id = "show">show the pie chart</button>
<div id="piechart"></div>
</container>
</body>
<html>
答案 0 :(得分:2)
您可以创建自己的数组并使用用户输入填充它,例如:
<html>
<head>
<!--bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- jQuery (CDN) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- google api of the pi chart -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<!-- Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Fill this with user input..
var chartObj = [
['Task', 'Hours per Day'],
['Work', 8],
['Friends', 2],
['Eat', 2],
['TV', 3],
['Gym', 2],
['Sleep', 7]
];
// Draw the chart and set the chart values
function drawChart() {
// Pass the chartObj to the function
var data = google.visualization.arrayToDataTable(this.chartObj);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Time Spent', 'width':700, 'height':500};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
//hide and show the pie chart
$(document).ready(function(){
$("#hide").click(function(){
$("#piechart").hide();
});
$("#show").click(function(){
$("#piechart").show();
});
});
</script>
</head>
<body>
<div class ="container-fluid">
<button id = "hide">hide the pie chart</button>
<button id = "show">show the pie chart</button>
<div id="piechart"></div>
</container>
</body>
<html>
答案 1 :(得分:1)
但我不确定如何更改默认活动 是用户输入的东西,而不是如图所示的预设值。
您的数据是一个数组数组。像这样正常访问它并重新定义值...
var data = ([
['Task', 'Hours per Day'], //ARRAY KEY 0
//Let's change work into Running
['Work', 8], //ARRAY KEY 1
['Friends', 2], //ARRAY KEY 2
['Eat', 2], //ARRAY KEY 3
//Let's change TV into Gaming
['TV', 3], //ARRAY KEY 4
['Gym', 2], //ARRAY KEY 5
['Sleep', 7] //ARRAY KEY 6
]);
//Let's assume these are user input values.
var userInputOne = "Running";
var userInputTwo = "Gaming";
data[1][0] = userInputOne ;
data[4][0] = userInputTwo;
console.log(data);
(我删除了google.visualization.arrayToDataTable
以避免此SO答案/示例中的错误)