Class: Chartify::WebChart::GoogleChart::PieChart

Inherits:
PieChart show all
Includes:
GoogleChartModule
Defined in:
lib/chartify/web_chart/google_chart/pie_chart.rb

Instance Attribute Summary

Attributes inherited from ChartBase

#columns, #data, #label_column, #title

Instance Method Summary collapse

Methods included from GoogleChartModule

#array_data_table, #chart_options, #include_js, #timestamp

Methods inherited from PieChart

#initialize, #to_blob

Methods inherited from ChartBase

#add_row, evaluate_js, #initialize, #render_chart

Constructor Details

This class inherits a constructor from Chartify::PieChart

Instance Method Details

#render(html_dom_id) ⇒ Object

Js data var data = google.visualization.arrayToDataTable([

    ['Task', 'Hours per Day'],
    ['Work',     11],
    ['Eat',      2],
    ['Commute',  2],
    ['Watch TV', 2],
    ['Sleep',    7]
]);


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chartify/web_chart/google_chart/pie_chart.rb', line 19

def render(html_dom_id)
  datasets = data.collect do |column|
    if column.kind_of?(Array)
      title, val = column[0], column[1]
    else
      title, val = column, column.to_s.humanize
    end
    [title, val]
  end
  datasets.insert 0, column_names

  js = <<-JS
    google.load("visualization", "1", {packages:["corechart"], callback:drawChart#{timestamp}});
    function drawChart#{timestamp}() {
      var data = google.visualization.arrayToDataTable(#{datasets.to_json});
      var chart = new google.visualization.PieChart(document.getElementById('#{html_dom_id}'));
      chart.draw(data, #{chart_options.to_json});
    }
  JS
  js.html_safe
end