2

I am looking for a way to change the direction of the pie charts of a layer from counter-clockwise to clockwise in QGIS 3. I looked in the docs but I don´t seem to find a way to do this. Does anybody know what I need to do?

I create the pie charts like this:

current_colors = {}  # maps the field names to QColor objects
diagram = QgsPieDiagram()
ds = QgsDiagramSettings()
ds.categoryColors = current_colors.values()
ds.categoryAttributes = current_colors.keys()
ds.categoryLabels = ds.categoryAttributes

dr = QgsLinearlyInterpolatedDiagramRenderer()
max_val = layer.maximumValue(layer.fields().indexFromName(pie_sum_col))
dr.setUpperValue(max_val)
dr.setLowerSize(QSizeF(2, 2))
dr.setUpperSize(QSizeF(40, 40))
dr.setClassificationField(pie_sum_col)
dr.setDiagram(diagram)
dr.setDiagramSettings(ds)
    
layer.setDiagramRenderer(dr)
dls = QgsDiagramLayerSettings()
dls.setPlacement(1)
layer.setDiagramLayerSettings(dls)
layer.triggerRepaint()
2
  • What is the current_colors ?
    – Taras
    May 3, 2022 at 14:00
  • Sorry, I forgot to clarify. It is a dictionary that maps the field names of the layer to some QColor objects (I also added a comment in the question) May 4, 2022 at 9:39

1 Answer 1

2

QGIS Diagram settings way :

ds = QgsDiagramSettings()
# counter-clockwise direction
ds.setDirection(QgsDiagramSettings.Counterclockwise)
# clockwise direction
ds.setDirection(QgsDiagramSettings.Clockwise)

Basic way :

Your current Python dictionnary is :

current_colors = {
    '"field_one"': QColor("#FF0000"),
    '"field_two"': QColor("#00FF00"),
    '"field_three"': QColor("#0000FF"),
}

The result are diagrams in counter-clockwise order. So, just reverse the order of current_colors :

current_colors = {
    '"field_three"': QColor("#0000FF"),
    '"field_two"': QColor("#00FF00"),
    '"field_one"': QColor("#FF0000"),
}

or if you don't want to do this manually :

current_colors = {k: current_colors[k] for k in reversed(current_colors)}

The result will be diagrams in clockwise order.

3
  • 1
    Thanks for the idea! Unfortunately this is not exactly what I intended. When you look in the GUI at the layer tree you will still have to inspect the different entries counter-clockwise since their order will also shift. If I however, click on the layer -> properties -> Diagrams -> Rendering and set the Direction to Clockwise it will do as intended. Is there not the possibility to trigger this via pyqgis? May 11, 2022 at 11:54
  • @Christian: ok, I didn't know this option, so, see my edit. May 11, 2022 at 12:15
  • Perfect, that works just fine! Thanks for the help. May 12, 2022 at 8:09

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.