gpt4 book ai didi

javascript - Google Visualization group() 聚合函数返回空值

转载 作者:行者123 更新时间:2023-12-04 00:58:51 26 4
gpt4 key购买 nike

我有一个名为 someNumber 的列,其中包含空值和零。将 google.visualization.data.group 应用到此列时,我无法将零传递到表中,只有 null。

documentation对于 .min.max 都表示忽略空值,但我似乎只能返回空值。

我怎样才能让零出现在我的 table 上?

谢谢各位专家!

工作示例:

// Load the Visualization API and the corechart package.
google.charts.load('current', {
'packages': ['corechart', 'table', 'gauge', 'controls']
});

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

function gChart0() {
drawChart();
};

function drawChart() {

var result = [{
"calendarWeek": "2017-W30",
"clnCount": 1,
"someNumber": null //NOTE THIS IS SET TO NULL
}, {
"calendarWeek": "2017-W30",
"clnCount": 3,
"someNumber": 0 //NOTE THIS IS SET TO ZERO
}];

//Create DataTable
var data = new google.visualization.DataTable();
data.addColumn('string', 'Calendar Week');
data.addColumn('number', 'Count');
data.addColumn('number', 'Some Number');

var dataArray = [];
$.each(result, function(i, obj) {
dataArray.push([
obj.calendarWeek,
obj.clnCount,
obj.someNumber
]);
});
data.addRows(dataArray);

//grouping
var groupView = google.visualization.data.group(data,
[{
column: 0,
type: 'string'
}],
[{
column: 1,
aggregation: google.visualization.data.sum,
type: 'number'
},
{
column: 2,
aggregation: google.visualization.data.min,
type: 'number'
}
]);

//Options
var options = {};

// Instantiate and draw chart, passing in options.
var chart = new google.visualization.Table(document.getElementById('chart_div'));
chart.draw(groupView, options);

} //END function drawChart()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>

<div id="chart_div"></div>

最佳答案

您可以提供自己的自定义聚合函数。
聚合函数应该接受一个值数组作为唯一的参数。

在自定义求和函数中,我们用零替换null

function customSum(values) {
var sum = 0;
values.forEach(function (value) {
sum += value || 0;
});
return sum;
}

在自定义 min 函数中,当两个值都为 null 时,Math.min 将返回零

function customMin(values) {
var min = null;
values.forEach(function (value) {
min = Math.min(value, min);
});
return min;
}

请参阅以下工作片段...

google.charts.load('current', {
'packages': ['corechart', 'table', 'gauge', 'controls']
});

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

function gChart0() {
drawChart();
};

function drawChart() {

var result = [{
"calendarWeek": "2017-W30",
"clnCount": 1,
"someNumber": null //NOTE THIS IS SET TO NULL
}, {
"calendarWeek": "2017-W30",
"clnCount": 3,
"someNumber": 0 //NOTE THIS IS SET TO ZERO
}];

//Create DataTable
var data = new google.visualization.DataTable();
data.addColumn('string', 'Calendar Week');
data.addColumn('number', 'Count');
data.addColumn('number', 'Some Number');

var dataArray = [];
$.each(result, function(i, obj) {
dataArray.push([
obj.calendarWeek,
obj.clnCount,
obj.someNumber
]);
});
data.addRows(dataArray);

//grouping
var groupView = google.visualization.data.group(data,
[{
column: 0,
type: 'string'
}],
[{
column: 1,
aggregation: customSum,
type: 'number'
},
{
column: 2,
aggregation: customMin,
type: 'number'
}
]);

function customSum(values) {
var sum = 0;
values.forEach(function (value) {
sum += value || 0;
});
return sum;
}

function customMin(values) {
var min = null;
values.forEach(function (value) {
min = Math.min(value, min);
});
return min;
}

//Options
var options = {};

// Instantiate and draw chart, passing in options.
var chart = new google.visualization.Table(document.getElementById('chart_div'));
chart.draw(groupView, options);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

关于javascript - Google Visualization group() 聚合函数返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58867102/

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