KoolReport's Forum

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

ChartJs DonutChart Legend Sort #2287

Open KarineG opened this topic on on Aug 18, 2021 - 2 comments

KarineG commented on Aug 18, 2021

Hi,

I have a donutChart, I want change order of legend : Null > Cold > Low > Medium > Hot.

How can I do this - without change name (cause for now it's alphabetical sorting) ?

Thanks :)

Sebastian Morales commented on Aug 19, 2021

I think the legend order is based on the data rows' order so we have to order data rows of the chart's datastore:

$labelOrder = array(
    "Cold" => 0,
    "Low" => 1,
    "Medium" => 2, 
    "Hot" => 3,
); //I'm not sure if your Null is a null value or a "Null" string
DonutChart::create(array(
    "dataSource" => $this->dataStore("myDS")->sort(array(
        "labelColumn" => function($a, $b) use ($labelOrder) {
            if (!isset($labelOrder[$a])) return 1; // if $labelOrder[$a] == null, return 1 or -1 depending on your needed order
            if (!isset($labelOrder[$b])) return -1;
            if ($labelOrder[$a] === $labelOrder[$b]) return 0;
            return $labelOrder[$a] > $labelOrder[$b] ? 1 : -1; //change 1 and -1 depending on your needed order
        } 
    )),

Let us know if we understand your question correctly. Tks,

KarineG commented on Aug 26, 2021

Thanks !

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
help needed
solved

ChartJS