Class: Gruff::Net

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

Overview

Experimental!!! See also the Spider graph.

Constant Summary

Constants inherited from Base

Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DATA_VALUES_X_INDEX, Base::DEBUG, Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN, Base::THOUSAND_SEPARATOR

Instance Attribute Summary collapse

Attributes inherited from Base

#additional_line_values, #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_formatting, #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, #show_labels_for_bar_values, #sort, #sorted_drawing, #stacked, #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_ivars, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #write

Methods included from Deprecated

#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height

Constructor Details

#initialize(*args) ⇒ Net

Returns a new instance of Net.



13
14
15
16
17
18
19
# File 'lib/gruff/net.rb', line 13

def initialize(*args)
  super

  @hide_dots = false
  @hide_line_numbers = true
  @sorted_drawing = true
end

Instance Attribute Details

#dot_radiusObject

Returns the value of attribute dot_radius.



11
12
13
# File 'lib/gruff/net.rb', line 11

def dot_radius
  @dot_radius
end

#hide_dotsObject

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



7
8
9
# File 'lib/gruff/net.rb', line 7

def hide_dots
  @hide_dots
end

#line_widthObject

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



10
11
12
# File 'lib/gruff/net.rb', line 10

def line_width
  @line_width
end

Instance Method Details

#drawObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
76
# File 'lib/gruff/net.rb', line 21

def draw
  super

  return unless @has_data

  @radius = @graph_height / 2.0
  @center_x = @graph_left + (@graph_width / 2.0)
  @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit

  @x_increment = @graph_width / (@column_count - 1).to_f
  circle_radius = dot_radius ||
      clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0)

  @d = @d.stroke_opacity 1.0
  @d = @d.stroke_width line_width ||
                           clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0)

  if defined?(@norm_baseline)
    level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
    @d = @d.push
    @d.stroke_color @baseline_color
    @d.fill_opacity 0.0
    @d.stroke_dasharray(10, 20)
    @d.stroke_width 5
    @d.line(@graph_left, level, @graph_left + @graph_width, level)
    @d = @d.pop
  end

  @norm_data.each do |data_row|
    @d = @d.stroke data_row[DATA_COLOR_INDEX]
    @d = @d.fill data_row[DATA_COLOR_INDEX]

    data_row[DATA_VALUES_INDEX].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[DATA_VALUES_INDEX].length ? index + 1 : 0

      next_rad_pos = next_index * Math::PI * 2 / @column_count
      next_point_distance = data_row[DATA_VALUES_INDEX][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

      @d = @d.line(start_x, start_y, end_x, end_y)

      @d = @d.circle(start_x, start_y, start_x - circle_radius, start_y) unless @hide_dots
    end

  end

  @d.draw(@base_image)
end

#draw_line_markersObject

the lines connecting in the center, with the first line vertical



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gruff/net.rb', line 80

def draw_line_markers
  return if @hide_line_markers


  # have to do this here (AGAIN)... see draw() in this class
  # because this funtion is called before the @radius, @center_x and @center_y are set
  @radius = @graph_height / 2.0
  @center_x = @graph_left + (@graph_width / 2.0)
  @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit


  # Draw horizontal line markers and annotate with numbers
  @d = @d.stroke(@marker_color)
  @d = @d.stroke_width 1


  (0..@column_count-1).each do |index|
    rad_pos = index * Math::PI * 2 / @column_count

    @d = @d.line(@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, marker_label)
  end
end