QGIS - How to differently colorize labels from two separate fields using color expression script?












4















I have 2 fields, I concatenate them [concat(to_string(field1), ' ', to_string(field2))] to show their labels side-by-side.



Is it possible to colorize from "text color expression" (using some scripts) first label in "color1" and second label in "color2"?



enter image description here



Clarification



I want the final result to be like below ("field1" data to be shown in red, "field2" data to be shown in green)



enter image description here










share|improve this question





























    4















    I have 2 fields, I concatenate them [concat(to_string(field1), ' ', to_string(field2))] to show their labels side-by-side.



    Is it possible to colorize from "text color expression" (using some scripts) first label in "color1" and second label in "color2"?



    enter image description here



    Clarification



    I want the final result to be like below ("field1" data to be shown in red, "field2" data to be shown in green)



    enter image description here










    share|improve this question



























      4












      4








      4








      I have 2 fields, I concatenate them [concat(to_string(field1), ' ', to_string(field2))] to show their labels side-by-side.



      Is it possible to colorize from "text color expression" (using some scripts) first label in "color1" and second label in "color2"?



      enter image description here



      Clarification



      I want the final result to be like below ("field1" data to be shown in red, "field2" data to be shown in green)



      enter image description here










      share|improve this question
















      I have 2 fields, I concatenate them [concat(to_string(field1), ' ', to_string(field2))] to show their labels side-by-side.



      Is it possible to colorize from "text color expression" (using some scripts) first label in "color1" and second label in "color2"?



      enter image description here



      Clarification



      I want the final result to be like below ("field1" data to be shown in red, "field2" data to be shown in green)



      enter image description here







      qgis labeling color expression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 at 7:42







      Vitruvius

















      asked Jan 24 at 8:57









      VitruviusVitruvius

      499211




      499211






















          2 Answers
          2






          active

          oldest

          votes


















          4














          You may be able to use Symbology, rather than Label.



          (1) Choose Symbology tab and add two layers of Centroid fill with the Font marker type. Give negative offset (e.g. -10.0mm) to red, and positive offset (e.g. 10.0mm) to green.



          enter image description here



          (2) Highlight Font marker. Scroll down a little bit further, and click on the Data defined override (a small pull-down menu). Select each of your field for the corresponding color.



          enter image description here



          (3) You will probably have to adjust font size and offset value, by visually checking what you see on the map.



          enter image description here






          share|improve this answer
























          • Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

            – Vitruvius
            Jan 24 at 11:27






          • 1





            AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

            – csk
            Jan 24 at 18:19



















          2














          EDIT: I misread your question, my apologies. I thought you wanted to select a color using an expression which depended on the value of two fields. Sorry about that.





          Yes you can.



          In the label options for the layer open the color selectio box. You don't need to set any colours here just find the colours you want and copy their hex codes into notepad:



          enter image description here



          In the Text tab for labelling add an expression for the color value by clicking the box shown below and choosing Edit:



          enter image description here



          Then add an expression like the following:



          CASE
          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98'
          WHEN "Field2" = 'value3' AND "Field2" = 'value4' THEN '#625974'
          END





          share|improve this answer
























          • WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

            – Vitruvius
            Jan 24 at 9:28













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f309717%2fqgis-how-to-differently-colorize-labels-from-two-separate-fields-using-color-e%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You may be able to use Symbology, rather than Label.



          (1) Choose Symbology tab and add two layers of Centroid fill with the Font marker type. Give negative offset (e.g. -10.0mm) to red, and positive offset (e.g. 10.0mm) to green.



          enter image description here



          (2) Highlight Font marker. Scroll down a little bit further, and click on the Data defined override (a small pull-down menu). Select each of your field for the corresponding color.



          enter image description here



          (3) You will probably have to adjust font size and offset value, by visually checking what you see on the map.



          enter image description here






          share|improve this answer
























          • Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

            – Vitruvius
            Jan 24 at 11:27






          • 1





            AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

            – csk
            Jan 24 at 18:19
















          4














          You may be able to use Symbology, rather than Label.



          (1) Choose Symbology tab and add two layers of Centroid fill with the Font marker type. Give negative offset (e.g. -10.0mm) to red, and positive offset (e.g. 10.0mm) to green.



          enter image description here



          (2) Highlight Font marker. Scroll down a little bit further, and click on the Data defined override (a small pull-down menu). Select each of your field for the corresponding color.



          enter image description here



          (3) You will probably have to adjust font size and offset value, by visually checking what you see on the map.



          enter image description here






          share|improve this answer
























          • Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

            – Vitruvius
            Jan 24 at 11:27






          • 1





            AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

            – csk
            Jan 24 at 18:19














          4












          4








          4







          You may be able to use Symbology, rather than Label.



          (1) Choose Symbology tab and add two layers of Centroid fill with the Font marker type. Give negative offset (e.g. -10.0mm) to red, and positive offset (e.g. 10.0mm) to green.



          enter image description here



          (2) Highlight Font marker. Scroll down a little bit further, and click on the Data defined override (a small pull-down menu). Select each of your field for the corresponding color.



          enter image description here



          (3) You will probably have to adjust font size and offset value, by visually checking what you see on the map.



          enter image description here






          share|improve this answer













          You may be able to use Symbology, rather than Label.



          (1) Choose Symbology tab and add two layers of Centroid fill with the Font marker type. Give negative offset (e.g. -10.0mm) to red, and positive offset (e.g. 10.0mm) to green.



          enter image description here



          (2) Highlight Font marker. Scroll down a little bit further, and click on the Data defined override (a small pull-down menu). Select each of your field for the corresponding color.



          enter image description here



          (3) You will probably have to adjust font size and offset value, by visually checking what you see on the map.



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 24 at 10:46









          KazuhitoKazuhito

          16.1k41883




          16.1k41883













          • Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

            – Vitruvius
            Jan 24 at 11:27






          • 1





            AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

            – csk
            Jan 24 at 18:19



















          • Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

            – Vitruvius
            Jan 24 at 11:27






          • 1





            AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

            – csk
            Jan 24 at 18:19

















          Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

          – Vitruvius
          Jan 24 at 11:27





          Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way.

          – Vitruvius
          Jan 24 at 11:27




          1




          1





          AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

          – csk
          Jan 24 at 18:19





          AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p>

          – csk
          Jan 24 at 18:19













          2














          EDIT: I misread your question, my apologies. I thought you wanted to select a color using an expression which depended on the value of two fields. Sorry about that.





          Yes you can.



          In the label options for the layer open the color selectio box. You don't need to set any colours here just find the colours you want and copy their hex codes into notepad:



          enter image description here



          In the Text tab for labelling add an expression for the color value by clicking the box shown below and choosing Edit:



          enter image description here



          Then add an expression like the following:



          CASE
          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98'
          WHEN "Field2" = 'value3' AND "Field2" = 'value4' THEN '#625974'
          END





          share|improve this answer
























          • WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

            – Vitruvius
            Jan 24 at 9:28


















          2














          EDIT: I misread your question, my apologies. I thought you wanted to select a color using an expression which depended on the value of two fields. Sorry about that.





          Yes you can.



          In the label options for the layer open the color selectio box. You don't need to set any colours here just find the colours you want and copy their hex codes into notepad:



          enter image description here



          In the Text tab for labelling add an expression for the color value by clicking the box shown below and choosing Edit:



          enter image description here



          Then add an expression like the following:



          CASE
          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98'
          WHEN "Field2" = 'value3' AND "Field2" = 'value4' THEN '#625974'
          END





          share|improve this answer
























          • WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

            – Vitruvius
            Jan 24 at 9:28
















          2












          2








          2







          EDIT: I misread your question, my apologies. I thought you wanted to select a color using an expression which depended on the value of two fields. Sorry about that.





          Yes you can.



          In the label options for the layer open the color selectio box. You don't need to set any colours here just find the colours you want and copy their hex codes into notepad:



          enter image description here



          In the Text tab for labelling add an expression for the color value by clicking the box shown below and choosing Edit:



          enter image description here



          Then add an expression like the following:



          CASE
          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98'
          WHEN "Field2" = 'value3' AND "Field2" = 'value4' THEN '#625974'
          END





          share|improve this answer













          EDIT: I misread your question, my apologies. I thought you wanted to select a color using an expression which depended on the value of two fields. Sorry about that.





          Yes you can.



          In the label options for the layer open the color selectio box. You don't need to set any colours here just find the colours you want and copy their hex codes into notepad:



          enter image description here



          In the Text tab for labelling add an expression for the color value by clicking the box shown below and choosing Edit:



          enter image description here



          Then add an expression like the following:



          CASE
          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98'
          WHEN "Field2" = 'value3' AND "Field2" = 'value4' THEN '#625974'
          END






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 24 at 9:18









          TeddyTedTedTeddyTedTed

          80819




          80819













          • WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

            – Vitruvius
            Jan 24 at 9:28





















          • WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

            – Vitruvius
            Jan 24 at 9:28



















          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

          – Vitruvius
          Jan 24 at 9:28







          WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases.

          – Vitruvius
          Jan 24 at 9:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f309717%2fqgis-how-to-differently-colorize-labels-from-two-separate-fields-using-color-e%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          WPF add header to Image with URL pettitions [duplicate]