1

In my standalone application, I show different layers based on queries into a canvas. Now I want to add a layer with charts over by the values of an attribute. Is it possible in a standalone application? I tried "QgsDiagramRenderer" but without Results. What should I do?

2

Here is some code to generate a histogram diagram for two layer attributes "attribute_1" and "attribute_2":

def setHistogram(layer):
        # Set histogram and diagram settings:
        hist = QgsHistogramDiagram()
        ds = QgsDiagramSettings()
        dColors = {'attribute_1': QColor("cyan"),
                   'attribute_2': QColor("red")}
        ds.categoryColors = dColors.values()
        ds.categoryAttributes = dColors.keys()
        ds.font = QFont("Helvetica", 12)
        ds.transparency = 0
        ds.size = QSizeF(50.0, 50.0)
        ds.sizeType = 0
        ds.labelPlacementMethod = 1
        ds.scaleByArea = True
        ds.minimumSize = 0
        ds.BackgroundColor = QColor(255, 255, 255, 255)
        ds.PenColor = QColor("black")
        ds.penWidth = 0

        # Set renderer:
        renderer = QgsLinearlyInterpolatedDiagramRenderer()
        renderer.setUpperValue(1000)  # Here you should set the maximum value of both attributes
        renderer.setUpperSize(QSizeF(20, 20))
        renderer.setLowerValue(0)
        renderer.setLowerSize(QSizeF(0, 0))
        renderer.setDiagram(hist)
        renderer.setDiagramSettings(ds)
        renderer.setClassificationAttribute(0)

        # Set diagram layer settings:
        layer.setDiagramRenderer(renderer)
        dls = QgsDiagramLayerSettings()
        dls.dist = 0
        dls.priority = 0
        dls.xPosColumn = -1
        dls.yPosColumn = -1
        dls.placement = 0

        layer.setDiagramLayerSettings(dls)
        layer.triggerRepaint()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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