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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
#Copyright ReportLab Europe Ltd. 2000-2016
#see license.txt for license details
"""
Tests for chart class.
"""
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import os, sys, copy
from os.path import join, basename, splitext
import unittest
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.validators import Auto
from reportlab.pdfgen.canvas import Canvas
from reportlab.graphics.shapes import *
from reportlab.graphics.charts.textlabels import Label, _text2Path
from reportlab.platypus.flowables import Spacer, PageBreak
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.xpreformatted import XPreformatted
from reportlab.platypus.frames import Frame
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.charts.linecharts import HorizontalLineChart
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.piecharts import Pie
from reportlab.graphics.charts.legends import Legend
from reportlab.graphics.charts.spider import SpiderChart
from reportlab.graphics.widgets.markers import makeMarker
try:
from reportlab.graphics import _renderPM
except ImportError:
_renderPM = None
def myMainPageFrame(canvas, doc):
"The page frame used for all PDF documents."
canvas.saveState()
#canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
canvas.setFont('Times-Roman', 12)
pageNumber = canvas.getPageNumber()
canvas.drawString(10*cm, cm, str(pageNumber))
canvas.restoreState()
class MyDocTemplate(BaseDocTemplate):
"The document template used for all PDF documents."
_invalidInitArgs = ('pageTemplates',)
def __init__(self, filename, **kw):
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
self.allowSplitting = 0
BaseDocTemplate.__init__(self, filename, **kw)
template = PageTemplate('normal', [frame1], myMainPageFrame)
self.addPageTemplates(template)
def sample1bar(data=[(13, 5, 20, 22, 37, 45, 19, 4)]):
drawing = Drawing(400, 200)
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 = 60
bc.valueAxis.valueStep = 15
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 8
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
catNames = 'Jan Feb Mar Apr May Jun Jul Aug'.split( ' ')
catNames = [n+'-99' for n in catNames]
bc.categoryAxis.categoryNames = catNames
drawing.add(bc)
return drawing
def sample2bar(data=[(13, 5, 20, 22, 37, 45, 19, 4),
(14, 6, 21, 23, 38, 46, 20, 5)]):
return sample1bar(data)
def sample1line(data=[(13, 5, 20, 22, 37, 45, 19, 4)]):
drawing = Drawing(400, 200)
bc = HorizontalLineChart()
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 = 60
bc.valueAxis.valueStep = 15
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 8
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
catNames = 'Jan Feb Mar Apr May Jun Jul Aug'.split(' ')
catNames = [n+'-99' for n in catNames]
bc.categoryAxis.categoryNames = catNames
drawing.add(bc)
return drawing
def sample2line(data=[(13, 5, 20, 22, 37, 45, 19, 4),
(14, 6, 21, 23, 38, 46, 20, 5)]):
return sample1line(data)
def sample3(drawing=None):
"Add sample swatches to a diagram."
d = drawing or Drawing(400, 200)
swatches = Legend()
swatches.alignment = 'right'
swatches.x = 80
swatches.y = 160
swatches.deltax = 60
swatches.dxTextSpace = 10
swatches.columnMaximum = 4
items = [(colors.red, 'before'), (colors.green, 'after')]
swatches.colorNamePairs = items
d.add(swatches, 'legend')
return d
def sample4pie():
width = 300
height = 150
d = Drawing(width, height)
pc = Pie()
pc.x = 150
pc.y = 50
pc.data = [1, 50, 100, 100, 100, 100, 100, 100, 100, 50]
pc.labels = ['0','a','b','c','d','e','f','g','h','i']
pc.slices.strokeWidth=0.5
pc.slices[3].popout = 20
pc.slices[3].strokeWidth = 2
pc.slices[3].strokeDashArray = [2,2]
pc.slices[3].labelRadius = 1.75
pc.slices[3].fontColor = colors.red
d.add(pc)
legend = Legend()
legend.x = width-5
legend.y = height-5
legend.dx = 20
legend.dy = 5
legend.deltax = 0
legend.boxAnchor = 'nw'
legend.colorNamePairs=Auto(chart=pc)
d.add(legend)
return d
def autoLegender(i,chart,styleObj,sym='symbol'):
if sym:
setattr(styleObj[0],sym, makeMarker('Diamond',size=6))
setattr(styleObj[1],sym,makeMarker('Square'))
width = 300
height = 150
legend = Legend()
legend.x = width-5
legend.y = 5
legend.dx = 20
legend.dy = 5
legend.deltay = 0
legend.boxAnchor = 'se'
if i=='col auto':
legend.colorNamePairs[0]=(Auto(chart=chart),'auto chart=self.chart')
legend.colorNamePairs[1]=(Auto(obj=chart,index=1),'auto chart=self.chart index=1')
elif i=='full auto':
legend.colorNamePairs=Auto(chart=chart)
elif i=='swatch set':
legend.swatchMarker=makeMarker('Circle')
legend.swatchMarker.size = 10
elif i=='swatch auto':
legend.swatchMarker=Auto(chart=chart)
d = Drawing(width,height)
d.background = Rect(0,0,width,height,strokeWidth=1,strokeColor=colors.red,fillColor=None)
m = makeMarker('Cross')
m.x = width-5
m.y = 5
m.fillColor = colors.red
m.strokeColor = colors.yellow
d.add(chart)
d.add(legend)
d.add(m)
return d
def lpleg(i=None):
chart = LinePlot()
return autoLegender(i,chart,chart.lines)
def hlcleg(i=None):
chart = HorizontalLineChart()
return autoLegender(i,chart,chart.lines)
def bcleg(i=None):
chart = VerticalBarChart()
return autoLegender(i,chart,chart.bars,None)
def pcleg(i=None):
chart = Pie()
return autoLegender(i,chart,chart.slices,None)
def scleg(i=None):
chart = SpiderChart()
return autoLegender(i,chart,chart.strands,None)
def plpleg(i=None):
from reportlab.lib.colors import pink, red, green
pie = Pie()
pie.x = 0
pie.y = 0
pie.pointerLabelMode='LeftAndRight'
pie.slices.label_boxStrokeColor = red
pie.simpleLabels = 0
pie.sameRadii = 1
pie.data = [1, 0.1, 1.7, 4.2,0,0]
pie.labels = ['abcdef', 'b', 'c', 'd','e','fedcba']
pie.strokeWidth=1
pie.strokeColor=green
pie.slices.label_pointer_piePad = 6
pie.width = 160
pie.direction = 'clockwise'
pie.pointerLabelMode = 'LeftRight'
return autoLegender(i,pie,pie.slices,None)
def notFail(d):
try:
return d.getContents()
except:
import traceback
traceback.print_exc()
return None
STORY = []
styleSheet = getSampleStyleSheet()
bt = styleSheet['BodyText']
h1 = styleSheet['Heading1']
h2 = styleSheet['Heading2']
h3 = styleSheet['Heading3']
FINISHED = 0
class ChartTestCase(unittest.TestCase):
"Test chart classes."
def setUp(self):
"Hook method for setting up the test fixture before exercising it."
global STORY
self.story = STORY
if self.story == []:
self.story.append(Paragraph('Tests for chart classes', h1))
def tearDown(self):
"Hook method for deconstructing the test fixture after testing it."
if FINISHED:
path=outputfile('test_graphics_charts.pdf')
doc = MyDocTemplate(path)
doc.build(self.story)
def test0(self):
"Test bar charts."
story = self.story
story.append(Paragraph('Single data row', h2))
story.append(Spacer(0, 0.5*cm))
drawing = sample1bar()
story.append(drawing)
story.append(Spacer(0, 1*cm))
def test1(self):
"Test bar charts."
story = self.story
story.append(Paragraph('Double data row', h2))
story.append(Spacer(0, 0.5*cm))
drawing = sample2bar()
story.append(drawing)
story.append(Spacer(0, 1*cm))
def test2(self):
"Test bar charts."
story = self.story
story.append(Paragraph('Double data row with legend', h2))
story.append(Spacer(0, 0.5*cm))
drawing = sample2bar()
drawing = sample3(drawing)
story.append(drawing)
story.append(Spacer(0, 1*cm))
def test3(self):
"Test line charts."
story = self.story
story.append(Paragraph('Single data row', h2))
story.append(Spacer(0, 0.5*cm))
drawing = sample1line()
story.append(drawing)
story.append(Spacer(0, 1*cm))
def test4(self):
"Test line charts."
story = self.story
story.append(Paragraph('Single data row', h2))
story.append(Spacer(0, 0.5*cm))
drawing = sample2line()
story.append(drawing)
story.append(Spacer(0, 1*cm))
def test4b(self):
story = self.story
for code in (lpleg, hlcleg, bcleg, pcleg, scleg, plpleg):
code_name = code.__code__.co_name
for i in ('standard', 'col auto', 'full auto', 'swatch set', 'swatch auto'):
d = code(i)
assert notFail(d),'getContents failed for %s %s' % (code_name,i)
story.append(Paragraph('%s %s' % (i,code_name), h2))
story.append(Spacer(0, 0.5*cm))
story.append(code(i))
story.append(Spacer(0, 1*cm))
def test5(self):
"Test pie charts."
story = self.story
story.append(Paragraph('Pie', h2))
story.append(Spacer(0, 0.5*cm))
drawing = sample4pie()
story.append(drawing)
story.append(Spacer(0, 1*cm))
def test7(self):
"Added some new side labelled pies"
story = self.story
story.append(Paragraph('Side Labelled Pie', h2))
story.append(Spacer(0,1*cm))
story.append(Paragraph('Here are two examples of side labelled pies.',bt))
story.append(Spacer(0, 0.5*cm))
from reportlab.graphics.charts.piecharts import sample5, sample6, sample7, sample8, sample9
drawing5 = sample5()
story.append(drawing5)
story.append(Spacer(0, 0.5*cm))
drawing9 = sample9()
story.append(drawing9)
story.append(Spacer(0, 1*cm))
story.append(Paragraph('Moving the pie', h3))
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))
story.append(Paragraph('The line represents x = 0',bt))
story.append(Paragraph('This has not been implemented and is on line 863 in piecharts.py', bt))
story.append(Spacer(0,0.5*cm))
drawing6 = sample6()
story.append(drawing6)
story.append(Spacer(0,1*cm))
story.append(Paragraph('Case with overlapping pointers', h3))
story.append(Paragraph('If there are many slices then the pointer labels can end up overlapping as shown below.', bt))
story.append(Spacer(0,0.5*cm))
drawing7 = sample7()
story.append(drawing7)
story.append(Spacer(0,1*cm))
story.append(Paragraph('Case with overlapping labels', h3))
story.append(Paragraph('Labels overlap if they do not belong to adjacent pie slices.', bt))
story.append(Spacer(0,0.5*cm))
drawing8 = sample8()
story.append(drawing8)
story.append(Spacer(0,1*cm))
@unittest.skipIf(not _renderPM,'no _renderPM')
def test8(self):
'''text _text2Path'''
story = self.story
story.append(Paragraph('Texts drawn using a Path', h3))
story.append(Spacer(0,0.5*cm))
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)
d = Drawing(400,50)
d.add(P)
story.append(d)
P=_text2Path('Hello World from font Helvetica!',x=10,y=20,fontName='Helvetica',fontSize=20,strokeColor=colors.blue,strokeWidth=0.1,fillColor=colors.red)
d = Drawing(400,50)
d.add(P)
story.append(d)
story.append(Spacer(0,1*cm))
def test999(self):
#keep this last
from reportlab.graphics.charts.piecharts import Pie, _makeSideArcDefs, intervalIntersection
L = []
def intSum(arcs,A):
s = 0
for a in A:
for arc in arcs:
i = intervalIntersection(arc[1:],a)
if i: s += i[1] - i[0]
return s
def subtest(sa,d):
pc = Pie()
pc.direction=d
pc.startAngle=sa
arcs = _makeSideArcDefs(sa,d)
A = [x[1] for x in pc.makeAngles()]
arcsum = sum([a[2]-a[1] for a in arcs])
isum = intSum(arcs,A)
mi = max([a[2]-a[1] for a in arcs])
ni = min([a[2]-a[1] for a in arcs])
l = []
s = (arcsum-360)
if s>1e-8: l.append('Arc length=%s != 360' % s)
s = abs(isum-360)
if s>1e-8: l.append('interval intersection length=%s != 360' % s)
if mi>360: l.append('max interval intersection length=%s >360' % mi)
if ni<0: l.append('min interval intersection length=%s <0' % ni)
if l:
l.append('sa: %s d: %s' % (sa,d))
l.append('sidearcs: %s' % str(arcs))
l.append('Angles: %s' % A)
raise ValueError('piecharts._makeSideArcDefs failure\n%s' % '\n'.join(l))
for d in ('anticlockwise','clockwise'):
for sa in (225, 180, 135, 90, 45, 0, -45, -90):
subtest(sa,d)
# This triggers the document build operation (hackish).
global FINISHED
FINISHED = 1
def makeSuite():
return makeSuiteForClasses(ChartTestCase)
#noruntests
if __name__ == "__main__":
unittest.TextTestRunner().run(makeSuite())
printLocation()
|