Skip to content

Instantly share code, notes, and snippets.

@aperson
Created November 9, 2011 01:59
Show Gist options
  • Save aperson/1350085 to your computer and use it in GitHub Desktop.
Save aperson/1350085 to your computer and use it in GitHub Desktop.
Quick and dirty function to build a google pie chart url
def piechart(data, labels=None, colors=None, size='600x400'):
'''Accepts a list to graph. Optionally accepts labels and colors. Both optional arguments
must have the same len as the data.'''
baseurl = 'https://chart.googleapis.com/chart?cht=p'
chs = 'chs={}'.format(size)
chd = 'chd=t:{}'.format(','.join([str(i) for i in data]))
chds = 'chds=0,{}'.format(sum(data))
if labels:
chl = 'chl={}'.format('|'.join(
['{}: {}'.format(y, data[x]) for x,y in enumerate(labels)]))
else:
chl = None
if colors:
if type(colors) == str:
chco = 'chco={}'.format(colors)
elif len(colors) == 2:
chco = 'chco={}'.format(','.join(colors))
else:
chco = 'chco={}'.format('|'.join(colors))
else:
chco = 'chco={}'.format('|'.join(
hex(random.randint(0, 16777215))[2:].upper().zfill(6) for i in range(len(data))))
output = []
for i in baseurl, chs, chd, chds, chl, chco:
if i:
output.append(i)
return '&'.join(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment