"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.32/tests/test_graphics_charts.py" (24 Oct 2019, 36708 Bytes) of package /linux/privat/reportlab-3.5.32.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_graphics_charts.py": 3.5.31_vs_3.5.32.

    1 #Copyright ReportLab Europe Ltd. 2000-2017
    2 #see license.txt for license details
    3 """
    4 Tests for chart class.
    5 """
    6 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
    7 setOutDir(__name__)
    8 
    9 import os, sys, copy
   10 from os.path import join, basename, splitext
   11 import unittest
   12 from reportlab.lib import colors
   13 from reportlab.lib.units import cm
   14 from reportlab.lib.pagesizes import A4
   15 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
   16 from reportlab.lib.validators import Auto
   17 from reportlab.pdfgen.canvas import Canvas
   18 from reportlab.graphics.shapes import *
   19 from reportlab.graphics.charts.textlabels import Label, _text2Path
   20 from reportlab.platypus.flowables import Spacer, PageBreak
   21 from reportlab.platypus.paragraph import Paragraph
   22 from reportlab.platypus.xpreformatted import XPreformatted
   23 from reportlab.platypus.frames import Frame
   24 from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
   25 from reportlab.graphics.charts.barcharts import VerticalBarChart
   26 from reportlab.graphics.charts.linecharts import HorizontalLineChart
   27 from reportlab.graphics.charts.lineplots import LinePlot, GridLinePlot
   28 from reportlab.graphics.charts.piecharts import Pie
   29 from reportlab.graphics.charts.legends import Legend
   30 from reportlab.graphics.charts.spider import SpiderChart
   31 from reportlab.graphics.widgets.markers import makeMarker
   32 
   33 try:
   34     from reportlab.graphics import _renderPM
   35 except ImportError:
   36     _renderPM = None
   37 
   38 def getFontName():
   39     try:
   40         from reportlab.pdfbase.pdfmetrics import registerFont
   41         from reportlab.pdfbase.ttfonts import TTFont
   42         fontName = 'Vera'
   43         registerFont(TTFont(fontName, "Vera.ttf"))
   44     except:
   45         fontName = 'Helvetica'
   46     return fontName
   47 fontName = getFontName()
   48 
   49 def myMainPageFrame(canvas, doc):
   50     "The page frame used for all PDF documents."
   51 
   52     canvas.saveState()
   53 
   54     #canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
   55     canvas.setFont('Times-Roman', 12)
   56     pageNumber = canvas.getPageNumber()
   57     canvas.drawString(10*cm, cm, str(pageNumber))
   58 
   59     canvas.restoreState()
   60 
   61 
   62 class MyDocTemplate(BaseDocTemplate):
   63     "The document template used for all PDF documents."
   64 
   65     _invalidInitArgs = ('pageTemplates',)
   66 
   67     def __init__(self, filename, **kw):
   68         frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
   69         self.allowSplitting = 0
   70         BaseDocTemplate.__init__(self, filename, **kw)
   71         template = PageTemplate('normal', [frame1], myMainPageFrame)
   72         self.addPageTemplates(template)
   73 
   74 
   75 def sample1bar(data=[(13, 5, 20, 22, 37, 45, 19, 4)]):
   76     drawing = Drawing(400, 200)
   77 
   78     bc = VerticalBarChart()
   79     bc.x = 50
   80     bc.y = 50
   81     bc.height = 125
   82     bc.width = 300
   83     bc.data = data
   84 
   85     bc.strokeColor = colors.black
   86 
   87     bc.valueAxis.valueMin = 0
   88     bc.valueAxis.valueMax = 60
   89     bc.valueAxis.valueStep = 15
   90 
   91     bc.categoryAxis.labels.boxAnchor = 'ne'
   92     bc.categoryAxis.labels.dx = 8
   93     bc.categoryAxis.labels.dy = -2
   94     bc.categoryAxis.labels.angle = 30
   95     bc.categoryAxis.labels.fontName = fontName
   96     bc.barLabels.fontName = fontName
   97     bc.barLabelFormat = u'\xc5%s'
   98 
   99     catNames = u'J\xe4n Feb M\xe4r \xc4pr M\xe4y J\xfcn J\xfcl \xc4\xfcg'.split( ' ')
  100     bc.barLabelArray = catNames
  101     catNames = [n+'-99' for n in catNames]
  102     bc.categoryAxis.categoryNames = catNames
  103     drawing.add(bc)
  104 
  105     return drawing
  106 
  107 
  108 def sample2bar(data=[(13, 5, 20, 22, 37, 45, 19, 4),
  109                   (14, 6, 21, 23, 38, 46, 20, 5)]):
  110     return sample1bar(data)
  111 
  112 
  113 def sample1line(data=[(13, 5, 20, 22, 37, 45, 19, 4)]):
  114     drawing = Drawing(400, 200)
  115 
  116     bc = HorizontalLineChart()
  117     bc.x = 50
  118     bc.y = 50
  119     bc.height = 125
  120     bc.width = 300
  121     bc.data = data
  122 
  123     bc.strokeColor = colors.black
  124 
  125     bc.valueAxis.valueMin = 0
  126     bc.valueAxis.valueMax = 60
  127     bc.valueAxis.valueStep = 15
  128 
  129     bc.categoryAxis.labels.boxAnchor = 'ne'
  130     bc.categoryAxis.labels.dx = 8
  131     bc.categoryAxis.labels.dy = -2
  132     bc.categoryAxis.labels.angle = 30
  133 
  134     catNames = 'Jan Feb Mar Apr May Jun Jul Aug'.split(' ')
  135     catNames = [n+'-99' for n in catNames]
  136     bc.categoryAxis.categoryNames = catNames
  137     drawing.add(bc)
  138 
  139     return drawing
  140 
  141 
  142 def sample2line(data=[(13, 5, 20, 22, 37, 45, 19, 4),
  143                   (14, 6, 21, 23, 38, 46, 20, 5)]):
  144     return sample1line(data)
  145 
  146 
  147 def sample3(drawing=None):
  148     "Add sample swatches to a diagram."
  149 
  150     d = drawing or Drawing(400, 200)
  151 
  152     swatches = Legend()
  153     swatches.alignment = 'right'
  154     swatches.x = 80
  155     swatches.y = 160
  156     swatches.deltax = 60
  157     swatches.dxTextSpace = 10
  158     swatches.columnMaximum = 4
  159     items = [(colors.red, 'before'), (colors.green, 'after')]
  160     swatches.colorNamePairs = items
  161 
  162     d.add(swatches, 'legend')
  163 
  164     return d
  165 
  166 
  167 def sample4pie():
  168     width = 300
  169     height = 150
  170     d = Drawing(width, height)
  171     pc = Pie()
  172     pc.x = 150
  173     pc.y = 50
  174     pc.data = [1, 50, 100, 100, 100, 100, 100, 100, 100, 50]
  175     pc.labels = u'0 \xe4 b c d e f g h i'.split()
  176     pc.slices.strokeWidth=0.5
  177     pc.slices[3].popout = 20
  178     pc.slices[3].strokeWidth = 2
  179     pc.slices[3].strokeDashArray = [2,2]
  180     pc.slices[3].labelRadius = 1.75
  181     pc.slices[3].fontColor = colors.red
  182     pc.slices[1].fontName = fontName
  183     d.add(pc)
  184     legend = Legend()
  185     legend.x = width-5
  186     legend.y = height-5
  187     legend.dx = 20
  188     legend.dy = 5
  189     legend.deltax = 0
  190     legend.boxAnchor = 'nw'
  191     legend.colorNamePairs=Auto(chart=pc)
  192     d.add(legend)
  193     return d
  194 
  195 def autoLegender(i,chart,styleObj,sym='symbol'):
  196     if sym:
  197         setattr(styleObj[0],sym, makeMarker('Diamond',size=6))
  198         setattr(styleObj[1],sym,makeMarker('Square'))
  199     width = 300
  200     height = 150
  201     legend = Legend()
  202     legend.x = width-5
  203     legend.y = 5
  204     legend.dx = 20
  205     legend.dy = 5
  206     legend.deltay = 0
  207     legend.boxAnchor = 'se'
  208     if i=='col auto':
  209         legend.colorNamePairs[0]=(Auto(chart=chart),'auto chart=self.chart')
  210         legend.colorNamePairs[1]=(Auto(obj=chart,index=1),'auto  chart=self.chart index=1')
  211     elif i=='full auto':
  212         legend.colorNamePairs=Auto(chart=chart)
  213     elif i=='swatch set':
  214         legend.swatchMarker=makeMarker('Circle')
  215         legend.swatchMarker.size = 10
  216     elif i=='swatch auto':
  217         legend.swatchMarker=Auto(chart=chart)
  218     else:
  219         cnp = legend.colorNamePairs
  220         legend.fontName = fontName
  221         legend.colorNamePairs = [(cnp[0][0],u'r\xf6t')] + cnp[1:]
  222 
  223     d = Drawing(width,height)
  224     d.background = Rect(0,0,width,height,strokeWidth=1,strokeColor=colors.red,fillColor=None)
  225     m = makeMarker('Cross')
  226     m.x = width-5
  227     m.y = 5
  228     m.fillColor = colors.red
  229     m.strokeColor = colors.yellow
  230     d.add(chart)
  231     d.add(legend)
  232     d.add(m)
  233     return d
  234 
  235 def lpleg(i=None):
  236     chart = LinePlot()
  237     return autoLegender(i,chart,chart.lines)
  238 
  239 def hlcleg(i=None):
  240     chart = HorizontalLineChart()
  241     return autoLegender(i,chart,chart.lines)
  242 
  243 def bcleg(i=None):
  244     chart = VerticalBarChart()
  245     return autoLegender(i,chart,chart.bars,None)
  246 
  247 def pcleg(i=None):
  248     chart = Pie()
  249     return autoLegender(i,chart,chart.slices,None)
  250 
  251 def scleg(i=None):
  252     chart = SpiderChart()
  253     return autoLegender(i,chart,chart.strands,None)
  254 
  255 def plpleg(i=None):
  256     from reportlab.lib.colors import pink, red, green
  257     pie = Pie()
  258     pie.x = 0
  259     pie.y = 0
  260     pie.pointerLabelMode='LeftAndRight'
  261     pie.slices.label_boxStrokeColor      = red
  262     pie.simpleLabels = 0
  263     pie.sameRadii = 1
  264     pie.data = [1, 0.1, 1.7, 4.2,0,0]
  265     pie.labels = ['abcdef', 'b', 'c', 'd','e','fedcba']
  266     pie.strokeWidth=1
  267     pie.strokeColor=green
  268     pie.slices.label_pointer_piePad      = 6
  269     pie.width = 160
  270     pie.direction = 'clockwise'
  271     pie.pointerLabelMode  = 'LeftRight'
  272     return autoLegender(i,pie,pie.slices,None)
  273 
  274 def notFail(d):
  275     try:
  276         return d.getContents()
  277     except:
  278         import traceback
  279         traceback.print_exc()
  280         return None
  281 
  282 STORY = []
  283 styleSheet = getSampleStyleSheet()
  284 bt = styleSheet['BodyText']
  285 h1 = styleSheet['Heading1']
  286 h2 = styleSheet['Heading2']
  287 h3 = styleSheet['Heading3']
  288 
  289 def eps():
  290     try:
  291         from rlextra.graphics import renderPS_SEP
  292     except ImportError:
  293         return []
  294     else:
  295         return ['eps']
  296 
  297 def run_samples(L):
  298     outDir = outputfile('charts-out')
  299     for k,f,kind in L:
  300         d = f()
  301         if not isinstance(d,Drawing): continue
  302         d.save(formats=['pdf', 'gif', 'svg', 'ps', 'py']+eps(),outDir=outDir, fnRoot='test_graphics_charts_%s_%s' % (kind,k))
  303 
  304 class ChartTestCase(unittest.TestCase):
  305     "Test chart classes."
  306 
  307     @classmethod
  308     def setUpClass(cls):
  309         "Hook method for setting up the test fixture before exercising it."
  310         cls.story = []
  311         cls.story.append(Paragraph('Tests for chart classes', h1))
  312 
  313     @classmethod
  314     def tearDownClass(cls):
  315         "Hook method for deconstructing the test fixture after testing it."
  316 
  317         path=outputfile('test_graphics_charts.pdf')
  318         doc = MyDocTemplate(path)
  319         doc.build(cls.story)
  320 
  321     def test0(self):
  322         "Test bar charts."
  323 
  324         story = self.story
  325         story.append(Paragraph('Single data row', h2))
  326 
  327         story.append(Spacer(0, 0.5*cm))
  328         drawing = sample1bar()
  329         story.append(drawing)
  330         story.append(Spacer(0, 1*cm))
  331 
  332 
  333     def test1(self):
  334         "Test bar charts."
  335 
  336         story = self.story
  337         story.append(Paragraph('Double data row', h2))
  338 
  339         story.append(Spacer(0, 0.5*cm))
  340         drawing = sample2bar()
  341         story.append(drawing)
  342         story.append(Spacer(0, 1*cm))
  343 
  344 
  345     def test2(self):
  346         "Test bar charts."
  347 
  348         story = self.story
  349         story.append(Paragraph('Double data row with legend', h2))
  350 
  351         story.append(Spacer(0, 0.5*cm))
  352         drawing = sample2bar()
  353         drawing = sample3(drawing)
  354         story.append(drawing)
  355         story.append(Spacer(0, 1*cm))
  356 
  357 
  358     def test3(self):
  359         "Test line charts."
  360 
  361         story = self.story
  362         story.append(Paragraph('Single data row', h2))
  363 
  364         story.append(Spacer(0, 0.5*cm))
  365         drawing = sample1line()
  366         story.append(drawing)
  367         story.append(Spacer(0, 1*cm))
  368 
  369 
  370     def test4(self):
  371         "Test line charts."
  372 
  373         story = self.story
  374         story.append(Paragraph('Single data row', h2))
  375 
  376         story.append(Spacer(0, 0.5*cm))
  377         drawing = sample2line()
  378         story.append(drawing)
  379         story.append(Spacer(0, 1*cm))
  380 
  381     def test4b(self):
  382         story = self.story
  383         for code in (lpleg, hlcleg, bcleg, pcleg, scleg, plpleg):
  384             code_name = code.__code__.co_name
  385             for i in ('standard', 'col auto', 'full auto', 'swatch set', 'swatch auto'):
  386                 d = code(i)
  387                 assert notFail(d),'getContents failed for %s %s' % (code_name,i)
  388                 story.append(Paragraph('%s %s' % (i,code_name), h2))
  389                 story.append(Spacer(0, 0.5*cm))
  390                 story.append(code(i))
  391                 story.append(Spacer(0, 1*cm))
  392 
  393     def test4c(self):
  394         story = self.story
  395         d=Drawing(215,115)
  396         d.add(GridLinePlot(),name='chart')
  397         d.chart.y = 20
  398         story.append(Paragraph('GridLinePlot', h2))
  399         story.append(Spacer(0, 0.5*cm))
  400         story.append(d)
  401         story.append(Spacer(0, 1*cm))
  402 
  403     def test5(self):
  404         "Test pie charts."
  405 
  406         story = self.story
  407         story.append(Paragraph('Pie', h2))
  408 
  409         story.append(Spacer(0, 0.5*cm))
  410         drawing = sample4pie()
  411         story.append(drawing)
  412         story.append(Spacer(0, 1*cm))
  413 
  414 
  415     def test7(self):
  416         "Added some new side labelled pies"
  417 
  418         story = self.story
  419         story.append(Paragraph('Side Labelled Pie', h2))
  420         story.append(Spacer(0,1*cm))
  421         story.append(Paragraph('Here are two examples of side labelled pies.',bt))
  422 
  423         story.append(Spacer(0, 0.5*cm))
  424         from reportlab.graphics.charts.piecharts import sample5, sample6, sample7, sample8, sample9
  425         drawing5 = sample5()
  426         story.append(drawing5)
  427 
  428         story.append(Spacer(0, 0.5*cm))
  429         drawing9 = sample9()
  430         story.append(drawing9)
  431         story.append(Spacer(0, 1*cm))
  432 
  433         story.append(Paragraph('Moving the pie', h3))
  434         story.append(Paragraph('Here is a pie that has pie.x = 0 and is moved sideways in order to make space for the labels.', bt))
  435         story.append(Paragraph('The line represents x = 0',bt))
  436         story.append(Paragraph('This has not been implemented and is on line 863 in piecharts.py', bt))
  437 
  438         story.append(Spacer(0,0.5*cm))
  439         drawing6 = sample6()
  440         story.append(drawing6)
  441         story.append(Spacer(0,1*cm))
  442 
  443         story.append(Paragraph('Case with overlapping pointers', h3))
  444         story.append(Paragraph('If there are many slices then the pointer labels can end up overlapping as shown below.', bt))
  445 
  446         story.append(Spacer(0,0.5*cm))
  447         drawing7 = sample7()
  448         story.append(drawing7)
  449         story.append(Spacer(0,1*cm))
  450 
  451         story.append(Paragraph('Case with overlapping labels', h3))
  452         story.append(Paragraph('Labels overlap if they do not belong to adjacent pie slices.', bt))
  453 
  454         story.append(Spacer(0,0.5*cm))
  455         drawing8 = sample8()
  456         story.append(drawing8)
  457         story.append(Spacer(0,1*cm))
  458 
  459     @unittest.skipIf(not _renderPM,'no _renderPM')
  460     def test8(self):
  461         '''text _text2Path'''
  462         story = self.story
  463         story.append(Paragraph('Texts drawn using a Path', h3))
  464         story.append(Spacer(0,0.5*cm))
  465         P=_text2Path('Hello World from font Times-Roman!',x=10,y=20,fontName='Times-Roman',fontSize=20,strokeColor=colors.blue,strokeWidth=0.1,fillColor=colors.red)
  466         d = Drawing(400,50)
  467         d.add(P)
  468         story.append(d)
  469         P=_text2Path('Hello World from font Helvetica!',x=10,y=20,fontName='Helvetica',fontSize=20,strokeColor=colors.blue,strokeWidth=0.1,fillColor=colors.red)
  470         d = Drawing(400,50)
  471         d.add(P)
  472         story.append(d)
  473         story.append(Spacer(0,1*cm))
  474 
  475 
  476     def test999(self):
  477         #keep this last
  478         from reportlab.graphics.charts.piecharts import Pie, _makeSideArcDefs, intervalIntersection
  479         L = []
  480 
  481         def intSum(arcs,A):
  482             s = 0
  483             for a in A:
  484                 for arc in arcs:
  485                     i = intervalIntersection(arc[1:],a)
  486                     if i: s += i[1] - i[0]
  487             return s
  488 
  489         def subtest(sa,d):
  490             pc = Pie()
  491             pc.direction=d
  492             pc.startAngle=sa
  493             arcs = _makeSideArcDefs(sa,d)
  494             A = [x[1] for x in pc.makeAngles()]
  495             arcsum = sum([a[2]-a[1] for a in arcs])
  496             isum = intSum(arcs,A)
  497             mi = max([a[2]-a[1] for a in arcs])
  498             ni = min([a[2]-a[1] for a in arcs])
  499             l = []
  500             s = (arcsum-360)
  501             if s>1e-8: l.append('Arc length=%s != 360' % s)
  502             s = abs(isum-360)
  503             if s>1e-8: l.append('interval intersection length=%s != 360' % s)
  504             if mi>360: l.append('max interval intersection length=%s >360' % mi)
  505             if ni<0: l.append('min interval intersection length=%s <0' % ni)
  506             if l:
  507                 l.append('sa: %s d: %s' % (sa,d))
  508                 l.append('sidearcs: %s' % str(arcs))
  509                 l.append('Angles: %s' % A)
  510                 raise ValueError('piecharts._makeSideArcDefs failure\n%s' % '\n'.join(l))
  511 
  512         for d in ('anticlockwise','clockwise'):
  513             for sa in (225, 180, 135, 90, 45, 0, -45, -90):
  514                 subtest(sa,d)
  515 
  516     def test801(self):
  517         '''test for bitbucket issue 105 reported by Johann Du Toit'''
  518         from reportlab.graphics.charts.doughnut import Doughnut
  519         from reportlab.graphics import renderSVG
  520         d = Drawing(500, 500)
  521         pie = Doughnut()
  522         pie.data = [5]
  523         pie.labels = ['Only 1 Value','']
  524         d.add(pie)
  525         s = renderSVG.drawToString(d)
  526 
  527     def test_axes(self):
  528         from reportlab.graphics.charts.axes import YValueAxis, XValueAxis, LogYValueAxis, LogXValueAxis, LogYValueAxis, XCategoryAxis, YCategoryAxis
  529         # Sample functions.
  530         def sample0a():
  531             "Sample drawing with one xcat axis and two buckets."
  532 
  533             drawing = Drawing(400, 200)
  534 
  535             data = [(10, 20)]
  536 
  537             xAxis = XCategoryAxis()
  538             xAxis.setPosition(75, 75, 300)
  539             xAxis.configure(data)
  540             xAxis.categoryNames = ['Ying', 'Yang']
  541             xAxis.labels.boxAnchor = 'n'
  542             drawing.add(xAxis)
  543             return drawing
  544 
  545         def sample0b():
  546             "Sample drawing with one xcat axis and one bucket only."
  547 
  548             drawing = Drawing(400, 200)
  549 
  550             data = [(10,)]
  551 
  552             xAxis = XCategoryAxis()
  553             xAxis.setPosition(75, 75, 300)
  554             xAxis.configure(data)
  555             xAxis.categoryNames = ['Ying']
  556             xAxis.labels.boxAnchor = 'n'
  557             drawing.add(xAxis)
  558             return drawing
  559 
  560         def sample1():
  561             "Sample drawing containing two unconnected axes."
  562             from reportlab.graphics.shapes import _baseGFontNameB
  563             drawing = Drawing(400, 200)
  564             data = [(10, 20, 30, 42)]
  565             xAxis = XCategoryAxis()
  566             xAxis.setPosition(75, 75, 300)
  567             xAxis.configure(data)
  568             xAxis.categoryNames = ['Beer','Wine','Meat','Cannelloni']
  569             xAxis.labels.boxAnchor = 'n'
  570             xAxis.labels[3].dy = -15
  571             xAxis.labels[3].angle = 30
  572             xAxis.labels[3].fontName = _baseGFontNameB
  573             yAxis = YValueAxis()
  574             yAxis.setPosition(50, 50, 125)
  575             yAxis.configure(data)
  576             drawing.add(xAxis)
  577             drawing.add(yAxis)
  578             return drawing
  579 
  580         def sample4a():
  581             "Sample drawing, xvalue/yvalue axes, y connected at 100 pts to x."
  582             drawing = Drawing(400, 200)
  583             data = [(10, 20, 30, 42)]
  584             yAxis = YValueAxis()
  585             yAxis.setPosition(50, 50, 125)
  586             yAxis.configure(data)
  587             xAxis = XValueAxis()
  588             xAxis._length = 300
  589             xAxis.joinAxis = yAxis
  590             xAxis.joinAxisMode = 'points'
  591             xAxis.joinAxisPos = 100
  592             xAxis.configure(data)
  593             drawing.add(xAxis)
  594             drawing.add(yAxis)
  595             return drawing
  596 
  597         def sample4b():
  598             "Sample drawing, xvalue/yvalue axes, y connected at value 35 of x."
  599             drawing = Drawing(400, 200)
  600             data = [(10, 20, 30, 42)]
  601             yAxis = YValueAxis()
  602             yAxis.setPosition(50, 50, 125)
  603             yAxis.configure(data)
  604             xAxis = XValueAxis()
  605             xAxis._length = 300
  606             xAxis.joinAxis = yAxis
  607             xAxis.joinAxisMode = 'value'
  608             xAxis.joinAxisPos = 35
  609             xAxis.configure(data)
  610             drawing.add(xAxis)
  611             drawing.add(yAxis)
  612             return drawing
  613 
  614         def sample4c():
  615             "Sample drawing, xvalue/yvalue axes, y connected to bottom of x."
  616             drawing = Drawing(400, 200)
  617             data = [(10, 20, 30, 42)]
  618             yAxis = YValueAxis()
  619             yAxis.setPosition(50, 50, 125)
  620             yAxis.configure(data)
  621             xAxis = XValueAxis()
  622             xAxis._length = 300
  623             xAxis.joinAxis = yAxis
  624             xAxis.joinAxisMode = 'bottom'
  625             xAxis.configure(data)
  626             drawing.add(xAxis)
  627             drawing.add(yAxis)
  628             return drawing
  629 
  630         def sample4c1():
  631             "xvalue/yvalue axes, without drawing axis lines/ticks."
  632             drawing = Drawing(400, 200)
  633             data = [(10, 20, 30, 42)]
  634             yAxis = YValueAxis()
  635             yAxis.setPosition(50, 50, 125)
  636             yAxis.configure(data)
  637             yAxis.visibleAxis = 0
  638             yAxis.visibleTicks = 0
  639             xAxis = XValueAxis()
  640             xAxis._length = 300
  641             xAxis.joinAxis = yAxis
  642             xAxis.joinAxisMode = 'bottom'
  643             xAxis.configure(data)
  644             xAxis.visibleAxis = 0
  645             xAxis.visibleTicks = 0
  646             drawing.add(xAxis)
  647             drawing.add(yAxis)
  648             return drawing
  649 
  650         def sample4d():
  651             "Sample drawing, xvalue/yvalue axes, y connected to top of x."
  652             drawing = Drawing(400, 200)
  653             data = [(10, 20, 30, 42)]
  654             yAxis = YValueAxis()
  655             yAxis.setPosition(50, 50, 125)
  656             yAxis.configure(data)
  657             xAxis = XValueAxis()
  658             xAxis._length = 300
  659             xAxis.joinAxis = yAxis
  660             xAxis.joinAxisMode = 'top'
  661             xAxis.configure(data)
  662             drawing.add(xAxis)
  663             drawing.add(yAxis)
  664             return drawing
  665 
  666         def sample5a():
  667             "Sample drawing, xvalue/yvalue axes, y connected at 100 pts to x."
  668             drawing = Drawing(400, 200)
  669             data = [(10, 20, 30, 42)]
  670             xAxis = XValueAxis()
  671             xAxis.setPosition(50, 50, 300)
  672             xAxis.configure(data)
  673             yAxis = YValueAxis()
  674             yAxis.setPosition(50, 50, 125)
  675             yAxis.joinAxis = xAxis
  676             yAxis.joinAxisMode = 'points'
  677             yAxis.joinAxisPos = 100
  678             yAxis.configure(data)
  679             drawing.add(xAxis)
  680             drawing.add(yAxis)
  681             return drawing
  682 
  683         def sample5b():
  684             "Sample drawing, xvalue/yvalue axes, y connected at value 35 of x."
  685             drawing = Drawing(400, 200)
  686             data = [(10, 20, 30, 42)]
  687             xAxis = XValueAxis()
  688             xAxis.setPosition(50, 50, 300)
  689             xAxis.configure(data)
  690             yAxis = YValueAxis()
  691             yAxis.setPosition(50, 50, 125)
  692             yAxis.joinAxis = xAxis
  693             yAxis.joinAxisMode = 'value'
  694             yAxis.joinAxisPos = 35
  695             yAxis.configure(data)
  696             drawing.add(xAxis)
  697             drawing.add(yAxis)
  698             return drawing
  699 
  700         def sample5c():
  701             "Sample drawing, xvalue/yvalue axes, y connected at right of x."
  702             drawing = Drawing(400, 200)
  703             data = [(10, 20, 30, 42)]
  704             xAxis = XValueAxis()
  705             xAxis.setPosition(50, 50, 300)
  706             xAxis.configure(data)
  707             yAxis = YValueAxis()
  708             yAxis.setPosition(50, 50, 125)
  709             yAxis.joinAxis = xAxis
  710             yAxis.joinAxisMode = 'right'
  711             yAxis.configure(data)
  712             drawing.add(xAxis)
  713             drawing.add(yAxis)
  714             return drawing
  715 
  716         def sample5d():
  717             "Sample drawing, xvalue/yvalue axes, y connected at left of x."
  718             drawing = Drawing(400, 200)
  719             data = [(10, 20, 30, 42)]
  720             xAxis = XValueAxis()
  721             xAxis.setPosition(50, 50, 300)
  722             xAxis.configure(data)
  723             yAxis = YValueAxis()
  724             yAxis.setPosition(50, 50, 125)
  725             yAxis.joinAxis = xAxis
  726             yAxis.joinAxisMode = 'left'
  727             yAxis.configure(data)
  728             drawing.add(xAxis)
  729             drawing.add(yAxis)
  730             return drawing
  731 
  732         def sample6a():
  733             "Sample drawing, xcat/yvalue axes, x connected at top of y."
  734             drawing = Drawing(400, 200)
  735             data = [(10, 20, 30, 42)]
  736             yAxis = YValueAxis()
  737             yAxis.setPosition(50, 50, 125)
  738             yAxis.configure(data)
  739             xAxis = XCategoryAxis()
  740             xAxis._length = 300
  741             xAxis.configure(data)
  742             xAxis.joinAxis = yAxis
  743             xAxis.joinAxisMode = 'top'
  744             xAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  745             xAxis.labels.boxAnchor = 'n'
  746             drawing.add(xAxis)
  747             drawing.add(yAxis)
  748             return drawing
  749 
  750         def sample6b():
  751             "Sample drawing, xcat/yvalue axes, x connected at bottom of y."
  752             drawing = Drawing(400, 200)
  753             data = [(10, 20, 30, 42)]
  754             yAxis = YValueAxis()
  755             yAxis.setPosition(50, 50, 125)
  756             yAxis.configure(data)
  757             xAxis = XCategoryAxis()
  758             xAxis._length = 300
  759             xAxis.configure(data)
  760             xAxis.joinAxis = yAxis
  761             xAxis.joinAxisMode = 'bottom'
  762             xAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  763             xAxis.labels.boxAnchor = 'n'
  764             drawing.add(xAxis)
  765             drawing.add(yAxis)
  766             return drawing
  767 
  768         def sample6c():
  769             "Sample drawing, xcat/yvalue axes, x connected at 100 pts to y."
  770             drawing = Drawing(400, 200)
  771             data = [(10, 20, 30, 42)]
  772             yAxis = YValueAxis()
  773             yAxis.setPosition(50, 50, 125)
  774             yAxis.configure(data)
  775             xAxis = XCategoryAxis()
  776             xAxis._length = 300
  777             xAxis.configure(data)
  778             xAxis.joinAxis = yAxis
  779             xAxis.joinAxisMode = 'points'
  780             xAxis.joinAxisPos = 100
  781             xAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  782             xAxis.labels.boxAnchor = 'n'
  783             drawing.add(xAxis)
  784             drawing.add(yAxis)
  785             return drawing
  786 
  787         def sample6d():
  788             "Sample drawing, xcat/yvalue axes, x connected at value 20 of y."
  789             drawing = Drawing(400, 200)
  790             data = [(10, 20, 30, 42)]
  791             yAxis = YValueAxis()
  792             yAxis.setPosition(50, 50, 125)
  793             yAxis.configure(data)
  794             xAxis = XCategoryAxis()
  795             xAxis._length = 300
  796             xAxis.configure(data)
  797             xAxis.joinAxis = yAxis
  798             xAxis.joinAxisMode = 'value'
  799             xAxis.joinAxisPos = 20
  800             xAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  801             xAxis.labels.boxAnchor = 'n'
  802             drawing.add(xAxis)
  803             drawing.add(yAxis)
  804             return drawing
  805 
  806         def sample7a():
  807             "Sample drawing, xvalue/ycat axes, y connected at right of x."
  808             drawing = Drawing(400, 200)
  809             data = [(10, 20, 30, 42)]
  810             xAxis = XValueAxis()
  811             xAxis._length = 300
  812             xAxis.configure(data)
  813             yAxis = YCategoryAxis()
  814             yAxis.setPosition(50, 50, 125)
  815             yAxis.joinAxis = xAxis
  816             yAxis.joinAxisMode = 'right'
  817             yAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  818             yAxis.labels.boxAnchor = 'e'
  819             yAxis.configure(data)
  820             drawing.add(xAxis)
  821             drawing.add(yAxis)
  822             return drawing
  823 
  824         def sample7b():
  825             "Sample drawing, xvalue/ycat axes, y connected at left of x."
  826             drawing = Drawing(400, 200)
  827             data = [(10, 20, 30, 42)]
  828             xAxis = XValueAxis()
  829             xAxis._length = 300
  830             xAxis.configure(data)
  831             yAxis = YCategoryAxis()
  832             yAxis.setPosition(50, 50, 125)
  833             yAxis.joinAxis = xAxis
  834             yAxis.joinAxisMode = 'left'
  835             yAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  836             yAxis.labels.boxAnchor = 'e'
  837             yAxis.configure(data)
  838             drawing.add(xAxis)
  839             drawing.add(yAxis)
  840             return drawing
  841 
  842         def sample7c():
  843             "Sample drawing, xvalue/ycat axes, y connected at value 30 of x."
  844             drawing = Drawing(400, 200)
  845             data = [(10, 20, 30, 42)]
  846             xAxis = XValueAxis()
  847             xAxis._length = 300
  848             xAxis.configure(data)
  849             yAxis = YCategoryAxis()
  850             yAxis.setPosition(50, 50, 125)
  851             yAxis.joinAxis = xAxis
  852             yAxis.joinAxisMode = 'value'
  853             yAxis.joinAxisPos = 30
  854             yAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  855             yAxis.labels.boxAnchor = 'e'
  856             yAxis.configure(data)
  857             drawing.add(xAxis)
  858             drawing.add(yAxis)
  859             return drawing
  860 
  861         def sample7d():
  862             "Sample drawing, xvalue/ycat axes, y connected at 200 pts to x."
  863             drawing = Drawing(400, 200)
  864             data = [(10, 20, 30, 42)]
  865             xAxis = XValueAxis()
  866             xAxis._length = 300
  867             xAxis.configure(data)
  868             yAxis = YCategoryAxis()
  869             yAxis.setPosition(50, 50, 125)
  870             yAxis.joinAxis = xAxis
  871             yAxis.joinAxisMode = 'points'
  872             yAxis.joinAxisPos = 200
  873             yAxis.categoryNames = ['Beer', 'Wine', 'Meat', 'Cannelloni']
  874             yAxis.labels.boxAnchor = 'e'
  875             yAxis.configure(data)
  876             drawing.add(xAxis)
  877             drawing.add(yAxis)
  878             return drawing
  879 
  880         def sample8a():
  881             "Sample drawing with one xlog axis"
  882             drawing = Drawing(400,200)
  883 
  884             data = [[float(i)**2. for i in range(10, 1001, 10)], ]
  885 
  886             xAxis = LogXValueAxis()
  887             xAxis.configure(data)
  888 
  889             drawing.add(xAxis)
  890 
  891             return drawing
  892 
  893         def sample8b():
  894             "Sample drawing with one xlog axis"
  895             drawing = Drawing(400,200)
  896 
  897             data = [[float(i)**2. for i in range(10, 1001, 10)], ]
  898 
  899             yAxis = LogYValueAxis()
  900             yAxis.configure(data)
  901 
  902             drawing.add(yAxis)
  903 
  904             return drawing
  905 
  906         def sample9a():
  907             lp = LinePlot()
  908             lp.yValueAxis = LogYValueAxis()
  909             lp.x += 20
  910             lp.y += 20
  911             data = [[(i, float(i)**2.) for i in range(10, 1001, 10)], ]
  912             data.append([(i, float(i)**3.) for i in range(10, 1001, 10)])
  913             data.append([(i, float(i)**1.6) for i in range(10, 1001, 10)])
  914             lp.data = data
  915             lp.lines.strokeWidth = .2
  916             lp.lines[0].strokeColor = colors.red
  917             lp.lines[1].strokeColor = colors.blue
  918             lp.lines[2].strokeColor = colors.green
  919 
  920             drawing = Drawing(400,200)
  921             drawing.add(lp)
  922 
  923             return drawing
  924 
  925         def sample9b():
  926             lp = LinePlot()
  927             lp.yValueAxis = LogYValueAxis()
  928             lp.x += 20
  929             lp.y += 20
  930             lp.height = 250
  931             lp.width = 350
  932             data = [[(i, float(i)**2.) for i in range(10, 1001, 10)], ]
  933             data.append([(i, float(i)**3.) for i in range(10, 1001, 10)])
  934             data.append([(i, float(i)**1.6) for i in range(10, 1001, 10)])
  935             lp.data = data
  936             lp.lines.strokeWidth = .2
  937             lp.lines[0].strokeColor = colors.red
  938             lp.lines[1].strokeColor = colors.blue
  939             lp.lines[2].strokeColor = colors.green
  940 
  941             lp.xValueAxis.visibleGrid = 1
  942             lp.xValueAxis.gridStrokeDashArray = [1, 1]
  943             lp.yValueAxis.visibleGrid = 1
  944             lp.yValueAxis.visibleSubTicks = 1
  945             lp.yValueAxis.visibleSubGrid = 1
  946             lp.yValueAxis.gridStrokeDashArray = [1, 1]
  947             lp.yValueAxis.subGridStrokeDashArray = [1, 1]
  948 
  949             drawing = Drawing(400,300)
  950             drawing.add(lp)
  951 
  952             return drawing
  953 
  954         def sample9c():
  955             lp = LinePlot()
  956             lp.yValueAxis = LogYValueAxis()
  957             lp.x += 20
  958             lp.y += 20
  959             lp.height = 250
  960             lp.width = 350
  961             data = [[(i, float(i)**2.) for i in range(10, 1001, 10)], ]
  962             data.append([(i, float(i)**3.) for i in range(10, 1001, 10)])
  963             data.append([(i, float(i)**1.6) for i in range(10, 1001, 10)])
  964             lp.data = data
  965             lp.lines.strokeWidth = .2
  966             lp.lines[0].strokeColor = colors.red
  967             lp.lines[1].strokeColor = colors.blue
  968             lp.lines[2].strokeColor = colors.green
  969 
  970             lp.xValueAxis.visibleGrid = 1
  971             lp.xValueAxis.gridStrokeDashArray = [1, 1]
  972             lp.yValueAxis.visibleGrid = 1
  973             lp.yValueAxis.visibleSubTicks = 1
  974             lp.yValueAxis.visibleSubGrid = 1
  975             lp.yValueAxis.subTickNum = 4
  976             lp.yValueAxis.gridStrokeDashArray = [.3, 1]
  977             lp.yValueAxis.subGridStrokeDashArray = [.15, 1]
  978 
  979             drawing = Drawing(400,300)
  980             drawing.add(lp)
  981 
  982             return drawing
  983 
  984         def extract_samples():
  985             S = [].extend
  986             for m in ('barcharts','doughnut','linecharts','lineplots','piecharts','spider'):
  987                 mod = {}
  988                 exec('from reportlab.graphics.charts import %s as mod' % m ,mod)
  989                 mod  = mod['mod']
  990                 L = [(k,getattr(mod,k)) for k in dir(mod) if k.lower().startswith('sample')]
  991                 S([(k,v,m) for k,v in L if callable(v)])
  992             return S.__self__
  993 
  994         run_samples([(k,v,'axes') for k,v in locals().items() if k.lower().startswith('sample')])
  995         run_samples(extract_samples())
  996 
  997     def test_legends(self):
  998         from reportlab.graphics.charts.legends import Legend, LineLegend, LineSwatch
  999 
 1000         def sample1c():
 1001             "Make sample legend."
 1002 
 1003             d = Drawing(200, 100)
 1004 
 1005             legend = Legend()
 1006             legend.alignment = 'right'
 1007             legend.x = 0
 1008             legend.y = 100
 1009             legend.dxTextSpace = 5
 1010             items = 'red green blue yellow pink black white'.split()
 1011             items = [(getattr(colors, i), i) for i in items]
 1012             legend.colorNamePairs = items
 1013 
 1014             d.add(legend, 'legend')
 1015 
 1016             return d
 1017 
 1018 
 1019         def sample2c():
 1020             "Make sample legend."
 1021 
 1022             d = Drawing(200, 100)
 1023 
 1024             legend = Legend()
 1025             legend.alignment = 'right'
 1026             legend.x = 20
 1027             legend.y = 90
 1028             legend.deltax = 60
 1029             legend.dxTextSpace = 10
 1030             legend.columnMaximum = 4
 1031             items = 'red green blue yellow pink black white'.split()
 1032             items = [(getattr(colors, i), i) for i in items]
 1033             legend.colorNamePairs = items
 1034 
 1035             d.add(legend, 'legend')
 1036 
 1037             return d
 1038 
 1039         def sample3():
 1040             "Make sample legend with line swatches."
 1041 
 1042             d = Drawing(200, 100)
 1043 
 1044             legend = LineLegend()
 1045             legend.alignment = 'right'
 1046             legend.x = 20
 1047             legend.y = 90
 1048             legend.deltax = 60
 1049             legend.dxTextSpace = 10
 1050             legend.columnMaximum = 4
 1051             items = 'red green blue yellow pink black white'.split()
 1052             items = [(getattr(colors, i), i) for i in items]
 1053             legend.colorNamePairs = items
 1054             d.add(legend, 'legend')
 1055 
 1056             return d
 1057 
 1058 
 1059         def sample3a():
 1060             "Make sample legend with line swatches and dasharrays on the lines."
 1061             d = Drawing(200, 100)
 1062             legend = LineLegend()
 1063             legend.alignment = 'right'
 1064             legend.x = 20
 1065             legend.y = 90
 1066             legend.deltax = 60
 1067             legend.dxTextSpace = 10
 1068             legend.columnMaximum = 4
 1069             items = 'red green blue yellow pink black white'.split()
 1070             darrays = ([2,1], [2,5], [2,2,5,5], [1,2,3,4], [4,2,3,4], [1,2,3,4,5,6], [1])
 1071             cnp = []
 1072             for i in range(0, len(items)):
 1073                 l =  LineSwatch()
 1074                 l.strokeColor = getattr(colors, items[i])
 1075                 l.strokeDashArray = darrays[i]
 1076                 cnp.append((l, items[i]))
 1077             legend.colorNamePairs = cnp
 1078             d.add(legend, 'legend')
 1079 
 1080             return d
 1081 
 1082         def sample4a():
 1083             '''Satish Darade failure''' 
 1084             from reportlab.graphics.charts.legends import LegendSwatchCallout
 1085             from reportlab.graphics import shapes
 1086             class LSwatchCallout(LegendSwatchCallout):
 1087                 def __init__(self,texts,fontName,fontSize):
 1088                     self._texts = texts
 1089                     self._fontName = fontName
 1090                     self._fontSize = fontSize
 1091 
 1092                 def __call__(self,legend,g,thisx,y,i,colName,swatch):
 1093                     g.add(shapes.String(swatch.x-2,y,self._texts[i],textAnchor='end',fontName=self._fontName,fontSize=self._fontSize))
 1094 
 1095             d = Drawing(200, 100)
 1096             legend = LineLegend()
 1097             d.add(legend, 'legend')
 1098             items = 'red green blue yellow pink black white'
 1099             sw_names = items.upper().split()
 1100             items = items.split()
 1101             legend.colorNamePairs = [(getattr(colors, i), i) for i in items]
 1102             legend.x = 20
 1103             legend.y = 90
 1104             d.legend.swatchCallout = LSwatchCallout(sw_names,'Helvetica',12)
 1105             return d
 1106 
 1107         run_samples([(k,v,'legends') for k,v in locals().items() if k.lower().startswith('sample')])
 1108 
 1109 def makeSuite():
 1110     return makeSuiteForClasses(ChartTestCase)
 1111 
 1112 
 1113 #noruntests
 1114 if __name__ == "__main__":
 1115     unittest.TextTestRunner().run(makeSuite())
 1116     printLocation()