#!/usr/local/bin/python 

from pylab import *
from matplotlib.numerix import arange
import matplotlib

matplotlib.use('Agg')

class sparkline:
 
 def __init__ (self,datos):
  self.datos = datos
  self.num_puntos = len(self.datos)

 def pinta (self,x,y,color,texto= True):
  plot([x,x],[y,y],color)
  if texto:
   text(x, y, str(y), size=6)

 def pinta_max (self):
  maximo = max(self.datos)
  pos_max = self.datos.index(maximo)
  self.pinta(pos_max, self.datos[pos_max], 'b.')

 def pinta_min (self):
  minimo = min(self.datos)
  pos_min = self.datos.index(minimo)
  self.pinta(pos_min, self.datos[pos_min], 'b.')

 def pinta_inicio(self):
  self.pinta(0,self.datos[0],'r.')

 def pinta_fin(self):
  num_puntos = len(self.datos) - 1
  self.pinta(num_puntos, self.datos[num_puntos],'r.')

 def genera(self):
  
  fig_alto = 0.4 #0.3 
  fig_ancho = 2 #min(5, max(1.5, 0.05 * self.num_puntos))

  self.fig = figure(figsize=(fig_ancho,fig_alto), dpi=150)

  plot(range(self.num_puntos), self.datos, color='gray')

  maximo = max(self.datos)
  minimo = min(self.datos)

  self.pinta_inicio()
  self.pinta_fin()
  self.pinta_max()
  self.pinta_min()
  
  axis([-1, self.num_puntos+1, minimo - (abs(minimo)*0.01), #minimo - (abs(minimo)*0.01), maximo + (abs(maximo)*0.01)]) #maximo + (abs(maximo)*0.01)])    

  axis('off')
  
 def guarda(self, nombre_fichero):
  savefig(nombre_fichero)

# del 20 de noviembre al 20 de febrero
euribor= [3.851, 3.841, 3.854, 3.870, 3.859, 3.859, 3.845,3.844,  3.859, 3.851, 3.804, 3.816, 3.832, 3.841, 3.884,3.897, 3.900, 3.901, 3.924, 3.939,  3.942, 3.962, 3.971, 3.989, 3.991, 3.991, 3.991, 4.003, 4.022, 4.028, 4.028, 4.030, 4.029, 4.030, 4.024, 4.047,4.051,  4.063, 4.078, 4.052, 4.062,4.062, 4.058, 4.075, 4.072,4.080,4.074,4.076,4.079,4.090,4.092,4.096,4.097,4.080,4.089,4.054,4.056, 4.065,4.078,4.100,4.093,4.099,4.107,4.101,4.089,4.115,4.122]

datos = [100, 123, 130, 110, 490, 85, 300, -123, 125, 130, 144, 90, 95]

a = sparkline(euribor)
a.genera()
a.guarda("p.png")

a = ""

b = sparkline(datos)
b.genera()
b.guarda("p2.png")
