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 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题