Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # vim: set fileencoding=utf-8 ts=4 sw=4 :
- import cairo
- from math import pi
- # parameters
- width=4*2.54*72 # 4 cm in pt
- height=0.9*2.54*72 # 0.9 cm in pt
- line_width=0.33
- rect_radius=1.0
- margin=2.0
- font_face='Liberation Sans'
- output_svg='mylanguages.svg'
- # language name -> list of year ranges when it was used, None for until now
- data = {
- 'BASIC': [(1993,1996)],
- 'Pascal': [(1995,1996)],
- 'Asm. Z80': [(1994,1995),(2001,2001)],
- 'C++': [(1997,2000),(2005,None)],
- 'C': [(2000,None)],
- 'Perl': [(2001,2001)],
- 'Java': [(2002,2003)],
- 'Python': [(2004,2005),(2007,None)],
- 'Ruby': [(2006,2006)],
- 'Haskell': [(2008,None)],
- 'PHP': [(2005,2005),(2008,2008)],
- 'Fortran':[(2002,2003)],
- }
- def plot_rounded_rect(context,x,y,width,height,r):
- c=context
- c.move_to(x+r,y)
- c.line_to(x+width-r,y)
- c.arc(x+width-r,y+r,r,-pi/2,0)
- c.line_to(x+width,y+height-r)
- c.arc(x+width-r,y+height-r,r,0,pi/2)
- c.line_to(x+r,y+height)
- c.arc(x+r,y+height-r,r,pi/2,pi)
- c.line_to(x,y+r)
- c.arc(x+r,y+r,r,pi,3*pi/2)
- c.close_path()
- def plot_bar(context,text,x,y,width,height,color=(0,0.5,0)):
- c=context
- lw,r,font=line_width,rect_radius,font_face
- c.set_line_width(lw)
- plot_rounded_rect(c,x,y,width,height,r)
- c.set_source_rgb(color[0],color[1],color[2])
- c.stroke()
- plot_rounded_rect(c,x,y,width,height,r)
- c.set_source_rgba(color[0],color[1],color[2],0.1)
- c.fill()
- c.move_to(x+2*lw,y+height-4*lw)
- c.select_font_face(font)
- c.set_source_rgb(color[0],color[1],color[2])
- c.set_font_size(height-4*lw)
- c.show_text(text)
- def plot_year_labels(context,yextent,ywidth,yheight):
- c=context
- c.select_font_face(font_face)
- c.set_font_size(yheight*0.8)
- c.set_source_rgb(0.5,0.5,0.5)
- baseline=yheight-margin
- for y in range(yextent[0],yextent[1]+1):
- x=margin+(y-yextent[0])*ywidth
- c.move_to(x,baseline)
- c.show_text(str(y))
- def get_color(row,rows):
- import colorsys
- h=0.3+row*1.0/(rows+1)
- rgb=colorsys.hsv_to_rgb(h%1,1,0.8)
- return rgb
- def plot_year_bar(context,text,yextent,yrange,row,ywidth,yheight,color):
- ymin,ymax=yextent
- yfrom,yuntil=yrange
- x=margin+(yfrom-ymin)*(ywidth)
- y=margin+(1+row)*(yheight)
- width=(yuntil-yfrom+1)*ywidth-margin
- height=yheight-margin
- plot_bar(context,text,x,y,width,height,color)
- def plot_svg(filename,width,height,bars):
- svg=cairo.SVGSurface(filename,width,height)
- ctx=cairo.Context(svg)
- ymin=min([b[1] for b in bars])
- ymax=max([b[2] for b in bars])
- rows=max([b[-1] for b in bars])
- ywidth=(width-2*margin)*1.0/(ymax-ymin+1)
- yheight=(height-2*margin)*1.0/(rows+2)
- plot_year_labels(ctx,(ymin,ymax),ywidth,yheight)
- for b in bars:
- c=get_color(b[3],rows)
- plot_year_bar(ctx,b[0],(ymin,ymax),(b[1],b[2]),b[3],ywidth,yheight,c)
- svg.finish()
- def calc_year_bars(data):
- bars=[]
- r=0
- data=zip(data.keys(),data.values())
- start=lambda d:min(yr[0] for yr in d[1])
- cmpuse=lambda x,y: start(x)>start(y) and +1 or start(x)<start(y) and -1 or 0
- data=sorted(data,cmpuse)
- for d in data:
- for period in d[1]:
- bar=[d[0],period[0],period[1],r]
- bars.append(bar)
- r=r+1
- # substitute None with the current year
- import time
- ynow=time.localtime()[0]
- nonesubst=lambda b:b[2]==None and [b[0],b[1],ynow,b[-1]] or b
- bars=[nonesubst(b) for b in bars]
- return bars
- def main():
- bars=calc_year_bars(data)
- plot_svg(output_svg,width,height,bars)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement