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)
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 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)
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)
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 draw(self): g = Group() #box g.add( Rect(self.x, self.y, len(self.xlabels) * self.gridDivWidth, len(self.ylabels) * self.gridDivWidth, strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=None)) #internal gridding for f in range(1, len(self.ylabels)): #horizontal g.add( Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1=self.x, y1=self.y + f * self.gridDivWidth, x2=self.x + len(self.xlabels) * self.gridDivWidth, y2=self.y + f * self.gridDivWidth)) for f in range(1, len(self.xlabels)): #vertical g.add( Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1=self.x + f * self.gridDivWidth, y1=self.y, x2=self.x + f * self.gridDivWidth, y2=self.y + len(self.ylabels) * self.gridDivWidth)) # draw the 'dot' g.add( Circle(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=self.dotColor, cx=self.x + (self.dotXPosition * self.gridDivWidth), cy=self.y + (self.dotYPosition * self.gridDivWidth), r=self.dotDiameter / 2.0)) #used for centering y-labels (below) ascent = getFont(self.labelFontName).face.ascent if ascent == 0: ascent = 0.718 # default (from helvetica) ascent = ascent * self.labelFontSize # normalize #do y-labels if self.ylabels != None: for f in range(len(self.ylabels) - 1, -1, -1): if self.ylabels[f] != None: g.add( String(strokeColor=self.gridColor, text=self.ylabels[f], fontName=self.labelFontName, fontSize=self.labelFontSize, fillColor=_PCMYK_black, x=self.x - self.labelOffset, y=self.y + (f * self.gridDivWidth + (self.gridDivWidth - ascent) / 2.0), textAnchor='end')) #do x-labels if self.xlabels != None: for f in range(0, len(self.xlabels)): if self.xlabels[f] != None: l = Label() l.x = self.x + (f * self.gridDivWidth ) + (self.gridDivWidth + ascent) / 2.0 l.y = self.y + (len(self.ylabels) * self.gridDivWidth) + self.labelOffset l.angle = 90 l.textAnchor = 'start' l.fontName = self.labelFontName l.fontSize = self.labelFontSize l.fillColor = _PCMYK_black l.setText(self.xlabels[f]) l.boxAnchor = 'sw' l.draw() g.add(l) return g
def get_pdf_results(task_id): # Flask response response = Response() response.status_code = 200 task = data.get_task_result(task_id) #Saving file to a in-memory file output_file = StringIO.StringIO() def header_footer(canvas, doc): canvas.saveState() background = 'static/img/pdf_bg.png' canvas.drawImage(background, 1 * inch, 5.75 * inch, width=8 * inch, height=6 * inch, mask='auto') # Header logo = Image('static/img/logo/logo.png') logo.drawHeight = 0.5 * inch logo.drawWidth = 1.75 * inch date = datetime.now().strftime("%y-%m-%d %H:%M") headerData = [[logo, '', date]] headerTable = Table(headerData, colWidths=[2 * inch, 3.58 * inch, 1.2 * inch], style=[('LINEBELOW', (0, 0), (2, 0), 1, colors.HexColor(0xcccccc)), ('TEXTCOLOR', (0, 0), (2, 0), colors.HexColor(0x807F83)), ('VALIGN', (1, 0), (1, 0), 'MIDDLE'), ('VALIGN', (2, 0), (2, 0), 'MIDDLE')]) headerTable.wrapOn(canvas, doc.width, doc.topMargin) headerTable.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin) pageNum = "Page %d" % doc.page footerData = [[ 'KAPSARC Building Energy Assessment Tool (BEAT)', pageNum ]] footerTable = Table(footerData, colWidths=[5.76 * inch, 1 * inch], style=[('LINEABOVE', (0, 0), (1, 0), 2, colors.HexColor(0xcccccc)), ('TEXTCOLOR', (0, 0), (1, 0), colors.HexColor(0x807F83)), ('ALIGN', (1, 0), (1, 0), 'RIGHT')]) footerTable.wrapOn(canvas, doc.width, doc.bottomMargin) footerTable.drawOn(canvas, doc.leftMargin, 0.5 * inch) canvas.restoreState() pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf')) pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf')) pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf')) pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf')) styles = getSampleStyleSheet() # Title styles.add( ParagraphStyle(name='styleTitle', alignment=TA_CENTER, fontSize=16, fontName='Vera', textColor=colors.HexColor(0x61a659), leading=30, spaceBefore=35, spaceAfter=10)) # Headings styles.add( ParagraphStyle(name='styleHeading', parent=styles['Heading2'], fontSize=14, textColor=colors.HexColor(0x807F83), leading=20, spaceBefore=10, underlineProportion=1.1, spaceAfter=10)) styles.add( ParagraphStyle(name='styleHeading2', parent=styles['Heading2'], fontSize=14, textColor=colors.HexColor(0x61a659), leading=20, spaceBefore=20, underlineProportion=1.1, spaceAfter=20)) styles.add( ParagraphStyle( name='styleHeading3', #alignment= TA_CENTER, fontSize=12, fontName='Vera', textColor=colors.HexColor(0x61a659), leading=20, spaceBefore=10, spaceAfter=5)) # Body text styles.add( ParagraphStyle(name='styleBodyText', parent=styles['Normal'], fontSize=9, textColor=colors.HexColor(0x666666), spaceBefore=5, spaceAfter=15)) styleTitle = styles['styleTitle'] styleHeading = styles['styleHeading'] styleHeading2 = styles['styleHeading2'] styleHeading3 = styles['styleHeading3'] styleBodyText = styles['styleBodyText'] pdf_chart_colors = [ "#3D6531", "#61a24f", "#89B97B", "#B0D1A7", "#cde5c7", "#7e7f82", "#9E9FA1", "#BFBFC1", "#DFDFE0", "#ffd200", "#FFE360", "#FFEE9F", ] Elements = [] doc = BaseDocTemplate(output_file, showBoundary=0, pagesize=A4, title='KASPSARC BEAT Report', author="KAPSARC", leftMargin=0.75 * inch, rightMargin=0.75 * inch, topMargin=inch, bottomMargin=inch) frame = Frame(doc.leftMargin, doc.topMargin, doc.width, doc.height, topPadding=0.3 * inch, showBoundary=0) template = PageTemplate(id='template', frames=[frame], onPage=header_footer) doc.addPageTemplates([template]) ## PAGE 1 #add some flowables Elements.append( Paragraph("KAPSARC Building Energy Assessment Tool (BEAT)", styleTitle)) Elements.append(Paragraph("Your Building Description", styleHeading)) rowHeights = 0.3 * inch calibrationData = task['calibrationData'] Elements.append(Paragraph("General Information:", styleHeading3)) infoTableData = [ [ Paragraph('<b>- Name: </b>' + calibrationData['txtBldgName'], styleBodyText), Paragraph('<b>- Address: </b>' + calibrationData['txtBldgAddress'], styleBodyText), Paragraph('<b>- Type: </b>' + calibrationData['cmbBldgType'], styleBodyText) ], [ Paragraph( '<b>- Location: </b>' + calibrationData['cmbBldgLocation'], styleBodyText), Paragraph('<b>- Shape: </b>' + calibrationData['cmbBldgShape'], styleBodyText), Paragraph( '<b>- Floor Area (m' + u"\u00b2" + '): </b>' + str(calibrationData['txtFloorArea']), styleBodyText) ] ] infoTable = Table(infoTableData, colWidths=[160, 165, 150], rowHeights=rowHeights) Elements.append(infoTable) Elements.append(Paragraph('<br />', styleBodyText)) Elements.append(Paragraph("Envelope Construction Details:", styleHeading3)) envTableData = [ [ Paragraph( '<b>- South Wall: </b>' + calibrationData['cmbSouthWall'], styleBodyText), Paragraph('<b>- West Wall: </b>' + calibrationData['cmbWestWall'], styleBodyText) ], [ Paragraph( '<b>- North Wall: </b>' + calibrationData['cmbNorthWall'], styleBodyText), Paragraph('<b>- East Wall: </b>' + calibrationData['cmbEastWall'], styleBodyText) ], [ Paragraph('<b>- Roof: </b>' + calibrationData['cmbRoof'], styleBodyText), Paragraph( '<b>- Floor: </b>' + calibrationData['cmbFirstFloorContact'], styleBodyText) ], [ Paragraph('<b>- Windows Type: </b>' + calibrationData['glasstype'], styleBodyText), Paragraph( '<b>- Overhang Depth (m): </b>' + str(calibrationData['txtWinSouthOverhang']), styleBodyText) ] ] envTable = Table(envTableData, colWidths=[240, 235], rowHeights=rowHeights) Elements.append(envTable) Elements.append(Paragraph('<br />', styleBodyText)) Elements.append(Paragraph("Air Conditioning Systems", styleHeading3)) hvacTableData = [[ Paragraph( '<b>- HVAC System Type: </b>' + calibrationData['cmbBldgSystem'], styleBodyText), Paragraph( '<b>- Cooling Temperature Setting (' + u"\u00b0" + 'C): </b>' + str(calibrationData['txtCoolSetTemp']), styleBodyText) ], [ Paragraph( '<b>- Energy Efficiency Ratio (EER): </b>' + str(calibrationData['eir']), styleBodyText), Paragraph( '<b>- Heating Temperature Setting (' + u"\u00b0" + 'C): </b>' + str(calibrationData['txtHeatSetTemp']), styleBodyText) ]] hvacTable = Table(hvacTableData, colWidths=[240, 235], rowHeights=rowHeights) Elements.append(hvacTable) Elements.append(Paragraph('<br />', styleBodyText)) Elements.append(Paragraph("Overall Assessment", styleHeading)) Elements.append( Paragraph( "Based on your description and current SASO requirements, the tool provides the following assessments:", styleBodyText)) if task['compliant']: compliant = "<strong><font color=green>meets</font></strong>" else: compliant = "<strong><font color=red>does not meet</font></strong>" if (task['ngEnergyDiff'] < 0): energyDiff = "<strong><font color=green>" + str( task['energyDiff']) + " kWh/year, less</font></strong>" else: energyDiff = "<strong><font color=red>" + str( task['energyDiff']) + " kWh/year, more</font></strong>" Elements.append( Paragraph( "- Your building " + compliant + " SASO requirements for all building envelope", styleBodyText)) Elements.append( Paragraph( "- Your building consumed " + energyDiff + " than the SASO Baseline", styleBodyText)) if task['compliant']: Elements.append( Paragraph( "- You may reduce even more your energy consumption in your building by using LED lamps and high efficient appliances and air conditioning system", styleBodyText)) else: Elements.append( Paragraph( " - You need to add more insulation to the walls and/or roof, or use more efficient window glazing to comply with SASO requirements", styleBodyText)) if not task['compliant'] and (task['ngEnergyDiff'] >= 0): Elements.append( Paragraph( " - You may also consider using LED lamps and energy efficient appliances and air conditioning system", styleBodyText)) Elements.append(PageBreak()) Elements.append( Paragraph("How electricity is used in your building?", styleHeading3)) Elements.append( Paragraph( "Your building needs electricity to operate several types of equipment including: air-conditioning, lighting, appliances and domestic hot water.", styleBodyText)) #add image Elements.append( Image('static/img/results-intro.png', width=4 * inch, height=1.2 * inch)) #add text Elements.append( Paragraph( "Based on the description you provided as well as typical lighting and appliances used in households, here how your building consumes electricity on annual basis:", styleBodyText)) bepuPieData = task['bepuPieData'] bepuTableData = [[0 for i in xrange(len(bepuPieData[0]))] for i in xrange(len(bepuPieData) + 1)] bepuChartLabel = [0 for i in xrange(len(bepuPieData))] bepuChartData = [0 for i in xrange(len(bepuPieData))] bepuTableData[0][0] = 'End-Use' bepuTableData[0][1] = 'Annual Electricity Use' for i, result in enumerate(bepuPieData): # write body cells bepuTableData[i + 1][0] = str(result['label']) bepuTableData[i + 1][1] = int(result['value']) bepuChartLabel[i] = str(result['label']) bepuChartData[i] = result['value'] #add chart bepuChart = Drawing(400, 200) pc = Pie() pc.data = bepuChartData labelc = [0 for i in xrange(len(bepuChartData))] for i, r in enumerate(bepuChartData): labelc[i] = str(round(r / sum(bepuChartData) * 100, 1)) + "%" pc.labels = labelc pc._seriesCount = len(bepuChartLabel) pc.slices.strokeColor = colors.HexColor(0xffffff) pc.slices.strokeWidth = 0.5 bepu_chart_colors = ['#FFC43E', '#A4A4A4', '#F67A40', '#5894D0', '#98cd99'] for i, r in enumerate(bepuChartLabel): pc.slices[i].fillColor = colors.HexColor(bepu_chart_colors[i]) pc.width = pc.height = 120 pc.x = 40 pc.y = 30 # add_legend(d, pc) legend = Legend() legend.alignment = 'right' legend.x = pc.width + pc.x + 80 legend.y = pc.height - 10 legend.dx = 8 legend.dy = 8 legend.fontName = 'Helvetica' legend.fillColor = colors.HexColor(0x807F83) legend.fontSize = 10 legend.boxAnchor = 'nw' legend.columnMaximum = 8 legend.strokeWidth = 0.5 legend.strokeColor = colors.HexColor(0xffffff) legend.deltax = 75 legend.deltay = 10 legend.autoXPadding = 10 legend.yGap = 0 legend.dxTextSpace = 5 legend.dividerLines = 1 | 2 | 4 legend.dividerOffsY = 6 legend.subCols.rpad = 70 legend.dividerColor = colors.HexColor(0xdedede) legend.colorNamePairs = [(pc.slices[i].fillColor, (bepuChartLabel[i][0:20], ' %s ' % "{:,}".format(int(pc.data[i])))) for i in xrange(len(pc.data))] legendHeader = Legend() legendHeader.colorNamePairs = [ ('', ('End-Use', 'Annual Electricity Use\n(kWh/year)')) ] legendHeader.alignment = 'right' legendHeader.x = legend.x - 20 legendHeader.y = legend.y + 30 legendHeader.fontName = 'Helvetica' legendHeader.fillColor = colors.HexColor(0x807F83) legendHeader.fontSize = 10 legendHeader.boxAnchor = 'nw' legendHeader.subCols.rpad = 80 legendFooter = Legend() legendFooter.colorNamePairs = [ ('', ('Total', str("{:,}".format(int(sum(bepuChartData)))) + '')) ] legendFooter.alignment = 'right' legendFooter.x = legendHeader.x + 5 legendFooter.y = legend.y - (len(bepuChartLabel) + 1) * 10 legendFooter.fontName = 'Helvetica-Bold' legendFooter.fillColor = colors.HexColor(0x807F83) legendFooter.fontSize = 10 legendFooter.boxAnchor = 'nw' legendFooter.subCols.rpad = 145 bepuChart.add(legend) bepuChart.add(legendHeader) bepuChart.add(legendFooter) pc.slices.fontColor = colors.HexColor(0x807F83) n = len(pc.data) bepuChart.add(pc, '') Elements.append(bepuChart) ## PAGE 2 Elements.append( Paragraph("When electricity is consumed in your building?", styleHeading3)) Elements.append( Paragraph( "Based on the weather of your location as well as typical lighting and appliances used in households, your building consumes electricity as noted in the following monthly profile:", styleBodyText)) #add chart pseBarData = task['pseBarData'] pseTableData = [[0 for i in xrange(len(pseBarData[0]['values']) + 1)] for i in xrange(len(pseBarData) + 1)] pseChartData = [[0 for i in xrange(len(pseBarData[0]['values']))] for i in xrange(len(pseBarData))] pseChartLegend = [0 for i in xrange(len(pseBarData))] pseTableData[0][0] = 'Key' month = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] for i, m in enumerate(month): pseTableData[0][i + 1] = str(month[i]) for i, result in enumerate(pseBarData): # write body cells pseTableData[i + 1][0] = str(result['key']) pseChartLegend[i] = str(result['key']) for j, value in enumerate(result['values']): pseTableData[i + 1][j + 1] = int(result['values'][j]['y']) pseChartData[i][j] = int(result['values'][j]['y']) pseChart = Drawing(400, 200) bc = VerticalBarChart() bc.x = 70 bc.y = 0 bc.height = 200 bc.width = 300 bc.data = pseChartData bc.strokeColor = colors.black bc.valueAxis.valueMin = 0 bc.strokeWidth = 0 bc.valueAxis.valueMin = 0 bc.categoryAxis.style = 'stacked' bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = 10 bc.categoryAxis.labels.dy = -2 bc.valueAxis.labels.fontName = 'Helvetica' bc.valueAxis.labels.fontSize = 10 bc.valueAxis.strokeWidth = 0.5 bc.valueAxis.strokeColor = colors.HexColor(0x807F83) bc.categoryAxis.strokeWidth = 0.5 bc.categoryAxis.strokeColor = colors.HexColor(0x807F83) bc.valueAxis.labels.fillColor = colors.HexColor(0x807F83) bc.categoryAxis.labels.fontName = 'Helvetica' bc.categoryAxis.labels.fontSize = 10 bc.categoryAxis.labels.fillColor = colors.HexColor(0x807F83) bc.categoryAxis.categoryNames = month # create a list and add the elements of our document (image, paragraphs, table, chart) to it #add our barchart and legend bc.barWidth = .3 * inch bc.groupSpacing = .2 * inch bc.bars.strokeColor = colors.HexColor(0xffffff) bc.bars.strokeWidth = 0. pse_chart_colors = ['#FFC43E', '#A4A4A4', '#F67A40', '#5894D0'] for i, r in enumerate(pseChartLegend): bc.bars[i].fillColor = colors.HexColor(pse_chart_colors[i]) # = colors.blue legend = Legend() legend.alignment = 'right' legend.x = bc.width + bc.x + 5 legend.y = bc.height + bc.y legend.deltax = 40 legend.dxTextSpace = 5 legend.dx = 8 legend.dy = 8 legend.fontName = 'Helvetica' legend.fillColor = colors.HexColor(0x807F83) legend.fontSize = 10 legend.boxAnchor = 'nw' legend.columnMaximum = (len(bc.data) + 1) / 2 legend.strokeWidth = 0.5 legend.strokeColor = colors.HexColor(0xffffff) legend.deltax = 75 legend.deltay = 12 legend.dividerColor = colors.HexColor(0xdedede) legend.columnMaximum = len(pseChartLegend) legend.colorNamePairs = [(bc.bars[i].fillColor, pseChartLegend[i]) for i in xrange(len(bc.data))] #pseChart.hAlign = 'RIGHT' label = Label() label.setOrigin(10, bc.height / 2) #label.boxAnchor = 'sw' label.angle = 90 label.fillColor = colors.HexColor(0x807F83) label.setText('Electricity Consumption (kWh)') label.fontName = 'Helvetica' pseChart.add(legend, 'legend') pseChart.add(bc) pseChart.add(label) Elements.append(pseChart) Elements.append(PageBreak()) ## PAGE 3 Elements.append( Paragraph( "Does your building meet SASO Thermal Performance Requirements?", styleHeading3)) Elements.append( Paragraph( "Based on your description, the thermal transmittance properties of the walls, roof and glazing are calculated, and compared with SASO thermal building performance requirements:", styleBodyText)) #add chart lvdData = task['lvdData'] lvdChartData = [[0 for i in xrange(len(lvdData[0]['values']))] for i in xrange(len(lvdData))] lvdChartCategoryNames = [0 for i in xrange(len(lvdData[0]['values']))] lvdComparedObjKey = [0 for i in xrange(len(lvdData))] for i, result in enumerate(lvdData): # write body cells lvdComparedObjKey[i] = str(lvdData[i]['key']) for j, value in enumerate(result['values']): lvdChartCategoryNames[j] = value['label'] lvdChartData[i][j] = value['value'] lvdChart = Drawing(400, 200) bc = VerticalBarChart() bc.x = 70 bc.y = 0 bc.height = 200 bc.width = 300 bc.data = lvdChartData bc.strokeColor = colors.black # bc.fillColor=colors.blue bc.valueAxis.valueMin = 0 bc.strokeWidth = 0 bc.valueAxis.valueMin = 0 bc.categoryAxis.labels.boxAnchor = 'n' bc.categoryAxis.labels.dx = 0 bc.categoryAxis.labels.dy = -2 # bc.categoryAxis.labels.angle = 20 bc.valueAxis.labels.fontName = 'Helvetica' bc.valueAxis.labels.fontSize = 10 bc.valueAxis.strokeWidth = 0.5 bc.valueAxis.strokeColor = colors.HexColor(0x807F83) bc.categoryAxis.strokeWidth = 0.5 bc.categoryAxis.strokeColor = colors.HexColor(0x807F83) bc.valueAxis.labels.fillColor = colors.HexColor(0x807F83) bc.categoryAxis.labels.fontName = 'Helvetica' bc.categoryAxis.labels.fontSize = 8 bc.categoryAxis.labels.fillColor = colors.HexColor(0x807F83) bc.categoryAxis.categoryNames = lvdChartCategoryNames bc.categoryAxis.labels.angle = 0 # create a list and add the elements of our document (image, paragraphs, table, chart) to it #add our barchart and legend bc.barWidth = .3 * inch bc.groupSpacing = .2 * inch bc.bars.strokeColor = colors.HexColor(0xffffff) bc.bars.strokeWidth = 0.5 lvd_chart_colors = ['#5894D0', '#F67A40'] for i, r in enumerate(lvdComparedObjKey): bc.bars[i].fillColor = colors.HexColor(lvd_chart_colors[i]) # = colors.blue legend = Legend() legend.alignment = 'right' legend.x = bc.width + bc.x + 5 legend.y = bc.height + bc.y legend.deltax = 40 legend.dxTextSpace = 5 legend.dx = 8 legend.dy = 8 legend.fontName = 'Helvetica' legend.fillColor = colors.HexColor(0x807F83) legend.fontSize = 10 legend.boxAnchor = 'nw' legend.columnMaximum = (len(bc.data) + 1) / 2 legend.strokeWidth = 0.5 legend.strokeColor = colors.HexColor(0xffffff) legend.deltax = 75 legend.deltay = 12 legend.dividerColor = colors.HexColor(0xdedede) legend.columnMaximum = len(lvdComparedObjKey) legend.colorNamePairs = [(bc.bars[i].fillColor, lvdComparedObjKey[i]) for i in xrange(len(bc.data))] #pseChart.hAlign = 'RIGHT' label = Label() label.setOrigin(10, bc.height / 2) #label.boxAnchor = 'sw' label.angle = 90 label.fillColor = colors.HexColor(0x807F83) label.setText('Envelope U-value (W/m' + u'\u00b2' + '.k)') label.fontName = 'Helvetica' lvdChart.add(label) lvdChart.add(legend, 'legend') lvdChart.add(bc) Elements.append(lvdChart) #Elements.append(PageBreak()) Elements.append(Paragraph('<br /><br />', styleBodyText)) ## PAGE 4 Elements.append( Paragraph("How energy efficient is your building?", styleHeading3)) Elements.append( Paragraph( "Using your input specifications, the annual electricity consumption is calculated and compared with a similar building that meets SASO requirements:", styleBodyText)) #add chart bepuComparisonData = task['bepuComparisonData'] bepuComparisonChartData = [[ 0 for i in xrange(len(bepuComparisonData[0]['values'])) ] for i in xrange(len(bepuComparisonData))] bepuChartCategoryNames = [ 0 for i in xrange(len(bepuComparisonData[0]['values'])) ] bepuComparedObjKey = [0 for i in xrange(len(bepuComparisonData))] for i, result in enumerate(bepuComparisonData): # write body cells bepuComparedObjKey[i] = str(bepuComparisonData[i]['key']) for j, value in enumerate(result['values']): bepuChartCategoryNames[j] = value['label'] bepuComparisonChartData[i][j] = value['value'] bepuComparisonChart = Drawing(400, 200) bc = VerticalBarChart() bc.x = 70 bc.y = 0 bc.height = 200 bc.width = 300 bc.data = bepuComparisonChartData bc.strokeColor = colors.black # bc.fillColor=colors.blue bc.valueAxis.valueMin = 0 bc.strokeWidth = 0 bc.valueAxis.valueMin = 0 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = 10 bc.categoryAxis.labels.dy = -2 # bc.categoryAxis.labels.angle = 20 bc.valueAxis.labels.fontName = 'Helvetica' bc.valueAxis.labels.fontSize = 10 bc.valueAxis.strokeWidth = 0.5 bc.valueAxis.strokeColor = colors.HexColor(0x807F83) bc.categoryAxis.strokeWidth = 0.5 bc.categoryAxis.strokeColor = colors.HexColor(0x807F83) bc.valueAxis.labels.fillColor = colors.HexColor(0x807F83) bc.categoryAxis.labels.fontName = 'Helvetica' bc.categoryAxis.labels.fontSize = 10 bc.categoryAxis.labels.fillColor = colors.HexColor(0x807F83) bc.categoryAxis.categoryNames = bepuChartCategoryNames bc.categoryAxis.labels.angle = 30 # create a list and add the elements of our document (image, paragraphs, table, chart) to it #add our barchart and legend bc.barWidth = .3 * inch bc.groupSpacing = .2 * inch bc.bars.strokeColor = colors.HexColor(0xffffff) bc.bars.strokeWidth = 0.5 bepu_chart_colors = ['#5894D0', '#F67A40'] for i, r in enumerate(bepuComparedObjKey): bc.bars[i].fillColor = colors.HexColor(bepu_chart_colors[i]) # = colors.blue # bc.bars[1].fillColor = colors.lightblue legend = Legend() legend.alignment = 'right' legend.x = bc.width + bc.x + 5 legend.y = bc.height + bc.y legend.deltax = 40 legend.dxTextSpace = 5 legend.dx = 8 legend.dy = 8 legend.fontName = 'Helvetica' legend.fillColor = colors.HexColor(0x807F83) legend.fontSize = 10 legend.boxAnchor = 'nw' legend.columnMaximum = (len(bc.data) + 1) / 2 legend.strokeWidth = 0.5 legend.strokeColor = colors.HexColor(0xffffff) legend.deltax = 75 legend.deltay = 12 legend.dividerColor = colors.HexColor(0xdedede) legend.columnMaximum = len(bepuComparedObjKey) legend.colorNamePairs = [(bc.bars[i].fillColor, bepuComparedObjKey[i]) for i in xrange(len(bc.data))] #pseChart.hAlign = 'RIGHT' label = Label() label.setOrigin(10, bc.height / 2) #label.boxAnchor = 'sw' label.angle = 90 label.fillColor = colors.HexColor(0x807F83) label.setText('Annual Energy Use (kWh/year)') label.fontName = 'Helvetica' bepuComparisonChart.add(label) bepuComparisonChart.add(legend, 'legend') bepuComparisonChart.add(bc) Elements.append(bepuComparisonChart) Elements.append(PageBreak()) doc.build(Elements) output_file.seek(0) # Set filname and mimetype file_name = 'K-BEAT_export_{}.pdf'.format( datetime.now().strftime("%Y-%m-%d %H:%M:%S")) #Returning the file from memory return send_file(output_file, attachment_filename=file_name, as_attachment=True)
def draw(self): g = Group() #box g.add(Rect(self.x,self.y,len(self.xlabels)*self.gridDivWidth,len(self.ylabels)*self.gridDivWidth, strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=None)) #internal gridding for f in range (1,len(self.ylabels)): #horizontal g.add(Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1 = self.x, y1 = self.y+f*self.gridDivWidth, x2 = self.x+len(self.xlabels)*self.gridDivWidth, y2 = self.y+f*self.gridDivWidth)) for f in range (1,len(self.xlabels)): #vertical g.add(Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1 = self.x+f*self.gridDivWidth, y1 = self.y, x2 = self.x+f*self.gridDivWidth, y2 = self.y+len(self.ylabels)*self.gridDivWidth)) # draw the 'dot' g.add(Circle(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=self.dotColor, cx = self.x+(self.dotXPosition*self.gridDivWidth), cy = self.y+(self.dotYPosition*self.gridDivWidth), r = self.dotDiameter/2.0)) #used for centering y-labels (below) ascent=getFont(self.labelFontName).face.ascent if ascent==0: ascent=0.718 # default (from helvetica) ascent=ascent*self.labelFontSize # normalize #do y-labels if self.ylabels != None: for f in range (len(self.ylabels)-1,-1,-1): if self.ylabels[f]!= None: g.add(String(strokeColor=self.gridColor, text = self.ylabels[f], fontName = self.labelFontName, fontSize = self.labelFontSize, fillColor=_PCMYK_black, x = self.x-self.labelOffset, y = self.y+(f*self.gridDivWidth+(self.gridDivWidth-ascent)/2.0), textAnchor = 'end')) #do x-labels if self.xlabels != None: for f in range (0,len(self.xlabels)): if self.xlabels[f]!= None: l=Label() l.x=self.x+(f*self.gridDivWidth)+(self.gridDivWidth+ascent)/2.0 l.y=self.y+(len(self.ylabels)*self.gridDivWidth)+self.labelOffset l.angle=90 l.textAnchor='start' l.fontName = self.labelFontName l.fontSize = self.labelFontSize l.fillColor = _PCMYK_black l.setText(self.xlabels[f]) l.boxAnchor = 'sw' l.draw() g.add(l) return g