示例#1
0
def add_findings_by_provider_chart():
    drawing = Drawing(300, 200)
    data = get_findings_by_provider()
    maxVal = max(data[0])

    if (maxVal > 1000):
        multiplier = 1000
        step = 4 * multiplier
    else:
        multiplier = 100
        step = 4 * multiplier

    value_step = int(ceil(maxVal / step)) * multiplier

    if (value_step < 10):
        value_step = 10

    bar = HorizontalBarChart()
    bar.x = 30
    bar.y = 0
    bar.height = 100
    bar.width = 400
    bar.data = data
    bar.strokeColor = colors.white
    bar.valueAxis.valueMin = 0
    bar.valueAxis.valueMax = maxVal * 2  ## graph displa twice as much as max violation
    bar.valueAxis.valueStep = value_step  ## Convert to neartest 100
    bar.categoryAxis.labels.boxAnchor = 'ne'
    bar.categoryAxis.labels.dx = -10
    bar.categoryAxis.labels.dy = -2
    bar.categoryAxis.labels.fontName = 'Helvetica'
    bar.categoryAxis.categoryNames = ["AWS", "Azure"]
    bar.bars[(0, 0)].fillColor = HexColor("#434476")
    bar.bars[(0, 1)].fillColor = HexColor("#B170DB")
    bar.barWidth = 3.5
    bar.barSpacing = 0.1
    bar.barLabelFormat = '%d'
    bar.barLabels.nudge = 15
    bar.bars[0].strokeColor = None

    drawing.add(bar)
    #  add_legend(drawing, bar)
    yLabel = Label()
    yLabel.setText("Number of Findings")
    yLabel.fontSize = 10
    yLabel.fontName = 'Helvetica'
    yLabel.dx = 250
    yLabel.dy = -30

    chartLabel = Label()
    chartLabel.setText("Findings by Provider")
    chartLabel.fontSize = 10
    chartLabel.fillColor = HexColor("#737373")
    chartLabel.fontName = 'Helvetica-Bold'
    chartLabel.dx = 250
    chartLabel.dy = 160

    drawing.add(chartLabel)
    drawing.add(yLabel)
    fields.append(drawing)
示例#2
0
    def _drawLabels(self, Title, xAxis, yAxis):
        self.graphCenterX = self.width/2
        self.graphCenterY = self.height/2
        Label_Xaxis = Label()
        Label_Xaxis.fontSize = 7
        Label_Xaxis.angle = 0
        Label_Xaxis.dx = self.graphCenterX - 50
        Label_Xaxis.dy = 0
        Label_Xaxis.boxAnchor = 's'
        Label_Xaxis.setText(xAxis)
        self.drawing.add(Label_Xaxis)

        Label_Yaxis = Label()
        Label_Yaxis.fontSize = 7
        Label_Yaxis.angle = 90
        Label_Yaxis.boxAnchor = 'n'
        Label_Yaxis.dx = -5
        Label_Yaxis.dy = self.graphCenterY
        Label_Yaxis.setText(yAxis)
        self.drawing.add(Label_Yaxis)

        Label_Graph = Label()
        Label_Graph.fontSize = 10
        Label_Graph.angle = 0
        Label_Graph.boxAnchor = 'n'
        Label_Graph.dx = self.graphCenterX - 50
        Label_Graph.dy = self.height
        Label_Graph.setText(Title)
        self.drawing.add(Label_Graph)
示例#3
0
def addChromosomes(drawing, chrNames, chrSizes, xmap, ymap, w=0.1*DPI, fillColor=colors.skyblue, strokeColor=colors.skyblue):
    for i,chrom in enumerate(chrNames):
        x = xmap(i+1)
        y = ymap(chrSizes[chrom])
        h = ymap(1)-ymap(chrSizes[chrom])
        
        chromosome = Rect(x,y,w,h, strokeColor=strokeColor, fillColor=fillColor)
        drawing.add(chromosome)
        
        topCap = Wedge(x+0.5*w, y+h, 0.5*w, 0, 180, strokeColor=strokeColor, fillColor=fillColor)
        bottomCap = Wedge(x+0.5*w, y, 0.5*w, 180, 0, strokeColor=strokeColor, fillColor=fillColor)
        drawing.add(topCap)
        drawing.add(bottomCap)
        
        label = Label()
        label.setOrigin(xmap(i+1)+w/2, ymap(0))
        label.boxAnchor = 's'
        label.textAnchor = 'middle'
        label.dx = 0
        label.dy = DPI/10
        label.setText(chrom)
        label.fontSize = 36
        label.fontName = 'Helvetica'
        drawing.add(label)
        
        chrLength = Label()
        chrLength.setOrigin(xmap(i+1)+w/2, ymap(chrSizes[chrom]))
        chrLength.boxAnchor = 'n'
        chrLength.textAnchor = 'middle'
        chrLength.dx = 0
        chrLength.dy = -DPI/10
        chrLength.setText('%iMb' % int(chrSizes[chrom]/1e6))
        chrLength.fontSize = 24
        chrLength.fontName = 'Helvetica'
        drawing.add(chrLength)
示例#4
0
def addChromosomes(drawing,
                   chrNames,
                   chrSizes,
                   xmap,
                   ymap,
                   w=0.1 * DPI,
                   fillColor=colors.skyblue,
                   strokeColor=colors.skyblue):
    for i, chrom in enumerate(chrNames):
        x = xmap(i + 1)
        y = ymap(chrSizes[chrom])
        h = ymap(1) - ymap(chrSizes[chrom])

        chromosome = Rect(x,
                          y,
                          w,
                          h,
                          strokeColor=strokeColor,
                          fillColor=fillColor)
        drawing.add(chromosome)

        topCap = Wedge(x + 0.5 * w,
                       y + h,
                       0.5 * w,
                       0,
                       180,
                       strokeColor=strokeColor,
                       fillColor=fillColor)
        bottomCap = Wedge(x + 0.5 * w,
                          y,
                          0.5 * w,
                          180,
                          0,
                          strokeColor=strokeColor,
                          fillColor=fillColor)
        drawing.add(topCap)
        drawing.add(bottomCap)

        label = Label()
        label.setOrigin(xmap(i + 1) + w / 2, ymap(0))
        label.boxAnchor = 's'
        label.textAnchor = 'middle'
        label.dx = 0
        label.dy = DPI / 10
        label.setText(chrom)
        label.fontSize = 36
        label.fontName = 'Helvetica'
        drawing.add(label)

        chrLength = Label()
        chrLength.setOrigin(xmap(i + 1) + w / 2, ymap(chrSizes[chrom]))
        chrLength.boxAnchor = 'n'
        chrLength.textAnchor = 'middle'
        chrLength.dx = 0
        chrLength.dy = -DPI / 10
        chrLength.setText('%iMb' % int(chrSizes[chrom] / 1e6))
        chrLength.fontSize = 24
        chrLength.fontName = 'Helvetica'
        drawing.add(chrLength)
示例#5
0
def add_trends_new_resolved_findings_chart():
    drawing = Drawing(200, 200)

    data, month = get_new_resolved_trends()

    max_val_new_findings = max(data[0])
    max_val_resolved_findings = max(data[1])

    maxVal = max(max_val_new_findings, max_val_resolved_findings)

    if (maxVal > 1000):
        multiplier = 1000
        step = 4 * multiplier
    else:
        multiplier = 100
        step = 4 * multiplier

    value_step = int(ceil(maxVal / step)) * multiplier

    if (value_step < 10):
        value_step = 1

    bar = VerticalBarChart()
    bar.x = 25
    bar.y = -35
    bar.height = 100
    bar.width = doc.width
    bar.barWidth = 2
    bar.data = data
    bar.valueAxis.valueMin = 0
    bar.valueAxis.valueMax = int(
        maxVal * 2)  ## graph displa twice as much as max violation
    bar.valueAxis.valueStep = value_step  ## Convert to neartest step
    bar.categoryAxis.categoryNames = month
    bar.bars[0].strokeColor = None
    bar.bars[1].strokeColor = None
    bar.bars[0].fillColor = HexColor("#E57300")
    bar.bars[1].fillColor = HexColor("#408F00")

    chartLabel = Label()
    chartLabel.setText("Trends - New Findings")
    chartLabel.fontSize = 10
    chartLabel.fillColor = HexColor("#737373")
    chartLabel.fontName = 'Helvetica-Bold'
    chartLabel.dx = 250
    chartLabel.dy = 90

    legend = Legend()
    legend.alignment = 'right'
    legend.colorNamePairs = [[HexColor("#E57300"), "New Findings"],
                             [HexColor("#408F00"), "Resolved Findings"]]
    legend.columnMaximum = 2
    legend.x = 400
    legend.y = 120

    drawing.add(legend)
    drawing.add(chartLabel)
    drawing.add(bar)
    fields.append(drawing)
    def _makeProtoLabel(self):
        "Return a label prototype for further modification."

        protoLabel = Label()
        protoLabel.dx = 0
        protoLabel.dy = 0
        protoLabel.boxStrokeWidth = 0.1
        protoLabel.boxStrokeColor = colors.black
        protoLabel.boxFillColor = colors.yellow
        # protoLabel.text = 'Hello World!' # Does not work as expected.

        return protoLabel
示例#7
0
    def _makeProtoLabel(self):
        "Return a label prototype for further modification."

        protoLabel = Label()
        protoLabel.dx = 0
        protoLabel.dy = 0
        protoLabel.boxStrokeWidth = 0.1
        protoLabel.boxStrokeColor = colors.black
        protoLabel.boxFillColor = colors.yellow
        # protoLabel.text = 'Hello World!' # Does not work as expected.

        return protoLabel
示例#8
0
def add_trends_open_findings_chart():
    drawing = Drawing(300, 200)

    data, months = get_open_findings_trends()
    maxVal = max(data[0])

    if (maxVal > 1000):
        multiplier = 1000
        step = 4 * multiplier
    else:
        multiplier = 100
        step = 4 * multiplier

    value_step = int(ceil(maxVal / step)) * multiplier

    if (value_step < 10):
        value_step = 1

    lc = HorizontalLineChart()
    lc.x = 25
    lc.y = 40
    lc.height = 100
    lc.width = doc.width
    lc.lines.symbol = makeMarker('Square')
    lc.joinedLines = 1
    lc.data = data
    lc.categoryAxis.categoryNames = months
    lc.categoryAxis.labels.boxAnchor = 'c'
    # lc.categoryAxis.valueMin = months[0]
    lc.valueAxis.valueMin = 0
    lc.valueAxis.valueStep = value_step
    lc.valueAxis.valueMax = max(data[0]) * 2
    lc.lines[0].strokeColor = colors.green
    #lc.fillColor = colors.whitesmoke
    #lc.inFill = 1
    #lc.categoryAxis.labels.dx = -20
    #lc.categoryAxis.labels.dy = -10
    lc.lineLabelFormat = "%d"

    chartLabel = Label()
    chartLabel.setText("Trends - Total Open Findings")
    chartLabel.fontSize = 10
    chartLabel.fillColor = HexColor("#737373")
    chartLabel.fontName = 'Helvetica-Bold'

    chartLabel.dx = 250
    chartLabel.dy = 160

    drawing.add(chartLabel)
    drawing.add(lc)
    fields.append(drawing)
示例#9
0
def add_azure_findings_by_severity_chart():
    drawing = Drawing(doc.width / 2 - 18, doc.height / 2 - 45)
    aws, azure = get_all_violations_by_severity()
    rules = [azure]

    maxVal = max(rules[0])

    if (maxVal > 1000):
        multiplier = 1000
        step = 4 * multiplier
    else:
        multiplier = 100
        step = 4 * multiplier

    value_step = int(ceil(maxVal / step)) * multiplier

    if (value_step < 10):
        value_step = 1

    bar = VerticalBarChart()
    bar.x = 10
    bar.y = 70
    bar.height = doc.height / 4
    bar.width = doc.width / 2 - 40
    bar.barWidth = 2
    bar.barSpacing = 0.5
    bar.data = rules
    bar.valueAxis.valueMin = 0
    bar.valueAxis.valueMax = int(
        maxVal * 1.5)  ## graph displa twice as much as max violation
    bar.valueAxis.valueStep = value_step  ## Convert to neartest 10
    bar.categoryAxis.categoryNames = ["high", "medium", "low"]
    bar.barLabelFormat = '%d'
    bar.barLabels.nudge = 15
    bar.bars[0].fillColor = HexColor("#B170DB")
    bar.bars[0].strokeColor = None
    bar.categoryAxis.labels.boxAnchor = 'n'

    chartLabel = Label()
    chartLabel.setText("Findings by Severity - Azure")
    chartLabel.fontSize = 10
    chartLabel.fontName = 'Helvetica-Bold'
    chartLabel.fillColor = HexColor("#737373")
    chartLabel.dx = doc.rightMargin
    chartLabel.dy = doc.height - 80

    drawing.add(chartLabel)
    drawing.add(bar)
    fields.append(drawing)
示例#10
0
def addAxis(drawing, xmap, y, strokeWidth=1, minorStrokeWidth=0.5, 
  tickDir='down', autoTicks=False, nTicks=20, tickLen=5, fontSize=10, nMinorTicks=80, 
  minorTickLen=2, angle=0, dx=0, dy=-2, textAnchor='middle', boxAnchor=None, 
  scale=1.0, format='%i'):
    """Add a horizontal axis to the drawing.
    
    To do: Round tick positions
    """
    line = Line(xmap.x0, y, xmap.x1, y, strokeWidth=strokeWidth)
    drawing.add(line)
    
    if not boxAnchor:
        if tickDir=='down':
            boxAnchor = 'n'
        else:
            boxAnchor = 's'
    signum = {'up': -1, 'down': 1}[tickDir]
    
    if nTicks>0:
        ticks = tick_generator(xmap.start, xmap.end, n=nTicks, convert=int)
    
    for p in ticks:
        x = xmap(p)
        line = Line(x, y, x, y-signum*tickLen, strokeWidth=strokeWidth)
        drawing.add(line)
        s = Label()
        s.setOrigin(x, y-signum*tickLen)
        s.setText(format % (p/scale))
        s.dx = dx
        s.dy = signum*dy
        s.fontName = 'Helvetica'
        s.fontSize = fontSize
        s.textAnchor = textAnchor
        s.boxAnchor = boxAnchor
        s.angle = angle
        drawing.add(s)
    
    minorticks = tick_generator(xmap.start, xmap.end, n=nMinorTicks, convert=int)
    for p in minorticks:
        x = xmap(p)
        line = Line(x, y, x, y-signum*minorTickLen, strokeWidth=minorStrokeWidth)
        drawing.add(line)
示例#11
0
def border():
    draw = Drawing(1, 1)
    rect = Polygon(points=[
        -12, cm / 6, (PAGE_WIDTH - (RIGHT_MARGIN + LEFT_MARGIN)), cm / 6,
        PAGE_WIDTH - (RIGHT_MARGIN + LEFT_MARGIN),
        -1 * (PAGE_HEIGHT - (TOP_MARGIN + BOTTOM_MARGIN + cm / 2)), -12,
        -1 * (PAGE_HEIGHT - (TOP_MARGIN + BOTTOM_MARGIN + cm / 2))
    ],
                   strokeColor=Color(*charts.BG_COLOR))
    rect.fillColor = Color(*charts.BG_COLOR, 0.1)
    draw.add(rect)
    draw.add(Circle(100, 90, 5, fillColor=colors.green))
    lab = Label()
    lab.setOrigin(350, -50)
    lab.boxAnchor = 'ne'
    lab.fillColor = Color(*charts.BG_COLOR, 0.15)
    lab.fontSize = 72
    lab.angle = 60
    lab.dx = 0
    lab.dy = 0
    lab.setText('Wisdom Tests')
    draw.add(lab)
    return draw
    def balance_statistics_chart(self, control_vars, match_vars, var_names):
        """
        Specify layout of the balance statistics chart and generate
        flowable object that can be added to the pdf
        """
        drawing = Drawing()
        vbc = VerticalBarChart()

        # Chart position in document
        vbc.x = self.chart_offset_x
        vbc.y = self.chart_offset_y
        vbc.height = self.chart_height
        vbc.width = self.chart_width

        # Specify data
        # [[control_var1, control_var2], [match_var1, match_var2]]
        vbc.data = [control_vars, match_vars]

        #Set Y-Axis ranges
        #axis_range = self._calculate_y_axis(vbc.data)
        #vbc.valueAxis.valueMin = axis_range['min']
        #vbc.valueAxis.valueMax = axis_range['max']
        #vbc.valueAxis.valueStep = axis_range['step']

        #Grid formatting
        vbc.valueAxis.visibleGrid = 1
        vbc.valueAxis.gridStrokeColor = colors.lightgrey

        #Bar formatting
        vbc.bars[0].fillColor = colors.blue
        vbc.bars[1].fillColor = colors.yellow
        vbc.bars.strokeColor = None
        vbc.groupSpacing = 1
        vbc.barWidth = 5

        # Callout label formatting (numbers above bar)
        #vbc.barLabels.fontName = "Arial"
        vbc.barLabels.fontSize = 8
        vbc.barLabels.fillColor = colors.black
        vbc.barLabelFormat = '%.2f'
        vbc.barLabels.nudge = 5

        # Central axis
        vbc.categoryAxis.visibleTicks = 1

        # X-axis labels
        #vbc.categoryAxis.labels.dy = -60
        vbc.valueAxis.labels.fontName = 'Helvetica'
        vbc.categoryAxis.categoryNames = var_names

        lab = Label()
        lab.setOrigin(10, 155)
        lab.boxAnchor = 'ne'
        lab.angle = 90
        lab.dx = 0
        lab.dy = -15

        #lab.boxStrokeColor = colors.green
        lab.setText('Percent Bias')
        drawing.add(lab)
        drawing.add(vbc)
        self.elements.append(drawing)
    def create_bar(self,
                   data_list,
                   label_x_axis,
                   contain,
                   y_label=None,
                   x_label=None,
                   bar_width=520,
                   bar_height=100,
                   draw_width=520,
                   draw_height=200,
                   user_color=None,
                   fontName="Times-Roman",
                   fontSize=6,
                   x_angle=0,
                   bar_space=0):

        d = Drawing(width=draw_width, height=draw_height)
        bar = VerticalBarChart()
        bar.width = bar_width
        bar.height = bar_height
        bar.y = bar.height - (bar_height / 4)
        bar.strokeColor = colors.black
        bar.barLabelFormat = '%s'
        bar.barLabels.nudge = 7
        bar.barLabels.fontSize = fontSize

        ################# X AXIS PROPERTIES #################
        bar.categoryAxis.labels.dx = 0
        bar.categoryAxis.labels.angle = x_angle
        bar.categoryAxis.labels.boxAnchor = 'autox'
        bar.categoryAxis.labels.fontSize = fontSize
        bar.categoryAxis.labels.fontName = self.master_font
        bar.categoryAxis.strokeWidth = 0.25
        bar.categoryAxis.tickDown = -(bar.height)
        bar.categoryAxis.categoryNames = label_x_axis

        labX = Label()
        labX.boxAnchor = 'ne'
        labX.dx = bar.width * 2.15
        labX.dy = bar.height
        labX.fontName = fontName
        labX.fontSize = fontSize
        labX.setText(x_label)
        d.add(labX)
        #####################################################

        ################# Y AXIS PROPERTIES #################
        bar.valueAxis.forceZero = 1
        bar.valueAxis.labels.fontSize = fontSize
        bar.valueAxis.labels.fontName = fontName
        bar.valueAxis.rangeRound = 'both'
        bar.valueAxis.valueMin = 0
        bar.valueAxis.visibleGrid = 1
        bar.valueAxis.visibleAxis = 1
        bar.valueAxis.labels.dx = -10

        labY = Label()
        labY.boxAnchor = 'autox'
        labY.dy = bar.y + (bar.height / 1.5)
        labY.dx = bar.x - 30
        labY.angle = 90
        labY.fontName = fontName
        labY.fontSize = fontSize
        labY.setText(y_label)
        d.add(labY)
        #####################################################

        bar.barSpacing = bar_space
        # bar.groupSpacing = 3

        bar.data = data_list

        # print len(data_list)
        # print len(contain)

        if user_color != None:
            usage_color = user_color
        else:
            random_range = [randint(0, 100) for i in range(len(contain))]
            usage_color = map(
                lambda item: PCMYKColor(randint(0, item), randint(0, item),
                                        randint(0, item), randint(0, item)),
                random_range)

        for i in range(len(data_list)):
            bar.bars[i].name = contain[i].upper()
            bar.bars[i].fillColor = usage_color[i]

        legend = Legend()
        # legend.autoXPadding = 10
        legend.alignment = 'right'
        legend.boxAnchor = 'sw'
        legend.dxTextSpace = 10
        legend.fontSize = fontSize
        legend.fontName = fontName
        legend.subCols.minWidth = 55
        legend.variColumn = 1
        legend.deltay = 15
        legend.x = bar.x
        legend.colorNamePairs = Auto(obj=bar)

        d.add(bar)
        d.add(legend)
        self.flowables.append(d)
示例#14
0
""")


from reportlab.graphics import shapes
from reportlab.graphics.charts.textlabels import Label

d = Drawing(200, 100)

# mark the origin of the label
d.add(Circle(100,90, 5, fillColor=colors.green))

lab = Label()
lab.setOrigin(100,90)
lab.boxAnchor = 'ne'
lab.angle = 45
lab.dx = 0
lab.dy = -20
lab.boxStrokeColor = colors.green
lab.setText('Some\nMulti-Line\nLabel')

d.add(lab)

draw(d, 'Label example')



disc("""
In the drawing above, the label is defined relative to the green blob.
The text box should have its north-east corner ten points down from
the origin, and be rotated by 45 degrees about that corner.
""")
        def _rawDraw(self, x, y):
            from reportlab.lib import colors 
            from reportlab.graphics.shapes import Drawing, Line, String, STATE_DEFAULTS
            from reportlab.graphics.charts.axes import XCategoryAxis,YValueAxis
            from reportlab.graphics.charts.textlabels import Label
            from reportlab.graphics.charts.barcharts  import VerticalBarChart
            
            self.originX = x
            self.originY = y
            self._setScale([self.dataBar])
            (x1, y1, Width, Height) = self._getGraphRegion(x, y)

            #Build the graph
            self.drawing = Drawing(self.width, self.height)

            #Size of the Axis
            SizeXaxis = 14
            countSteps = int(self.valueMax / self.valueStep)
            SizeYaxis = 0.0
            for n in range(countSteps + 1):
                eachValue = self.valueMin + n * self.valueStep
                textString = self._customSecondsLabelFormat( eachValue )
                SizeYaxis = max(SizeYaxis, self._stringWidth(textString, STATE_DEFAULTS['fontName'], STATE_DEFAULTS['fontSize']) )

            bc = VerticalBarChart()
            SizeYaxis += bc.valueAxis.tickLeft
            bc.x = x1 - x + SizeYaxis
            bc.y = y1 - y + SizeXaxis
            bc.height = Height - SizeXaxis
            bc.width  = Width  - SizeYaxis
            self.graphCenterX = bc.x + bc.width/2
            self.graphCenterY = bc.y + bc.height/2
            if self.validData:
                # add valid data to chart
                bc.data = self.dataBar
                bc.categoryAxis.categoryNames = self.dataNames
                # axis values
                bc.valueAxis.valueMin  = self.valueMin
                bc.valueAxis.valueMax  = self.valueMax
                bc.valueAxis.valueStep = self.valueStep
                # add value labels above bars
                bc.barLabelFormat = self._customSecondsLabelFormat
                bc.barLabels.dy = 0.08*inch
                bc.barLabels.fontSize = 6
            else:
                # no valid data
                bc.data = [ (0, ), ]
                bc.categoryAxis.categoryNames = [ '' ]
                bc.valueAxis.valueMin  = 0
                bc.valueAxis.valueMax  = 1
                bc.valueAxis.valueStep = 1
                Nodata = Label()
                Nodata.fontSize = 12
                Nodata.angle = 0
                Nodata.boxAnchor = 'c'
                Nodata.dx = self.graphCenterX
                Nodata.dy = self.graphCenterY
                Nodata.setText("NO VALID DATA")
                self.drawing.add(Nodata)
            
            # chart formatting
            (R,G,B) = VeriwaveYellow
            bc.bars[0].fillColor   = colors.Color(R,G,B)
            bc.valueAxis.labelTextFormat = self._customSecondsLabelFormat
            # axis labels
            bc.categoryAxis.labels.boxAnchor = 'c'
            bc.categoryAxis.labels.dx    = 0
            bc.categoryAxis.labels.dy    = -10
            bc.categoryAxis.labels.angle = 0
            bc.categoryAxis.labels.fontSize = 8

            # add chart
            self.drawing.add(bc)

            #Adjust the labels to be the center of the graph
            self._drawLabels(self.title, "", "")

            # Add Legend in upper right corner
            legendHeight  = 9 
            legendX = bc.x + 5
            legendY = bc.y + bc.height - 12
            self.drawing.add(Line(legendX, legendY + 3 , legendX + 20, legendY + 3, strokeColor=bc.bars[0].fillColor, strokeWidth=3 ))
            self.drawing.add(String(legendX + 22, legendY, 'MIN', fontName='Helvetica', fontSize=8))
            legendY -= legendHeight
            self.drawing.add(Line(legendX, legendY + 3 , legendX + 20, legendY + 3, strokeColor=bc.bars[1].fillColor, strokeWidth=3 ))
            self.drawing.add(String(legendX + 22, legendY, 'MAX', fontName='Helvetica', fontSize=8))
            legendY -= legendHeight
            self.drawing.add(Line(legendX, legendY + 3 , legendX + 20, legendY + 3, strokeColor=bc.bars[2].fillColor, strokeWidth=3 ))
            self.drawing.add(String(legendX + 22, legendY, 'AVG', fontName='Helvetica', fontSize=8))
            legendY -= legendHeight
    def results_chart(self, control_mean, match_mean, treated_mean, att):
        """
        Specify layout of the results chart and generate
        flowable object that can be added to the pdf
        """
        drawing = Drawing()
        vbc = VerticalBarChart()

        # Offset chart from border and text
        vbc.x = self.chart_offset_x
        vbc.y = self.chart_offset_y

        # Set figure size
        vbc.height = self.chart_height
        vbc.width = self.chart_width

        # Specify chart -- list of lists -- list of series with enteries
        vbc.data = [[control_mean, match_mean, treated_mean, att]]

        #Set Y-Axis ranges
        #axis_range = self._calculate_y_axis(vbc.data)
        #vbc.valueAxis.valueMin = axis_range['min']
        #vbc.valueAxis.valueMax = axis_range['max']
        #vbc.valueAxis.valueStep = axis_range['step']

        #Grid formatting
        vbc.valueAxis.visibleGrid = 1
        vbc.valueAxis.gridStrokeColor = colors.lightgrey

        # Set bar characteristics
        vbc.bars[(0, 0)].fillColor = colors.blue
        vbc.bars[(0, 1)].fillColor = colors.yellow
        vbc.bars[(0, 2)].fillColor = colors.red
        vbc.bars[(0, 3)].fillColor = colors.green
        vbc.bars.strokeColor = None
        vbc.barSpacing = 2

        # Create callout labels
        #vbc.barLabels.fontName = "Helvetica"
        vbc.barLabels.fontSize = 8
        vbc.barLabels.fillColor = colors.black
        vbc.barLabelFormat = '%.2f'
        vbc.barLabels.nudge = 5

        # X-axis labels
        #vbc.categoryAxis.labels.dy = -60
        #vbc.valueAxis.labels.fontName = 'Helvetica'
        vbc.categoryAxis.categoryNames = [
            'Control Mean', 'Matched Control Mean', 'Treatment mean', 'ATT'
        ]

        lab = Label()
        lab.setOrigin(10, 155)
        lab.boxAnchor = 'ne'
        lab.angle = 90
        lab.dx = 0
        lab.dy = -15

        #lab.boxStrokeColor = colors.green
        lab.setText('Result Values')
        drawing.add(lab)

        drawing.add(vbc)
        self.elements.append(drawing)
示例#17
0
    def exportMonthlyReport(self, file, month, year):
        """Outputs the monthly report to the specified file location
            Parameters:
                self: the class instance
                file: str - file location and name ending in .pdf
                month: str - required month
                year: int - required year
        """

        # For conversion of month to three letter abbreviation
        months = [
            'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
            'Oct', 'Nov', 'Dec'
        ]

        # Creating a title
        title = Label()
        title.setOrigin(300, 20)
        title.boxAnchor = 'ne'
        title.dx = 0
        title.dy = -5
        title.fontSize = 30
        title.setText("Monthly Report")

        # Adding title to a drawing
        draw_title = Drawing(0, 40)
        draw_title.add(title)

        # Creating a subtitle
        subtitle = Label()
        subtitle.setOrigin(320, 20)
        subtitle.boxAnchor = 'ne'
        subtitle.dx = 0
        subtitle.dy = -10
        subtitle.fontSize = 14

        # Converts month to three letter abbreviation
        str_month = months[int(month) - 1]

        # Setting the subtitle's text
        subtitle.setText("Australia Zoo Wildlife Hospital, " + str_month +
                         " " + str(year))

        # Adding subtitle to a drawing
        draw_subtitle = Drawing(0, 30)
        draw_subtitle.add(subtitle)

        # Creating a label for the first chart
        label_lga = Label()
        label_lga.setOrigin(180, 20)
        label_lga.boxAnchor = 'ne'
        label_lga.dx = 0
        label_lga.dy = -20
        label_lga.setText("Local Government Area Totals")

        # Adding label to a drawing
        draw_label_lga = Drawing(0, 40)
        draw_label_lga.add(label_lga)

        # Creating drawing for the lga chart
        draw_lga = Drawing(0, 270)
        draw_lga.add(self.getSpecificBarChart("LGA", month, year))

        # Creating a label for the second chart
        label_taxons = Label()
        label_taxons.setOrigin(180, 20)
        label_taxons.boxAnchor = 'ne'
        label_taxons.dx = 0
        label_taxons.dy = -20
        label_taxons.setText("Taxon Grouping Totals")

        # Adding label to a drawing
        draw_label_taxons = Drawing(0, 40)
        draw_label_taxons.add(label_taxons)

        # Creating drawing for the taxons chart
        draw_taxons = Drawing(0, 270)
        draw_taxons.add(self.getSpecificBarChart("Taxons", month, year))

        # List of drawings in order of how to place them in the canvas
        drawlist = [
            draw_title, draw_subtitle, draw_label_lga, draw_lga,
            draw_label_taxons, draw_taxons
        ]

        # Creating a canvas (pdf file) and saving it to a location
        canvas = Canvas(file)

        # Creating a frame to add flowables (drawings) to
        frame = Frame(inch, 0, 15.92 * cm, 29.7 * cm)

        # Adding flowables
        frame.addFromList(drawlist, canvas)

        # Saving the pdf
        canvas.save()