def main(): # bar chart if(c.charttype==2): svgport = svg.svg(viewbox='0 0 1000 2%d50' % 3*(c.magnitudelegend==True)) if c.magnitudelegend: chart1 = svg.g() else: chart1 = svg.g() chart1.add(makebarsvert(c.magnitudebenefit,c.saliencebenefit,c.magnitudecost,c.saliencecost)) svgport.add(chart1) # salience legend for bar if(c.saliencelegend): sal = svg.g(p='transform="rotate(90 1000 50) translate(%s 0)"' % (550+c.textradius)) sal.add(makesaliencelegend()) svgport.add(sal) else: # amoeba chart if(c.charttype==0): svgport = svg.svg(viewbox='0 0 2%d50 1%d00' % (3*(c.magnitudelegend==True), 1*(c.saliencelegend==True))) chart1 = svg.g() chart1.add(makecircle(c.magnitudebenefit,c.saliencebenefit,benefit=1)) chart2 = svg.g(p='transform="translate(1050)"') chart2.add(makecircle(c.magnitudecost,c.saliencecost,benefit=0)) svgport.add(chart1) svgport.add(chart2) if (c.magnitudelegend): mag = svg.g(p='transform="translate(2050)"') mag.add(makemagnitudelegend()) svgport.add(mag) # column chart (default) else: svgport = svg.svg(viewbox='0 0 2%d50 1%d00' % (3*(c.magnitudelegend==True), 1*(c.saliencelegend==True))) if c.magnitudelegend: chart1 = svg.g() else: chart1 = svg.g(p='transform="translate(-200)"') chart1.add(makebars(c.magnitudebenefit,c.saliencebenefit,c.magnitudecost,c.saliencecost)) svgport.add(chart1) # salience legend for amoeba and column if(c.saliencelegend): sal = svg.g(p='transform="translate(0 %s)"' % (550+c.textradius)) sal.add(makesaliencelegend()) svgport.add(sal) svg.gen(svgport, c.savename)
def makebarsvert(magnitudebenefit, saliencebenefit, magnitudecost, saliencecost): if c.swapsaliencemagnitude: tm = saliencebenefit[:] saliencebenefit = magnitudebenefit[:] magnitudebenefit = tm tm = saliencecost[:] saliencecost = magnitudecost[:] magnitudecost = tm #general variables t = 0 if c.centerlabel is not None: t = 50 out = svg.svg(width=1000, height=2250 + t, viewbox='0 0 1000 2150') barwidth = 2050 / (21 + c.spacebars * 2) chart = svg.g('transform="translate(0 %s)"' % t) out.add(chart) for cb in range(0, 2): if c.centerlabel is not None: out.add( svg.text( c.centerlabel[1 - cb], 250 + (cb) * 500, 25, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"') ) if (cb == 1): colors = c.colorsbenefit magnitude = magnitudebenefit salience = saliencebenefit else: magnitude = magnitudecost salience = saliencecost colors = c.colorscost #draw bars for i in range(0, 21): h = barheight(magnitude[i] + c.includezeromagnitude) if (c.saliencebywidth == 1): w = barwidth / (3 + c.includezerosalience) * ( salience[i] + c.includezerosalience) elif (c.saliencebywidth > 1): w = barwidth / (c.saliencebywidth**3) * (c.saliencebywidth** salience[i]) else: w = barwidth if (c.saliencebycolor): s = ((salience[i] + c.includezerosalience)** c.saliencebycolor) / ( (3.0 + c.includezerosalience)**c.saliencebycolor) else: s = 1 red, green, blue = colors[i // 7] if (salience[i] > 0 or c.includezerosalience): barlinecolor = (int(red * c.barlineshade), int(green * c.barlineshade), int(blue * c.barlineshade)) chart.add( svg.rect( 500 + (cb - 1) * h, (i + (i // 7) * c.spacebars) * barwidth + (barwidth - w) / 2, h, w, fill='rgb(%s,%s,%s)' % (red, green, blue), stroke='rgb(%s,%s,%s)' % (barlinecolor[0], barlinecolor[1], barlinecolor[2]), strokewidth=c.barline, p='fill-opacity="%s"' % s)) #invisible link and tooltip box newrect = svg.rect(500 + (cb - 1) * c.textradius, (i + (i // 7) * c.spacebars) * barwidth, c.textradius, barwidth, fill='white', p='fill-opacity="0.0"') newrect.add( svg.title(' %s: %s\n Magnitude %s\tSalience %s' % (c.indicatornames[i], c.indicatorfullnames[i], magnitude[i], salience[i]))) chart.add(newrect) #draw ygrid for i in range(0, c.maxvalue + c.includezeromagnitude + 1): if (i == c.includezeromagnitude): w = 5 else: w = 1 if (c.ygrid or i == 0): x2 = 0 else: x2 = 2050 - c.ticksize chart.add( svg.line(500 + barheight(i) * (cb - .5) * 2, 2050 + c.ticksize, 500 + barheight(i) * (cb - .5) * 2, x2, 'black', w)) #draw xgrid dividers chart.add( svg.line(500 - c.edgeradius, 2050, 500 + c.edgeradius, 2050, 'black', 5)) for i in range(0, 21 + c.spacebars * 2): if (c.xgrid or (c.saliencebywidth and c.drawsaliencedividers)): chart.add( svg.line(500 - c.edgeradius, barwidth * i, 500 + c.edgeradius, barwidth * i, 'black', 1)) elif (ticksize): chart.add( svg.line(500 - c.ticksize, barwidth * i, 500 + c.ticksize, barwidth * i, 'black', 1)) #draw salience dividers and column headings for i in range(0, 21): if (c.saliencebywidth and c.drawsaliencedividers): for j in range(1 - c.includezerosalience, 3): if (c.saliencebywidth > 1): w = barwidth / 2 / (c.saliencebywidth** 3) * (c.saliencebywidth**j) else: w = barwidth / 2 / (3 + c.includezerosalience) * ( j + c.includezerosalience) for k in [1, -1]: chart.add( svg.line(500 - c.edgeradius, barwidth * (i + (i // 7) + .5) + k * w, 500 + c.edgeradius, barwidth * (i + (i // 7) + .5) + k * w, 'black', .5)) x = (500 - c.textradius) y = ((i + (i // 7) * c.spacebars + .5) * barwidth) chart.add( svg.text( c.indicatornames[i], x, y, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) #draw magnitude legend if c.magnitudelegend: if c.swapsaliencemagnitude: te = 'Salience' else: te = 'Magnitude' mag = svg.g(p='transform="translate(0 2050)"') mag.add( svg.text( te, 500, 100, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) lastnumber = 1 for i in range(0, c.maxvalue + c.includezeromagnitude + 1): if (i - c.includezeromagnitude == 0): mag.add( svg.text( i - c.includezeromagnitude, 502 - barheight(i), 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) if (barheight(i) * 2 > c.font): mag.add( svg.text( i - c.includezeromagnitude, 502 + barheight(i), 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) elif (i - c.includezeromagnitude > 0): # alternate top and bottom if axis lables too close mag.add( svg.text( i - c.includezeromagnitude, 502 + barheight(i) * lastnumber, 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) if (barheight(i) - barheight(i - 1) > c.font): mag.add( svg.text( i - c.includezeromagnitude, 502 - barheight(i) * lastnumber, 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) else: lastnumber = lastnumber * -1 chart.add(mag) return out
def makebarsvert(magnitudebenefit,saliencebenefit,magnitudecost,saliencecost): if c.swapsaliencemagnitude: tm = saliencebenefit[:] saliencebenefit = magnitudebenefit[:] magnitudebenefit = tm tm = saliencecost[:] saliencecost = magnitudecost[:] magnitudecost = tm #general variables t = 0 if c.centerlabel is not None: t = 50 out = svg.svg(width = 1000, height = 2250+t, viewbox='0 0 1000 2150') barwidth = 2050/(21+c.spacebars*2) chart = svg.g('transform="translate(0 %s)"' % t) out.add(chart) for cb in range(0,2): if c.centerlabel is not None: out.add(svg.text(c.centerlabel[1-cb], 250+(cb)*500, 25, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) if(cb==1): colors=c.colorsbenefit magnitude=magnitudebenefit salience=saliencebenefit else: magnitude=magnitudecost salience=saliencecost colors=c.colorscost #draw bars for i in range(0,21): h = barheight(magnitude[i]+c.includezeromagnitude) if(c.saliencebywidth==1): w = barwidth/(3+c.includezerosalience)*(salience[i]+c.includezerosalience) elif(c.saliencebywidth>1): w = barwidth/(c.saliencebywidth**3)*(c.saliencebywidth**salience[i]) else: w = barwidth if(c.saliencebycolor): s = ((salience[i]+c.includezerosalience)**c.saliencebycolor)/((3.0+c.includezerosalience)**c.saliencebycolor) else: s = 1 red,green,blue = colors[i//7] if(salience[i]>0 or c.includezerosalience): barlinecolor=(int(red*c.barlineshade),int(green*c.barlineshade),int(blue*c.barlineshade)) chart.add(svg.rect(500+(cb-1)*h,(i+(i//7)*c.spacebars)*barwidth+(barwidth-w)/2,h,w, fill='rgb(%s,%s,%s)' % (red,green,blue), stroke='rgb(%s,%s,%s)' % (barlinecolor[0],barlinecolor[1],barlinecolor[2]), strokewidth=c.barline, p='fill-opacity="%s"' % s)) #invisible link and tooltip box newrect = svg.rect(500+(cb-1)*c.textradius,(i+(i//7)*c.spacebars)*barwidth,c.textradius,barwidth, fill='white', p='fill-opacity="0.0"') newrect.add(svg.title(' %s: %s\n Magnitude %s\tSalience %s' % (c.indicatornames[i], c.indicatorfullnames[i], magnitude[i], salience[i]))) chart.add(newrect) #draw ygrid for i in range(0,c.maxvalue+c.includezeromagnitude+1): if(i==c.includezeromagnitude): w=5 else: w=1 if (c.ygrid or i==0): x2 = 0 else: x2 = 2050-c.ticksize chart.add(svg.line(500+barheight(i)*(cb-.5)*2,2050+c.ticksize,500+barheight(i)*(cb-.5)*2,x2,'black',w)) #draw xgrid dividers chart.add(svg.line(500-c.edgeradius,2050,500+c.edgeradius,2050,'black',5)) for i in range(0,21+c.spacebars*2): if (c.xgrid or (c.saliencebywidth and c.drawsaliencedividers)): chart.add(svg.line(500-c.edgeradius,barwidth*i,500+c.edgeradius,barwidth*i, 'black', 1)) elif(ticksize): chart.add(svg.line(500-c.ticksize,barwidth*i,500+c.ticksize,barwidth*i, 'black', 1)) #draw salience dividers and column headings for i in range(0,21): if(c.saliencebywidth and c.drawsaliencedividers): for j in range(1-c.includezerosalience,3): if(c.saliencebywidth>1): w = barwidth/2/(c.saliencebywidth**3)*(c.saliencebywidth**j) else: w = barwidth/2/(3+c.includezerosalience)*(j+c.includezerosalience) for k in [1,-1]: chart.add(svg.line(500-c.edgeradius,barwidth*(i+(i//7)+.5)+k*w,500+c.edgeradius,barwidth*(i+(i//7)+.5)+k*w, 'black', .5)) x = (500 - c.textradius) y = ((i+(i//7)*c.spacebars+.5)*barwidth) chart.add(svg.text(c.indicatornames[i], x, y, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) #draw magnitude legend if c.magnitudelegend: if c.swapsaliencemagnitude: te = 'Salience' else: te = 'Magnitude' mag=svg.g(p='transform="translate(0 2050)"') mag.add(svg.text(te, 500, 100, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) lastnumber=1 for i in range(0,c.maxvalue+c.includezeromagnitude+1): if (i-c.includezeromagnitude==0): mag.add(svg.text(i-c.includezeromagnitude,502-barheight(i),40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) if (barheight(i)*2>c.font): mag.add(svg.text(i-c.includezeromagnitude,502+barheight(i), 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) elif (i-c.includezeromagnitude>0): # alternate top and bottom if axis lables too close mag.add(svg.text(i-c.includezeromagnitude, 502+barheight(i)*lastnumber,40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) if (barheight(i)-barheight(i-1)>c.font): mag.add(svg.text(i-c.includezeromagnitude, 502-barheight(i)*lastnumber,40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) else: lastnumber = lastnumber * -1 chart.add(mag) return out
def main(): # bar chart if (c.charttype == 2): svgport = svg.svg(viewbox='0 0 1000 2%d50' % 3 * (c.magnitudelegend == True)) if c.magnitudelegend: chart1 = svg.g() else: chart1 = svg.g() chart1.add( makebarsvert(c.magnitudebenefit, c.saliencebenefit, c.magnitudecost, c.saliencecost)) svgport.add(chart1) # salience legend for bar if (c.saliencelegend): sal = svg.g(p='transform="rotate(90 1000 50) translate(%s 0)"' % (550 + c.textradius)) sal.add(makesaliencelegend()) svgport.add(sal) else: # amoeba chart if (c.charttype == 0): svgport = svg.svg(viewbox='0 0 2%d50 1%d00' % (3 * (c.magnitudelegend == True), 1 * (c.saliencelegend == True))) chart1 = svg.g() chart1.add( makecircle(c.magnitudebenefit, c.saliencebenefit, benefit=1)) chart2 = svg.g(p='transform="translate(1050)"') chart2.add(makecircle(c.magnitudecost, c.saliencecost, benefit=0)) svgport.add(chart1) svgport.add(chart2) if (c.magnitudelegend): mag = svg.g(p='transform="translate(2050)"') mag.add(makemagnitudelegend()) svgport.add(mag) # column chart (default) else: svgport = svg.svg(viewbox='0 0 2%d50 1%d00' % (3 * (c.magnitudelegend == True), 1 * (c.saliencelegend == True))) if c.magnitudelegend: chart1 = svg.g() else: chart1 = svg.g(p='transform="translate(-200)"') chart1.add( makebars(c.magnitudebenefit, c.saliencebenefit, c.magnitudecost, c.saliencecost)) svgport.add(chart1) # salience legend for amoeba and column if (c.saliencelegend): sal = svg.g(p='transform="translate(0 %s)"' % (550 + c.textradius)) sal.add(makesaliencelegend()) svgport.add(sal) svg.gen(svgport, c.savename)