1

I have added some charts using the google_visular gem. Im am trying to figure out what is causing my column chart to be created with extremely thin columns when I call an order method on my active record statement.

Eg:

code to generate chart:

  def self.get_graph(options={})
     data_table = GoogleVisualr::DataTable.new
      data_table.new_column('datetime', 'Date')
      data_table.new_column('number', "#{options[:model_name]}")
      options[:model_name].where(:generated_at => options[:start_date].beginning_of_day..options[:end_date].end_of_day).each do |p|
          data_table.add_row([p.generated_at.in_time_zone('Pacific Time (US & Canada)'), p.__send__(options[:col_name])])
      end
       opts = { :width => 1100, :height => 280, :chartArea => {:width => 950, :height => 220}, :colors => [options[:color]], :legend => 'none' }
       @chart = GoogleVisualr::Interactive::ColumnChart.new(data_table, opts)
  end

Code to call method:

@users_chart  = Graphing.get_graph({:model_name =>  User, :col_name => "users", :start_date => start_date, :end_date => end_date, :color => "#D48F00"})

JavaScript Generate:

  google.load('visualization','1', {packages: ['corechart'], callback: function() {
    var data_table = new google.visualization.DataTable();data_table.addColumn({"type":"datetime","label":"Date"});data_table.addColumn({"type":"number","label":"User"});data_table.addRow([{v: new Date(2013, 6, 29, 20, 0, 0)}, {v: 182313}]);data_table.addRow([{v: new Date(2013, 6, 29, 23, 0, 0)}, {v: 62751}]);data_table.addRow([{v: new Date(2013, 6, 29, 17, 0, 0)}, {v: 234533}]);data_table.addRow([{v: new Date(2013, 6, 29, 21, 0, 0)}, {v: 132029}]);data_table.addRow([{v: new Date(2013, 6, 29, 22, 0, 0)}, {v: 92957}]);data_table.addRow([{v: new Date(2013, 6, 29, 19, 0, 0)}, {v: 222587}]);data_table.addRow([{v: new Date(2013, 6, 30, 0, 0, 0)}, {v: 9353}]);data_table.addRow([{v: new Date(2013, 6, 29, 18, 0, 0)}, {v: 240253}]);data_table.addRow([{v: new Date(2013, 6, 30, 10, 0, 0)}, {v: 240635}]);data_table.addRow([{v: new Date(2013, 6, 30, 14, 0, 0)}, {v: 225663}]);data_table.addRow([{v: new Date(2013, 6, 30, 1, 0, 0)}, {v: 34734}]);data_table.addRow([{v: new Date(2013, 6, 30, 16, 0, 0)}, {v: 213179}]);data_table.addRow([{v: new Date(2013, 6, 30, 2, 0, 0)}, {v: 31031}]);data_table.addRow([{v: new Date(2013, 6, 30, 9, 0, 0)}, {v: 231830}]);data_table.addRow([{v: new Date(2013, 6, 30, 12, 0, 0)}, {v: 247994}]);data_table.addRow([{v: new Date(2013, 6, 30, 4, 0, 0)}, {v: 63644}]);data_table.addRow([{v: new Date(2013, 6, 30, 6, 0, 0)}, {v: 151377}]);data_table.addRow([{v: new Date(2013, 6, 30, 11, 0, 0)}, {v: 252827}]);data_table.addRow([{v: new Date(2013, 6, 30, 13, 0, 0)}, {v: 244994}]);data_table.addRow([{v: new Date(2013, 6, 30, 5, 0, 0)}, {v: 107412}]);data_table.addRow([{v: new Date(2013, 6, 30, 15, 0, 0)}, {v: 214306}]);data_table.addRow([{v: new Date(2013, 6, 30, 0, 0, 0)}, {v: 41760}]);data_table.addRow([{v: new Date(2013, 6, 30, 8, 0, 0)}, {v: 199565}]);data_table.addRow([{v: new Date(2013, 6, 30, 3, 0, 0)}, {v: 40525}]);data_table.addRow([{v: new Date(2013, 6, 30, 7, 0, 0)}, {v: 185940}]);
    var chart = new google.visualization.ColumnChart(document.getElementById('users_chart'));
    chart.draw(data_table, {width: 1100, height: 280, chartArea: {width: 950, height: 220}, colors: ["#D48F00"], legend: "none"});
  }});

Output: Output without order clause

However if i change my where method to include the order eg:

def self.get_graph(options={})
         data_table = GoogleVisualr::DataTable.new
          data_table.new_column('datetime', 'Date')
          data_table.new_column('number', "#{options[:model_name]}")
          options[:model_name].where(:generated_at => options[:start_date].beginning_of_day..options[:end_date].end_of_day).order('generated_at ASC').each do |p|
              data_table.add_row([p.generated_at.in_time_zone('Pacific Time (US & Canada)'), p.__send__(options[:col_name])])
          end
           opts = { :width => 1100, :height => 280, :chartArea => {:width => 950, :height => 220}, :colors => [options[:color]], :legend => 'none' }
           @chart = GoogleVisualr::Interactive::ColumnChart.new(data_table, opts)
      end

JavaScript generated:

  google.load('visualization','1', {packages: ['corechart'], callback: function() {
    var data_table = new google.visualization.DataTable();data_table.addColumn({"type":"datetime","label":"Date"});data_table.addColumn({"type":"number","label":"User"});data_table.addRow([{v: new Date(2013, 6, 29, 17, 0, 0)}, {v: 234533}]);data_table.addRow([{v: new Date(2013, 6, 29, 18, 0, 0)}, {v: 240253}]);data_table.addRow([{v: new Date(2013, 6, 29, 19, 0, 0)}, {v: 222587}]);data_table.addRow([{v: new Date(2013, 6, 29, 20, 0, 0)}, {v: 182313}]);data_table.addRow([{v: new Date(2013, 6, 29, 21, 0, 0)}, {v: 132029}]);data_table.addRow([{v: new Date(2013, 6, 29, 22, 0, 0)}, {v: 92957}]);data_table.addRow([{v: new Date(2013, 6, 29, 23, 0, 0)}, {v: 62751}]);data_table.addRow([{v: new Date(2013, 6, 30, 0, 0, 0)}, {v: 9353}]);data_table.addRow([{v: new Date(2013, 6, 30, 0, 0, 0)}, {v: 41760}]);data_table.addRow([{v: new Date(2013, 6, 30, 1, 0, 0)}, {v: 34734}]);data_table.addRow([{v: new Date(2013, 6, 30, 2, 0, 0)}, {v: 31031}]);data_table.addRow([{v: new Date(2013, 6, 30, 3, 0, 0)}, {v: 40525}]);data_table.addRow([{v: new Date(2013, 6, 30, 4, 0, 0)}, {v: 63644}]);data_table.addRow([{v: new Date(2013, 6, 30, 5, 0, 0)}, {v: 107412}]);data_table.addRow([{v: new Date(2013, 6, 30, 6, 0, 0)}, {v: 151377}]);data_table.addRow([{v: new Date(2013, 6, 30, 7, 0, 0)}, {v: 185940}]);data_table.addRow([{v: new Date(2013, 6, 30, 8, 0, 0)}, {v: 199565}]);data_table.addRow([{v: new Date(2013, 6, 30, 9, 0, 0)}, {v: 231830}]);data_table.addRow([{v: new Date(2013, 6, 30, 10, 0, 0)}, {v: 240635}]);data_table.addRow([{v: new Date(2013, 6, 30, 11, 0, 0)}, {v: 252827}]);data_table.addRow([{v: new Date(2013, 6, 30, 12, 0, 0)}, {v: 247994}]);data_table.addRow([{v: new Date(2013, 6, 30, 13, 0, 0)}, {v: 244994}]);data_table.addRow([{v: new Date(2013, 6, 30, 14, 0, 0)}, {v: 225663}]);data_table.addRow([{v: new Date(2013, 6, 30, 15, 0, 0)}, {v: 214306}]);data_table.addRow([{v: new Date(2013, 6, 30, 16, 0, 0)}, {v: 213179}]);
    var chart = new google.visualization.ColumnChart(document.getElementById('users_chart'));
    chart.draw(data_table, {width: 1100, height: 280, chartArea: {width: 950, height: 220}, bar_groupWidth: "80%", colors: ["#D48F00"], legend: "none"});
  }});

The generated chart looks like: Image with order clause

What can I do to fix this? ( I need the order clause on there so that the charts timescale is linear)

4

1 回答 1

0

您最终会得到单个时间点的两行数据:

data_table.addRow([{v: new Date(2013, 6, 30, 0, 0, 0)}, {v: 9353}]);
data_table.addRow([{v: new Date(2013, 6, 30, 0, 0, 0)}, {v: 41760}]);

这导致条形图如此狭窄。您可以看到两行都到位(http://jsfiddle.net/asgallant/zHwnb/)与注释掉一行(http://jsfiddle.net/asgallant/zHwnb/1/)的结果。

修复重复线问题,您将修复条形宽度问题。

顺便说一句,bar group width 选项指定不正确,它应该是bar:{groupWidth:"80%"},而不是bar_groupWidth: "80%",但是一旦您修复了其余部分,您就不需要使用它。

于 2013-08-01T00:55:18.427 回答