Module: Daru::View::Adapter::HighchartsAdapter

Extended by:
HighchartsAdapter
Included in:
HighchartsAdapter
Defined in:
lib/daru/view/adapters/highcharts.rb

Instance Method Summary collapse

Instance Method Details

#add_series(plot, opts = {}) ⇒ Object

Generally, in opts Hash, :name, :type, :data , :center=> [X, Y], :size=> Integer, :showInLegend=> Bool, etc may present.



107
108
109
110
# File 'lib/daru/view/adapters/highcharts.rb', line 107

def add_series(plot, opts={})
  plot.series(opts)
  plot
end

#export(plot, export_type = 'png', file_name = 'chart') ⇒ Object

Exporting in web frameworks is completely offline. In IRuby notebook,

offline-export supports only the exporting to png, jpeg and svg format.
Export to PDF is not working (not even through the exporting button in
highchart). So, online exporting is done in IRuby notebook. There is a
problem in online exporting that if we run-all all the cells of IRuby
notebook then only the last chart will be exported. Individually,
running the cells works fine.

See Also:

  • Daru::View::Adapter::HighchartsAdapter#Daru#Daru::View#Daru::View::Plot#Daru::View::Plot.export


83
84
85
86
87
# File 'lib/daru/view/adapters/highcharts.rb', line 83

def export(plot, export_type='png', file_name='chart')
  plot.export_iruby(export_type, file_name) if defined? IRuby
rescue NameError
  plot.export(export_type, file_name)
end

#export_html_file(plot, path = './plot.html') ⇒ Object



68
69
70
71
72
# File 'lib/daru/view/adapters/highcharts.rb', line 68

def export_html_file(plot, path='./plot.html')
  path = File.expand_path(path, Dir.pwd)
  str = generate_html(plot)
  File.write(path, str)
end

#generate_body(plot) ⇒ Object



64
65
66
# File 'lib/daru/view/adapters/highcharts.rb', line 64

def generate_body(plot)
  plot.to_html
end

#generate_html(plot) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/daru/view/adapters/highcharts.rb', line 93

def generate_html(plot)
  path = File.expand_path('../templates/highcharts/static_html.erb', __dir__)
  template = File.read(path)
  initial_script = init_script
  chart_div = generate_body(plot)
  ERB.new(template).result(binding)
end

#init(data = [], options = {}, user_options = {}) ⇒ Object

Read : www.highcharts.com/docs/chart-concepts to understand the highcharts option concept.

TODO : this docs must be improved

options in chart Hash, same as LazyHighCahrts or HighCharts) type is configured

Parameters:

  • options (Hash) (defaults to: {})

    the options to create a chart with.

  • data (Array/Daru::DataFrame/Daru::Vector) (defaults to: [])
  • opts (Hash)

    a customizable set of options



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/daru/view/adapters/highcharts.rb', line 31

def init(data=[], options={}, user_options={})
  # Alternate way is using `add_series` method.
  #
  # There are many options present in Highcharts so it is better to use
  # directly all the options. That means Daru::View::Plot will
  # behave same as LazyHighCharts when `data` is an Array and
  # `options` are passed.
  #
  @chart = LazyHighCharts::HighChart.new do |f|
    # all the options present in `options` and about the
    # series (means name, type, data) used in f.series(..)
    f.options = options.empty? ? LazyHighCharts::HighChart.new.defaults_options : options
    # For multiple series when data is in a series format as in
    # HighCharts official examples
    # TODO: Add support for multiple series when data as
    #   Daru::DataFrame/Daru::Vector
    if data.is_a?(Array) && data[0].is_a?(Hash)
      f.series_data = data
    else
      data_new = guess_data(data)
      series_type = options[:type] unless options[:type].nil?
      series_name = options[:name] unless options[:name].nil?
      f.series(type: series_type, name: series_name, data: data_new)
    end
  end
  @chart.user_options = user_options
  @chart
end

#init_irubyObject



101
102
103
# File 'lib/daru/view/adapters/highcharts.rb', line 101

def init_iruby
  LazyHighCharts.init_iruby
end

#init_scriptObject



60
61
62
# File 'lib/daru/view/adapters/highcharts.rb', line 60

def init_script
  LazyHighCharts.init_script
end

#show_in_iruby(plot) ⇒ Object



89
90
91
# File 'lib/daru/view/adapters/highcharts.rb', line 89

def show_in_iruby(plot)
  plot.show_in_iruby
end