duandangqin0559 2015-02-12 14: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 14:48
    关注

    My solution using 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)
    );
    

    And a solution using 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)
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?