I am using QGIS 3.0.2. I am working on a print composer including an attribute table. I would like to set different colors to each rows, depending on an expression, refering to an attribute. I didn't find a way.
Is that possible?
I am using QGIS 3.0.2. I am working on a print composer including an attribute table. I would like to set different colors to each rows, depending on an expression, refering to an attribute. I didn't find a way.
Is that possible?
As of QGIS 3.12, you can conditionally format row background and text colour in the attribute table in Layouts by setting the formatting in the Attribute Table for the layer in the main window.
So if you set up conditional formatting in the Attribute Table to "field" = 'value' this will be reflected in the attribute table in the map layout if you check 'Apply layer conditional styling colous' (see link for screenshots).
If you are trying to modify these rows you see as blue in the screenshot(?), I don't think it's currently change those based on a value:
I don't think it would be a really big development to make it possible to read an expression to the value like you can do with most of the inputs in the print layout. So I suggest you to open a feature request on GitHub.
You can do by it writing a custom expression and put in a html- field. It is quite hard to make a general solution, but for a customized case, someting like this will work
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Testing')
def dataset(table, feature, parent):
output=""
layer=QgsProject.instance().mapLayersByName(table)[0]
colmap={'NL':'Red','NR':'Green'}
for feature in layer.getFeatures():
output+='<p style="color:{}">{}</p>'.format(colmap[feature['net_code']],feature['sta_code'])
return(output)
Call it as dataset("layername")
When testing, I had a dataset where I printed out the sta_code values colored based on the value of net_code. As net_code is a string, I could use a dict, if the field you base the coding on is a number you will need to use a few if-thens to get the right colors.
You would also probably use tr and td fields to make it all into a table, but the principle would be the same.
(Well, since this is a strongly customized function, it would make as much sense to hardcode the table name and just call it by dataset() - and probably make a more descriptive name)