|
| 1 | +module Rubyplot |
| 2 | + module MagickWrapper |
| 3 | + module Plot |
| 4 | + class Dot < Artist |
| 5 | + def draw |
| 6 | + @geometry.has_left_labels = true |
| 7 | + super |
| 8 | + return unless @geometry.has_data |
| 9 | + |
| 10 | + # Setup spacing. |
| 11 | + spacing_factor = 1.0 |
| 12 | + @items_width = @graph_height / @geometry.column_count.to_f |
| 13 | + @item_width = @items_width * spacing_factor / @geometry.norm_data.size |
| 14 | + @d = @d.stroke_opacity 0.0 |
| 15 | + padding = (@items_width * (1 - spacing_factor)) / 2 |
| 16 | + |
| 17 | + @geometry.norm_data.each_with_index do |data_row, row_index| |
| 18 | + data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| |
| 19 | + x_pos = @graph_left + (data_point * @graph_width) |
| 20 | + y_pos = @graph_top + (@items_width * point_index) + |
| 21 | + padding + (@items_width.to_f / 2.0).round |
| 22 | + |
| 23 | + if row_index == 0 |
| 24 | + @d = @d.stroke(@marker_color) |
| 25 | + @d = @d.fill(@marker_color) |
| 26 | + @d = @d.stroke_width 1.0 |
| 27 | + @d = @d.stroke_opacity 0.1 |
| 28 | + @d = @d.fill_opacity 0.1 |
| 29 | + @d = @d.line(@graph_left, y_pos, @graph_left + @graph_width, y_pos) |
| 30 | + @d = @d.fill_opacity 1 |
| 31 | + end |
| 32 | + |
| 33 | + @d = @d.fill @plot_colors[row_index] |
| 34 | + @d = @d.circle(x_pos, y_pos, x_pos + (@item_width.to_f / 3.0).round, y_pos) |
| 35 | + draw_label(y_pos, point_index) |
| 36 | + end |
| 37 | + end |
| 38 | + @d.draw(@base_image) |
| 39 | + end |
| 40 | + |
| 41 | + # Instead of base class version, draws vertical background lines and label |
| 42 | + def draw_line_markers |
| 43 | + @d = @d.stroke_antialias false |
| 44 | + |
| 45 | + # Draw horizontal line markers and annotate with numbers |
| 46 | + @d = @d.stroke(@marker_color) |
| 47 | + @d = @d.stroke_width 1 |
| 48 | + if @y_axis_increment |
| 49 | + increment = @y_axis_increment |
| 50 | + number_of_lines = (@spread / @y_axis_increment).to_i |
| 51 | + else |
| 52 | + # Try to use a number of horizontal lines that will come out even. |
| 53 | + # |
| 54 | + # TODO Do the same for larger numbers...100, 75, 50, 25 |
| 55 | + if @geometry.marker_count.nil? |
| 56 | + (3..7).each do |lines| |
| 57 | + if @spread % lines == 0.0 |
| 58 | + @geometry.marker_count = lines |
| 59 | + break |
| 60 | + end |
| 61 | + end |
| 62 | + @geometry.marker_count ||= 5 |
| 63 | + end |
| 64 | + # TODO: Round maximum marker value to a round number like 100, 0.1, 0.5, etc. |
| 65 | + @geometry.increment = @spread > 0 && @geometry.marker_count > 0 ? significant(@spread / @geometry.marker_count) : 1 |
| 66 | + |
| 67 | + number_of_lines = @geometry.marker_count |
| 68 | + increment = @geometry.increment |
| 69 | + end |
| 70 | + |
| 71 | + (0..number_of_lines).each do |index| |
| 72 | + marker_label = @geometry.minimum_value + index * increment |
| 73 | + x = @graph_left + (marker_label - @geometry.minimum_value) * @graph_width / @spread |
| 74 | + @d = @d.line(x, @graph_bottom, x, @graph_bottom + 0.5 * LABEL_MARGIN) |
| 75 | + |
| 76 | + unless @geometry.hide_line_numbers |
| 77 | + @d.fill = @font_color |
| 78 | + @d.font = @font if @font |
| 79 | + @d.stroke = 'transparent' |
| 80 | + @d.pointsize = scale_fontsize(@marker_font_size) |
| 81 | + @d.gravity = CenterGravity |
| 82 | + # TODO: Center text over line |
| 83 | + @d = @d.scale_annotation(@base_image, |
| 84 | + 0, 0, # Width of box to draw text in |
| 85 | + x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text |
| 86 | + label(marker_label, increment), @scale) |
| 87 | + end # unless |
| 88 | + @d = @d.stroke_antialias true |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + # Draw on the Y axis instead of the X |
| 93 | + def draw_label(y_offset, index) |
| 94 | + if !@axes.y_ticks[index].nil? && @geometry.labels_seen[index].nil? |
| 95 | + @d.fill = @font_color |
| 96 | + @d.font = @font if @font |
| 97 | + @d.stroke = 'transparent' |
| 98 | + @d.font_weight = NormalWeight |
| 99 | + @d.pointsize = scale_fontsize(@marker_font_size) |
| 100 | + @d.gravity = EastGravity |
| 101 | + @d = @d.scale_annotation(@base_image, |
| 102 | + 1, 1, |
| 103 | + -@graph_left + LABEL_MARGIN * 2.0, y_offset, |
| 104 | + @axes.y_ticks[index], @scale) |
| 105 | + @geometry.labels_seen[index] = 1 |
| 106 | + end |
| 107 | + end |
| 108 | + end # class Dot |
| 109 | + end # module Plot |
| 110 | + end # module MagickWrapper |
| 111 | +end # module Rubyplot |
0 commit comments