4

Trying to set different font color types from the same field in rule-based labeling in QGIS.

I have a "C°" field with only 2 values: +56.7 C° and -89.2 C°.

Trying this expression to have it:

CASE
    WHEN "C°" = '56.7 C°' THEN '#ff0101'
    ELSE
        CASE
            WHEN "C°" = '89.2 C°' THEN '#0dff01'
        END
END

But as a return is all font the same:

enter image description here enter image description here.

Am I wondering if there is any solution to get different font colors for each value ?

enter image description here

8
  • 1
    It works fine using your expression... something else is overriding it, maybe another rule, or the expression is not evaluated at all. Please post more details about the rules/data override.
    – JGH
    Jun 23, 2021 at 15:59
  • Added my rule-based window. No other expressions not executed
    – GeoEth
    Jun 23, 2021 at 16:21
  • Do you mean 'Death Valley'?
    – Pfalbaum
    Jun 23, 2021 at 18:19
  • Oh, yeah, my bad, thanks
    – GeoEth
    Jun 23, 2021 at 19:33
  • Just as an idea: from a cartographic view, your colors are not ideal, difficult to read - especially the green color on dark yellow background. At least, consider using text buffers.
    – Babel
    Jun 24, 2021 at 6:07

2 Answers 2

8

It seems unusual to have a case when then in the filter expression of the rule, so I am guessing you have put the expression to get the color there.

You can replace the filter to be an ELSE by checking the corresponding radio-box (as in the screenshot below), or to replace it with a filter such as "C°" IS NOT NULL.

Then, click the little epsilon sign beside the color and select expression / edit and enter it there.

Note that it could be simplified to

CASE 
WHEN  "C°"  =  '56.7 C°'   
 THEN '#ff0101'
WHEN  "C°"  =  '89.2 C°'   
 THEN '#0dff01' 
END

or even to a single if else statement.

enter image description here

1
  • 1
    It works! Thanks a lot!
    – GeoEth
    Jun 23, 2021 at 17:28
5

Since you have just two values (+56.7 C° and -89.2 C°), I would use the if() condition instead of the CASE statement.

if("C°" = '56.7 C°','#ff0101','#0dff01')
1
  • 1
    Thanks, it works too
    – GeoEth
    Jun 24, 2021 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.