Class: LazyHighCharts::HighChart
- Inherits:
-
Object
- Object
- LazyHighCharts::HighChart
- Defined in:
- lib/daru/view/adapters/highcharts/display.rb
Instance Attribute Summary collapse
-
#div_id ⇒ String
Holds a value only when to_html or to_html_iruby method is invoked.
-
#user_options ⇒ Object
Returns the value of attribute user_options.
Instance Method Summary collapse
-
#append_chart_type(export_type = 'png') ⇒ String
Code stating the type to which chart has to be exported.
- #chart_hash_must_be_present ⇒ Object
- #export(export_type = 'png', file_name = 'chart') ⇒ Object
-
#export_iruby(export_type = 'png', file_name = 'chart') ⇒ void
Exports chart to different formats in IRuby notebook.
-
#extract_chart_class ⇒ String
The class of the chart.
-
#extract_dependencies ⇒ Array
Extracts the required dependencies for the chart.
-
#extract_export_code(placeholder = random_canvas_id, export_type = 'png', file_name = 'chart') ⇒ String
Returns the script to export the chart in different formats for web frameworks.
-
#extract_export_code_iruby(placeholder = random_canvas_id, export_type = 'png', file_name = 'chart') ⇒ String
Returns the script to export the chart in different formats in IRuby notebook.
-
#get_map_data_dependencies(dep_js) ⇒ void
Appends the map data in dep_js.
-
#high_chart_css(placeholder) ⇒ String
Css code of the chart.
-
#load_dependencies(type) ⇒ void, String
Loads the dependent mapdata and dependent modules of the chart.
- #show_in_iruby(placeholder = random_canvas_id) ⇒ Object
-
#to_html(placeholder = random_canvas_id) ⇒ Object
To display the html code of the chart, use ‘to_html`.
-
#to_html_iruby(placeholder = random_canvas_id) ⇒ Object
This method is not needed if ‘to_html` generates the same code.
Instance Attribute Details
#div_id ⇒ String
Holds a value only when to_html or to_html_iruby method is invoked
55 56 57 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 55 def div_id @div_id end |
#user_options ⇒ Object
Returns the value of attribute user_options.
56 57 58 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 56 def @user_options end |
Instance Method Details
#append_chart_type(export_type = 'png') ⇒ String
Returns code stating the type to which chart has to be exported.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 249 def append_chart_type(export_type='png') case export_type when 'pdf' "type: 'application/pdf'," when 'png' "type: 'image/png'," when 'jpg', 'jpeg' "type: 'image/jpeg'," when 'svg' "type: 'image/svg+xml'," else raise TypeError, 'Invalid format' end end |
#chart_hash_must_be_present ⇒ Object
278 279 280 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 278 def chart_hash_must_be_present @options[:chart] ||= {} end |
#export(export_type = 'png', file_name = 'chart') ⇒ Object
176 177 178 179 180 181 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 176 def export(export_type='png', file_name='chart') js = '' js << to_html js << extract_export_code(@div_id, export_type, file_name) js end |
#export_iruby(export_type = 'png', file_name = 'chart') ⇒ void
This method returns an undefined value.
Exports chart to different formats in IRuby notebook
189 190 191 192 193 194 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 189 def export_iruby(export_type='png', file_name='chart') js = '' js << to_html_iruby js << extract_export_code_iruby(@div_id, export_type, file_name) IRuby.html js end |
#extract_chart_class ⇒ String
Returns the class of the chart.
265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 265 def extract_chart_class # Provided by user and can take two values ('stock' or 'map'). chart_class = [:chart_class].to_s.capitalize if && [:chart_class] chart_class = 'StockChart' if chart_class == 'Stock' chart_class = 'Chart' if chart_class.nil? unless %w[Chart StockChart Map].include?(chart_class) raise 'chart_class must be selected as either chart, stock or map' end chart_class end |
#extract_dependencies ⇒ Array
Extracts the required dependencies for the chart. User does not need
to provide any mapdata requirement explicity in the `options`.
MapData will be extracted using `options[:chart][:map]` already
provided by the user. In `modules` user needs to provide the required
modules (like tilemap in highcharts) in the form of Array. Once the
dependency is loaded on a page, there is no need to provide it again in
the `modules` option.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 153 def extract_dependencies dep_js = [] # Mapdata dependencies get_map_data_dependencies(dep_js) # Dependencies provided in modules option (of highcharts mainly # like tilemap) by the user dep_js |= .delete(:modules).map! { |js| "#{js}.js" } if && [:modules] dep_js end |
#extract_export_code(placeholder = random_canvas_id, export_type = 'png', file_name = 'chart') ⇒ String
Returns the script to export the chart in different formats for
web frameworks
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 204 def extract_export_code( placeholder=random_canvas_id, export_type='png', file_name='chart' ) js = '' js << "\n <script>" js << "\n (function() {" js << "\n \tvar onload = window.onload;" js << "\n \twindow.onload = function(){" js << "\n \t\tif (typeof onload == 'function') onload();" js << "\n \t\tvar chartDom = document.getElementById('#{placeholder}');" js << "\n \t\tvar chart = Highcharts.charts[Highcharts.attr(chartDom," js << " 'data-highcharts-chart')]" js << "\n \t\tchart.exportChartLocal({" js << "\n \t\t\t" + append_chart_type(export_type) js << "\n \t\t\tfilename: '#{file_name}'" js << "\n \t\t});\n \t};\n })();" js << "\n </script>" js end |
#extract_export_code_iruby(placeholder = random_canvas_id, export_type = 'png', file_name = 'chart') ⇒ String
Returns the script to export the chart in different formats in
IRuby notebook
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 229 def extract_export_code_iruby( placeholder=random_canvas_id, export_type='png', file_name='chart' ) js = '' js << "\n <script>" js << "\n (function() {" js << "\n \tvar chartDom = document.getElementById('#{placeholder}');" js << "\n \tvar chart = Highcharts.charts[Highcharts.attr(chartDom," js << " 'data-highcharts-chart')]" js << "\n \tchart.exportChart({" js << "\n \t\t" + append_chart_type(export_type) js << "\n \t\tfilename: '#{file_name}'" js << "\n \t});" js << "\n })();" js << "\n </script>" js end |
#get_map_data_dependencies(dep_js) ⇒ void
This method returns an undefined value.
Returns Appends the map data in dep_js.
166 167 168 169 170 171 172 173 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 166 def get_map_data_dependencies(dep_js) if && [:chart_class] && [:chart_class].capitalize == 'Map' && [:chart] && [:chart][:map] dep_js.push([:chart][:map].to_s) dep_js.map! { |js| "mapdata/#{js}.js" } end end |
#high_chart_css(placeholder) ⇒ String
Returns css code of the chart.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 108 def high_chart_css(placeholder) # contains the css provided by the user as a String array css_data = if && [:css] [:css] else '' end css_script = '' if css_data != '' css_script << "\n<style>" # Applying the css to chart div css_data.each do |css| css_script << "\n #" + placeholder + ' ' css_script << css end css_script << "\n</style>" end css_script end |
#load_dependencies(type) ⇒ void, String
Loads the dependent mapdata and dependent modules of the chart
134 135 136 137 138 139 140 141 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 134 def load_dependencies(type) dep_js = extract_dependencies if type == 'iruby' LazyHighCharts.init_iruby(dep_js) unless dep_js.nil? elsif type == 'web_frameworks' dep_js.nil? ? '' : LazyHighCharts.init_javascript(dep_js) end end |
#show_in_iruby(placeholder = random_canvas_id) ⇒ Object
87 88 89 90 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 87 def show_in_iruby(placeholder=random_canvas_id) # TODO : placeholder pass, in plot#div IRuby.html to_html_iruby(placeholder) end |
#to_html(placeholder = random_canvas_id) ⇒ Object
To display the html code of the chart, use ‘to_html`. To see the same in IRuby notebook use `show_in_iruby`. User can also use : `IRuby.html chart.to_html` (or) `IRuby.html chart.to_html.to_s` (or) `IRuby.display chart.to_html, mime: ’text/html’‘ to get the same chart in IRuby notebook.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 67 def to_html(placeholder=random_canvas_id) chart_hash_must_be_present script = load_dependencies('web_frameworks') @div_id = placeholder script << high_chart_css(placeholder) # Helps to denote either of the three classes. chart_class = extract_chart_class # When user wants to plot a HighMap if chart_class == 'Map' script << high_map(placeholder, self) # When user wants to plot a HighStock elsif chart_class == 'StockChart' script << high_stock(placeholder, self) # When user wants to plot a HighChart elsif chart_class == 'Chart' script << high_chart(placeholder, self) end script end |
#to_html_iruby(placeholder = random_canvas_id) ⇒ Object
This method is not needed if ‘to_html` generates the same code. Here `to_html` generates the code with `onload`, so there is need of `high_chart_iruby` which doesn’t use ‘onload` in chart script.
95 96 97 98 99 100 101 102 103 |
# File 'lib/daru/view/adapters/highcharts/display.rb', line 95 def to_html_iruby(placeholder=random_canvas_id) # TODO : placeholder pass, in plot#div @div_id = placeholder load_dependencies('iruby') chart_hash_must_be_present script = high_chart_css(placeholder) script << high_chart_iruby(extract_chart_class, placeholder, self) script end |