Skip to content

Commit e3e1b04

Browse files
committed
added specs for line plots
1 parent 1e4a432 commit e3e1b04

8 files changed

Lines changed: 227 additions & 48 deletions

File tree

lib/rubyplot.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ def self.backend
1818
:magick
1919
end
2020
end
21-

lib/rubyplot/axes.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ class Axes
99
:bounding_box, :x_axis_padding, :y_axis_padding, :origin,
1010
:title_shift, :title_margin
1111

12+
# A hash of names for the individual columns, where the key is the array
13+
# index for the column this label represents.
14+
#
15+
# Not all columns need to be named.
16+
#
17+
# Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008
18+
attr_accessor :x_ticks
19+
1220
# Main title for this Axes.
1321
attr_accessor :title
1422

@@ -40,6 +48,7 @@ def initialize figure, position
4048
@bounding_box = true
4149
@x_axis_padding = :default
4250
@y_axis_padding = :default
51+
@x_ticks = {}
4352
@plots = []
4453
end
4554

lib/rubyplot/magick_wrapper/artist.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def initialize_variables
7272
}
7373

7474
@raw_rows = 800.0 * (@rows / @columns)
75-
@labels = {}
7675
@sort = false
7776
@title_font = nil
7877

@@ -244,11 +243,15 @@ def setup_graph_measurements
244243
# For now, the labels feature only focuses on the dot graph so it makes sense to only have
245244
# this as an attribute for this kind of graph and not for others.
246245
if @geometry.has_left_labels
247-
longest_left_label_width = calculate_width(@marker_font_size,
248-
labels.values.inject('') { |value, memo| value.to_s.length > memo.to_s.length ? value : memo }) * 1.25
246+
longest_left_label_width = calculate_width(
247+
@marker_font_size,
248+
@axes.x_ticks.values.inject('') { |value, memo|
249+
value.to_s.length > memo.to_s.length ? value : memo
250+
}) * 1.25
249251
else
250-
longest_left_label_width = calculate_width(@marker_font_size,
251-
label(@geometry.maximum_value.to_f, @geometry.increment))
252+
longest_left_label_width = calculate_width(
253+
@marker_font_size,
254+
label(@geometry.maximum_value.to_f, @geometry.increment))
252255
end
253256

254257
# Shift graph if left line numbers are hidden
@@ -261,9 +264,11 @@ def setup_graph_measurements
261264
(@geometry.y_axis_label .nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2)
262265

263266
# Make space for half the width of the rightmost column label.
264-
last_label = @labels.keys.max.to_i
265-
extra_room_for_long_label = last_label >= (@geometry.column_count - 1) && @geometry.center_labels_over_point ?
266-
calculate_width(@marker_font_size, @labels[last_label]) / 2.0 : 0
267+
last_label = @axes.x_ticks.keys.max.to_i
268+
extra_room_for_long_label = last_label >= (@geometry.column_count - 1) &&
269+
@geometry.center_labels_over_point ?
270+
calculate_width(@marker_font_size,
271+
@axes.x_ticks[last_label]) / 2.0 : 0
267272

268273
# Margins
269274
@graph_right_margin = @geometry.right_margin + extra_room_for_long_label
@@ -443,9 +448,7 @@ def draw_line_markers!
443448
(0..@geometry.marker_count).each do |index|
444449
y = @graph_top + @graph_height - index.to_f * @geometry.increment_scaled
445450
y_next = @graph_top + @graph_height - (index.to_f + 1) * @geometry.increment_scaled
446-
447451
@d = @d.fill(@marker_color)
448-
449452
@d = @d.line(@graph_left, y, @graph_right, y) if !@geometry.hide_line_markers || (index == 0)
450453
# If the user specified a marker shadow color, draw a shadow just below it
451454
unless @marker_shadow_color.nil?
@@ -497,14 +500,14 @@ def render_gradiated_background(top_color, bottom_color, direct = :top_bottom)
497500

498501
# Draws column labels below graph, centered over x_offset
499502
def draw_label(x_offset, index)
500-
if !@labels[index].nil? && @geometry.labels_seen[index].nil?
503+
if !@axes.x_ticks[index].nil? && @geometry.labels_seen[index].nil?
501504
y_offset = @graph_bottom + LABEL_MARGIN
502505

503506
# TESTME
504507
# TODO: See if index.odd? is the best stragegy
505508
y_offset += @label_stagger_height if index.odd?
506509

507-
label_text = labels[index].to_s
510+
label_text = @axes.x_ticks[index].to_s
508511

509512
# TESTME
510513
# FIXME: Consider chart types other than bar

lib/rubyplot/magick_wrapper/artist/attributes.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@ class Artist
44
module Attributes
55
# Blank space below the legend
66
attr_accessor :legend_margin
7-
8-
# A hash of names for the individual columns, where the key is the array
9-
# index for the column this label represents.
10-
#
11-
# Not all columns need to be named.
12-
#
13-
# Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008
14-
attr_accessor :labels
15-
7+
168
# Used internally for horizontal graph types.
179
attr_accessor :has_left_labels
1810

@@ -23,9 +15,6 @@ module Attributes
2315

2416
attr_accessor :font_color
2517

26-
# Prevent drawing of the legend
27-
attr_accessor :hide_legend
28-
2918
# Prevent drawing of line numbers
3019
attr_accessor :hide_line_numbers
3120

lib/rubyplot/magick_wrapper/artist/geometry.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ class Geometry
5555

5656
attr_accessor :font_color
5757

58-
# Prevent drawing of the legend
59-
attr_accessor :hide_legend
60-
6158
# Prevent drawing of line numbers
6259
attr_accessor :hide_line_numbers
6360

@@ -107,7 +104,7 @@ class Geometry
107104
attr_accessor :legend_at_bottom, :legend_box_size, :legend_margin
108105

109106
# Helper variables to hide geometry objects
110-
attr_accessor :hide_line_markers, :hide_legend, :hide_title, :hide_line_numbers
107+
attr_accessor :hide_line_markers, :hide_title, :hide_line_numbers
111108
attr_accessor :show_labels_for_bar_values
112109
attr_accessor :center_labels_over_point, :label_truncation_style
113110

@@ -137,7 +134,7 @@ def initialize(*)
137134
@label_stagger_height = 0
138135
@label_truncation_style = :absolute
139136
@label_max_size = 0
140-
@hide_line_markers = @hide_legend = @hide_title = false
137+
@hide_line_markers = @hide_title = false
141138
@hide_line_numbers = @legend_at_bottom = @show_labels_for_bar_values = false
142139
@center_labels_over_point = true
143140

lib/rubyplot/magick_wrapper/plot/bar.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def draw
2424
# Labels will be centered over the left of the bar if
2525
# there are more labels than columns. This is basically the same
2626
# as where it would be for a line graph.
27-
@geometry.center_labels_over_point = (@labels.keys.length > @geometry.column_count)
27+
@geometry.center_labels_over_point = (@axes.x_ticks.keys.length > @geometry.column_count)
2828
super
2929
return unless @geometry.has_data
3030
draw_bars
@@ -79,7 +79,9 @@ def draw_bars
7979
@d = @d.fill @plot_colors[row_index]
8080
# Use incremented x and scaled y
8181
# x
82-
left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding
82+
left_x = @graph_left + (
83+
@bar_width *
84+
(row_index + point_index + ((@data.length - 1) * point_index))) + padding
8385
right_x = left_x + @bar_width * @bar_spacing
8486
# y
8587
conv = []
@@ -98,7 +100,8 @@ def draw_bars
98100
(@geometry.center_labels_over_point ? @bar_width / 2.0 : 0.0),
99101
point_index)
100102
if @geometry.show_labels_for_bar_values
101-
val = (@geometry.label_formatting || '%.2f') % @geometry.norm_data[row_index][3][point_index]
103+
val = (@geometry.label_formatting || '%.2f') %
104+
@geometry.norm_data[row_index][3][point_index]
102105
draw_value_label(left_x + (right_x - left_x) / 2, conv[0] - 30, val.commify, true)
103106
end
104107
end

0 commit comments

Comments
 (0)