Code snippets are bits of re-usable code submitted by the ReportLab community. We don't promise that they are accurate, up-to-date or correct - use them at your own risk!
We'd love it if you could participate by submitting your own snippets and sharing your experience with others by commenting. You will need to create a user account by filling out our very simple form.
View all snippets
Customized Labels for Pie Chart
In this snippet, we will Illustrate how to create customized labels when drawing a pie chart. This functionality allows you to fully configure the following label properties:
- Font name and size (also customizable with simple labels)
- Size, vertical and horizontal offset (to push or pull the label box in any direction)
- Padding (to limit the space occupied by the label text within the label box)
- Border and filling color of the label box
- Border and color of the label text
- Angle of the label (to tilt the label)
- Anchor point of the label (where the label will be located related to the chart)
All properties below can be specified for each slice independently. In addition, the use of customized labels allows you to add simple pointers, and to change the color and line width of these pointers. Customized labels are also compatible with Side Labels.
How to use Customized Labels in Drawing Editor
While editing a chart using ReportLab Drawing Editor, you need to activate customized labels by turning off simple labels: simpleLabels = 0
Label properties can then be changed by opening the option slices. You can specify a property globally, in this case it will apply to all slices: label_height = 10 or you can specify this property for only one slice: [0].label_height = 10
#Autogenerated by ReportLab guiedit do not edit from reportlab.graphics.charts.piecharts import Pie from reportlab.lib.colors import darkcyan, blue, cyan from reportlab.graphics.charts.legends import Legend from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin from reportlab.lib.styles import black, white from reportlab.graphics.charts.textlabels import Label class Drawing_000(_DrawingEditorMixin,Drawing): def __init__(self,width=400,height=200,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) self._add(self,Pie(),name=None,validate=None,desc=None) # Set the size and location of the chart self.contents[0].width = 120 self.contents[0].height = 120 self.contents[0].y = 40 self.contents[0].x = 130 # Fill in the chart with sample data and labels self.contents[0].data = [1, 2, 1.8, 4, 1, 8] self.contents[0].labels = ['Label1','Label2','Label3','Label4','Label5','Label6','Label7'] # Make the slices pop out and add a border to the slices self.contents[0].slices.popout = 5 self.contents[0].slices.strokeWidth = 1 # Set the font size and position the label self.contents[0].slices.fontSize = 14 self.contents[0].slices.labelRadius = 1.25 # Turn off simple labels so we can use customized labels self.contents[0].simpleLabels = 0 # Define a simple pointer for all labels self.contents[0].slices.label_simple_pointer = 1 # Define a first label with a black border, # white text and dark cyan background self.contents[0].slices[0].label_boxStrokeColor = black self.contents[0].slices[0].fontColor = white self.contents[0].slices[0].label_boxFillColor = darkcyan # Change the anchor point of the label box self.contents[0].slices[0].label_boxAnchor = 's' # Turn off pointer for the third label, change the text color self.contents[0].slices[2].label_simple_pointer = 0 self.contents[0].slices[2].fontColor = blue # and position it closer to the chart self.contents[0].slices[2].labelRadius = 1.05 # Turn off pointer for the fourth label # and position it within the chart self.contents[0].slices[3].label_simple_pointer = 0 self.contents[0].slices[3].labelRadius = 0.25 # Define a fifth label with a black border, text padding # and position it closer to the chart self.contents[0].slices[4].label_boxStrokeColor = black self.contents[0].slices[4].label_height = 22 self.contents[0].slices[4].label_topPadding = 2 self.contents[0].slices[4].label_leftPadding = 6 self.contents[0].slices[4].label_rightPadding = 6 self.contents[0].slices[4].labelRadius = 1.15 # Change font size for the sixth label self.contents[0].slices[5].fontSize = 20 if __name__=="__main__": #NORUNTESTS Drawing_000().save(formats=['png'],outDir='.',fnRoot=None)