@@ -75,18 +75,61 @@ def get_html_spreadsheet(data, dom)
75
75
end
76
76
77
77
def to_html ( id = nil , options = { } )
78
+ if data . is_a? ( Array ) &&
79
+ ( data [ 0 ] . is_a? ( Daru ::View ::Plot ) || data [ 0 ] . is_a? ( Daru ::View ::Table ) )
80
+ id , chart_script , path = extract_multiple_charts_id_script_path
81
+ else
82
+ id , chart_script , path = extract_chart_id_script_path ( id )
83
+ end
84
+ template = File . read ( path )
85
+ ERB . new ( template ) . result ( binding )
86
+ end
87
+
88
+ def show_in_iruby ( dom = SecureRandom . uuid )
89
+ IRuby . html ( to_html ( dom ) )
90
+ end
91
+
92
+ # @return [Array] Array of IDs of the multiple charts, Array of scripts
93
+ # of the multiple charts, path of the template used to render the
94
+ # multiple charts
95
+ def extract_multiple_charts_id_script_path
96
+ path = File . expand_path (
97
+ '../../templates/googlecharts/multiple_charts_div.erb' , __dir__
98
+ )
99
+ id = [ ]
100
+ chart_script = [ ]
101
+ data . each_with_index do |plot , index |
102
+ id [ index ] ||= SecureRandom . uuid
103
+ chart_script [ index ] = set_chart_script ( plot , id [ index ] )
104
+ end
105
+ [ id , chart_script , path ]
106
+ end
107
+
108
+ # @param id [String] The ID of the DIV element that the Google Chart
109
+ # should be rendered in
110
+ # @return [Array] ID of the div element, script of the chart, path of
111
+ # the template which will be used to render the chart
112
+ def extract_chart_id_script_path ( id = nil )
78
113
path = File . expand_path (
79
114
'../../templates/googlecharts/chart_div.erb' , __dir__
80
115
)
81
- template = File . read ( path )
82
116
id ||= SecureRandom . uuid
83
117
@html_id = id
84
118
chart_script = show_script ( id , script_tag : false )
85
- ERB . new ( template ) . result ( binding )
119
+ [ id , chart_script , path ]
86
120
end
87
121
88
- def show_in_iruby ( dom = SecureRandom . uuid )
89
- IRuby . html ( to_html ( dom ) )
122
+ # @param plot [Daru::View::Plot, Daru::View::Table] one of the plot or
123
+ # table objects that will be shown in a row
124
+ # @param id [String] The ID of the DIV element that the Google Chart
125
+ # should be rendered in
126
+ # @return [String] Javascript of the table or chart
127
+ def set_chart_script ( plot , id )
128
+ if plot . is_a? ( Daru ::View ::Plot )
129
+ plot . chart . show_script ( id , script_tag : false )
130
+ else
131
+ plot . table . show_script ( id , script_tag : false )
132
+ end
90
133
end
91
134
end
92
135
0 commit comments