gpt4 book ai didi

javascript - 获取表格列的总和并添加到谷歌图表

转载 作者:行者123 更新时间:2023-12-02 17:46:08 24 4
gpt4 key购买 nike

我需要从多个 (5) 个表中获取一列的总和,并将这些总和添加到 Google 饼图中的 5 个实例。我认为我拥有的代码非常接近,但只是无法找出哪里出错了。

function drawChart() {

// calculate the sum of each table
function sum($inputs) {
var sum=0;
//iterate through each input and add to sum
$inputs.each(function() {
sum += parseInt($(this).text());
});

var data = google.visualization.arrayToDataTable([
['Type', 'Cost'],
['Expense', sum($('#expenses .amount'))],
['Savings', sum($('#savings.amount'))],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);

var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);
}
}

最佳答案

您需要在函数 sum 中返回总和值。

function drawChart() {

// calculate the sum of each table
function sum($inputs) {
var sum=0;
//iterate through each input and add to sum
$inputs.each(function() {
sum += parseInt($(this).text());
});
return sum;
}

var data = google.visualization.arrayToDataTable([
['Type', 'Cost'],
['Expense', sum($('#expenses .amount'))],
['Savings', sum($('#savings.amount'))],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);

var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);

}

关于javascript - 获取表格列的总和并添加到谷歌图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com