-1

我有以下使用谷歌饼图的代码,但是饼图预先填充了数据,我希望通过一些带有 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>
4

2 回答 2

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>
于 2017-10-03T14:15:26.937 回答
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 答案/示例中的错误)

于 2017-10-03T14:12:57.830 回答