Class: Gruff::Net

Inherits:
Base
  • Object
show all
Defined in:
lib/gruff/net.rb

Overview

See also the Spider graph.

Here’s how to make a Gruff::Net.

g = Gruff::Net.new
g.title = "Net Graph"
g.labels = {
  0 => '5/6',
  1 => '5/15',
  2 => '5/24',
  3 => '5/30',
  4 => '6/4',
  5 => '6/12',
  6 => '6/21',
  7 => '6/28'
}
g.line_width = 3
g.dot_radius = 4
g.data :Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]
g.data :Julie, [22, 29, 35, 38, 36, 40, 46, 57]
g.write("net.png")

Constant Summary

Constants inherited from Base

Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN

Instance Attribute Summary collapse

Attributes inherited from Base

#bold_title, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_max_size, #label_stagger_height, #label_truncation_style, #labels, #left_margin, #legend_at_bottom, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #sorted_drawing, #title, #title_font, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_increment, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #initialize, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #write

Constructor Details

This class inherits a constructor from Gruff::Base

Instance Attribute Details

#dot_radiusObject

Returns the value of attribute dot_radius.



33
34
35
# File 'lib/gruff/net.rb', line 33

def dot_radius
  @dot_radius
end

#hide_dotsObject

Hide parts of the graph to fit more datapoints, or for a different appearance.



29
30
31
# File 'lib/gruff/net.rb', line 29

def hide_dots
  @hide_dots
end

#line_widthObject

Dimensions of lines and dots; calculated based on dataset size if left unspecified.



32
33
34
# File 'lib/gruff/net.rb', line 32

def line_width
  @line_width
end

Instance Method Details

#drawObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gruff/net.rb', line 44

def draw
  super

  return unless data_given?

  stroke_width = line_width  || clip_value_if_greater_than(@columns / (store.norm_data.first.points.size * 4), 5.0)
  circle_radius = dot_radius || clip_value_if_greater_than(@columns / (store.norm_data.first.points.size * 2.5), 5.0)

  store.norm_data.each do |data_row|
    data_row.points.each_with_index do |data_point, index|
      next if data_point.nil?

      rad_pos = index * Math::PI * 2 / column_count
      point_distance = data_point * @radius
      start_x = @center_x + Math.sin(rad_pos) * point_distance
      start_y = @center_y - Math.cos(rad_pos) * point_distance

      next_index = index + 1 < data_row.points.length ? index + 1 : 0

      next_rad_pos = next_index * Math::PI * 2 / column_count
      next_point_distance = data_row.points[next_index] * @radius
      end_x = @center_x + Math.sin(next_rad_pos) * next_point_distance
      end_y = @center_y - Math.cos(next_rad_pos) * next_point_distance

      Gruff::Renderer::Line.new(color: data_row.color, width: stroke_width).render(start_x, start_y, end_x, end_y)

      Gruff::Renderer::Circle.new(color: data_row.color, width: stroke_width).render(start_x, start_y, start_x - circle_radius, start_y) unless @hide_dots
    end
  end

  Gruff::Renderer.finish
end