from reportlab.graphics.charts.barcharts import VerticalBarChart
Here we have two set of data used to generate bar graphs.
data = [
(13, 15, 20, 22, 37, 42, 19, 4),
(14, 16, 21, 23, 38, 41, 20, 5)
]
Using this data the graph is here.
from reportlab.lib import colors
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
d = Drawing(600, 400) # width , height
bc = VerticalBarChart()
data = [
(13, 15, 20, 22, 37, 42, 19, 4),
(14, 16, 21, 23, 38, 41, 20, 5)
]
bc.x = 50 # horizontal position
bc.y = 50 # vertical position
bc.height = 340 # height of the chart area
bc.width = 530 # width of the chart area
bc.data = data
bc.strokeColor = colors.black # bar edges colour
bc.bars[0].fillColor = colors.yellow # first member of group bar
bc.bars[1].fillColor = colors.lightgreen # second member of group
bc.groupSpacing = 10 # gap between groups
bc.barSpacing = 2.5 # gap within groups ( among members )
bc.valueAxis.valueMin = 0 # value Axis
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 10
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 5
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
#bc.categoryAxis.style = 'stacked' # stacked bar graph, parallel
bc.categoryAxis.categoryNames = ['Jan-22','Feb-22','Mar-22',
'Apr-22','May-22','Jun-22','Jul-22','Aug-22']
d.add(bc, '')
from reportlab.graphics import renderPDF
renderPDF.drawToFile(d, my_path, '')
bc.fillColor=colors.yellow # background colour
bc.bars[0].fillColor=colors.yellow #first member of group bar
bc.bars[1].fillColor=colors.lightgreen # second member of group bar
bc.bars[(0, 0)].fillColor = colors.blue # left bar of first group
bc.bars[(0, 1)].fillColor = colors.yellow # left bar of second group
bc.bars[(1,2)].fillColor=colors.red # right bar of 3rd group
from reportlab.lib import colors
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import HorizontalBarChart
d=Drawing(620,400) # width , height
#bc=VerticalBarChart()
bc=HorizontalBarChart()
data = [
(13, 15, 20, 22, 37, 42, 19, 4),
(14, 16, 21, 23, 38, 41, 20, 5)
]
bc.x = 50 # horizontal position
bc.y = 50 # vertical position
bc.height = 345 # height of the chart area
bc.width = 545 # width of the chart area
bc.data = data
bc.strokeColor = colors.black
#bc.fillColor=colors.yellow
bc.bars[0].fillColor=colors.yellow
bc.bars[1].fillColor=colors.lightgreen
bc.bars[(0,5)].fillColor=colors.red
bc.groupSpacing = 8
bc.barSpacing = 2
bc.valueAxis.valueMin = 0
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 5
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = -10
bc.categoryAxis.labels.dy = 5
bc.categoryAxis.labels.angle = 30
bc.categoryAxis.categoryNames = ['Jan-22','Feb-22','Mar-22',
'Apr-22','May-22','Jun-22','Jul-22','Aug-22']
d.add(bc, '')
from reportlab.graphics import renderPDF
renderPDF.drawToFile(d, my_path, '')
bc.categoryAxis.style='stacked'
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.