Class: ActiveCharts::BarChart

Inherits:
RectangularChart show all
Defined in:
lib/active_charts/bar_chart.rb

Constant Summary collapse

DEFAULT_BAR_WIDTH =
40

Constants inherited from RectangularChart

RectangularChart::TOP_LEFT_OFFSET

Constants inherited from Chart

Chart::CSS_CLASSES, Chart::MARGIN

Instance Attribute Summary collapse

Attributes inherited from RectangularChart

#grid_height, #grid_width, #svg_height, #svg_width

Attributes inherited from Chart

#collection, #columns_count, #data_formatters, #extra_css_classes, #label_height, #rows_count, #series_labels, #title

Instance Method Summary collapse

Methods inherited from RectangularChart

#grid_rect_tag, #initialize

Methods inherited from Chart

#initialize, #legend_list_tag, #to_html

Constructor Details

This class inherits a constructor from ActiveCharts::RectangularChart

Instance Attribute Details

#bar_widthObject (readonly)

Returns the value of attribute bar_width.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def bar_width
  @bar_width
end

#bars_countObject (readonly)

Returns the value of attribute bars_count.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def bars_count
  @bars_count
end

#max_bar_heightObject (readonly)

Returns the value of attribute max_bar_height.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def max_bar_height
  @max_bar_height
end

#max_valuesObject (readonly)

Returns the value of attribute max_values.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def max_values
  @max_values
end

#section_widthObject (readonly)

Returns the value of attribute section_width.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def section_width
  @section_width
end

#x_labelsObject (readonly)

Returns the value of attribute x_labels.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def x_labels
  @x_labels
end

#x_offsetObject (readonly)

Returns the value of attribute x_offset.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def x_offset
  @x_offset
end

#y_multipliersObject (readonly)

Returns the value of attribute y_multipliers.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def y_multipliers
  @y_multipliers
end

#y_offsetObject (readonly)

Returns the value of attribute y_offset.



5
6
7
# File 'lib/active_charts/bar_chart.rb', line 5

def y_offset
  @y_offset
end

Instance Method Details

#barsObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/active_charts/bar_chart.rb', line 15

def bars
  whitelist = %w[width height x y class]
  
  bars_specs.flatten.map do |bar| 
    label = formatted_val(bar[:val], bar[:formatter])
    
    [%(<rect #{tag_options(bar.merge(width: bar_width), whitelist)} />),
     tag.text(label, label_options(bar))]
  end
end

#bars_specsObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_charts/bar_chart.rb', line 26

def bars_specs
  collection.map.with_index do |row, row_index|
    row.map.with_index do |cell, col_index|
      height = bar_height(cell, col_index)
      x = bar_x(col_index, row_index)
      y = grid_height - height

      { height: height, x: x, y: y, class: bar_classes(col_index), val: cell, 
        formatter: data_formatters[col_index] }
    end
  end
end

#bottom_label_text_tagsObject



39
40
41
42
43
# File 'lib/active_charts/bar_chart.rb', line 39

def bottom_label_text_tags
  x_labels.map.with_index do |label, index| 
    tag.text(label, x: section_width * (index + 0.5), y: grid_height + label_height * 1.5)
  end.join
end

#chart_svg_tagObject



8
9
10
11
12
13
# File 'lib/active_charts/bar_chart.rb', line 8

def chart_svg_tag
  inner_html = [grid_rect_tag, bars, bottom_label_text_tags].flatten.join('
      ')
  
  tag.svg(inner_html.html_safe, svg_options)
end