diagtest.py

Code snippet set up for the python console - Angus Carr, 2013-02-27 08:42 PM

Download (2.12 KB)

 
1
from PyQt4.QtCore import *
2
from PyQt4.QtGui import *
3
from qgis.core import *
4

    
5

    
6
l = qgis.utils.iface.activeLayer()
7
#print l.diagramRenderer().rendererName()
8

    
9
#line numbers in comments refer to 
10
#   https://github.com/qgis/Quantum-GIS/blob/master/src/app/qgsdiagramproperties.cpp
11
diagram = QgsPieDiagram() #500
12
ds = QgsDiagramSettings() #507
13
ds.font = QFont( "Helvetica", 12 ) #508
14
ds.transparency = 0 #509 - not transparent...
15
#dColors = dict() #511 - keys are the field numbers, values are the QColors
16
#categoryAttributes = dict() #512 - field numbers of fields
17
dColors = {4:QColor("cyan"), 5:QColor("darkCyan"), 6:QColor("red"), 7:QColor("magenta"), 8:QColor("darkYellow"), 9:QColor("blue"), 10:QColor("darkGreen"), 11:QColor("darkRed"), 12:QColor("green"), 13:QColor("darkBlue"), 14:QColor("grey"), 15:QColor("darkMagenta")} #or QColor(20,60,106)
18
ds.categoryColors = dColors.values() #520
19
ds.categoryIndices = dColors.keys()  #521
20
ds.size = QSizeF(100.0, 100.0) #522
21
ds.sizeType = 0 #523 - mm(0), map units (1)
22
ds.labelPlacementMethod = 1 #524 - magic - from an existing example...
23
ds.scaleByArea = False #525
24
ds.minimumSize = 0 #533
25
ds.BackgroundColor = QColor(255,255,255,0) #Transparent White #536
26
ds.PenColor = QColor("black") #537
27
ds.penWidth = 0 #538
28
ds.minScaleDenominator = -1; #546
29
ds.maxScaleDenominator = -1; #547
30
#ds.diagramOrientation = 2 #551 - May only be required for histograms
31
#ds.barWidth = 5.0 #553
32
#We want a linear size interpolated version, so that Total_BA = 0 means they disappear
33
#564-572
34
dr = QgsLinearlyInterpolatedDiagramRenderer()
35
dr.setLowerValue( 0.0 )
36
dr.setLowerSize( QSizeF( 0.0, 0.0 ) )
37
dr.setUpperValue( 50 )
38
dr.setUpperSize( QSizeF(100,100) )
39
dr.setClassificationAttribute( 16 )
40
dr.setDiagram( diagram )
41
dr.setDiagramSettings( ds )
42

    
43
#And now finally put it into the layer... :-)
44
l.setDiagramRenderer( dr ) #572
45

    
46
dls = QgsDiagramLayerSettings() #575
47
dls.dist = 0
48
dls.priority = 0
49
dls.xPosColumn = -1  #585
50
dls.yPosColumn = -1
51
dls.placement = 0 #588
52
l.setDiagramLayerSettings( dls ) #593
53

    
54
#Refresh map
55
qgis.utils.iface.mapCanvas().refresh()
56

    
57
#Refresh map
58
if hasattr(l, "setCacheImage"): 
59
        l.setCacheImage(None)
60
l.triggerRepaint()