KoolReport's Forum

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

Change color of line from a selected moment #2841

Open fiuncho opened this topic on on Oct 4, 2022 - 12 comments

fiuncho commented on Oct 4, 2022

Dear team.

Using "\koolreport\dashboard\widgets\chartjs\ComboChart"

How can i change the color of a line from a selected date?, like example:

I want to chage the color of the "blue line" after the "blue point selected". How can i do?

Thanks

Sebastian Morales commented on Oct 4, 2022

With Chartjs' LineChart, you could set the each line's color dynamically with its "borderColor" property like this:

    LineChart::create(array(
        "title"=>"Sale vs Cost",
        "dataSource"=>$time_sale,
        "columns"=>array(
            "month",
            "sale"=>array(
                "label"=>"Sale",
                "type"=>"number",
                "prefix"=>"$",
                "borderColor" => "function(context) { // use this javascript function to set sale line's line color
                    console.log(context);
                    var index = context.dataIndex;
                    var value = context.dataset.data[index];
                    console.log(index, value);
                    return ...; // return line color based on context
                }",
            ),
            "cost"=>array(
                "label"=>"Cost",
                "type"=>"number",
                "prefix"=>"$"
            ),
        )
    )); 

Let us know if you have any issue. Tks,

fiuncho commented on Oct 14, 2022

dear team

i couldnt do it.

I can change the color of hole line, but, i only want to change a color of the values 0 or less than 0.

Any idea?

I want to do something like this:

https://jsfiddle.net/9psxbz4c/1/

best

Sebastian Morales commented on Oct 17, 2022

If you want to change color of points instead of lines use "pointBorderColor" and "pointBackgroundColor" instead of "borderColor":

                "pointBorderColor" => "function(context) { 
                    console.log(context);
                    var index = context.dataIndex;
                    var value = context.dataset.data[index];
                    console.log(index, value);
                    return ...; // return point border color based on context
                }", 
                "pointBackgroundColor" => "function(context) { 
                    console.log(context);
                    var index = context.dataIndex;
                    var value = context.dataset.data[index];
                    console.log(index, value);
                    return ...; // return point background color based on context
                }", 
fiuncho commented on Oct 17, 2022

Thanks Sebastian.

But did not run :(

I have done this:

                "pointBackgroundColor" => "function(context) { 
                    var index = context.dataIndex;
                    var value = context.dataset.data[index];
                    var rtn_color = 'blue';

                    if (value == null) {
                        rtn_color='white';
                    }

                    if (value <= 0) {
                        rtn_color='white';
                    }
                    
                    rtn_color = 'blue';

                    return rtn_color;
                }", 

Best

Sebastian Morales commented on Oct 18, 2022

Pls replace these lines:

                    var value = context.dataset.data[index];
                    ...
                    return rtn_color;

with these ones:

                    var value = parseFloat(context.dataset.data[index]);
                    console.log('value = ', value);
                    ...
                    console.log('rtn_color = ', rtn_color);
                    return rtn_color;

Then open your browser's dev tool (F12), reload your page and see console.log output in dev tool's console. Rgds,

fiuncho commented on Oct 18, 2022

Dear Sebastian

I tried it, but didnt work. This is the result:

If you follow the console trace, it is easy to find the point where the values changes to 0, and the color, change to "pink". For values >0 the color must be blue, but, all values are pink

Best regards

Sebastian Morales commented on Oct 19, 2022

What is wrong here? I can see the point border color is blue for those with value > 0. You can set point background color using the same function to see the color more clearly.

fiuncho commented on Oct 19, 2022

Dear Sebastian

The wrong situation is that the values before "20221018 18:00" must be "blue", and they are pink. The trace console, show that the color must be blue, but the object is painting "pink"

Best

Sebastian Morales commented on Oct 20, 2022

It seems "borderColor" only applies the last value and the line can only have one color. This is a limitation of Chartjs client 2.x. We will add Chartjs client 3.x in the next version of KoolReport so that you could apply different colors/styles for each line segment. Rgds,

fiuncho commented on Oct 20, 2022

Dear Sebastian

Thanks for you attention and effors.

Where will be the new lease enable to download?

Best

Sebastian Morales commented on Oct 21, 2022

It's probably a few weeks away. We will send you a development version as soon as it's available. Tks,

fiuncho commented on Oct 25, 2022

Thanks Sebastian

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
None yet

None