Advertisement
Guest User

jetxee

a guest
Dec 9th, 2008
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # vim: set fileencoding=utf-8 ts=4 sw=4 :
  3.  
  4. import cairo
  5. from math import pi
  6.  
  7. # parameters
  8. width=4*2.54*72 # 4 cm in pt
  9. height=0.9*2.54*72 # 0.9 cm in pt
  10. line_width=0.33
  11. rect_radius=1.0
  12. margin=2.0
  13. font_face='Liberation Sans'
  14. output_svg='mylanguages.svg'
  15.  
  16. # language name -> list of year ranges when it was used, None for until now
  17. data = {
  18.     'BASIC': [(1993,1996)],
  19.     'Pascal': [(1995,1996)],
  20.     'Asm. Z80': [(1994,1995),(2001,2001)],
  21.     'C++': [(1997,2000),(2005,None)],
  22.     'C': [(2000,None)],
  23.     'Perl': [(2001,2001)],
  24.     'Java': [(2002,2003)],
  25.     'Python': [(2004,2005),(2007,None)],
  26.     'Ruby': [(2006,2006)],
  27.     'Haskell': [(2008,None)],
  28.     'PHP': [(2005,2005),(2008,2008)],
  29.     'Fortran':[(2002,2003)],
  30. }
  31.  
  32. def plot_rounded_rect(context,x,y,width,height,r):
  33.     c=context
  34.     c.move_to(x+r,y)
  35.     c.line_to(x+width-r,y)
  36.     c.arc(x+width-r,y+r,r,-pi/2,0)
  37.     c.line_to(x+width,y+height-r)
  38.     c.arc(x+width-r,y+height-r,r,0,pi/2)
  39.     c.line_to(x+r,y+height)
  40.     c.arc(x+r,y+height-r,r,pi/2,pi)
  41.     c.line_to(x,y+r)
  42.     c.arc(x+r,y+r,r,pi,3*pi/2)
  43.     c.close_path()
  44.  
  45. def plot_bar(context,text,x,y,width,height,color=(0,0.5,0)):
  46.     c=context
  47.     lw,r,font=line_width,rect_radius,font_face
  48.     c.set_line_width(lw)
  49.     plot_rounded_rect(c,x,y,width,height,r)
  50.     c.set_source_rgb(color[0],color[1],color[2])
  51.     c.stroke()
  52.     plot_rounded_rect(c,x,y,width,height,r)
  53.     c.set_source_rgba(color[0],color[1],color[2],0.1)
  54.     c.fill()
  55.     c.move_to(x+2*lw,y+height-4*lw)
  56.     c.select_font_face(font)
  57.     c.set_source_rgb(color[0],color[1],color[2])
  58.     c.set_font_size(height-4*lw)
  59.     c.show_text(text)
  60.  
  61. def plot_year_labels(context,yextent,ywidth,yheight):
  62.     c=context
  63.     c.select_font_face(font_face)
  64.     c.set_font_size(yheight*0.8)
  65.     c.set_source_rgb(0.5,0.5,0.5)
  66.     baseline=yheight-margin
  67.     for y in range(yextent[0],yextent[1]+1):
  68.         x=margin+(y-yextent[0])*ywidth
  69.         c.move_to(x,baseline)
  70.         c.show_text(str(y))
  71.  
  72. def get_color(row,rows):
  73.     import colorsys
  74.     h=0.3+row*1.0/(rows+1)
  75.     rgb=colorsys.hsv_to_rgb(h%1,1,0.8)
  76.     return rgb
  77.  
  78. def plot_year_bar(context,text,yextent,yrange,row,ywidth,yheight,color):
  79.     ymin,ymax=yextent
  80.     yfrom,yuntil=yrange
  81.     x=margin+(yfrom-ymin)*(ywidth)
  82.     y=margin+(1+row)*(yheight)
  83.     width=(yuntil-yfrom+1)*ywidth-margin
  84.     height=yheight-margin
  85.     plot_bar(context,text,x,y,width,height,color)
  86.  
  87. def plot_svg(filename,width,height,bars):
  88.     svg=cairo.SVGSurface(filename,width,height)
  89.     ctx=cairo.Context(svg)
  90.     ymin=min([b[1] for b in bars])
  91.     ymax=max([b[2] for b in bars])
  92.     rows=max([b[-1] for b in bars])
  93.     ywidth=(width-2*margin)*1.0/(ymax-ymin+1)
  94.     yheight=(height-2*margin)*1.0/(rows+2)
  95.     plot_year_labels(ctx,(ymin,ymax),ywidth,yheight)
  96.     for b in bars:
  97.         c=get_color(b[3],rows)
  98.         plot_year_bar(ctx,b[0],(ymin,ymax),(b[1],b[2]),b[3],ywidth,yheight,c)
  99.     svg.finish()
  100.  
  101. def calc_year_bars(data):
  102.     bars=[]
  103.     r=0
  104.     data=zip(data.keys(),data.values())
  105.     start=lambda d:min(yr[0] for yr in d[1])
  106.     cmpuse=lambda x,y: start(x)>start(y) and +1 or start(x)<start(y) and -1 or 0
  107.     data=sorted(data,cmpuse)
  108.     for d in data:
  109.         for period in d[1]:
  110.             bar=[d[0],period[0],period[1],r]
  111.             bars.append(bar)
  112.         r=r+1
  113.     # substitute None with the current year
  114.     import time
  115.     ynow=time.localtime()[0]
  116.     nonesubst=lambda b:b[2]==None and [b[0],b[1],ynow,b[-1]] or b
  117.     bars=[nonesubst(b) for b in bars]
  118.     return bars
  119.  
  120. def main():
  121.     bars=calc_year_bars(data)
  122.     plot_svg(output_svg,width,height,bars)
  123.  
  124. if __name__ == '__main__':
  125.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement