def makeLineChart(self, context, width, height, data, xvalues, backgroundColor, borderColor, labelAngles, labelXOffsets, labelYOffsets, lineColors, lineWidths, lineLabelFormat, yAxisMin, yAxisMax, yAxisStep): content = [] drawing = Drawing(width, height) lp = HorizontalLineChart() lp.x = 0 lp.y = 0 lp.height = height lp.width = width lp.data = data lp.joinedLines = 1 if backgroundColor: lp.fillColor = colors.HexColor(backgroundColor) if borderColor: lp.strokeColor = colors.HexColor(borderColor) if yAxisMin is not None: lp.valueAxis.valueMin = yAxisMin if yAxisMax is not None: lp.valueAxis.valueMax = yAxisMax if yAxisStep: lp.valueAxis.valueStep = yAxisStep if xvalues: lp.categoryAxis.categoryNames = xvalues if labelAngles is not None: self.handleSingleOrList(lp.categoryAxis.labels, labelAngles, 'angle', len(data[0])) if labelXOffsets is not None: self.handleSingleOrList(lp.categoryAxis.labels, labelXOffsets, 'dx', len(data[0])) if labelYOffsets is not None: self.handleSingleOrList(lp.categoryAxis.labels, labelYOffsets, 'dy', len(data[0])) if lineLabelFormat: lp.lineLabelFormat = lineLabelFormat if lineColors: self.handleSingleOrList(lp.lines, lineColors, 'strokeColor', len(data), colors.HexColor) if lineWidths: self.handleSingleOrList(lp.lines, lineWidths, 'strokeWidth', len(data)) drawing.add(lp) content.append( drawing ) return content
def create_graph(graph_data): color=[green,red,blue,yellow,black] color_index = 0 line_chart = Drawing(400, 200) data_length=len(graph_data["data"]) lc = HorizontalLineChart() lc.x = 50 lc.y = 50 lc.height = 125 lc.width = 300 lc.data = graph_data["data"] lc.fillColor=lightblue lc.joinedLines = 1 lc.categoryAxis.categoryNames = list(graph_data["label"][0]) lc.categoryAxis.labels.boxAnchor ='n' lc.valueAxis.valueMin = utils.get_least_pt(graph_data["data"]) lc.valueAxis.valueMax = utils.get_max_pt(graph_data["data"]) lc.valueAxis.valueStep = (lc.valueAxis.valueMax - lc.valueAxis.valueMin)/10 if (lc.valueAxis.valueMax - lc.valueAxis.valueMin) > 40 else (lc.valueAxis.valueMax - lc.valueAxis.valueMin)/5 for i in xrange(data_length): lc.lines[i].strokeWidth = 2 lc.lines[i].strokeColor = color[i] line_chart.add(lc) line_leg = LineLegend() line_leg.boxAnchor = 'sw' line_leg.x = 100 line_leg.y = -1 line_leg.columnMaximum = 1 line_leg.yGap = 0 line_leg.deltax = 50 line_leg.deltay = 0 line_leg.dx = 10 line_leg.dy = 1.5 line_leg.fontSize = 10 line_leg.alignment = 'right' line_leg.dxTextSpace = 5 line_leg.colorNamePairs = [(color[i],graph_data["legend"][0][i]) for i in xrange(data_length)] line_chart.add(line_leg) return line_chart
def create_single_grade_pdf(student_id, class_id, assignment_count, grade_standard_dict, grade_student_dict, assignment_line_all, assignment_names, assignment_dict): '''--Variables--''' Story=[] Elements=[] buff = StringIO() formatted_time = time.ctime() minimum = 100 standard_averages=[[]] standard_table=[] #content_areas = [] '''------''' styles = getSampleStyleSheet() HeaderStyle = styles["Heading1"] #get the student Name #Create the name for the PDf being returned pdfName = get_student_name(student_id).first_name+"_"+get_student_name(student_id).last_name+"_SR"+".pdf" #set up the response headers so the browser knows to return a PDF document response.headers['Content-Type'] = 'application/pdf' response.headers['Content-Disposition'] ='attachment;filename=%s;'%pdfName doc = SimpleDocTemplate(buff,pagesize=letter,rightMargin=72,leftMargin=72,topMargin=72,bottomMargin=18) doc.title=pdfName #Set up some styles styles=getSampleStyleSheet() styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) styles.add(ParagraphStyle(name='Indent', rightIndent=3)) styles.add(ParagraphStyle(name = 'Title2', parent = styles['Normal'], fontName = 'DejaVuSansCondensed', fontSize = 18, leading = 22, #alignment = TA_LEFT, spaceAfter = 6), alias = 'title2') #Time-Stamp ptext = '<font size=12>%s</font>' % formatted_time Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 12)) Elements.extend(ptext) #Administrator ptext='<font size=12><b>%s %s</b></font>'%(get_student_name(student_id).first_name, get_student_name(student_id).last_name) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 12)) Elements.extend(ptext) #Grade Number and Content Area ptext = '<font size=12><b>%s Student Report</b></font>'%(get_class_name(class_id).name) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 7)) Elements.extend(ptext) #Total Assignments ptext = '<font size=12>Total Assignments: %s</font>'%(assignment_count) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 7)) Elements.extend(ptext) Story.append(Spacer(1,20)) #Graph Title ptext = '<font size=15><b>Current Performance by Standard</b></font>' Story.append(Paragraph(ptext, styles["title"])) Story.append(Spacer(1,50)) #get all the standards for a specific grade and content area standard_query = ((db.classes.id == class_id)& (student_id == db.student.id)& (db.classes.id == db.student_classes.class_id)& (db.student.id == db.student_classes.student_id)& (db.student.id == db.student_grade.student_id)& (db.grade.id == db.student_grade.grade_id)& (db.grade.id == db.grade_standard.grade_id)& (db.standard.id == db.grade_standard.standard_id)& (db.classes.id == db.class_grade.class_id)& (db.grade.id == db.class_grade.grade_id)& (db.standard.content_area == db.contentarea.id)) standard_list = db(standard_query).select(db.standard.id, db.standard.short_name, db.standard.reference_number,db.student_grade.student_score, db.grade.score, db.contentarea.name) standard_ref_list=[] #Setup the Dictionary of standard averages standard_dict = {} standard_table = [] standard_averages=[[]] for row in standard_list: if row.standard.id in standard_dict.keys(): if((row.grade.score != 0.0) | (row.student_grade.student_score != 0.0)): max_score = standard_dict[row.standard.id][0] + row.grade.score student_score = standard_dict[row.standard.id][1] + row.student_grade.student_score standard_dict[row.standard.id] = [max_score, student_score, row.standard.reference_number, row.standard.short_name] else: standard_dict[row.standard.id] = [row.grade.score, row.student_grade.student_score, row.standard.reference_number, row.standard.short_name] i = 0 #set up the 2D list of Standard Averages for standard in sorted(standard_dict.keys()): standard_ref_list.append(standard_dict[standard][2]) standard_table.append([]) current_avg = (standard_dict[standard][1]/standard_dict[standard][0])*100 if minimum > current_avg: minimum = current_avg #int/round was here standard_table[i].append(standard_dict[standard][3]+": "+format((standard_dict[standard][1]/standard_dict[standard][0])*100,'.2f')+"%") #int/round was here standard_averages[0].append((standard_dict[standard][1]/standard_dict[standard][0])*100) for grade in grade_standard_dict.keys(): for standardId in grade_standard_dict[grade][1]: if(standardId == standard): standard_table[i].append(grade_standard_dict[grade][0]+":"+format((grade_student_dict[grade][1]/grade_student_dict[grade][0])*100, '.2f')+"%") i+=1 sorted(standard_table,key=lambda l:l[0]) '''---Graph---''' drawing = Drawing(600, 200) data = standard_averages bc = VerticalBarChart() #location in the document (x,y) bc.x = 10 bc.y = 30 #width and height of the graph bc.height = 225 bc.width = 400 bc.data = data bc.categoryAxis.drawGridLast=True bc.categoryAxis.gridStart=0 bc.categoryAxis.gridStrokeLineCap = 2 bc.categoryAxis.gridEnd=3 #bc.barLabels = #Update colors of the bars in the graph bc.bars.symbol = ShadedRect() bc.bars.symbol.fillColorStart = colors.lightblue bc.bars.symbol.fillColorEnd = colors.lightblue bc.bars.symbol.strokeWidth = 0 #this draws a line at the top of the graph to close it. bc.strokeColor = colors.black #Y-axis min, max, and steps. if minimum != 100: bc.valueAxis.valueMin = minimum -10 else: bc.valueAxis.valueMin = 50 bc.valueAxis.valueMax = 100 bc.valueAxis.valueStep = 5 #where to anchor the origin of the graph bc.categoryAxis.labels.boxAnchor = 'ne' #Locations of labels for the X-axis bc.categoryAxis.labels.dx = 2 bc.categoryAxis.labels.dy = -2 bc.barLabels.nudge = -10 bc.barLabelFormat = '%.2f%%' bc.barLabels.dx = 0 bc.barLabels.dy = 0 #The angle of the lables for the X-axis bc.categoryAxis.labels.angle = 30 #List of the categories to place on the X-axis bc.categoryAxis.categoryNames = standard_ref_list drawing.add(bc) '''------''' '''--Graph Legend--''' #Graph Legend legend = Legend() legend.alignment = 'right' legend.x = 420 legend.y = 150 legend.deltax = 60 legend.dxTextSpace = 10 legend.columnMaximum = 4 legend.colorNamePairs = [(colors.lightblue, 'grade average')] drawing.add(legend, 'legend') drawing_title = "Bar Graph" Story.append(drawing) Story.append(Spacer(1,15)) #LineGraph Title ptext = '<font size=15><b>Class Performance by Assignment</b></font>' Story.append(Paragraph(ptext, styles["title"])) Story.append(Spacer(1,30)) ''' Line Plot Graph ------ ''' assignment_data_all =[[],[]] for key in assignment_dict.keys(): assignment_data_all[0].append(assignment_dict[key][2]) assignment_data_all[1].append(assignment_dict[key][1]) drawing2 = Drawing(600, 200) data2 = assignment_data_all #lp = LinePlot() #data[0] = preprocessData(data[0]) lp = HorizontalLineChart() lp.x = -20 lp.y = 0 lp.height = 225 lp.width = 500 lp.data = data2 lp.joinedLines = 1 lp.lines.symbol = makeMarker('FilledCircle') lp.lines[0].strokeColor = colors.grey lp.lines[1].strokeColor = colors.lightblue lp.strokeColor = colors.black lp.categoryAxis.labels.fontSize = 7 lp.categoryAxis.categoryNames = assignment_names lp.categoryAxis.labels.boxAnchor = 'ne' lp.categoryAxis.labels.angle = 30 lp.categoryAxis.drawGridLast=True #lp.categoryAxis.gridStart=0 lp.categoryAxis.gridStrokeLineCap = 2 #lp.categoryAxis.gridEnd=3 #lp.categoryAxis.visibleGrid = 1 lp.valueAxis.visibleGrid = 1 lp.valueAxis.visible = 1 lp.valueAxis.drawGridLast=False #lp.valueAxis.gridStart = 0 #lp.valueAxis.gridEnd = 100 lp.valueAxis.gridStrokeColor = colors.black lp.valueAxis.valueMin = 0 lp.valueAxis.valueMax = 105 lp.valueAxis.valueStep = 10 lp.lineLabelFormat = '%2.0f' lp.strokeColor = colors.black lp.fillColor = colors.white drawing2.add(lp) legend = Legend() legend.alignment = 'right' legend.x = 482 legend.y = 150 legend.deltax = 60 legend.dxTextSpace = 2 legend.colorNamePairs = [(colors.lightblue, 'Student'),(colors.grey, 'Class')] drawing2.add(legend, 'legend') Story.append(drawing2) Story.append(Spacer(1,30)) ptext = '<font size=15><b>Assignments by Standard</b></font>' Story.append(Paragraph(ptext, styles["title"])) Story.append(Spacer(1,10)) t=Table(standard_table) t.setStyle(t.setStyle(TableStyle([('BOX', (0,0), (-1,-1), 0.25, colors.black), ('FONTSIZE', (0,0), (-1,-1), 7), ('BACKGROUND',(0,0),(0,-1),colors.lightgrey), ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),]))) Story.append(t) #build PDF document and return it doc.build(Story) pdf = buff.getvalue() buff.close() return pdf
def create_single_grade_pdf(student_id, class_id, assignment_count, grade_standard_dict, grade_student_dict, assignment_line_all, assignment_names, assignment_dict): '''--Variables--''' Story = [] Elements = [] buff = StringIO() formatted_time = time.ctime() minimum = 100 standard_averages = [[]] standard_table = [] #content_areas = [] '''------''' styles = getSampleStyleSheet() HeaderStyle = styles["Heading1"] #get the student Name #Create the name for the PDf being returned pdfName = get_student_name(student_id).first_name + "_" + get_student_name( student_id).last_name + "_SR" + ".pdf" #set up the response headers so the browser knows to return a PDF document response.headers['Content-Type'] = 'application/pdf' response.headers[ 'Content-Disposition'] = 'attachment;filename=%s;' % pdfName doc = SimpleDocTemplate(buff, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18) doc.title = pdfName #Set up some styles styles = getSampleStyleSheet() styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) styles.add(ParagraphStyle(name='Indent', rightIndent=3)) styles.add( ParagraphStyle( name='Title2', parent=styles['Normal'], fontName='DejaVuSansCondensed', fontSize=18, leading=22, #alignment = TA_LEFT, spaceAfter=6), alias='title2') #Time-Stamp ptext = '<font size=12>%s</font>' % formatted_time Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 12)) Elements.extend(ptext) #Administrator ptext = '<font size=12><b>%s %s</b></font>' % (get_student_name( student_id).first_name, get_student_name(student_id).last_name) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 12)) Elements.extend(ptext) #Grade Number and Content Area ptext = '<font size=12><b>%s Student Report</b></font>' % ( get_class_name(class_id).name) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 7)) Elements.extend(ptext) #Total Assignments ptext = '<font size=12>Total Assignments: %s</font>' % (assignment_count) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 7)) Elements.extend(ptext) Story.append(Spacer(1, 20)) #Graph Title ptext = '<font size=15><b>Current Performance by Standard</b></font>' Story.append(Paragraph(ptext, styles["title"])) Story.append(Spacer(1, 50)) #get all the standards for a specific grade and content area standard_query = ((db.classes.id == class_id) & (student_id == db.student.id) & (db.classes.id == db.student_classes.class_id) & (db.student.id == db.student_classes.student_id) & (db.student.id == db.student_grade.student_id) & (db.grade.id == db.student_grade.grade_id) & (db.grade.id == db.grade_standard.grade_id) & (db.standard.id == db.grade_standard.standard_id) & (db.classes.id == db.class_grade.class_id) & (db.grade.id == db.class_grade.grade_id) & (db.standard.content_area == db.contentarea.id)) standard_list = db(standard_query).select( db.standard.id, db.standard.short_name, db.standard.reference_number, db.student_grade.student_score, db.grade.score, db.contentarea.name) standard_ref_list = [] #Setup the Dictionary of standard averages standard_dict = {} standard_table = [] standard_averages = [[]] for row in standard_list: if row.standard.id in standard_dict.keys(): if ((row.grade.score != 0.0) | (row.student_grade.student_score != 0.0)): max_score = standard_dict[row.standard.id][0] + row.grade.score student_score = standard_dict[ row.standard.id][1] + row.student_grade.student_score standard_dict[row.standard.id] = [ max_score, student_score, row.standard.reference_number, row.standard.short_name ] else: standard_dict[row.standard.id] = [ row.grade.score, row.student_grade.student_score, row.standard.reference_number, row.standard.short_name ] i = 0 #set up the 2D list of Standard Averages for standard in sorted(standard_dict.keys()): standard_ref_list.append(standard_dict[standard][2]) standard_table.append([]) current_avg = (standard_dict[standard][1] / standard_dict[standard][0]) * 100 if minimum > current_avg: minimum = current_avg #int/round was here standard_table[i].append(standard_dict[standard][3] + ": " + format( (standard_dict[standard][1] / standard_dict[standard][0]) * 100, '.2f') + "%") #int/round was here standard_averages[0].append( (standard_dict[standard][1] / standard_dict[standard][0]) * 100) for grade in grade_standard_dict.keys(): for standardId in grade_standard_dict[grade][1]: if (standardId == standard): standard_table[i].append( grade_standard_dict[grade][0] + ":" + format((grade_student_dict[grade][1] / grade_student_dict[grade][0]) * 100, '.2f') + "%") i += 1 sorted(standard_table, key=lambda l: l[0]) '''---Graph---''' drawing = Drawing(600, 200) data = standard_averages bc = VerticalBarChart() #location in the document (x,y) bc.x = 10 bc.y = 30 #width and height of the graph bc.height = 225 bc.width = 400 bc.data = data bc.categoryAxis.drawGridLast = True bc.categoryAxis.gridStart = 0 bc.categoryAxis.gridStrokeLineCap = 2 bc.categoryAxis.gridEnd = 3 #bc.barLabels = #Update colors of the bars in the graph bc.bars.symbol = ShadedRect() bc.bars.symbol.fillColorStart = colors.lightblue bc.bars.symbol.fillColorEnd = colors.lightblue bc.bars.symbol.strokeWidth = 0 #this draws a line at the top of the graph to close it. bc.strokeColor = colors.black #Y-axis min, max, and steps. if minimum != 100: bc.valueAxis.valueMin = minimum - 10 else: bc.valueAxis.valueMin = 50 bc.valueAxis.valueMax = 100 bc.valueAxis.valueStep = 5 #where to anchor the origin of the graph bc.categoryAxis.labels.boxAnchor = 'ne' #Locations of labels for the X-axis bc.categoryAxis.labels.dx = 2 bc.categoryAxis.labels.dy = -2 bc.barLabels.nudge = -10 bc.barLabelFormat = '%.2f%%' bc.barLabels.dx = 0 bc.barLabels.dy = 0 #The angle of the lables for the X-axis bc.categoryAxis.labels.angle = 30 #List of the categories to place on the X-axis bc.categoryAxis.categoryNames = standard_ref_list drawing.add(bc) '''------''' '''--Graph Legend--''' #Graph Legend legend = Legend() legend.alignment = 'right' legend.x = 420 legend.y = 150 legend.deltax = 60 legend.dxTextSpace = 10 legend.columnMaximum = 4 legend.colorNamePairs = [(colors.lightblue, 'grade average')] drawing.add(legend, 'legend') drawing_title = "Bar Graph" Story.append(drawing) Story.append(Spacer(1, 15)) #LineGraph Title ptext = '<font size=15><b>Class Performance by Assignment</b></font>' Story.append(Paragraph(ptext, styles["title"])) Story.append(Spacer(1, 30)) ''' Line Plot Graph ------ ''' assignment_data_all = [[], []] for key in assignment_dict.keys(): assignment_data_all[0].append(assignment_dict[key][2]) assignment_data_all[1].append(assignment_dict[key][1]) drawing2 = Drawing(600, 200) data2 = assignment_data_all #lp = LinePlot() #data[0] = preprocessData(data[0]) lp = HorizontalLineChart() lp.x = -20 lp.y = 0 lp.height = 225 lp.width = 500 lp.data = data2 lp.joinedLines = 1 lp.lines.symbol = makeMarker('FilledCircle') lp.lines[0].strokeColor = colors.grey lp.lines[1].strokeColor = colors.lightblue lp.strokeColor = colors.black lp.categoryAxis.labels.fontSize = 7 lp.categoryAxis.categoryNames = assignment_names lp.categoryAxis.labels.boxAnchor = 'ne' lp.categoryAxis.labels.angle = 30 lp.categoryAxis.drawGridLast = True #lp.categoryAxis.gridStart=0 lp.categoryAxis.gridStrokeLineCap = 2 #lp.categoryAxis.gridEnd=3 #lp.categoryAxis.visibleGrid = 1 lp.valueAxis.visibleGrid = 1 lp.valueAxis.visible = 1 lp.valueAxis.drawGridLast = False #lp.valueAxis.gridStart = 0 #lp.valueAxis.gridEnd = 100 lp.valueAxis.gridStrokeColor = colors.black lp.valueAxis.valueMin = 0 lp.valueAxis.valueMax = 105 lp.valueAxis.valueStep = 10 lp.lineLabelFormat = '%2.0f' lp.strokeColor = colors.black lp.fillColor = colors.white drawing2.add(lp) legend = Legend() legend.alignment = 'right' legend.x = 482 legend.y = 150 legend.deltax = 60 legend.dxTextSpace = 2 legend.colorNamePairs = [(colors.lightblue, 'Student'), (colors.grey, 'Class')] drawing2.add(legend, 'legend') Story.append(drawing2) Story.append(Spacer(1, 30)) ptext = '<font size=15><b>Assignments by Standard</b></font>' Story.append(Paragraph(ptext, styles["title"])) Story.append(Spacer(1, 10)) t = Table(standard_table) t.setStyle( t.setStyle( TableStyle([ ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ('FONTSIZE', (0, 0), (-1, -1), 7), ('BACKGROUND', (0, 0), (0, -1), colors.lightgrey), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ]))) Story.append(t) #build PDF document and return it doc.build(Story) pdf = buff.getvalue() buff.close() return pdf
def makeLineChart(self, context, width, height, data, xvalues, backgroundColor, borderColor, labelAngles, labelXOffsets, labelYOffsets, lineColors, lineWidths, lineLabelFormat, yAxisMin, yAxisMax, yAxisStep): content = [] drawing = Drawing(width, height) lp = HorizontalLineChart() lp.x = 0 lp.y = 0 lp.height = height lp.width = width lp.data = data lp.joinedLines = 1 if backgroundColor: lp.fillColor = colors.HexColor(backgroundColor) if borderColor: lp.strokeColor = colors.HexColor(borderColor) if yAxisMin: lp.valueAxis.valueMin = yAxisMin if yAxisMax: lp.valueAxis.valueMax = yAxisMax if yAxisStep: lp.valueAxis.valueStep = yAxisStep if xvalues: lp.categoryAxis.categoryNames = xvalues def handleSingleOrList(targetObject, value, propertyName, defaultLength, mapFunc=None): if isinstance(value, list): for i in range(0, len(value)): setattr(targetObject[i], propertyName, value[i] if not mapFunc else mapFunc(value[i])) else: for i in range(0, defaultLength): setattr(targetObject[i], propertyName, value if not mapFunc else mapFunc(value)) if labelAngles: handleSingleOrList(lp.categoryAxis.labels, labelAngles, 'angle', len(data[0])) if labelXOffsets: handleSingleOrList(lp.categoryAxis.labels, labelXOffsets, 'dx', len(data[0])) if labelYOffsets: handleSingleOrList(lp.categoryAxis.labels, labelYOffsets, 'dy', len(data[0])) if lineLabelFormat: lp.lineLabelFormat = lineLabelFormat if lineColors: handleSingleOrList(lp.lines, lineColors, 'strokeColor', len(data), colors.HexColor) if lineWidths: handleSingleOrList(lp.lines, lineWidths, 'strokeWidth', len(data)) drawing.add(lp) content.append(drawing) return content