PyMuPDF now supports drawing pie charts on a PDF page.
Important parameters for the function are center of the circle, one of the two arc's end points and the angle of the circular sector. The function will draw the pie piece (in a variety of options) and return the arc's calculated other end point for any subsequent processing.
This example creates a chart of the parliament seat distribution for political parties in the current German Bundestag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #!/usr/bin/python
# -*- coding: latin-1 -*-
from __future__ import print_function
import fitz
import sys
#==============================================================================
# Pie Chart program - semi circle version
#==============================================================================
from fitz.utils import getColor # for getting RGB colors by name
doc = fitz.open() # new empty PDF
doc.insertPage() # creates an ISO-A4 portrait page
page = doc[-1] # this is the page
# title of the page
title = "Sitzverteilung nach der Bundestagswahl 2013"
# pie chart center and point of 1st data pie
center = fitz.Point(200, 250)
point = fitz.Point(100, 250) # will cycle through table data
# this is the radius
radius = abs(point - center)
blue = getColor("blue") # we need some colors
white = getColor("white")
lineheight = 20 # legend line height
ts_v = 150 # vertical start of legend block
ts_h = center.x + radius + 50 # horizontal coord of legend block
# these are the data to visualize:
# number of seats of political parties in German parliament since 2013
table = ( # seats, party color & name
(64, "violetred", "Die Linke"),
(193, "red", "SPD"),
(63, "green", "Die GrĂ¼nen"),
(253, "black", "CDU"),
(56, "dodgerblue", "CSU"),
(1, "gray", "fraktionslos"),
)
seats = float(sum([c[0] for c in table])) # total seats
stitle = "Bundestagssitze insgesamt: %i" % (seats,)
page.insertText(fitz.Point(72, 72),title, fontsize = 14, color = blue)
page.drawLine(fitz.Point(72, 80), fitz.Point(550, 80), color = blue)
page.insertText(fitz.Point(ts_h - 30, ts_v - 30), stitle,
fontsize = 13, color = blue)
# draw the table data
for i, c in enumerate(table):
beta = c[0] / seats * 180 # express seats as semi circle angles
color = getColor(c[1]) # avoid multiple color lookups
# the method delivers point of other end of the constructed arc
# we will use it as input for next round
point = page.drawSector(center, point, beta, color = white,
fullSector = True, fill = color)
text = "%s, %i %s" % (c[2], c[0], "Sitze" if c[0] > 1 else "Sitz")
pos = fitz.Point(ts_h, ts_v + i*lineheight)
page.insertText(pos, text, color = blue)
tl = fitz.Point(pos.x - 30, ts_v - 10 + i*lineheight)
br = fitz.Point(pos.x - 10, ts_v + i*lineheight)
rect = fitz.Rect(tl, br) # legend color bar
page.drawRect(rect, fill = color), color = color)
# overlay center of circle with white to simulate an auditorium
page.drawCircle(center, radius - 70, color = white, fill = white)
doc.save("piechart2.pdf")
|
Have a look at https://github.com/rk700/PyMuPDF/edit/master/demo/ to see a full circle version and images of example outputs.