1
+ # frozen_string_literal: true
1
2
2
3
require 'picrate'
3
4
require 'csv'
4
-
5
+ # Grafica Sketch
5
6
class Oktoberfest < Processing ::App
6
-
7
7
load_library :grafica
8
8
java_import 'grafica.GPlot'
9
9
java_import 'grafica.GPointsArray'
10
10
11
- MONTH_NAMES = %w[ January February March April May June July August September October November December ]
12
- DAYS_PER_MONTH = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
13
- DAYS_PER_MONTH_LEAP_YEAR = [ 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
14
-
11
+ MONTH_NAMES = %w[
12
+ January February March April May June July August September October November
13
+ December
14
+ ] . freeze
15
+ DAYS_PER_MONTH = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] . freeze
16
+ DAYS_PER_MONTH_LEAP_YEAR = [ 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
17
+ . freeze
15
18
attr_reader :plot , :points_oktoberfest , :points_elections
16
19
17
20
def settings
@@ -29,23 +32,27 @@ def setup
29
32
@points_oktoberfest = GPointsArray . new
30
33
@points_elections = GPointsArray . new
31
34
32
- CSV . foreach ( data_path ( 'OktoberfestVSGermanElections.csv' ) , headers : true ) do |row |
35
+ CSV . foreach (
36
+ data_path ( 'OktoberfestVSGermanElections.csv' ) , headers : true
37
+ ) do |row |
33
38
year = row [ 'year' ] . to_i
34
39
month = row [ 'month' ] . to_i
35
40
day = row [ 'day' ] . to_i
36
41
date = get_exact_date ( year , month , day )
37
42
oktoberfest_count = row [ 'oktoberfest' ] . to_i
38
43
elections_count = row [ 'bundestagswahl' ] . to_i
39
- points_oktoberfest . add ( date , oktoberfest_count , MONTH_NAMES [ month ] )
40
- points_elections . add ( date , elections_count , MONTH_NAMES [ month ] )
44
+ month_name = MONTH_NAMES [ month ]
45
+ points_oktoberfest . add ( date , oktoberfest_count , month_name )
46
+ points_elections . add ( date , elections_count , month_name )
41
47
end
42
48
# Create the plot
43
49
@plot = GPlot . new ( self )
44
50
plot . set_dim ( 700 , 300 )
45
51
plot . set_title_text ( 'Oktoberfest vs. Bundestagwahl Google search history' )
46
- plot . getXAxis . set_axis_label_text ( 'Year' )
52
+ x_axis = plot . getXAxis
53
+ x_axis . set_axis_label_text ( 'Year' )
47
54
plot . getYAxis . set_axis_label_text ( 'Google normalized searches' )
48
- plot . getXAxis . setNTicks ( 10 )
55
+ x_axis . setNTicks ( 10 )
49
56
plot . set_points ( points_oktoberfest )
50
57
plot . set_line_color ( color ( 100 , 100 , 100 ) )
51
58
plot . add_layer ( 'German elections day' , points_elections )
@@ -74,7 +81,8 @@ def draw
74
81
75
82
def get_exact_date ( year , month , day )
76
83
leap = ( year % 4 ) . zero? && !( year % 100 ) . zero? || ( year % 400 ) . zero?
77
- return year + ( month + ( day - 1 ) / DAYS_PER_MONTH [ month ] ) / 12.0 unless leap
84
+ return year + ( month + ( day - 1 ) / DAYS_PER_MONTH [ month ] ) / 12.0 unless leap
85
+
78
86
year + ( month + ( day - 1 ) / DAYS_PER_MONTH_LEAP_YEAR [ month ] ) / 12.0
79
87
end
80
88
end
0 commit comments