Skip to main content
added 656 characters in body
Source Link
Praful Bagai
  • 17.6k
  • 56
  • 150
  • 281

EDIT

Pie Chart

I'm referring this link for generation of Pie Chart. In this the colors are pre-defined for each data, ie 10 colors for 10 data points. What if I have 11 data points and then there will be no color for that 11th data point. I want something dynamic which should automatically pick a color by itself and plot it on the pie.

I found this link and this link, but the colors over here are not catchy and not at all good.

Can you please brief me about how to get catchy colors in pie graph?

Thanks

EDIT

Pie Chart

I'm referring this link for generation of Pie Chart. In this the colors are pre-defined for each data, ie 10 colors for 10 data points. What if I have 11 data points and then there will be no color for that 11th data point. I want something dynamic which should automatically pick a color by itself and plot it on the pie.

I found this link and this link, but the colors over here are not catchy and not at all good.

Can you please brief me about how to get catchy colors in pie graph?

Thanks

Notice removed Draw attention by Praful Bagai
Bounty Ended with user3850's answer chosen by Praful Bagai
Notice added Draw attention by Praful Bagai
Bounty Started worth 100 reputation by Praful Bagai
deleted 417 characters in body; edited tags; edited title
Source Link
Praful Bagai
  • 17.6k
  • 56
  • 150
  • 281

Generating Reports using ReportLabs in DjangoPython

I've the following code where I'm generatingdrawing a report having Pie Chartvertical bar graph and a line graph as well inside a PDF.

How do I save these 2 graphs in 2 different pages of the PDF. I saw that it can be done using -

c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

But, instead of using Canvas, I'm using Drawing object in which showPage() method does not exist.

How do I referredsave the 2 graphs in 2 different pages of the PDF? Right the second graph(line chart) overlaps the first graph this link for(vertical bar chart), thereby hindering the tutorials purposebar chart.

Here is my code.code.

from reportlab.lib.pagesizes import cm, inch  
from reportlab.graphics.charts.piechartsshapes import Pie  Drawing
from reportlab.graphics.charts.legendsbarcharts import Legend  VerticalBarChart
from reportlab.lib.colors import Color, PCMYKColor  
from reportlab.graphics.shapesdrawing import= Drawing(400, _DrawingEditorMixin  


def generate_report(request200):
  data = MyDrawing().save[
(formats=['pdf'13,'png'] 5,outDir='.' 20,fnRoot=None) 22, 


class37, MyDrawing(_DrawingEditorMixin45,Drawing): 19, 4),
    def __init__(self,width=400,height=200,*args14,**kw):  
       6, apply(Drawing.__init__21,(self 23,width 38,height)+args 46,kw) 20, 5)
      ]
bc = self._add(self,PieVerticalBarChart(),name='chart',validate=None,desc=None)  
        self.chartbc.x                        = 20  50
        self.chartbc.y                        = (self.height-self.chart.height)/2  50
        self.chart.slicesbc.strokeWidth      height = 1  125
        self.chart.slicesbc.popout           width = 1  300
        self.chartbc.direction               data = 'clockwise'  data
        self.chart#bc.width                   strokeColor = self.chartcolors.height  black
        selfbc.chartvalueAxis.startAngle              valueMin = 90  
        self._add(self,Legend(),name='legend',validate=None,desc=None)  0
        selfbc.legendvalueAxis.x                      valueMax = width - 20  50
        selfbc.legendvalueAxis.y                      valueStep = 0  10
        selfbc.legendcategoryAxis.labels.boxAnchor               = 'se'  'ne'
        selfbc.legendcategoryAxis.subCols[1]labels.align       dx = 'right'  8
        selfbc.chartcategoryAxis.height                  labels.dy = 99  -2
        selfbc.chartcategoryAxis.width                   labels.angle = 99  30
        selfbc.legendcategoryAxis.alignment              categoryNames = 'right'  ['Jan-99','Feb-99','Mar-99',
        self'Apr-99','May-99','Jun-99','Jul-99','Aug-99']

drawing.legendadd(bc)
drawing.columnMaximum           = 9  save()
       
from selfreportlab.legendgraphics.fontSize                = 13charts.lineplots import LinePlot
        # these data can be read from external sourcesreportlab.graphics.widgets.markers import makeMarker
     
drawing = Drawing(400, 200)
data = [
(5(1, 41), 3(2, 2), (2.5,1)  
        categories =, ('sale','marketing'3,'travel'3),'health' (4,'misc'5)  ),
        colors = [PCMYKColor(100(1,502),30 (2,x3) for x in, (502.5,402),30 (3.5,205),10 (4,6)) 
]
lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = self.chart300
lp.data         = data  
      lp.joinedLines = self1
lp.chartlines[0].labels      symbol = mapmakeMarker(str, self'FilledCircle')
lp.chartlines[1].data)symbol = makeMarker('Circle')
lp.lineLabelFormat = '%2.0f'
#lp.strokeColor = colors.black
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax = self5
lp.legendxValueAxis.colorNamePairsvalueSteps = zip(colors[1, categories)2, 2.5, 3, 4, 5]
lp.xValueAxis.labelTextFormat = '%2.1f'
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 7
lp.yValueAxis.valueSteps = for[1, i2, color3, in5, enumerate6]
drawing.add(colorslp): self
drawing.chartsave()
drawing.slices[i]save(formats=['pdf'],outDir='.fillColor  = color ',fnRoot=None) 

But, I'm getting an error saying Can't setFont(Times-Roman) missing the T1 files? Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be string, not None.

So, my first question is Why I'm getting this error. Where I'm going wrong?

My second question - Is this the standard way to create a report in Django? Is my aprroach to create the report correct?

Generating Reports using ReportLabs in Django

I'm generating a report having Pie Chart inside it. I referred this link for the tutorials purpose.

Here is my code.

from reportlab.lib.pagesizes import cm, inch  
from reportlab.graphics.charts.piecharts import Pie  
from reportlab.graphics.charts.legends import Legend  
from reportlab.lib.colors import Color, PCMYKColor  
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin  


def generate_report(request):
    MyDrawing().save(formats=['pdf','png'],outDir='.',fnRoot=None)  


class MyDrawing(_DrawingEditorMixin,Drawing):  
    def __init__(self,width=400,height=200,*args,**kw):  
        apply(Drawing.__init__,(self,width,height)+args,kw)  
        self._add(self,Pie(),name='chart',validate=None,desc=None)  
        self.chart.x                        = 20  
        self.chart.y                        = (self.height-self.chart.height)/2  
        self.chart.slices.strokeWidth       = 1  
        self.chart.slices.popout            = 1  
        self.chart.direction                = 'clockwise'  
        self.chart.width                    = self.chart.height  
        self.chart.startAngle               = 90  
        self._add(self,Legend(),name='legend',validate=None,desc=None)  
        self.legend.x                       = width - 20  
        self.legend.y                       = 0  
        self.legend.boxAnchor               = 'se'  
        self.legend.subCols[1].align        = 'right'  
        self.chart.height                   = 99  
        self.chart.width                    = 99  
        self.legend.alignment               = 'right'  
        self.legend.columnMaximum           = 9  
        self.legend.fontSize                = 13  
        # these data can be read from external sources  
        data = (5, 4, 3, 2, 1)  
        categories = ('sale','marketing','travel','health','misc')  
        colors = [PCMYKColor(100,50,30,x) for x in (50,40,30,20,10)]  
        self.chart.data         = data  
        self.chart.labels       = map(str, self.chart.data)  
        self.legend.colorNamePairs = zip(colors, categories)  
        for i, color in enumerate(colors): self.chart.slices[i].fillColor  = color  

But, I'm getting an error saying Can't setFont(Times-Roman) missing the T1 files? Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be string, not None.

So, my first question is Why I'm getting this error. Where I'm going wrong?

My second question - Is this the standard way to create a report in Django? Is my aprroach to create the report correct?

Generating Reports using ReportLabs in Python

I've the following code where I'm drawing a vertical bar graph and a line graph as well inside a PDF.

How do I save these 2 graphs in 2 different pages of the PDF. I saw that it can be done using -

c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

But, instead of using Canvas, I'm using Drawing object in which showPage() method does not exist.

How do I save the 2 graphs in 2 different pages of the PDF? Right the second graph(line chart) overlaps the first graph (vertical bar chart), thereby hindering the bar chart.

Here is my code.

from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart

drawing = Drawing(400, 200)
data = [
(13, 5, 20, 22, 37, 45, 19, 4),
(14, 6, 21, 23, 38, 46, 20, 5)
]
bc = VerticalBarChart()
bc.x = 50
bc.y = 50
bc.height = 125
bc.width = 300
bc.data = data
#bc.strokeColor = colors.black
bc.valueAxis.valueMin = 0
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 10
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 8
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
bc.categoryAxis.categoryNames = ['Jan-99','Feb-99','Mar-99',
'Apr-99','May-99','Jun-99','Jul-99','Aug-99']

drawing.add(bc)
drawing.save()

from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.widgets.markers import makeMarker

drawing = Drawing(400, 200)
data = [
((1,1), (2,2), (2.5,1), (3,3), (4,5)),
((1,2), (2,3), (2.5,2), (3.5,5), (4,6)) 
]
lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp.data = data
lp.joinedLines = 1
lp.lines[0].symbol = makeMarker('FilledCircle')
lp.lines[1].symbol = makeMarker('Circle')
lp.lineLabelFormat = '%2.0f'
#lp.strokeColor = colors.black
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax = 5
lp.xValueAxis.valueSteps = [1, 2, 2.5, 3, 4, 5]
lp.xValueAxis.labelTextFormat = '%2.1f'
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 7
lp.yValueAxis.valueSteps = [1, 2, 3, 5, 6]
drawing.add(lp)
drawing.save()
drawing.save(formats=['pdf'],outDir='.',fnRoot=None) 
Source Link
Praful Bagai
  • 17.6k
  • 56
  • 150
  • 281

Generating Reports using ReportLabs in Django

I'm generating a report having Pie Chart inside it. I referred this link for the tutorials purpose.

Here is my code.

from reportlab.lib.pagesizes import cm, inch  
from reportlab.graphics.charts.piecharts import Pie  
from reportlab.graphics.charts.legends import Legend  
from reportlab.lib.colors import Color, PCMYKColor  
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin  


def generate_report(request):
    MyDrawing().save(formats=['pdf','png'],outDir='.',fnRoot=None)  


class MyDrawing(_DrawingEditorMixin,Drawing):  
    def __init__(self,width=400,height=200,*args,**kw):  
        apply(Drawing.__init__,(self,width,height)+args,kw)  
        self._add(self,Pie(),name='chart',validate=None,desc=None)  
        self.chart.x                        = 20  
        self.chart.y                        = (self.height-self.chart.height)/2  
        self.chart.slices.strokeWidth       = 1  
        self.chart.slices.popout            = 1  
        self.chart.direction                = 'clockwise'  
        self.chart.width                    = self.chart.height  
        self.chart.startAngle               = 90  
        self._add(self,Legend(),name='legend',validate=None,desc=None)  
        self.legend.x                       = width - 20  
        self.legend.y                       = 0  
        self.legend.boxAnchor               = 'se'  
        self.legend.subCols[1].align        = 'right'  
        self.chart.height                   = 99  
        self.chart.width                    = 99  
        self.legend.alignment               = 'right'  
        self.legend.columnMaximum           = 9  
        self.legend.fontSize                = 13  
        # these data can be read from external sources  
        data = (5, 4, 3, 2, 1)  
        categories = ('sale','marketing','travel','health','misc')  
        colors = [PCMYKColor(100,50,30,x) for x in (50,40,30,20,10)]  
        self.chart.data         = data  
        self.chart.labels       = map(str, self.chart.data)  
        self.legend.colorNamePairs = zip(colors, categories)  
        for i, color in enumerate(colors): self.chart.slices[i].fillColor  = color  

But, I'm getting an error saying Can't setFont(Times-Roman) missing the T1 files? Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be string, not None.

So, my first question is Why I'm getting this error. Where I'm going wrong?

My second question - Is this the standard way to create a report in Django? Is my aprroach to create the report correct?