################################ tgraphlibxyy.py ############################
# Peter Halverson 5/25/2015
# This new version handles multiple y values, to make multiple graphs
# A simple library for making xy graphs
# for an example of how to use it, look at cubic_tgraphlibxyy.py
# The "x" version has an added feature:  It optionally creates an xgraph compatible file
# with the graph.

import turtle

def smallest_element(values):   #find smallest element in 2-d array
  smallest=1.0E99
  for i in range(0,len(values)):
    if min(values[i]) < smallest:
      smallest = min(values[i])
  return smallest   
  # Alternate way to do it:  min(map(min,ll))

def biggest_element(values):   #find largest element in 2-d array
  #print "entering biggest==========================================="
  #print values
  biggest=1.0E-99
  for i in range(0,len(values)):
    if max(values[i]) > biggest:
      biggest = max(values[i])
  return biggest    
  # Alternate way to do it:  max(map(max,ll))
  
def graph_xyy(x_values,y_values):
  #print "x_values=",x_values
  #print "y_values=",y_values
  print "Making a x-y graph..."
  t = turtle.Pen()                # "Pen" must start with a capital P!
  t.speed(0)                      # 0 is fastest
  t.hideturtle()
  window_max_x = t.window_width()/2.1
  window_max_y = t.window_height()/2.1
  #print "window_max_x",window_max_x
  #print "window_max_y",window_max_y
  biggest_x=max(x_values)              #Find the biggest x value
  biggest_y=biggest_element(y_values)
  print "Biggest x value=",biggest_x
  print "Biggest y value=",biggest_y
  smallest_x=min(x_values)             #find the smallest x value
  smallest_y=smallest_element(y_values)
  print "Smallest x value=",smallest_x
  print "Smallest y value=",smallest_y
  for j in range(0,len(y_values[0])):
    t.penup()
    for i in range(0,len(x_values)):
      x=1.0*(-smallest_x+x_values[i])/(biggest_x-smallest_x)  #Make x be tween 0 and 1
      #print "norm x=",x
      y=1.0*(-smallest_y+y_values[i][j])/(biggest_y-smallest_y)  #Make y be tween 0 and 1
      #print "norm y=",y
      x=-window_max_x+(x*2.0*window_max_x)  #Make x have the full window width range
      y=-window_max_y+(y*2.0*window_max_y)  #Make x have the full window height range
      t.goto(x,y)
      t.pendown()
  # Draw a box
  t.penup()
  t.goto(-window_max_x,-window_max_y)
  t.pendown()
  t.goto(-window_max_x,window_max_y)
  t.goto(window_max_x,window_max_y)
  t.goto(window_max_x,-window_max_y)
  t.goto(-window_max_x,-window_max_y)  
  
  # Draw x and y axes
  # Draw the X axis
  t.penup()
  y=1.0*(-smallest_y+0)/(biggest_y-smallest_y)  #Make y be tween 0 and 1
  y=-window_max_y+(y*2.0*window_max_y)  #Make y have the full window width range
  t.goto(-window_max_x,y)
  t.pendown()
  t.goto(window_max_x,y)      #X axis
  t.write("x",align="right",font=("Arial", 18, "normal"))

  # Draw the Y axis
  t.penup()
  x=1.0*(-smallest_x+0)/(biggest_x-smallest_x)  #Make x be tween 0 and 1
  x=-window_max_x+(x*2.0*window_max_x)  #Make x have the full window width range
  t.goto(x,-window_max_y)
  t.pendown()
  t.goto(x,window_max_y)      #Y axis
  t.penup()
  t.goto(x+window_max_x*0.025,0.92*window_max_y)
  t.write("y",align="left",font=("Arial", 18, "normal"))
  print '''Enter a file name to make a XGraph (I will add ".dat" for you)'''
  file_name = raw_input('''or Press Enter to finish: ''')  
  # Make the xgraph file
  colors = ['black','white','red','blue','green','violet','orange','yellow','pink','cyan','light-gray','dark-gray','fuchsia','aqua','navy','gold']
  if file_name != "":
    file_name=file_name+".dat"
    title = raw_input("Enter the title of the graph. (Or just press Enter): ")
    data_file=open(file_name,"w")
    if title != "":
      data_file.write("Title =")
      data_file.write(title)
      data_file.write("\n")
    data_file.write("thickness = 2\n")
    data_file.write("title_x = x\n")
    data_file.write("title_y = y\n")
    for j in range(0,len(y_values[0])):
      print "Writing graph number ",j+1," which will have color ",colors[j+2]
      #Set the color of the graph
      data_file.write("color "+str(j+2)+"\n")
      for i in range(0,len(x_values)):
        data_file.write(str(x_values[i])+" "+str(y_values[i][j])+"\n")
      data_file.write("next\n")  
    data_file.close()
