Created
September 6, 2010 02:15
-
-
Save askedrelic/566547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Script to generate a yearly-timeline google Chart API graph, | |
using pygooglechart for the graph and PIL to join the graphs together. | |
2010/09/05 22:04:45 | |
Matt Behrens <[email protected]> | |
Assuming date data in some format like this: | |
2003-04 | |
2003-08 | |
2004-02 | |
2003-07 | |
""" | |
import calendar | |
import glob, os | |
import pygooglechart | |
import Image | |
def graphThreeYears(orig_start_year): | |
total_years = 3 | |
#start empty | |
months = [] | |
years = [] | |
#setup graph | |
chart = pygooglechart.GroupedVerticalBarChart(825, 250, y_range=(0, 20)) | |
chart.set_bar_width(14) | |
chart.set_grid(0, 10, 5, 5) | |
#build empty arrays of month/year combo | |
start_year = orig_start_year | |
for y in xrange(total_years): | |
#build the monthly abbrevations | |
months += [x for x in calendar.month_abbr if x] | |
#build the empty year array | |
years += [''] * 12 | |
#then fill in January position | |
years[y * 12] = ' %s' % start_year | |
start_year += 1 | |
#fill in the dates | |
dates = [] | |
start_year = orig_start_year | |
for y in xrange(total_years): | |
for m in xrange(1,13): | |
count = 0 | |
dates.append(count) | |
start_year += 1 | |
chart.add_data(dates) | |
#setup the double label axes | |
chart.set_axis_labels('x', months) | |
chart.set_axis_labels('x', years) | |
#y labels every 2 points, except for the starting point, which is going to be empty | |
chart.set_axis_labels('y', [''] + [x for x in range(21) if x % 2 == 0 and x != 0 ]) | |
chart.download('%s-%s.png' % (orig_start_year, orig_start_year+total_years)) | |
#actually create the graphs | |
start_year = 2002 | |
end_year = 2002+3*5 | |
for year in xrange(start_year,end_year, 3): | |
graphThreeYears(year) | |
#new large canvas to work with | |
nim = Image.new("RGB", (5000,300), "white") | |
#most of the cropping and specific pixel values come from trial/error tweaking | |
x = 809 | |
counter = 0 | |
for file in glob.iglob('20*-*.png'): | |
#if the first graph, start at 0 | |
if counter == 0: | |
im = Image.open(file) | |
nim.paste(im, (0,40)) | |
#otherwise, crop the y labels from future graphs | |
else: | |
im = Image.open(file) | |
im = im.crop((18,0,825,250)) | |
nim.paste(im, (x,40)) | |
x += 790 | |
counter += 1 | |
nim.save('graph.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment