from __future__ import division, print_function
import os

path = os.path.dirname(os.path.abspath(__file__))

from collections import Counter
from datetime import date, timedelta
import codecs

from string import Template

def create_timeline(data):

    dates = map( lambda d: d['timestamp'].date(), filter( lambda d: d['timestamp'] is not '', data ) )

    y_axis = Counter( dates )

    start_date = min( y_axis )
    end_date = max( y_axis )
    delta = end_date - start_date

    x_axis = map( lambda date: start_date + timedelta( days = date ), range( delta.days + 1 ) )

    data_points = create_data_points( x_axis, y_axis )

    js_text_template = Template( codecs.open( path + '/js/timeline.js', 'r').read() )
    html_template = Template( codecs.open( path + '/html/timeline.html', 'r').read() )

    css_text = codecs.open( path + '/css/timeline.css', 'r').read()
    js_text = js_text_template.substitute({'data' : data_points})

    return html_template.substitute( {'css': css_text, 'js': js_text} )


def create_data_points( x_axis, y_axis ):
    data_points = []

    for date in x_axis:
        if date in y_axis:
            data_points.append({'close' : y_axis[date], 'date' : str(date)})
        else:
            data_points.append({'close' : 0, 'date' : str(date)})

    return data_points

@@ -185,16 +185,28 @@
 t codecs
+%0Aimport time
 %0A%0Afrom s
@@ -252,124 +252,147 @@
 ine(
+ 
 data
-):%0A%0A    dates = map( lambda d: d%5B'timestamp'%5D.date(), filter( lambda d: d%5B'timestamp'%5D is not '',
+sets = %5B%5D, colors = %5B%5D ):%0A%0A    if not datasets: return %22No data given to create timeline.%22%0A%0A    plots = create_plots(
  data
- )
+sets
  )%0A%0A    
 y_ax
@@ -391,244 +391,623 @@
     
-y_axis = Counter( dates )%0A%0A    start_date = min( y_axis )%0A    end_date = max( y_axis )%0A    delta = end_date - start_date%0A%0A    x_axis = map( lambda date: start_date + timedelta( days = date ), range( delta.days + 1 ) )%0A%0A    data_points =
+graph_div_id = int( time.time() * 1000 )%0A%0A    html_template = create_html_template( graph_div_id )%0A    js_template = Template( codecs.open( path + '/js/timeline.js', 'r').read() )%0A%0A    css_text = create_css_text()%0A    js_text = js_template.substitute( %7B'graph_div_id' : graph_div_id,%0A                                      'plots' : plots,%0A                                      'line_colors' : colors%7D )%0A%0A    return html_template.substitute( %7B'css': css_text, 'js': js_text%7D )%0A%0A%0Adef create_plots( datasets ):%0A    plots = %5B%5D%0A    for data in datasets:%0A        x_axis, y_axis = create_axes( data )%0A        plots.append(
  cre
@@ -1043,390 +1043,408 @@
 is )
+ )
 %0A%0A    
-js_text_template = Template( codecs.open( path + '/js/timeline.js', 'r').read()
+return plots%0A%0A%0Adef create_axes( data
  )
+:
 %0A    
-html_template = Template( codecs.open( path + '/html/timeline.html', 'r').read() )%0A%0A    css_text = codecs.open( path + '/css/timeline.css', 'r').read()%0A    js_text = js_text_template.substitute(%7B'data' : data_points%7D)%0A%0A    return html_template.substitute( %7B'css': css_text, 'js': js_text%7D )
+dates = map( lambda d: d%5B'timestamp'%5D.date(), filter( lambda d: d%5B'timestamp'%5D is not '', data ) )%0A%0A    y_axis = Counter( dates )%0A%0A    start_date = min( y_axis )%0A    end_date = max( y_axis )%0A    delta = end_date - start_date%0A%0A    x_axis = map( lambda date: start_date + timedelta( days = date ), range( delta.days + 1 ) )%0A%0A    return x_axis, y_axis
 %0A%0A%0Ad
@@ -1738,8 +1738,567 @@
 _points%0A
+%0A%0Adef create_html_template( graph_div_id ):%0A%0A    html_template = Template('''%0A        %3Cstyle%3E $css %3C/style%3E%0A        %3Cdiv id=%22timeline_graph_''' + str(graph_div_id) + '''%22%3E%3C/div%3E%0A        %3Cscript%3E $js %3C/script%3E%0A    ''')%0A%0A    return html_template%0A%0A%0Adef create_css_text():%0A    css_text = '''%0A        path %7B%0A            stroke-width: 2;%0A            fill: none;%0A        %7D%0A%0A        .axis path, .axis line %7B%0A            fill: none;%0A            stroke: grey;%0A            stroke-width: 1;%0A            shape-rendering: crispEdges;%0A        %7D%0A    '''%0A%0A    return css_text%0A

Support for multiple plots and color parameters