Skip to content

Commit

Permalink
Added loans-by-published year graph.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Chitipothu committed Dec 6, 2013
1 parent 05768a1 commit f7d2f7a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
10 changes: 9 additions & 1 deletion openlibrary/core/loanstats.py
Expand Up @@ -4,6 +4,7 @@
"""
import re
import time
import datetime
import urllib
import logging
import simplejson
Expand Down Expand Up @@ -78,7 +79,7 @@ def _run_solr_facet_query(self, facet_fields, facet_limit=None):
params["facet.limit"] = facet_limit

response = self.solr_select(params)
return dict((name, web.group(counts, 2)) for name, counts in response['facet_counts']['facet_fields'].items())
return dict((name, list(web.group(counts, 2))) for name, counts in response['facet_counts']['facet_fields'].items())

def _get_all_facet_counts(self):
if not self._facet_counts:
Expand Down Expand Up @@ -128,6 +129,13 @@ def get_facet_counts(self, name, limit=20):
facets = list(self.solr_select_facet(name))[:limit]
return [self.make_facet(name, key, count) for key, count in facets]

def get_loans_by_published_year(self):
d = self._run_solr_facet_query("publish_year")['publish_year']
# strip bad years
current_year = datetime.date.today().year
min_year = 1800
return [[int(y), count] for y, count in d if min_year < int(y) <= current_year]

def make_facet(self, name, key, count):
if name == "library_s":
title = self._get_library_title(key)
Expand Down
45 changes: 44 additions & 1 deletion openlibrary/templates/stats/lending.html
Expand Up @@ -42,8 +42,15 @@ <h2>Loans Per Type</h2>
<noscript>$_("You need to have JavaScript turned on to see the nifty chart!")</noscript>
</div>
</div>

<div class="clearfix"></div>

<h2>Loans by Published Year</h2>
<div class="chart">
<div id="loans-by-published-year" class="graph">
<noscript>$_("You need to have JavaScript turned on to see the nifty chart!")</noscript>
</div>
</div>

<br/>
<br/>

Expand Down Expand Up @@ -143,6 +150,42 @@ <h2>Loans Per Type</h2>
show: false
}
});

\$.plot("#loans-by-published-year", [{data: $:json_encode(stats.get_loans_by_published_year())}], {
series: {
bars: {
show: true,
fill: 0.6,
color: "#ffa337",
align: "center"
},
points: {
show: true
},
color: "#ffa337"
},
grid: {
/*
hoverable: true,
clickable: true,
autoHighlight: true,
*/
tickColor: "#d9d9d9",
borderWidth: 1,
borderColor: "#d9d9d9",
backgroundColor: "#fff"
},
xaxis: { tickDecimals: 0 },
yaxis: { tickDecimals: 0 }
/*
selection: { mode: "xy", color: "#00636a" },
crosshair: {
mode: "xy",
color: "rgba(000, 099, 106, 0.4)",
lineWidth: 1
}
*/
});
});
</script>

Expand Down

0 comments on commit f7d2f7a

Please sign in to comment.