Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite chart Bar & Line and other features #167

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
(new features)
- format string for marker label
- add secondary y axis
- add property to specify space between data column (not bars)
- add composite chart of bar and line
- `bar n line` chart can use reference lines from line chart
- colored boxes for legends can be vary sized (through `legend_box_sizes` property)

(fixes)
- legend at bottom

v0.7.1b: fix pre normalize for secondary y axis

v0.7.2:
- add `x_axis_on_top`, if true, x axis markers will be drawn on top instead of bottom
- fix `marker_format` not working for `side bar` chart

# Gruff Graphs

[![Build Status](https://travis-ci.org/topfunky/gruff.svg?branch=master)](https://travis-ci.org/topfunky/gruff)
Expand Down
2 changes: 2 additions & 0 deletions lib/gruff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

%w(
themes
axis
base
area
bar
bar_n_line
bezier
bullet
dot
Expand Down
61 changes: 61 additions & 0 deletions lib/gruff/axis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
##
# Original Author: Hoang Nguyen Minh
#
# This class contains data for secondary y axis
#
class Gruff::Axis
attr_accessor :label

attr_accessor :label_color

attr_accessor :label_font

attr_accessor :line_color

attr_accessor :shadow_color

attr_accessor :font_size

attr_accessor :maximum_value

attr_accessor :minimum_value

attr_accessor :count

attr_accessor :increment

attr_accessor :format

attr_accessor :skip_lines

attr_accessor :stroke_dash

attr_accessor :stroke_opacity

attr_accessor :m_increment

attr_accessor :m_spread

attr_accessor :m_offset

def initialize
@label = nil
@label_color = 'black'
@label_font = nil
@line_color = 'black'
@shadow_color = nil
@font_size = 21
@maximum_value = nil
@minimum_value = nil
@count = nil
@increment = nil
@format = nil
@stroke_dash = nil
@stroke_opacity = 1
@skip_lines = nil
@m_increment = nil
@m_spread = 1
@m_offset = 0
end

end
14 changes: 11 additions & 3 deletions lib/gruff/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ class Gruff::Bar < Gruff::Base
# Spacing factor applied between bars
attr_accessor :bar_spacing

# Spacing factor applied between column of data,
# formula: percent of total with * <this value> / (number of data columns - 1)
attr_accessor :data_column_spacing

def initialize(*args)
super
@spacing_factor = 0.9
@data_column_spacing = 0
end

def draw
Expand Down Expand Up @@ -40,9 +45,11 @@ def draw_bars
# Setup spacing.
#
# Columns sit side-by-side.
col_space = @data_column_spacing * @graph_width
@bar_spacing ||= @spacing_factor # space between the bars
@bar_width = @graph_width / (@column_count * @data.length).to_f
@bar_width = (@graph_width - col_space) / (@column_count * @data.length).to_f
padding = (@bar_width * (1 - @bar_spacing)) / 2
margin = [col_space / (@column_count - 1), 0].max

@d = @d.stroke_opacity 0.0

Expand Down Expand Up @@ -74,7 +81,7 @@ def draw_bars
data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
# Use incremented x and scaled y
# x
left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding
left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding + point_index * margin
right_x = left_x + @bar_width * @bar_spacing
# y
conv = []
Expand All @@ -87,7 +94,8 @@ def draw_bars
# Calculate center based on bar_width and current row
label_center = @graph_left +
(@data.length * @bar_width * point_index) +
(@data.length * @bar_width / 2.0)
(@data.length * @bar_width / 2.0) +
point_index * margin

# Subtract half a bar width to center left if requested
draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
Expand Down
Loading