Skip to content

Commit

Permalink
local time plays barchart
Browse files Browse the repository at this point in the history
  • Loading branch information
camillol committed Nov 22, 2011
1 parent b8131b7 commit 98a60a4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
13 changes: 13 additions & 0 deletions ArtistDetailView.pde
Expand Up @@ -124,3 +124,16 @@ class ArtistDetailView extends View {
drawImage();
}
}

class SongDetailView extends View {
TextField searchField;

SongDetailView(float x_, float y_, float w_, float h_){
super(x_,y_,w_,h_);

searchField = new TextField(20, 20, 300, 20);
subviews.add(searchField);
}
}


51 changes: 51 additions & 0 deletions Data.pde
Expand Up @@ -358,6 +358,32 @@ class ArtistCountryBreakdown {
}
}

class LocalTimePlays implements BarChartDataSource {
Map<Country, int[]> byCountry;
int byHour[];
int maxValue;

LocalTimePlays() {
byCountry = new HashMap<Country,int[]>();
byHour = new int[24];
maxValue = 0;
}

void put(Country c, int hr, int count) {
if (!byCountry.containsKey(c)) byCountry.put(c, new int[24]);
// byHour[hr] -= byCountry.get(c)[hr];
byCountry.get(c)[hr] = count;
byHour[hr] += count;
maxValue = max(maxValue, byHour[hr]);
}

String getLabel(int index) {return str(index);}
float getValue(int index) { return byHour[index]; }
int count() {return 24;}
float getMaxValue() {return maxValue;}
color getColor(int index) { return #aaaaaa; }
}

class WebDataSource {
String baseURL;
ExecutorService loadExec;
Expand Down Expand Up @@ -625,6 +651,31 @@ class WebDataSource {
});
}

Future<LocalTimePlays> getLocalTimePlays()
{
return loadExec.submit(new Callable<LocalTimePlays>() {
public LocalTimePlays call() {
LocalTimePlays ltp = null;
String request = baseURL + "plays_by_hour";
println(request);
try {
JSONArray result = new JSONArray(join(loadStrings(request), ""));
ltp = new LocalTimePlays();
for (int i = 0; i < result.length(); i++){
JSONObject aj = result.getJSONObject(i);
Country c = getCountryByCode(aj.getString("country_code"));
ltp.put(c, aj.getInt("local_hour"), aj.getInt("plays"));
}
}
catch (JSONException e) {
println("getLocalTimePlays");
println (e);
}
return ltp;
}
});
}

public Artist findArtist(int id){
String request = baseURL + "artists/" + id + ".json";
println(request);
Expand Down
17 changes: 14 additions & 3 deletions cs424p4.pde
Expand Up @@ -20,6 +20,7 @@ int normalFontSize;

TabView mainTabView;
ArtistDetailView artistDetailView;
SongDetailView songDetailView;

void setup()
{
Expand All @@ -41,7 +42,7 @@ void setup()

rootView = new View(0, 0, width, height);

mainTabView = new TabView(10, 10, width-20, height-20, Arrays.asList("Top Artists", "Artist Details", "Map Test"));
mainTabView = new TabView(10, 10, width-20, height-20, Arrays.asList("Top Artists", "Artist Details", "Songs", "Time"));
rootView.subviews.add(mainTabView);

View topArtistsPane = mainTabView.tabs.get(0).pane;
Expand All @@ -64,7 +65,11 @@ void setup()
// artistDetailView.setArtist(findArtist(4112));
artistDetailPane.subviews.add(artistDetailView);

View mapTestPane = mainTabView.tabs.get(2).pane;
View songPane = mainTabView.tabs.get(2).pane;
songDetailView = new SongDetailView(0,0,songPane.w,songPane.h);
songPane.subviews.add(songDetailView);

/* View mapTestPane = mainTabView.tabs.get(2).pane;
MapView mapView = new MapView(100,100,400,300) {
public void drawCountry(PShape cShape, String cc) {
Expand All @@ -89,7 +94,13 @@ void setup()
public int count() { return barLabels.length; }
public float getMaxValue() { return barMax; }
public color getColor(int index) { return barColors[index % barColors.length]; }
}, true, true));
}, true, true));*/

View timePane = mainTabView.tabs.get(3).pane;
timePane.subviews.add(new Label(40, 20, 300, 20, "Songs played by local time"));
BarChart timeBarChart = new BarChart(40, 40, 700, 200, new AsyncBarChartDataSource(data.getLocalTimePlays()), true, true);
timePane.subviews.add(timeBarChart);


// I want to add true multitouch support, but let's have this as a stopgap for now
addMouseWheelListener(new java.awt.event.MouseWheelListener() {
Expand Down

0 comments on commit 98a60a4

Please sign in to comment.