操作 PHP 数组以将小值分组以在饼图中使用
Manipulate PHP array to group small values for use in Pie Chart
我正在为以下内容寻找一些 PHP 的想法 -
我有一个数值数组,用于提供饼图(使用 High Charts 创建)。这些值的大小取决于图表显示的内容,并且数组可能长达 100 个值。
问题是,如果我将这些值发送到饼图,但很多值都很低,最终会产生较小的值,从而创建很多饼图 'slices',这些饼图太小而无法读取,因此无效。我想要做的是将阈值 % 以下的所有值分组到一个名为其他的组中。
根据研究,我似乎无法在 High Charts 中自然地做到这一点,但我认为我应该能够使用 PHP 做到这一点。所以 PHP 必须 -
- 取数组 (100, 96, 72, 25, 3, 2, 1, 1, 1) 等
- 计算出值的总和,从而计算出每个值的百分比 -
(33%, 32%, 23%, 8%, 1%, 0.6%, 0.3%, 0.3%, 0.3%)等
- 删除低于某个百分比(即 1%)的任何值,以便数组变为 -
(100, 96, 72)
- 将一个值重新添加到数组中,该值表示已删除的总数
(100, 96, 72, 33)
- 数组现在可以发送到饼图了
有什么想法吗?
你的意思是这样的吗?
$data = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
// $data = array of data
// $p = percentage which any value below that
// percentage will be removed.
function filter($data, $p) {
$total = array_sum($data);
$treshold = $p * $total/ 100;
$result = array();
foreach ($data as $value) {
if ($value > $treshold) $result[] = $value;
}
$remain = $total - array_sum($result);
if ($remain > 0) $result[] = $remain;
return $result;
}
// Remove any value under 1%
print_r(filter($data, 1));
我的解决方案使用array_filter()
:
$input = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
$thresh = 0.01; // Threshold; 0.01 == 1%
$extra = 0; // The sum of the combined slices ('Other')
// Compute the sum of all values (needed for the percentages)
$sum = array_sum($input);
$output = array_merge(
// Process the input list (combine and remove)
array_filter(
$input,
function ($item) use ($sum, $thresh, &$extra) {
// Compute the percentage and check it against the threshold
if ($item / $sum < $thresh) {
// Too small
$extra += $item; // Add it to 'Other'
return FALSE; // Remove it from the input list
} else {
// Above the threshold
return TRUE; // Let it go to the final list
}
}
),
// Don't forget to add the combined value to the output
array($extra)
);
以及使用 array_reduce()
的解决方案:
$input = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
$thresh = 0.01; // Threshold; 0.01 == 1%
$extra = 0; // The sum of the combined slices ('Other')
// Compute the sum of all values (needed for the percentages)
$sum = array_sum($input);
$output = array_merge(
// Process the input list (keep, remove, combine)
array_reduce(
$input,
function (array $carry, $item) use ($sum, $thresh, &$extra) {
// Compute the percentage and check it against the threshold
if ($item / $sum < $thresh) {
// Too small
$extra += $item; // Add it to 'Other'
} else {
// Above the threshold
$carry[] = $item; // Put it into the "reduced" list
}
// Always return $carry (the reduced list)
return $carry;
},
// Start with an empty list; here will be added the values above the threshold
array()
),
// Don't forget to add the combined value to the output
array($extra)
);
我正在为以下内容寻找一些 PHP 的想法 -
我有一个数值数组,用于提供饼图(使用 High Charts 创建)。这些值的大小取决于图表显示的内容,并且数组可能长达 100 个值。
问题是,如果我将这些值发送到饼图,但很多值都很低,最终会产生较小的值,从而创建很多饼图 'slices',这些饼图太小而无法读取,因此无效。我想要做的是将阈值 % 以下的所有值分组到一个名为其他的组中。
根据研究,我似乎无法在 High Charts 中自然地做到这一点,但我认为我应该能够使用 PHP 做到这一点。所以 PHP 必须 -
- 取数组 (100, 96, 72, 25, 3, 2, 1, 1, 1) 等
- 计算出值的总和,从而计算出每个值的百分比 - (33%, 32%, 23%, 8%, 1%, 0.6%, 0.3%, 0.3%, 0.3%)等
- 删除低于某个百分比(即 1%)的任何值,以便数组变为 - (100, 96, 72)
- 将一个值重新添加到数组中,该值表示已删除的总数 (100, 96, 72, 33)
- 数组现在可以发送到饼图了
有什么想法吗?
你的意思是这样的吗?
$data = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
// $data = array of data
// $p = percentage which any value below that
// percentage will be removed.
function filter($data, $p) {
$total = array_sum($data);
$treshold = $p * $total/ 100;
$result = array();
foreach ($data as $value) {
if ($value > $treshold) $result[] = $value;
}
$remain = $total - array_sum($result);
if ($remain > 0) $result[] = $remain;
return $result;
}
// Remove any value under 1%
print_r(filter($data, 1));
我的解决方案使用array_filter()
:
$input = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
$thresh = 0.01; // Threshold; 0.01 == 1%
$extra = 0; // The sum of the combined slices ('Other')
// Compute the sum of all values (needed for the percentages)
$sum = array_sum($input);
$output = array_merge(
// Process the input list (combine and remove)
array_filter(
$input,
function ($item) use ($sum, $thresh, &$extra) {
// Compute the percentage and check it against the threshold
if ($item / $sum < $thresh) {
// Too small
$extra += $item; // Add it to 'Other'
return FALSE; // Remove it from the input list
} else {
// Above the threshold
return TRUE; // Let it go to the final list
}
}
),
// Don't forget to add the combined value to the output
array($extra)
);
以及使用 array_reduce()
的解决方案:
$input = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
$thresh = 0.01; // Threshold; 0.01 == 1%
$extra = 0; // The sum of the combined slices ('Other')
// Compute the sum of all values (needed for the percentages)
$sum = array_sum($input);
$output = array_merge(
// Process the input list (keep, remove, combine)
array_reduce(
$input,
function (array $carry, $item) use ($sum, $thresh, &$extra) {
// Compute the percentage and check it against the threshold
if ($item / $sum < $thresh) {
// Too small
$extra += $item; // Add it to 'Other'
} else {
// Above the threshold
$carry[] = $item; // Put it into the "reduced" list
}
// Always return $carry (the reduced list)
return $carry;
},
// Start with an empty list; here will be added the values above the threshold
array()
),
// Don't forget to add the combined value to the output
array($extra)
);