code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def draw_no_data font = @no_data_font text_renderer = Gruff::Renderer::Text.new(renderer, @no_data_message, font: font) text_renderer.render(@raw_columns, @raw_rows, 0, 0, Magick::CenterGravity) end
Shows an error message because you have no data.
draw_no_data
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def reset_themes @theme_options = {} end
Resets everything to defaults (except data).
reset_themes
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def clip_value_if_greater_than(value, max_value) [value, max_value].min end
@rbs value: Float | Integer @rbs max_value: Float | Integer @rbs return: Float | Integer
clip_value_if_greater_than
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def significant(i) return 1.0 if i == 0 # Keep from going into infinite loop inc = BigDecimal(i.to_s) factor = BigDecimal('1.0') while inc < 10 inc *= 10 factor /= 10 end while inc > 100 inc /= 10 factor *= 10 end res = inc.floor * factor if res.to_i.to_f == res res.to_i elsif res.to_f == res res.to_f else res end end
@rbs i: Integer @rbs return: Integer | Float | BigDecimal TODO: Fix return RBS signature
significant
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def truncate_label_text(text) text = text.to_s return text if text.size <= @label_max_size if @label_truncation_style == :trailing_dots # 4 because '...' takes up 3 chars text = "#{text[0..(@label_max_size - 4)]}..." if @label_max_size > 3 else text = text[0..(@label_max_size - 1)] end text || '' end
@rbs text: String | _ToS @rbs return: String
truncate_label_text
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def label(value, increment) label = begin if increment if increment >= 10 || (increment * 1) == (increment * 1).to_i.to_f sprintf('%0i', value) elsif increment >= 1.0 || (increment * 10) == (increment * 10).to_i.to_f sprintf('%0.1f', value) elsif increment >= 0.1 || (increment * 100) == (increment * 100).to_i.to_f sprintf('%0.2f', value) elsif increment >= 0.01 || (increment * 1000) == (increment * 1000).to_i.to_f sprintf('%0.3f', value) elsif increment >= 0.001 || (increment * 10_000) == (increment * 10_000).to_i.to_f sprintf('%0.4f', value) else value.to_s end elsif (@spread % (marker_count == 0 ? 1 : marker_count) == 0) || !@y_axis_increment.nil? value.to_i.to_s elsif @spread > 10.0 sprintf('%0i', value) elsif @spread >= 3.0 sprintf('%0.2f', value) else value.to_s end end parts = label.split('.') parts[0] = parts[0].commify # steep:ignore parts.join('.') end
Return a formatted string representing a number value that should be printed as a label. @rbs value: Float | Integer | BigDecimal @rbs increment: Float | Integer | BigDecimal @rbs return: String
label
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def x_axis_label(value, increment) if @x_axis_label_format @x_axis_label_format.call(value) else label(value, increment) end end
@rbs value: Float | Integer | BigDecimal @rbs increment: Float | Integer | BigDecimal @rbs return: String
x_axis_label
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def y_axis_label(value, increment) if @y_axis_label_format @y_axis_label_format.call(value) else label(value, increment) end end
@rbs value: Float | Integer | BigDecimal @rbs increment: Float | Integer @rbs return: String
y_axis_label
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def calculate_caps_height(font) calculate_height(font, 'X') end
Returns the height of the capital letter 'X' for the current font and size. Not scaled since it deals with dimensions that the regular scaling will handle. @rbs font: Gruff::Font @rbs return: Float
calculate_caps_height
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def calculate_labels_height(font) @labels.values.map { |label| calculate_height(font, label, rotation: @label_rotation) }.max || marker_caps_height end
@rbs font: Gruff::Font @rbs return: Float
calculate_labels_height
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def calculate_height(font, text, rotation: 0) text = text.to_s return 0.0 if text.empty? metrics = text_metrics(font, text, rotation: rotation) # Calculate manually because it does not return the height after rotation. (metrics.width * Math.sin(deg2rad(rotation))).abs + (metrics.height * Math.cos(deg2rad(rotation))).abs end
Returns the height of a string at this point size. Not scaled since it deals with dimensions that the regular scaling will handle. @rbs font: Gruff::Font @rbs text: String @rbs rotation: Float | Integer @rbs return: Float
calculate_height
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def calculate_width(font, text, rotation: 0) text = text.to_s return 0 if text.empty? metrics = text_metrics(font, text, rotation: rotation) # Calculate manually because it does not return the width after rotation. (metrics.width * Math.cos(deg2rad(rotation))).abs - (metrics.height * Math.sin(deg2rad(rotation))).abs end
Returns the width of a string at this point size. Not scaled since it deals with dimensions that the regular scaling will handle. @rbs font: Gruff::Font @rbs text: String @rbs rotation: Float | Integer @rbs return: Float | Integer
calculate_width
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def text_metrics(font, text, rotation: 0) Gruff::Renderer::Text.new(renderer, text, font: font, rotation: rotation).metrics end
@rbs font: Gruff::Font @rbs text: String @rbs rotation: Float | Integer @rbs return: untyped
text_metrics
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def calculate_label_offset(font, label, margin, rotation) width = calculate_width(font, label, rotation: rotation) height = calculate_height(font, label, rotation: rotation) x_offset = begin case rotation when 0 0 when 0..45 width / 2.0 when -45..0 -(width / 2.0) end end x_offset ||= 0 y_offset = [(height / 2.0), margin].max [x_offset, y_offset] end
@rbs font: Gruff::Font @rbs label: String @rbs margin: Float | Integer @rbs rotation: Float | Integer @rbs return: [Float | Integer, Float | Integer]
calculate_label_offset
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def deg2rad(angle) (angle * Math::PI) / 180.0 end
Used for degree <=> radian conversions @rbs angle: Float | Integer @rbs return: Float
deg2rad
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def rad2deg(angle) (angle / Math::PI) * 180.0 end
@rbs angle: Float | Integer @rbs return: Float
rad2deg
ruby
topfunky/gruff
lib/gruff/base.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/base.rb
MIT
def data(name, x_data_points = [], y_data_points = [], point_sizes = [], color = nil) # make sure it's an array x_data_points = Array(x_data_points) y_data_points = Array(y_data_points) point_sizes = Array(point_sizes) raise ArgumentError, 'Data Points contain nil Value!' if x_data_points.include?(nil) || y_data_points.include?(nil) raise ArgumentError, 'x_data_points is empty!' if x_data_points.empty? raise ArgumentError, 'y_data_points is empty!' if y_data_points.empty? raise ArgumentError, 'point_sizes is empty!' if point_sizes.empty? raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length raise ArgumentError, 'x_data_points.length != point_sizes.length!' if x_data_points.length != point_sizes.length store.add(name, x_data_points, y_data_points, point_sizes, color) end
The first parameter is the name of the dataset. The next two are the x and y axis data points contain in their own array in that respective order. The 4th argument represents sizes of points. The final parameter is the color. Can be called multiple times with different datasets for a multi-valued graph. If the color argument is nil, the next color from the default theme will be used. @note If you want to use a preset theme, you must set it before calling {#data}. @param name [String, Symbol] containing the name of the dataset. @param x_data_points [Array] An Array of x-axis data points. @param y_data_points [Array] An Array of y-axis data points. @param point_sizes [Array] An Array of sizes for points. @param color [String] The hex string for the color of the dataset. Defaults to nil. @raise [ArgumentError] Data points contain nil values. This error will get raised if either the x or y axis data points array contains a +nil+ value. The graph will not make an assumption as how to graph +nil+. @raise [ArgumentError] +x_data_points+ is empty. This error is raised when the array for the x-axis points are empty @raise [ArgumentError] +y_data_points+ is empty. This error is raised when the array for the y-axis points are empty. @raise [ArgumentError] +point_sizes+ is empty. This error is raised when the array for the point_sizes are empty @raise [ArgumentError] +x_data_points.length != y_data_points.length+. Error means that the x and y axis point arrays do not match in length. @raise [ArgumentError] +x_data_points.length != point_sizes.length+. Error means that the x and point_sizes arrays do not match in length. @example g = Gruff::Bubble.new g.title = "Bubble Graph" g.data :A, [-1, 19, -4, -23], [-35, 21, 23, -4], [4.5, 1.0, 2.1, 0.9] @rbs name: String | Symbol @rbs x_data_points: Array[nil | Float | Integer] | nil @rbs y_data_points: Array[nil | Float | Integer] | nil @rbs point_sizes: Array[nil | Float | Integer] | nil @rbs color: String
data
ruby
topfunky/gruff
lib/gruff/bubble.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/bubble.rb
MIT
def initialize(target_width = '400x40') super if target_width.is_a?(String) @columns, @rows = target_width.split('x').map(&:to_f) else @columns = target_width.to_f @rows = target_width.to_f / 5.0 end @columns.freeze @rows.freeze self.theme = Gruff::Themes::GREYSCALE end
@rbs target_width: String | Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/bullet.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/bullet.rb
MIT
def data(value, maximum_value, options = {}) @value = value.to_f self.maximum_value = maximum_value.to_f @options = options @options.map { |k, v| @options[k] = v.to_f if v.respond_to?(:to_f) } end
@rbs value: Float | Integer @rbs maximum_value: Float | Integer @rbs options: Hash[Symbol, Float | Integer]
data
ruby
topfunky/gruff
lib/gruff/bullet.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/bullet.rb
MIT
def data(low:, high:, open:, close:) super('', [low, high, open, close]) end
@rbs low: Float | Integer @rbs high: Float | Integer @rbs open: Float | Integer @rbs close: Float | Integer
data
ruby
topfunky/gruff
lib/gruff/candlestick.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/candlestick.rb
MIT
def initialize(target_width = DEFAULT_TARGET_WIDTH) super @has_left_labels = true @dot_style = 'circle' end
@rbs target_width: (String | Float | Integer) @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/dot.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/dot.rb
MIT
def draw_line_markers return if @hide_line_markers (0..marker_count).each do |index| marker_label = (BigDecimal(index.to_s) * BigDecimal(@increment.to_s)) + BigDecimal(minimum_value.to_s) x = @graph_left + ((marker_label - minimum_value) * @graph_width / @spread) draw_marker_vertical_line(x, tick_mark_mode: true) unless @hide_line_numbers label = y_axis_label(marker_label, @increment) y = @graph_bottom + @label_margin + (labels_caps_height / 2.0) + 5 # 5px offset for tick_mark_mode text_renderer = Gruff::Renderer::Text.new(renderer, label, font: @marker_font) text_renderer.add_to_render_queue(0, 0, x, y, Magick::CenterGravity) end end end
Instead of base class version, draws vertical background lines and label
draw_line_markers
ruby
topfunky/gruff
lib/gruff/dot.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/dot.rb
MIT
def draw_label(y_offset, index) draw_unique_label(index) do draw_label_at(@graph_left - @label_margin, 1.0, 0.0, y_offset, @labels[index], gravity: Magick::EastGravity) end end
# Draw on the Y axis instead of the X @rbs y_offset: Float | Integer @rbs index: Integer
draw_label
ruby
topfunky/gruff
lib/gruff/dot.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/dot.rb
MIT
def initialize(path: nil, size: 20.0, bold: false, color: 'white') @path = path @bold = bold @size = size @color = color end
@rbs path: String | nil @rbs size: Float | Integer @rbs bold: bool @rbs color: String
initialize
ruby
topfunky/gruff
lib/gruff/font.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/font.rb
MIT
def weight @bold ? Magick::BoldWeight : Magick::NormalWeight end
Get font weight. @return [Magick::WeightType] font weight TODO: type annotation of return value
weight
ruby
topfunky/gruff
lib/gruff/font.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/font.rb
MIT
def initialize(target_width = DEFAULT_TARGET_WIDTH) super @data = [] end
@rbs target_width: (String | Float | Integer) @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/histogram.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/histogram.rb
MIT
def data(name, data_points = [], color = nil) @data << [name, Array(data_points), color] end
@rbs name: String | Symbol @rbs data_points: Array[Float | Integer] | nil @rbs color: String
data
ruby
topfunky/gruff
lib/gruff/histogram.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/histogram.rb
MIT
def baseline_value if @reference_lines.key?(:baseline) @reference_lines[:baseline][:value] end end
Get the value if somebody has defined it. @rbs return: Float | Integer | nil
baseline_value
ruby
topfunky/gruff
lib/gruff/line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/line.rb
MIT
def data(name, data_points = [], color = nil) store.add(name, nil, data_points, color) end
Input the data in the graph. Parameters are an array where the first element is the name of the dataset and the value is an array of values to plot. Can be called multiple times with different datasets for a multi-valued graph. If the color argument is nil, the next color from the default theme will be used. @param name [String, Symbol] The name of the dataset. @rbs name: String | Symbol @param data_points [Array] The array of dataset. @rbs data_points: Array[nil | Float | Integer] | nil @param color [String] The color for drawing graph of dataset. @rbs color: String @note If you want to use a preset theme, you must set it before calling {#data}. @example data("Bart S.", [95, 45, 78, 89, 88, 76], '#ffcc00')
data
ruby
topfunky/gruff
lib/gruff/line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/line.rb
MIT
def dataxy(name, x_data_points = [], y_data_points = [], color = nil) # make sure it's an array x_data_points = Array(x_data_points) raise ArgumentError, 'x_data_points is nil!' if x_data_points.empty? if x_data_points.all? { |p| p.is_a?(Array) && p.size == 2 } color = y_data_points if y_data_points.is_a?(String) x_data_points, y_data_points = x_data_points.transpose else y_data_points = Array(y_data_points) end raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length # steep:ignore # call the existing data routine for the x/y data. store.add(name, x_data_points, y_data_points, color) end
This method allows one to plot a dataset with both X and Y data. @overload dataxy(name, x_data_points = [], y_data_points = [], color = nil) @param name [String] the title of the dataset. @param x_data_points [Array] an array containing the x data points for the graph. @param y_data_points [Array] an array containing the y data points for the graph. @param color [String] hex number indicating the line color as an RGB triplet. @overload dataxy(name, xy_data_points = [], color = nil) @param name [String] the title of the dataset. @param xy_data_points [Array] an array containing both x and y data points for the graph. @param color [String] hex number indicating the line color as an RGB triplet. @note - if (x_data_points.length != y_data_points.length) an error is returned. - if the color argument is nil, the next color from the default theme will be used. - if you want to use a preset theme, you must set it before calling {#dataxy}. @example g = Gruff::Line.new g.title = "X/Y Dataset" g.dataxy("Apples", [1,3,4,5,6,10], [1, 2, 3, 4, 4, 3]) g.dataxy("Bapples", [1,3,4,5,7,9], [1, 1, 2, 2, 3, 3]) g.dataxy("Capples", [[1,1],[2,3],[3,4],[4,5],[5,7],[6,9]]) # you can still use the old data method too if you want: g.data("Capples", [1, 1, 2, 2, 3, 3]) # labels will be drawn at the x locations of the keys passed in. In this example the labels are drawn at x positions 2, 4, and 6: g.labels = {0 => '2003', 2 => '2004', 4 => '2005', 6 => '2006'} # The 0 => '2003' label will be ignored since it is outside the chart range. @rbs name: String | Symbol @rbs x_data_points: Array[nil | Float | Integer] | Array[[nil | Float | Integer, nil | Float | Integer]] | nil @rbs y_data_points: Array[nil | Float | Integer] | nil | String @rbs color: String
dataxy
ruby
topfunky/gruff
lib/gruff/line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/line.rb
MIT
def draw_line_markers return if @hide_line_markers # Draw horizontal line markers and annotate with numbers (0..column_count - 1).each do |index| rad_pos = index * Math::PI * 2 / column_count Gruff::Renderer::Line.new(renderer, color: @marker_color) .render(@center_x, @center_y, @center_x + (Math.sin(rad_pos) * @radius), @center_y - (Math.cos(rad_pos) * @radius)) marker_label = @labels[index] ? @labels[index].to_s : '000' draw_label(@center_x, @center_y, rad_pos * 360 / (2 * Math::PI), @radius + @circle_radius, marker_label) end end
the lines connecting in the center, with the first line vertical
draw_line_markers
ruby
topfunky/gruff
lib/gruff/net.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/net.rb
MIT
def draw_label(center_x, center_y, angle, radius, amount) x_offset = center_x # + 15 # The label points need to be tweaked slightly y_offset = center_y # + 0 # This one doesn't though x = x_offset + ((radius + @label_margin) * Math.sin(deg2rad(angle))) y = y_offset - ((radius + @label_margin) * Math.cos(deg2rad(angle))) draw_label_at(1.0, 1.0, x, y, amount, gravity: Magick::CenterGravity) end
@rbs center_x: Float | Integer @rbs center_y: Float | Integer @rbs angle: Float | Integer @rbs radius: Float @rbs amount: String | Integer
draw_label
ruby
topfunky/gruff
lib/gruff/net.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/net.rb
MIT
def update_chart_degrees_with(degrees) @chart_degrees = chart_degrees + degrees end
General Helper Methods @rbs degree: Float | Integer
update_chart_degrees_with
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def chart_degrees @chart_degrees ||= @start_degree end
Spatial Value-Related Methods @rbs return: Float | Integer
chart_degrees
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def process_label_for(slice) if slice.percentage >= @hide_labels_less_than x, y = label_coordinates_for slice label = @label_formatting.call(slice.value, slice.percentage) draw_label_at(1.0, 1.0, x, y, label, gravity: Magick::CenterGravity) end end
Label-Related Methods @rbs slice: Gruff::Pie::PieSlice
process_label_for
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def label_coordinates_for(slice) angle = chart_degrees + (slice.degrees / 2.0) [x_label_coordinate(angle), y_label_coordinate(angle)] end
@rbs slice: Gruff::Pie::PieSlice @rbs return: [Float | Integer, Float | Integer]
label_coordinates_for
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def x_label_coordinate(angle) center_x + ((radius_offset + ellipse_factor) * Math.cos(deg2rad(angle))) #: Float end
@rbs angle: Float | Integer @rbs return: Float
x_label_coordinate
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def y_label_coordinate(angle) center_y + (radius_offset * Math.sin(deg2rad(angle))) end
@rbs angle: Float | Integer @rbs return: Float
y_label_coordinate
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def initialize(label, value, color) @label = label @value = value || 0.0 @color = color end
@rbs label: String | Symbol @rbs value: nil | Float | Integer @rbs color: String @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/pie.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/pie.rb
MIT
def data(name, x_data_points = [], y_data_points = [], color = nil) # make sure it's an array x_data_points = Array(x_data_points) y_data_points = Array(y_data_points) raise ArgumentError, 'Data Points contain nil Value!' if x_data_points.include?(nil) || y_data_points.include?(nil) raise ArgumentError, 'x_data_points is empty!' if x_data_points.empty? raise ArgumentError, 'y_data_points is empty!' if y_data_points.empty? raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length # Call the existing data routine for the x/y axis data store.add(name, x_data_points, y_data_points, color) end
The first parameter is the name of the dataset. The next two are the x and y axis data points contain in their own array in that respective order. The final parameter is the color. Can be called multiple times with different datasets for a multi-valued graph. If the color argument is nil, the next color from the default theme will be used. @note If you want to use a preset theme, you must set it before calling {#data}. @param name [String, Symbol] containing the name of the dataset. @param x_data_points [Array] An Array of x-axis data points. @param y_data_points [Array] An Array of y-axis data points. @param color [String] The hex string for the color of the dataset. Defaults to nil. @raise [ArgumentError] Data points contain nil values. This error will get raised if either the x or y axis data points array contains a +nil+ value. The graph will not make an assumption as how to graph +nil+. @raise [ArgumentError] +x_data_points+ is empty. This error is raised when the array for the x-axis points are empty @raise [ArgumentError] +y_data_points+ is empty. This error is raised when the array for the y-axis points are empty. @raise [ArgumentError] +x_data_points.length != y_data_points.length+. Error means that the x and y axis point arrays do not match in length. @example g = Gruff::Scatter.new g.data(:apples, [1,2,3], [3,2,1]) g.data('oranges', [1,1,1], [2,3,4]) g.data('bitter_melon', [3,5,6], [6,7,8], '#000000') @rbs name: String | Symbol @rbs x_data_points: Array[nil | Float | Integer] | nil @rbs y_data_points: Array[nil | Float | Integer] | nil @rbs color: String
data
ruby
topfunky/gruff
lib/gruff/scatter.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/scatter.rb
MIT
def initialize(target_width = DEFAULT_TARGET_WIDTH) super @has_left_labels = true end
@rbs target_width: (String | Float | Integer) @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/side_bar.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/side_bar.rb
MIT
def draw_line_markers return if @hide_line_markers # Draw horizontal line markers and annotate with numbers number_of_lines = marker_count number_of_lines = 1 if number_of_lines == 0 # TODO: Round maximum marker value to a round number like 100, 0.1, 0.5, etc. increment = significant(@spread / number_of_lines) (0..number_of_lines).each do |index| line_diff = (@graph_right - @graph_left) / number_of_lines x = @graph_right - (line_diff * index) - 1 draw_marker_vertical_line(x) unless @hide_line_numbers diff = index - number_of_lines marker_label = (BigDecimal(diff.abs.to_s) * BigDecimal(increment.to_s)) + BigDecimal(minimum_value.to_s) label = x_axis_label(marker_label, @increment) y = @graph_bottom + @label_margin + (labels_caps_height / 2.0) text_renderer = Gruff::Renderer::Text.new(renderer, label, font: @marker_font) text_renderer.add_to_render_queue(0, 0, x, y, Magick::CenterGravity) end end end
Instead of base class version, draws vertical background lines and label
draw_line_markers
ruby
topfunky/gruff
lib/gruff/side_bar.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/side_bar.rb
MIT
def draw_label(y_offset, index) draw_unique_label(index) do draw_label_at(@graph_left - @label_margin, 1.0, 0.0, y_offset, @labels[index], gravity: Magick::EastGravity) end end
# Draw on the Y axis instead of the X
draw_label
ruby
topfunky/gruff
lib/gruff/side_bar.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/side_bar.rb
MIT
def initialize(target_width = DEFAULT_TARGET_WIDTH) super @has_left_labels = true end
@rbs target_width: (String | Float | Integer) @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/side_stacked_bar.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/side_stacked_bar.rb
MIT
def initialize(max_value, target_width = 800) super(target_width) @max_value = max_value end
@rbs max_value: Float | Integer @rbs target_width: (String | Float | Integer) @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/spider.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/spider.rb
MIT
def draw_graph # Setup spacing. # # Columns sit stacked. bar_width = @graph_width / column_count padding = (bar_width * (1.0 - @bar_spacing)) / 2.0 # Setup the BarConversion Object conversion = Gruff::BarConversion.new( top: @graph_top, bottom: @graph_bottom, minimum_value: minimum_value, maximum_value: maximum_value, spread: @spread ) normalized_stacked_bars.each_with_index do |stacked_bars, stacked_index| total = 0.0 left_x = @graph_left + (bar_width * stacked_index) + padding right_x = left_x + (bar_width * @bar_spacing) top_y = 0.0 stacked_bars.each do |bar| next if bar.point.nil? || bar.point == 0 bottom_y, = conversion.get_top_bottom_scaled(total) bottom_y -= @segment_spacing top_y, = conversion.get_top_bottom_scaled(total + bar.point) rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: bar.color) rect_renderer.render(left_x, bottom_y, right_x, top_y) total += bar.point end label_center = left_x + (bar_width * @bar_spacing / 2.0) draw_label(label_center, stacked_index) if @show_labels_for_bar_values bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, top_y, right_x, @graph_bottom], stacked_bars.sum(&:value)) bar_value_label.prepare_rendering(@label_formatting, proc_text_metrics) do |x, y, text, _text_width, text_height| draw_value_label(bar_width * @bar_spacing, text_height, x, y, text) end end end end
Draws a bar graph, but multiple sets are stacked on top of each other.
draw_graph
ruby
topfunky/gruff
lib/gruff/stacked_bar.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/stacked_bar.rb
MIT
def initialize(top:, bottom:, minimum_value:, maximum_value:, spread:) @graph_top = top @graph_height = bottom - top @spread = spread @minimum_value = minimum_value @maximum_value = maximum_value if minimum_value >= 0 # all bars go from zero to positive @mode = 1 elsif maximum_value <= 0 # all bars go from 0 to negative @mode = 2 else # bars either go from zero to negative or to positive @mode = 3 @zero = -minimum_value / @spread end end
@rbs top: Float | Integer @rbs bottom: Float | Integer @rbs minimum_value: Float | Integer @rbs maximum_value: Float | Integer @rbs spread: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/helper/bar_conversion.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_conversion.rb
MIT
def get_top_bottom_scaled(data_point) data_point = data_point.to_f result = [] case @mode when 1 # minimum value >= 0 ( only positive values ) result[0] = @graph_top + (@graph_height * (1 - data_point)) result[1] = @graph_top + @graph_height when 2 # only negative values result[0] = @graph_top result[1] = @graph_top + (@graph_height * (1 - data_point)) when 3 # positive and negative values val = data_point - (@minimum_value / @spread) result[0] = @graph_top + (@graph_height * (1 - (val - @zero))) result[1] = @graph_top + (@graph_height * (1 - @zero)) end # TODO: Remove RBS type annotation result #: [Float, Float] end
@rbs data_point: Float | Integer @rbs return: [Float, Float]
get_top_bottom_scaled
ruby
topfunky/gruff
lib/gruff/helper/bar_conversion.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_conversion.rb
MIT
def normalized_group_bars # steep:ignore:start @normalized_group_bars ||= begin group_bars = Array.new(column_count) { [] } store.norm_data.each_with_index do |data_row, row_index| data_row.points.each_with_index do |data_point, point_index| group_bars[point_index] << BarData.new(data_point, store.data[row_index].points[point_index], data_row.color) end # Adjust the number of each group with empty bar (data_row.points.size..(column_count - 1)).each do |index| group_bars[index] << BarData.new(0, nil, data_row.color) end end group_bars end # steep:ignore:end end
@rbs return: Array[Array[Gruff::Base::BarMixin::BarData]]
normalized_group_bars
ruby
topfunky/gruff
lib/gruff/helper/bar_mixin.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_mixin.rb
MIT
def initialize(point, value, color) @point = point @value = value @color = color end
@rbs point: Float | Integer @rbs value: nil | Float | Integer @rbs color: String @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/helper/bar_mixin.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_mixin.rb
MIT
def initialize(coordinate, value) @coordinate = coordinate @value = value end
@rbs coordinate: [nil | Float | Integer, nil | Float | Integer, nil | Float | Integer, nil | Float | Integer] @rbs value: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/helper/bar_value_label.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_value_label.rb
MIT
def prepare_rendering(format, proc_text_metrics) left_x, left_y, _right_x, _right_y = @coordinate val, metrics = Gruff::BarValueLabel.metrics(@value, format, proc_text_metrics) y = @value >= 0 ? left_y - metrics.height - 5 : left_y + 5 #: Float yield left_x, y, val, metrics.width, metrics.height end
@rbs format: nil | String | Proc @rbs proc_text_metrics: Proc @rbs &: (Float | Integer, Float | Integer, String, Float, Float) -> void
prepare_rendering
ruby
topfunky/gruff
lib/gruff/helper/bar_value_label.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_value_label.rb
MIT
def prepare_rendering(format, proc_text_metrics) left_x, left_y, right_x, _right_y = @coordinate val, metrics = Gruff::BarValueLabel.metrics(@value, format, proc_text_metrics) x = @value >= 0 ? right_x + 10 : left_x - metrics.width - 10 #: Float yield x, left_y, val, metrics.width, metrics.height end
@rbs format: nil | String | Proc @rbs proc_text_metrics: Proc @rbs &: (Float | Integer, Float | Integer, String, Float, Float) -> void
prepare_rendering
ruby
topfunky/gruff
lib/gruff/helper/bar_value_label.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/bar_value_label.rb
MIT
def calculate_maximum_by_stack # steep:ignore:start # Get sum of each stack max_hash = Hash.new { |h, k| h[k] = 0.0 } store.data.each do |data_set| data_set.points.each_with_index do |data_point, i| max_hash[i] += data_point.to_f end end max_hash.each_key do |key| self.maximum_value = max_hash[key] if max_hash[key] > maximum_value self.minimum_value = max_hash[key] if max_hash[key] < minimum_value end raise "Can't handle negative values in stacked graph" if minimum_value < 0 # steep:ignore:end end
Used by StackedBar and child classes. tsal: moved from Base 03 FEB 2007
calculate_maximum_by_stack
ruby
topfunky/gruff
lib/gruff/helper/stacked_mixin.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/stacked_mixin.rb
MIT
def normalized_stacked_bars # steep:ignore:start @normalized_stacked_bars ||= begin stacked_bars = Array.new(column_count) { [] } store.norm_data.each_with_index do |data_row, row_index| data_row.points.each_with_index do |data_point, point_index| stacked_bars[point_index] << BarData.new(data_point, store.data[row_index].points[point_index], data_row.color) end end stacked_bars end # steep:ignore:end end
@rbs return: Array[Array[Gruff::Base::StackedMixin::BarData]]
normalized_stacked_bars
ruby
topfunky/gruff
lib/gruff/helper/stacked_mixin.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/stacked_mixin.rb
MIT
def initialize(point, value, color) @point = point @value = value @color = color end
@rbs point: Float | Integer @rbs value: Float | Integer @rbs color: String @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/helper/stacked_mixin.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/helper/stacked_mixin.rb
MIT
def expand_canvas_for_vertical_legend # steep:ignore:start return if @hide_mini_legend @legend_labels = store.data.map(&:label) legend_height = scale((store.length * calculate_line_height) + @top_margin + @bottom_margin) @original_rows = @raw_rows @original_columns = @raw_columns case @legend_position when :right @rows = [@rows, legend_height].max @columns += calculate_legend_width + @left_margin else font = @legend_font.dup font.size = scale(font.size) @rows += store.length * calculate_caps_height(font) * 1.7 end @renderer = Gruff::Renderer.new(@columns, @rows, @scale, @theme_options) # steep:ignore:end end
The canvas needs to be bigger so we can put the legend beneath it.
expand_canvas_for_vertical_legend
ruby
topfunky/gruff
lib/gruff/mini/legend.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/mini/legend.rb
MIT
def draw_vertical_legend # steep:ignore:start return if @hide_mini_legend legend_square_width = 40.0 # small square with color of this item @legend_left_margin = 100.0 legend_top_margin = 40.0 case @legend_position when :right current_x_offset = @original_columns + @left_margin current_y_offset = @top_margin + legend_top_margin else current_x_offset = @legend_left_margin current_y_offset = @original_rows + legend_top_margin end @legend_labels.each_with_index do |legend_label, index| # Draw label x_offset = current_x_offset + (legend_square_width * 1.7) label = truncate_legend_label(legend_label, x_offset) text_renderer = Gruff::Renderer::Text.new(renderer, label, font: @legend_font) text_renderer.add_to_render_queue(@raw_columns, 1.0, x_offset, current_y_offset, Magick::WestGravity) # Now draw box with color of this dataset rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: store.data[index].color) rect_renderer.render(current_x_offset, current_y_offset - (legend_square_width / 2.0), current_x_offset + legend_square_width, current_y_offset + (legend_square_width / 2.0)) current_y_offset += calculate_line_height end # steep:ignore:end end
Draw the legend beneath the existing graph.
draw_vertical_legend
ruby
topfunky/gruff
lib/gruff/mini/legend.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/mini/legend.rb
MIT
def truncate_legend_label(label, x_offset) # steep:ignore:start truncated_label = label.to_s font = @legend_font.dup font.size = scale(font.size) max_width = @columns - scale(x_offset) - @right_margin while calculate_width(font, "#{truncated_label}...") > max_width && truncated_label.length > 1 truncated_label = truncated_label[0..truncated_label.length - 2] end truncated_label + (truncated_label.length < label.to_s.length ? '...' : '') # steep:ignore:end end
Shorten long labels so they will fit on the canvas.
truncate_legend_label
ruby
topfunky/gruff
lib/gruff/mini/legend.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/mini/legend.rb
MIT
def annotate_scaled(img, width, height, x, y, text, scale) scaled_width = [(width * scale), 1].max scaled_height = [(height * scale), 1].max # steep:ignore:start annotate(img, scaled_width, scaled_height, x * scale, y * scale, text.gsub('%', '%%')) # steep:ignore:end end
Additional method to scale annotation text since Draw.scale doesn't.
annotate_scaled
ruby
topfunky/gruff
lib/gruff/patch/rmagick.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/patch/rmagick.rb
MIT
def commify(delimiter = THOUSAND_SEPARATOR) gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") # steep:ignore end
Taken from http://codesnippets.joyent.com/posts/show/330
commify
ruby
topfunky/gruff
lib/gruff/patch/string.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/patch/string.rb
MIT
def initialize(renderer, color:, width: 1.0) @renderer = renderer @color = color @width = width end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/bezier.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/bezier.rb
MIT
def initialize(renderer, color:, width: 1.0, opacity: 1.0) @renderer = renderer @color = color @width = width @opacity = opacity end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs opacity: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/circle.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/circle.rb
MIT
def render(origin_x, origin_y, perim_x, perim_y) @renderer.draw.push @renderer.draw.stroke_width(@width) @renderer.draw.stroke(@color) @renderer.draw.fill_opacity(@opacity) @renderer.draw.fill(@color) @renderer.draw.circle(origin_x, origin_y, perim_x, perim_y) @renderer.draw.pop end
@rbs origin_x: Float | Integer @rbs origin_y: Float | Integer @rbs perim_x: Float | Integer @rbs perim_y: Float | Integer
render
ruby
topfunky/gruff
lib/gruff/renderer/circle.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/circle.rb
MIT
def initialize(renderer, color:, width:, dasharray: [10, 20]) @renderer = renderer @color = color @width = width @dasharray = dasharray end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs dasharray: Array[Float | Integer] @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/dash_line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/dash_line.rb
MIT
def render(start_x, start_y, end_x, end_y) @renderer.draw.push @renderer.draw.stroke_color(@color) @renderer.draw.stroke_dasharray(*@dasharray) @renderer.draw.stroke_width(@width) @renderer.draw.fill_opacity(0.0) @renderer.draw.line(start_x, start_y, end_x, end_y) @renderer.draw.pop end
@rbs start_x: Float | Integer @rbs start_y: Float | Integer @rbs end_x: Float | Integer @rbs end_y: Float | Integer
render
ruby
topfunky/gruff
lib/gruff/renderer/dash_line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/dash_line.rb
MIT
def initialize(renderer, style, color:, width: 1.0, opacity: 1.0) @renderer = renderer @style = style.to_sym @color = color @width = width @opacity = opacity end
@rbs renderer: Gruff::Renderer @rbs style: :square | :circle | :diamond | 'square' | 'circle' | 'diamond' @rbs color: String @rbs width: Float | Integer @rbs opacity: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/dot.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/dot.rb
MIT
def render(new_x, new_y, radius) @renderer.draw.push @renderer.draw.stroke_width(@width) @renderer.draw.stroke(@color) @renderer.draw.fill_opacity(@opacity) @renderer.draw.fill(@color) case @style when :square square(new_x, new_y, radius) when :diamond diamond(new_x, new_y, radius) else circle(new_x, new_y, radius) end @renderer.draw.pop end
@rbs new_x: Float | Integer @rbs new_y: Float | Integer @rbs radius: Float | Integer
render
ruby
topfunky/gruff
lib/gruff/renderer/dot.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/dot.rb
MIT
def initialize(renderer, color:, width: 1.0) @renderer = renderer @color = color @width = width end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/ellipse.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/ellipse.rb
MIT
def render(origin_x, origin_y, width, height, arc_start, arc_end) @renderer.draw.push @renderer.draw.stroke_width(@width) @renderer.draw.stroke(@color) @renderer.draw.fill('transparent') @renderer.draw.ellipse(origin_x, origin_y, width, height, arc_start, arc_end) @renderer.draw.pop end
@rbs origin_x: Float | Integer @rbs origin_y: Float | Integer @rbs width: Float | Integer @rbs height: Float | Integer @rbs arc_start: Float | Integer @rbs arc_end: Float | Integer
render
ruby
topfunky/gruff
lib/gruff/renderer/ellipse.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/ellipse.rb
MIT
def initialize(renderer, color:, width: nil) @renderer = renderer @color = color @width = width end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/line.rb
MIT
def render(start_x, start_y, end_x, end_y) render_line(start_x, start_y, end_x, end_y, @color) end
@rbs start_x: Float | Integer @rbs start_y: Float | Integer @rbs end_x: Float | Integer @rbs end_y: Float | Integer
render
ruby
topfunky/gruff
lib/gruff/renderer/line.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/line.rb
MIT
def initialize(renderer, color:, width: 1.0, opacity: 1.0) @renderer = renderer @color = color @width = width @opacity = opacity end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs opacity: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/polygon.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/polygon.rb
MIT
def initialize(renderer, color:, width: 1.0, linejoin: 'bevel') @renderer = renderer @color = color @width = width @linejoin = linejoin end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs linejoin: String @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/polyline.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/polyline.rb
MIT
def initialize(renderer, color: nil, width: 1.0, opacity: 1.0) @renderer = renderer @color = color @width = width @opacity = opacity end
@rbs renderer: Gruff::Renderer @rbs color: String @rbs width: Float | Integer @rbs opacity: Float | Integer @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/rectangle.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/rectangle.rb
MIT
def render(upper_left_x, upper_left_y, lower_right_x, lower_right_y) @renderer.draw.push @renderer.draw.stroke_width(@width) @renderer.draw.stroke(@color) if @color && @width > 1.0 @renderer.draw.fill_opacity(@opacity) @renderer.draw.fill(@color) if @color @renderer.draw.rectangle(upper_left_x, upper_left_y, lower_right_x, lower_right_y) @renderer.draw.pop end
@rbs upper_left_x: Float | Integer @rbs upper_left_y: Float | Integer @rbs lower_right_x: Float | Integer @rbs lower_right_y: Float | Integer
render
ruby
topfunky/gruff
lib/gruff/renderer/rectangle.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/rectangle.rb
MIT
def initialize(columns, rows, scale, theme_options) @draw = Magick::Draw.new @text_renderers = [] @scale = scale @draw.scale(scale, scale) @image = background(columns, rows, scale, theme_options) end
@rbs columns: Integer @rbs rows: Integer @rbs scale: Float | Integer @rbs theme_options: ::Hash[Symbol, untyped] @rbs return: void
initialize
ruby
topfunky/gruff
lib/gruff/renderer/renderer.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/renderer.rb
MIT
def transparent_background(columns, rows) @image = render_transparent_background(columns, rows) end
@rbs columns: Integer @rbs rows: Integer
transparent_background
ruby
topfunky/gruff
lib/gruff/renderer/renderer.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/renderer.rb
MIT
def image_background(scale, image_path) image = Magick::Image.read(image_path) if scale != 1.0 image[0].resize!(scale) # TODO: Resize with new scale (crop if necessary for wide graph) end image[0] end
Use with a theme to use an image (800x600 original) background.
image_background
ruby
topfunky/gruff
lib/gruff/renderer/renderer.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/renderer.rb
MIT
def solid_background(columns, rows, color) Magick::Image.new(columns, rows) do |img| img.background_color = color end end
Make a new image at the current size with a solid +color+.
solid_background
ruby
topfunky/gruff
lib/gruff/renderer/renderer.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/renderer.rb
MIT
def gradated_background(columns, rows, top_color, bottom_color, direct = :top_bottom) gradient_fill = begin case direct when :bottom_top Magick::GradientFill.new(0, 0, 100, 0, bottom_color, top_color) when :left_right Magick::GradientFill.new(0, 0, 0, 100, top_color, bottom_color) when :right_left Magick::GradientFill.new(0, 0, 0, 100, bottom_color, top_color) when :topleft_bottomright Magick::GradientFill.new(0, 100, 100, 0, top_color, bottom_color) when :topright_bottomleft Magick::GradientFill.new(0, 0, 100, 100, bottom_color, top_color) else Magick::GradientFill.new(0, 0, 100, 0, top_color, bottom_color) end end image = Magick::Image.new(columns, rows, gradient_fill) @gradated_background_retry_count = 0 image rescue StandardError => e @gradated_background_retry_count ||= 0 GC.start if @gradated_background_retry_count < 3 @gradated_background_retry_count += 1 gradated_background(columns, rows, top_color, bottom_color, direct) else raise e end end
Use with a theme definition method to draw a gradated background.
gradated_background
ruby
topfunky/gruff
lib/gruff/renderer/renderer.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/renderer.rb
MIT
def render_transparent_background(columns, rows) Magick::Image.new(columns, rows) do |img| img.background_color = 'transparent' end end
Use with a theme to make a transparent background
render_transparent_background
ruby
topfunky/gruff
lib/gruff/renderer/renderer.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/renderer.rb
MIT
def initialize(renderer, text, font:, rotation: nil) @renderer = renderer @text = text.to_s @font = font @rotation = rotation end
@rbs renderer: Gruff::Renderer @rbs text: String @rbs font: Gruff::Font @rbs rotation: Float | Integer
initialize
ruby
topfunky/gruff
lib/gruff/renderer/text.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/text.rb
MIT
def add_to_render_queue(width, height, x, y, gravity = Magick::NorthGravity) @width = width @height = height @x = x @y = y @gravity = gravity @renderer.text_renderers << self end
@rbs width: Float | Integer @rbs height: Float | Integer @rbs x: Float | Integer @rbs y: Float | Integer @rbs gravity: untyped
add_to_render_queue
ruby
topfunky/gruff
lib/gruff/renderer/text.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/text.rb
MIT
def render(width, height, x, y, gravity = Magick::NorthGravity) @renderer.draw.push @renderer.draw.rotation = @rotation if @rotation @renderer.draw.fill = @font.color @renderer.draw.stroke = 'transparent' @renderer.draw.font = @font.file_path @renderer.draw.font_weight = @font.weight @renderer.draw.pointsize = @font.size * @renderer.scale @renderer.draw.gravity = gravity @renderer.draw.annotate_scaled(@renderer.image, width, height, x, y, @text, @renderer.scale) @renderer.draw.rotation = -@rotation if @rotation @renderer.draw.pop end
@rbs width: Float | Integer @rbs height: Float | Integer @rbs x: Float | Integer @rbs y: Float | Integer @rbs gravity: untyped
render
ruby
topfunky/gruff
lib/gruff/renderer/text.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/renderer/text.rb
MIT
def initialize(label, points, color) @label = label.to_s @points = Array(points) @color = color end
@rbs label: String | Symbol @rbs points: Array[nil | Float | Integer] | nil @rbs color: String
initialize
ruby
topfunky/gruff
lib/gruff/store/basic_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/basic_data.rb
MIT
def normalize(minimum:, spread:) norm_points = points.map do |point| point.nil? ? nil : (point.to_f - minimum.to_f) / spread end self.class.new(label, norm_points, color) end
@rbs minimum: Float | Integer @rbs spread: Float | Integer @rbs return: Gruff::Store::BasicData
normalize
ruby
topfunky/gruff
lib/gruff/store/basic_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/basic_data.rb
MIT
def norm_data @norm_data || [] end
@rbs return: Array[Gruff::Store::BasicData | Gruff::Store::XYData | Gruff::Store::XYPointsizeData]
norm_data
ruby
topfunky/gruff
lib/gruff/store/store.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/store.rb
MIT
def initialize(label, x_points, y_points, color) y_points = Array(y_points) x_points = x_points ? Array(x_points) : Array.new(y_points.length) raise ArgumentError, 'x_points.length != y_points.length!' if x_points.length != y_points.length @label = label.to_s @x_points = x_points @y_points = y_points @color = color end
@rbs label: String | Symbol @rbs x_points: Array[nil | Float | Integer] | nil @rbs y_points: Array[nil | Float | Integer] | nil @rbs color: String
initialize
ruby
topfunky/gruff
lib/gruff/store/xy_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/xy_data.rb
MIT
def normalize(minimum_x:, minimum_y:, spread_x:, spread_y:) norm_x_points = x_points.map do |x| x.nil? ? nil : (x.to_f - minimum_x.to_f) / spread_x end norm_y_points = y_points.map do |y| y.nil? ? nil : (y.to_f - minimum_y.to_f) / spread_y end self.class.new(label, norm_x_points, norm_y_points, color) end
@rbs minimum_x: Float | Integer @rbs minimum_y: Float | Integer @rbs spread_x: Float | Integer @rbs spread_y: Float | Integer @rbs return: Gruff::Store::XYData
normalize
ruby
topfunky/gruff
lib/gruff/store/xy_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/xy_data.rb
MIT
def initialize(label, x_points, y_points, point_sizes, color) y_points = Array(y_points) x_points = x_points ? Array(x_points) : Array.new(y_points.length) raise ArgumentError, 'x_points.length != y_points.length!' if x_points.length != y_points.length raise ArgumentError, 'x_points.length != point_sizes.length!' if x_points.length != point_sizes.length @label = label.to_s @x_points = x_points @y_points = y_points @point_sizes = point_sizes @color = color end
@rbs label: String | Symbol @rbs x_points: Array[nil | Float | Integer] | nil @rbs y_points: Array[nil | Float | Integer] | nil @rbs point_sizes: Array[nil | Float | Integer] @rbs color: String
initialize
ruby
topfunky/gruff
lib/gruff/store/xy_pointsizes_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/xy_pointsizes_data.rb
MIT
def coordinate_and_pointsizes x_points.zip(y_points, point_sizes) #: Array[[Float | Integer | nil, Float | Integer | nil, Float | Integer]] end
@rbs return: Array[[Float | Integer | nil, Float | Integer | nil, Float | Integer]]
coordinate_and_pointsizes
ruby
topfunky/gruff
lib/gruff/store/xy_pointsizes_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/xy_pointsizes_data.rb
MIT
def normalize(minimum_x:, minimum_y:, spread_x:, spread_y:) norm_x_points = x_points.map do |x| x.nil? ? nil : (x.to_f - minimum_x.to_f) / spread_x end norm_y_points = y_points.map do |y| y.nil? ? nil : (y.to_f - minimum_y.to_f) / spread_y end self.class.new(label, norm_x_points, norm_y_points, point_sizes, color) end
@rbs minimum_x: Float | Integer @rbs minimum_y: Float | Integer @rbs spread_x: Float | Integer @rbs spread_y: Float | Integer @rbs return: Gruff::Store::XYPointsizeData
normalize
ruby
topfunky/gruff
lib/gruff/store/xy_pointsizes_data.rb
https://github.com/topfunky/gruff/blob/master/lib/gruff/store/xy_pointsizes_data.rb
MIT
def banner "Usage: #{$0} gruff ControllerName" end
Override with your own usage banner.
banner
ruby
topfunky/gruff
rails_generators/gruff/gruff_generator.rb
https://github.com/topfunky/gruff/blob/master/rails_generators/gruff/gruff_generator.rb
MIT
def show g = Gruff::Line.new # Uncomment to use your own theme or font # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas # g.theme = { # :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'], # :marker_color => 'white', # :background_colors => ['black', '#333333'] # } # g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT) g.title = "Gruff-o-Rama" g.data("Apples", [1, 2, 3, 4, 4, 3]) g.data("Oranges", [4, 8, 7, 9, 8, 9]) g.data("Watermelon", [2, 3, 1, 5, 6, 8]) g.data("Peaches", [9, 9, 10, 8, 7, 9]) g.labels = {0 => '2004', 2 => '2005', 4 => '2006'} send_data(g.to_image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png") end
To make caching easier, add a line like this to config/routes.rb: map.graph "graph/:action/:id/image.png", :controller => "graph" Then reference it with the named route: image_tag graph_url(:action => 'show', :id => 42)
show
ruby
topfunky/gruff
rails_generators/gruff/templates/controller.rb
https://github.com/topfunky/gruff/blob/master/rails_generators/gruff/templates/controller.rb
MIT
def test_show get :show assert_response :success assert_equal 'image/png', @response.headers['Content-Type'] end
TODO Replace this with your actual tests
test_show
ruby
topfunky/gruff
rails_generators/gruff/templates/functional_test.rb
https://github.com/topfunky/gruff/blob/master/rails_generators/gruff/templates/functional_test.rb
MIT
def graph_sized(filename, sizes = ['', 400]) class_name = self.class.name.gsub(/^TestGruff/, '') Array(sizes).each do |size| g = instance_eval("Gruff::#{class_name}.new #{size}", __FILE__, __LINE__) g.title = "#{class_name} Graph" yield g write_test_file g, "#{filename}_#{size}.png" end end
Generate graphs at several sizes. Also writes the graph to disk. graph_sized 'bar_basic' do |g| g.data('students', [1, 2, 3, 4]) end
graph_sized
ruby
topfunky/gruff
test/gruff_test_case.rb
https://github.com/topfunky/gruff/blob/master/test/gruff_test_case.rb
MIT
def setup super @datasets = [ (1..20).to_a.map { rand(10) } ] end
TODO: Delete old output files once when starting tests
setup
ruby
topfunky/gruff
test/test_accumulator_bar.rb
https://github.com/topfunky/gruff/blob/master/test/test_accumulator_bar.rb
MIT
def setup super @datasets = [ [:Jimmy, [25, 36, 86, 39]], [:Charles, [80, 54, 67, 54]], [:Julie, [22, 29, 35, 38]] ] end
TODO: Delete old output files once when starting tests
setup
ruby
topfunky/gruff
test/test_bar.rb
https://github.com/topfunky/gruff/blob/master/test/test_bar.rb
MIT
def test_no_labels g = setup_basic_graph(400) g.title = 'No Labels' g.hide_labels = true g.write('test/output/bar_no_labels.png') assert_same_image('test/expected/bar_no_labels.png', 'test/output/bar_no_labels.png') end
Somewhat worthless test. Should an error be thrown? def test_nil_font g = setup_basic_graph 400 g.title = "Nil Font" g.font = nil g.write "test/output/bar_nil_font.png" end
test_no_labels
ruby
topfunky/gruff
test/test_bar.rb
https://github.com/topfunky/gruff/blob/master/test/test_bar.rb
MIT