Skip to content

Commit

Permalink
graph by hour
Browse files Browse the repository at this point in the history
  • Loading branch information
camillol committed Oct 30, 2011
1 parent e677d17 commit e88e4fb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
11 changes: 6 additions & 5 deletions GraphView.pde
Expand Up @@ -33,7 +33,7 @@ class GraphView extends View {
int maxTotal;

List<String> modes = Arrays.asList("Month", "Airport dist.", "Pop. density", "Time of day", "Season");
int activeMode;
String activeMode = "Month";

GraphView(float x_, float y_, float w_, float h_)
{
Expand All @@ -47,19 +47,20 @@ class GraphView extends View {
public String getText(int index) { return modes.get(index); }
public Object get(int index) { return modes.get(index); }
public int count() { return modes.size(); }
public boolean selected(int index) { return index == activeMode; }
public boolean selected(int index) { return get(index).equals(activeMode); }
};
}

void setActiveMode(int index)
void setActiveMode(String mode)
{
activeMode = index;
activeMode = mode;
fillBuckets();
}

void fillBuckets()
{
buckets = data.sightingCountsByMonth();
if (activeMode.equals("Month")) buckets = data.sightingCountsByMonth();
else if (activeMode.equals("Time of day")) buckets = data.sightingCountsByHour();
maxTotal = 0;
for (Bucket bucket : buckets) {
int total = 0;
Expand Down
31 changes: 23 additions & 8 deletions Sighting.pde
Expand Up @@ -175,6 +175,7 @@ interface DataSource {
void loadWeatherStations();
List<Sighting> sightingsForCity(Place p);
List<Bucket> sightingCountsByMonth();
List<Bucket> sightingCountsByHour();
}

class Bucket {
Expand Down Expand Up @@ -363,25 +364,39 @@ class SQLiteDataSource implements DataSource {
}

List<Bucket> sightingCountsByMonth()
{
return sightingCountsByCategoryQuery(
"select strftime('%m',occurred_at) as month, type_id, count(*) as sighting_count"
+ " from sightings join shapes on shape_id = shapes.id group by month, type_id;",
"month");
}

List<Bucket> sightingCountsByHour()
{
return sightingCountsByCategoryQuery(
"select strftime('%H',occurred_at) as hour, type_id, count(*) as sighting_count"
+ " from sightings join shapes on shape_id = shapes.id group by hour, type_id;",
"hour");
}

List<Bucket> sightingCountsByCategoryQuery(String query, String categoryName)
{
List<Bucket> buckets = new ArrayList();

/* let's just do months for a start */
db.query("select cast(strftime('%m',occurred_at) as integer) as month, type_id, count(*) as sighting_count"
+ " from sightings join shapes on shape_id = shapes.id group by month, type_id;");
db.query(query);

int prev_m = -1;
String prev_cat = "NOPE!";
Bucket bucket = null;

while (db.next()) {
int m = db.getInt("month");
String cat = db.getString(categoryName);
SightingType type = sightingTypeMap.get(db.getInt("type_id"));
int count = db.getInt("sighting_count");

if (m != prev_m) {
bucket = new Bucket(str(m));
if (!cat.equals(prev_cat)) {
bucket = new Bucket(cat);
buckets.add(bucket);
prev_m = m;
prev_cat = cat;
}
bucket.counts.put(type, count);
}
Expand Down
6 changes: 3 additions & 3 deletions cs424p3.pde
Expand Up @@ -121,9 +121,9 @@ void setup()
});

graphContainer = new View(10, 100, width-20, height-120);
graphView = new GraphView(0, 0, graphContainer.w - 100, graphContainer.h);
graphView = new GraphView(0, 0, graphContainer.w - 120, graphContainer.h);
graphContainer.subviews.add(graphView);
graphModeList = new ListBox(graphContainer.w - 100, 0, 100, 60, graphView.modesDataSource());
graphModeList = new ListBox(graphContainer.w - 120, 0, 120, 100, graphView.modesDataSource());
graphContainer.subviews.add(graphModeList);

graphButton = new Button(width-80, 0, 80, 20, "Graph");
Expand Down Expand Up @@ -158,7 +158,7 @@ void buttonClicked(Checkbox button)
void listClicked(ListBox lb, int index, Object item)
{
if (lb == graphModeList) {
graphView.setActiveMode(index);
graphView.setActiveMode((String)item);
}
}

Expand Down

0 comments on commit e88e4fb

Please sign in to comment.