Module: JqPlotRails::ActionView

Includes:
RailsJavaScriptHelpers
Defined in:
lib/jqplot_rails/jqplot_action_view.rb

Instance Method Summary collapse

Instance Method Details

#jqplot(dom_id, data, opts = {}) ⇒ Object

dom_id

DOM ID to contain plot (also used for referencing instance)

data

Array of plot data

opts

Options hash provided to jqplot

Creates a new plot instance NOTE: If :raw => true is passed in the opts, the result will be the raw javascript not wrapped within tags



13
14
15
16
17
# File 'lib/jqplot_rails/jqplot_action_view.rb', line 13

def jqplot(dom_id, data, opts={})
  output = jqplot_setup
  output << "window._jqplot_rails['#{_j_key(dom_id)}'] = jQuery.jqplot('#{_j_key(dom_id)}', #{format_type_to_js(data)}, #{format_type_to_js(opts)});".html_safe
  _j_wrap(opts[:raw], output)
end

#jqplot_data_onclick(dom_id, opts = {}) ⇒ Object

dom_id

DOM ID used for the plot

opts

Ooptions hash for building plot binding

- :function -> RawJS instance with full function defined
- :link_to -> {:url, :remote}

Bind to click event on data and make request



24
25
26
27
28
29
30
31
32
33
# File 'lib/jqplot_rails/jqplot_action_view.rb', line 24

def jqplot_data_onclick(dom_id, opts={})
  output = jqplot_setup
  raise 'Only :function or :link_to may be defined, not both.' if opts[:function].present? && opts[:link_to].present?
  raise 'Must provide :function or :link_to for event.' if opts[:function].blank? && opts[:link_to].blank?
  function = opts[:link_to].present? ? _j_build_click_url_event(dom_id, opts[:link_to]) : opts[:function]
  output << jqplot_exists(dom_id) do
    "jQuery(#{format_type_to_js(format_id(dom_id))}).bind('jqplotDataClick', #{format_type_to_js(function)});".html_safe
  end
  _j_wrap(opts[:raw], output)
end

#jqplot_reset_plot(dom_id, *args) ⇒ Object

dom_id

DOM ID used for the plot

args

Extra arguments

Resets the plot to its original state NOTE: If :raw is passed in the args, the result will be the raw javascript not wrapped within tags



39
40
41
42
43
44
45
# File 'lib/jqplot_rails/jqplot_action_view.rb', line 39

def jqplot_reset_plot(dom_id, *args)
  output = jqplot_setup
  output << jqplot_exists(dom_id) do
    "#{jqplot_instance(dom_id)}.replot({clear:true,resetAxes:true});"
  end
  _j_wrap(args.include?(:raw), output)
end

#jqplot_reset_plot_button(text, dom_id, *args) ⇒ Object

text

Button text

dom_id

DOM ID used for the plot

args

Extra arguments passed to #button_to_function

Returns a button for resetting the given plot



51
52
53
# File 'lib/jqplot_rails/jqplot_action_view.rb', line 51

def jqplot_reset_plot_button(text, dom_id, *args)
  button_to_function(text, jqplot_reset_plot(dom_id, :raw), *args)
end
text

Link text

dom_id

DOM ID used for the plot

args

Extra arguments passed to #link_to_function

Returns a link for resetting the given plot



59
60
61
# File 'lib/jqplot_rails/jqplot_action_view.rb', line 59

def jqplot_reset_plot_link(text, dom_id, *args)
  link_to_function(text, jqplot_reset_plot(dom_id, :raw), *args)
end

#jqplot_resizable(dom_id, *args) ⇒ Object

dom_id

DOM ID used for the plot

args

Extra arguments

Makes the provided plot resizable NOTE: If :raw is passed in the args, the result will be the raw javascript not wrapped within tags



67
68
69
70
71
72
73
74
75
76
# File 'lib/jqplot_rails/jqplot_action_view.rb', line 67

def jqplot_resizable(dom_id, *args)
  resize_args = args.last if args.last.is_a?(Hash)
  resize_args ||= {:delay => 20}
  output = jqplot_setup
  output << jqplot_exists(dom_id) do
    "jQuery(#{format_type_to_js(format_id(dom_id))}).resizable(#{format_type_to_js(resize_args)});" +
    "jQuery(#{format_type_to_js(format_id(dom_id))}).bind('resize', function(event,ui){ #{jqplot_instance(dom_id)}.replot(); });"
  end
  _j_wrap(args.include?(:raw), output)
end