KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

LIMIT BARS AND CREATE A NEW ONE #369

Open Elcid opened this topic on on Jul 12, 2018 - 3 comments

Elcid commented on Jul 12, 2018

I'd like to LIMIT the quantity of bars (5 bars) in the chart, but I need to show the SUM of these others bars into one BAR called "Others", and I have no idea how to do this. Chart

KoolReport commented on Jul 13, 2018

That 's really a nice question. So far from what I have seen, it is possible but let me sometime to write a sample code to test before answer you.

David Winterburn commented on Jul 16, 2018

Hi Elcid,

Please download our latest KoolReport package and try the following code:

use \koolreport\core\Utility as Util;

...
->pipe(new Sort(array(
    'sales' => 'desc',
)))
->pipe(new Map([
    '{aggregate}' => function($row, $meta, $index, $data) {
        if ($index >= 5) {
            $sum = Util::init($data, 'others', []);
            foreach ($row as $c => $v) {
                Util::init($sum, $c, 0);
                $sum[$c] = is_numeric($v) ? $sum[$c] + $v : 'Others';
            }
            $data['others'] = $sum;
            return $data;
        }
    },
    '{value}' => function($row, $meta, $index) {
        if ($index < 5) 
            return $row;
    },
    '{end}' => function($count, $data) {
        return $data;
    },
]))
->pipe($this->dataStore('myDatastore'));

Please remember to change the "sales" field to your data column. Let us know if you have any problem. Thanks!

Elcid commented on Jul 16, 2018

Thanks for help! Worked fine. :D

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

None