Skip to content

Commit

Permalink
Evidence graph for Explore data works
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluestreak2k5 committed Dec 1, 2011
1 parent c2470e5 commit 1610e81
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
11 changes: 7 additions & 4 deletions hunchworks/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import json
import urlparse
from hunchworks.utils.data.worldbank import indicator as wb_indicator
from hunchworks.utils.data.worldbank import chart as wb_chart
from django.template.loader import render_to_string


def worldbank(url):
if url.startswith("http://localhost:8000/explore?"):
if url.startswith("http://127.0.0.1:8000/explore?"):
qs = urlparse.parse_qs(urlparse.urlparse(url).query)
data = wb_chart(qs["indicator"][0], qs["country"])

data = wb_indicator(qs["indicator"][0], qs["country"][0].split(","))
countries, data = wb_chart(data)

return render_to_string(
"embeds/worldbank.html", {
"data": json.dumps(data)
"flat_data": json.dumps(data),
"flat_countries": json.dumps(countries)
}
)
1 change: 1 addition & 0 deletions hunchworks/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>

{% compress js %}
<!-- vendor libs -->
Expand Down
4 changes: 3 additions & 1 deletion hunchworks/templates/embeds/worldbank.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<div class="worldbank-chart" data-x-axis="Year" data-y-axis="What" data-data="{{ data }}"></div>
<section class="graph">
<div class="worldbank-chart" data-x-axis="Year" data-countries="{{ flat_countries }}" data-data="{{ flat_data }}"></div>
</section>
4 changes: 0 additions & 4 deletions hunchworks/templates/explore/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<h1>Explore Data</h1>
{% endblock %}

{% block js %}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
{% endblock %}

{% block content %}
{% if show_graph %}
<section class="graph">
Expand Down
37 changes: 34 additions & 3 deletions hunchworks/utils/data/worldbank.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,44 @@ def countries():
def indicator(indicator_code, country_codes):
return _api("countries/%s/indicators/%s" % (
";".join(country_codes), indicator_code))



def _value(value):
if value is not None:
return float(value)

def chart(indicator_code, country_codes):
def chart(data):
all_country_data = {}
country = ""

for item in reversed(data):

if country != item["country"]["value"]:
country = item["country"]["value"]
all_country_data[country] = {}

all_country_data[country][item["date"]] = _value(item["value"])

# This will be a unique set of years across all countries in all_country_data
final_year_set = set()

for country in sorted(all_country_data.keys()):
final_year_set.update(all_country_data[country].keys())

# country_keys are the unique country names for all countries in the data.
country_keys = all_country_data.keys()

# final_country_data will be an array of arrays looking like this:
# [ ["1998", 13000, 14000], ["1999", 15000, 16000], ["2000", 17000, 20000] ]
final_country_data = []
for year in sorted(final_year_set):
temp_data = [year]
for country in country_keys:
temp_data.append( all_country_data[country][year] )
final_country_data.append(temp_data)

return (country_keys, final_country_data)

def old_chart(indicator_code, country_codes):
data = indicator(indicator_code, country_codes)

flat_data = [
Expand Down
49 changes: 8 additions & 41 deletions hunchworks/views/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ def _render(req, template, more_context):
def worldbank_indicators(indicator_id, country_ids):
data = worldbank.indicator(indicator_id, country_ids)

def _extract(key):
# transform worldbank-style structure into tokeninput style structure
# { "id": 1, "value": "US" } => { "id": 1, "name": "US" }
def _format_for_tokeninput(key):
things = []

for item in data:
for item in data.copy():
thing = item[key]
thing["name"] = thing["value"]
del thing["value"]
Expand All @@ -33,47 +35,12 @@ def _extract(key):

return things

countries = _extract("country")
indicators = _extract("indicator")

def _value(value):
if value is not None:
return float(value)

# all_country_data will output a dictionary that looks like this:
# { Brazil: { 2003: 10000, 2004: 11000, 2005: 12000 },
# China: { 2003: 12000, 2004: 14000, 2006: 16000 }
# }
all_country_data = {}
country = ""

for item in reversed(data):

if country != item["country"]["name"]:
country = item["country"]["name"]
all_country_data[country] = {}

all_country_data[country][item["date"]] = _value(item["value"])

# This will be a unique set of years across all countries in all_country_data
final_year_set = set()

for country in sorted(all_country_data.keys()):
final_year_set.update(all_country_data[country].keys())

# country_keys are the unique country names for all countries in the data.
country_keys = all_country_data.keys()
countries_prepop = _format_for_tokeninput("country")
indicators_prepop = _format_for_tokeninput("indicator")

# final_country_data will be an array of arrays looking like this:
# [ ["1998", 13000, 14000], ["1999", 15000, 16000], ["2000", 17000, 20000] ]
final_country_data = []
for year in sorted(final_year_set):
temp_data = [year]
for country in country_keys:
temp_data.append( all_country_data[country][year] )
final_country_data.append(temp_data)
country_keys, final_country_data = worldbank.chart( data )

return (indicators, countries, country_keys, final_country_data)
return (indicators_prepop, countries_prepop, country_keys, final_country_data)


@login_required
Expand Down

0 comments on commit 1610e81

Please sign in to comment.