duandangqin0559 2015-02-12 06:19
浏览 18
已采纳

操作PHP数组以将小值分组以在饼图中使用

I am looking for ideas on some PHP for the following -

I have an array of numerical values that feed a pie chart (created with High Charts). These values vary in size depending what the chart is showing and the array might be up to 100 values long.

The problem is that if I send these values to the pie and a lot of the values are low, it ends up with the smaller values creating lots of pie 'slices' that are too small to read and so ineffective. What I want to do is group all the values under a threshold % into a group called Other.

From research I can't seem to do this natively in High Charts but I think I should be able to do it with PHP. So the PHP would have to -

  1. Take an array (100, 96, 72, 25, 3, 2, 1, 1, 1) etc
  2. Work out the total of the values and thus % of each - (33%, 32%, 23%, 8%, 1%, 0.6%, 0.3%, 0.3%, 0.3%) etc
  3. Remove any values below a certain % ie 1% so that the array becomes - (100, 96, 72)
  4. Add back into the array a value representing the total of those removed (100, 96, 72, 33)
  5. The array can now be sent to the pie

Any ideas please?

  • 写回答

2条回答 默认 最新

  • dongling2038 2015-02-12 06:48
    关注

    My solution using array_filter():

    1. $input = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
    2. $thresh = 0.01; // Threshold; 0.01 == 1%
    3. $extra = 0; // The sum of the combined slices ('Other')
    4. // Compute the sum of all values (needed for the percentages)
    5. $sum = array_sum($input);
    6. $output = array_merge(
    7. // Process the input list (combine and remove)
    8. array_filter(
    9. $input,
    10. function ($item) use ($sum, $thresh, &$extra) {
    11. // Compute the percentage and check it against the threshold
    12. if ($item / $sum < $thresh) {
    13. // Too small
    14. $extra += $item; // Add it to 'Other'
    15. return FALSE; // Remove it from the input list
    16. } else {
    17. // Above the threshold
    18. return TRUE; // Let it go to the final list
    19. }
    20. }
    21. ),
    22. // Don't forget to add the combined value to the output
    23. array($extra)
    24. );

    And a solution using array_reduce():

    1. $input = array(100, 96, 72, 25, 3, 2, 1, 1, 1);
    2. $thresh = 0.01; // Threshold; 0.01 == 1%
    3. $extra = 0; // The sum of the combined slices ('Other')
    4. // Compute the sum of all values (needed for the percentages)
    5. $sum = array_sum($input);
    6. $output = array_merge(
    7. // Process the input list (keep, remove, combine)
    8. array_reduce(
    9. $input,
    10. function (array $carry, $item) use ($sum, $thresh, &$extra) {
    11. // Compute the percentage and check it against the threshold
    12. if ($item / $sum < $thresh) {
    13. // Too small
    14. $extra += $item; // Add it to 'Other'
    15. } else {
    16. // Above the threshold
    17. $carry[] = $item; // Put it into the "reduced" list
    18. }
    19. // Always return $carry (the reduced list)
    20. return $carry;
    21. },
    22. // Start with an empty list; here will be added the values above the threshold
    23. array()
    24. ),
    25. // Don't forget to add the combined value to the output
    26. array($extra)
    27. );

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部