Ejemplo n.º 1
0
def compass_url(points, width=500, height=500):
    legend = lambda p: map(lambda x: x[0], p)
    data_x = lambda p: map(lambda x: float(x[1]), p)
    data_y = lambda p: map(lambda x: float(x[2]), p)
    
    chart = ScatterChart(width, height, x_range=(-10, 10), y_range=(-10, 10))
    chart.add_data(data_x(points))
    chart.add_data(data_y(points))
    chart.set_legend(legend(points))
    chart.set_legend_position('b')
    chart.set_grid(5, 5, 5, 3)
    chart.set_colours_within_series(colors(points))
    
    axis = (chart.set_axis_labels(Axis.LEFT, ['Left']),
            chart.set_axis_labels(Axis.RIGHT, ['Right']),
            chart.set_axis_labels(Axis.TOP, ['Authoritarian']),
            chart.set_axis_labels(Axis.BOTTOM, ['Libertarian']))
    
    for a in axis:
        chart.set_axis_positions(a, [50])
        chart.set_axis_style(a, '000000', '15')

        url_bits = ['%s,%s' % (a, '-180') for a in axis]
        url = chart.get_url()
        url = url + '&chxtc=' + '|'.join(url_bits)
    return url
Ejemplo n.º 2
0
    def to_gchart(self):
        # local import, to allow setup.py to include this file, before
        # pygooglechart is installed
        from pygooglechart import ScatterChart

        chart = ScatterChart(self.width, self.height,
                             x_range=(-1, 24), y_range=(-1, 7))

        chart.add_data([(h / 7) for h in range(24 * 7)])
        chart.add_data([(h % 7) for h in range(24 * 7)])

        day_names = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
        if self.monday_first:
            days = (0, 6, 5, 4, 3, 2, 1)
        else:
            days = (6, 5, 4, 3, 2, 1, 0)

        sizes = []
        for h in range(24):
            sizes.extend([self.h["%d %02d" % (d, h)] for d in days])
        chart.add_data(sizes)

        if self.colors:
            colors = self.colors[:]
            colors.reverse()
            chart.set_colours_within_series(colors)

        if self.title:
            chart.set_title(self.title)
        if self.is12h:
            xlabels = ('|12am|1|2|3|4|5|6|7|8|9|10|11|'
                       '12pm|1|2|3|4|5|6|7|8|9|10|11|')
        else:
            xlabels = ('|0|1|2|3|4|5|6|7|8|9|10|11|'
                       '12|13|14|15|16|17|18|19|20|21|22|23|')
        chart.set_axis_labels('x', [xlabels])
        chart.set_axis_labels('y', [''] + [day_names[n] for n in days] + [''])

        chart.add_marker(1, 1.0, 'o', '333333', 25)
        chart.download(self.output)
Ejemplo n.º 3
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)