Ejemplo n.º 1
0
def saveGraph(clusters, width = 500, height = 500):
    """
    Generate graph and save it as a PNG file.
    """

#    width = 500
#    height = width
    padding = 30
    xlist = []
    ylist = []
    coords = []
    i = 0
    j = 0
    pointColours = []

    for cluster in clusters:
	for point in cluster.points:
	    dimension = point.n
	    for i in xrange(dimension):
		coords.append([])
	    break
	break
    coords.append([]) # For the z-axis/colors/point size

    # Because of the braindead way of serializing the point coordinates and
    # other data about the points, this is hairy.  We specify the colour for
    # each point individually, despite really caring about the colour of the
    # whole series

    # 28 clusters supported
    palette = [
	    "FF0000" ,"00FF00" ,"0000FF" ,"FFFF00" ,"FF00FF"
	    ,"FFFF00" ,"00FFFF" ,"FF00FF" ,"00FFFF" ,"AA0000" ,"00AA00"
	    ,"0000AA" ,"AAAA00" ,"AA00AA" ,"AAAA00" ,"00AAAA" ,"AA00AA"
	    ,"00AAAA" ,"550000" ,"005500" ,"000055" ,"555500" ,"550055"
	    ,"555500" ,"005555" ,"550055" ,"005555"
	    ,
	    "FF3333" ,"33FF33" ,"3333FF" ,"FFFF33" ,"FF33FF"
	    ,"FFFF33" ,"33FFFF" ,"FF33FF" ,"33FFFF" ,"AA3333" ,"33AA33"
	    ,"3333AA" ,"AAAA33" ,"AA33AA" ,"AAAA33" ,"33AAAA" ,"AA33AA"
	    ,"33AAAA" ,"553333" ,"335533" ,"333355" ,"555533" ,"553355"
	    ,"555533" ,"335555" ,"553355" ,"335555"
	    ,
	    "FF9999" ,"99FF99" ,"9999FF" ,"FFFF99" ,"FF99FF"
	    ,"FFFF99" ,"99FFFF" ,"FF99FF" ,"99FFFF" ,"AA9999" ,"99AA99"
	    ,"9999AA" ,"AAAA99" ,"AA99AA" ,"AAAA99" ,"99AAAA" ,"AA99AA"
	    ,"99AAAA" ,"559999" ,"995599" ,"999955" ,"555599" ,"559955"
	    ,"555599" ,"995555" ,"559955" ,"995555"
	    ]
    clusterNumber = 1
    for cluster in clusters:
	for point in cluster.points:
	    for i in xrange(point.n):
		coords[i].append(point.coords[i])
	    # Different size dots
	    #coords[2].append(clusterNumber)
	    coords[2].append(5)
	    try:
		pointColours.append(palette[clusterNumber])
	    except:
		pointColours.append("000000")
	clusterNumber += 1



    xlist = coords[0]
    ylist = coords[1]
    zlist = coords[2]

    chart = ScatterChart(width, height, 
                         x_range=(min(xlist) - padding, max(xlist) + padding),
                         y_range=(min(ylist) - padding, max(ylist) + padding))
    chart.set_axis_range('x', min(xlist) - padding, max(xlist) + padding)
    chart.set_axis_range('t', min(xlist) - padding, max(xlist) + padding)
    chart.set_axis_range('y', min(ylist) - padding, max(ylist) + padding)
    chart.set_axis_range('r', min(ylist) - padding, max(ylist) + padding)

#    # Add an invisible point to allow scalling of the other points
#    # This must come after the min/max above
#    for cluster in clusters:
#	for point in cluster.points:
#	    for i in xrange(point.n):
#		coords[i].append(point.coords[i] + 16300)
#	    # Must be bigger then the rest
#	    coords[2].append(10)
#	    # White-on-white, i.e. invisible
#	    pointColours.append("FFFFFF")
#	    break
#	break

    chart.add_data(xlist)
    chart.add_data(ylist)
    chart.add_data(zlist)
    chart.set_colours_within_series(pointColours)


    chart.download(_image_file_name)