8

I'm learning QGIS. I have drilling locations showing on a map with a specific SVG icon (D1 and D2). I also have my labels showing the name of the soil sample taken in each drilling and the contaminants analysis results (like shown in the attached table). One or more samples results can be displayed for one drilling (ex. D2-S1 and D2-S2). I would like to insert a symbol (like the ones in the table) showing the result for each contaminant at the end (or at the beginning) of the line of the label where the result is displayed.

enter image description here enter image description here

Type of display needed:

enter image description here

I have tried to add layers for each contaminant with rule based symbols but it doesn't allow me to move the labels freely, more so when 2 or more samples are related to one drilling.

Is there a way to do such a thing in QGIS ?

3 Answers 3

5

One way to do it is to add background symbol to your label, as shape choose 'Marker Symbol' (at this stage let the default circle) for Size type set 'Fixed'. Adjust the symbol size and the X offset (do not adjust the Y here) to place the symbol after your text.

Then go to the Marker Symbol setting to further adjust it :

  • Duplicate your Marker two time to get three symbol
  • Adjust the y offset of each symbol to align them to the line of text
  • Use data defined override to set the symbol shape and color

For the fill color of the first line symbol set :

CASE
    WHEN  "Contamin1"  =  '<A'  THEN '#32CD32'
    WHEN "Contamin1"  =  'A-B'  THEN '#FFFF00'
    When  "Contamin1" =  'B-C'  Then '#FF0000'
END

As expression (for the second and third symbol change the field from "Contamin1" to "Contamin2" and "Contamin3" respectively)

For the shape of the first line symbol set :

CASE
    WHEN  "Contamin1"  =  '<A'  THEN 'Circle'
    WHEN "Contamin1"  =  'A-B'  THEN 'Triangle'
    When  "Contamin1" =  'B-C'  Then 'Square'
END

As expression (for the second and third symbol change the field from "Contamin1" to "Contamin2" and "Contamin3" respectively)

enter image description here

enter image description here

This way your labels and symbol stay one object and you should be able to move them easily

1
  • Wonderful! Thank you, it works very well.
    – c morin
    May 30, 2022 at 13:04
3

Another route is to use virtual fields and unicode symbols. This might or not be useful, since it produces monochrome labels, but it is probably quite straightforward.

From the table view of the sampling vector layer:

enter image description here

you should obtain:

enter image description here

Selecting the virtual field as the label to be displayed:

enter image description here

Expression used for the Virtual Field:

Concat(
CASE
    WHEN  "Contamin1"  =  '<A'   THEN concat('๐ŸŸฉ',' Contaminant 1:',"Contamin1")
    WHEN  "Contamin1"  =  'A'    THEN concat('โ—น',' Contaminant 1:',"Contamin1")
    WHEN  "Contamin1"  =  'B'    THEN concat('๐Ÿ’ ',' Contaminant 1:',"Contamin1")
    WHEN  "Contamin1"  =  'C'    THEN concat('๐ŸŸ ',' Contaminant 1:',"Contamin1")
    WHEN  "Contamin1"  =  'A-B'  THEN concat('๐Ÿ”บ',' Contaminant 1:',"Contamin1")
END
,
'\n',
CASE
    WHEN  "Contamin2"  =  '<A'   THEN concat('๐ŸŸฉ',' Contaminant 2:',"Contamin2")
    WHEN  "Contamin2"  =  'A'    THEN concat('โ—น',' Contaminant 2:',"Contamin2")
    WHEN  "Contamin2"  =  'B'    THEN concat('๐Ÿ’ ',' Contaminant 2:',"Contamin2")
    WHEN  "Contamin2"  =  'C'    THEN concat('๐ŸŸ ',' Contaminant 2:',"Contamin2")
    WHEN  "Contamin2"  =  'A-B'  THEN concat('๐Ÿ”บ',' Contaminant 2:',"Contamin2")
END
,
'\n',
CASE
    WHEN  "Contamin3"  =  '<A'   THEN concat('๐ŸŸฉ',' Contaminant 3:',"Contamin3")
    WHEN  "Contamin3"  =  'A'    THEN concat('โ—น',' Contaminant 3:',"Contamin3")
    WHEN  "Contamin3"  =  'B'    THEN concat('๐Ÿ’ ',' Contaminant 3:',"Contamin3")
    WHEN  "Contamin3"  =  'C'    THEN concat('๐ŸŸ ',' Contaminant 3:',"Contamin3")
    WHEN  "Contamin3"  =  'A-B'  THEN concat('๐Ÿ”บ',' Contaminant 3:',"Contamin3")
END
)
1
3

Building on @J.R suggestion:

enter image description here

Please note that:

  1. you need to enable thee Allow HTML formatting
  2. you need to insert a '@' symbol in the Wrap on character field

enter image description here

concat (
CASE
    WHEN  "Contamin1"  =  '<A'   THEN concat('<span style="color:green">๐ŸŸฉ</span>',' Contaminant 1:&lt;A')
    WHEN  "Contamin1"  =  'RESC' THEN concat('<span style="color:blue">โ—น</span>',' Contaminant 1:',Contamin1)
    WHEN  "Contamin1"  =  'B-C'  THEN concat('<span style="color:red">๐ŸŸ </span>',' Contaminant 1:',Contamin1)
    WHEN  "Contamin1"  =  'A-B'  THEN concat('<span style="color:#ECDE1C">๐Ÿ”บ</span>',' Contaminant 1:',Contamin1)
    ELSE  ' '
END,
'@',
CASE
    WHEN  "Contamin2"  =  '<A'   THEN concat('<span style="color:green">๐ŸŸฉ</span>',' Contaminant 2:&lt;A')
    WHEN  "Contamin2"  =  'RESC' THEN concat('<span style="color:blue">โ—น</span>',' Contaminant 2:',Contamin2)
    WHEN  "Contamin2"  =  'B-C'  THEN concat('<span style="color:red">๐ŸŸ </span>',' Contaminant 2:',Contamin2)
    WHEN  "Contamin2"  =  'A-B'  THEN concat('<span style="color:#ECDE1C">๐Ÿ”บ</span>',' Contaminant 2:',Contamin2)
    ELSE  ' '
END,
'@',
CASE
    WHEN  "Contamin3"  =  '<A'   THEN concat('<span style="color:green">๐ŸŸฉ</span>',' Contaminant 3:&lt;A')
    WHEN  "Contamin3"  =  'RESC' THEN concat('<span style="color:blue">โ—น</span>','  Contaminant 3:',Contamin3)
    WHEN  "Contamin3"  =  'B-C'  THEN concat('<span style="color:red">๐ŸŸ </span>','  Contaminant 3:',Contamin3)
    WHEN  "Contamin3"  =  'A-B'  THEN concat('<span style="color:#ECDE1C">๐Ÿ”บ</span>',' Contaminant 3:',Contamin3)
    ELSE  ' '
END
)
2

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

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