image_paradise 0.1.12

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of image_paradise might be problematic. Click here for more details.

Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +38 -0
  3. data/README.md +179 -0
  4. data/USAGE.md +10 -0
  5. data/bin/black_white +10 -0
  6. data/bin/display_text_from_this_image +7 -0
  7. data/bin/image_paradise +7 -0
  8. data/bin/image_paradise_shell +7 -0
  9. data/bin/image_to_ascii +54 -0
  10. data/bin/image_to_pdf +7 -0
  11. data/bin/make_this_image_transparent +7 -0
  12. data/bin/rotate_left +7 -0
  13. data/bin/rotate_right +7 -0
  14. data/doc/README.gen +162 -0
  15. data/doc/TODO_FOR_THE_GTK_GUI.md +1 -0
  16. data/image_paradise.gemspec +50 -0
  17. data/lib/image_paradise.rb +5 -0
  18. data/lib/image_paradise/base/base.rb +91 -0
  19. data/lib/image_paradise/black_white.rb +89 -0
  20. data/lib/image_paradise/confree_generator/class.rb +614 -0
  21. data/lib/image_paradise/confree_generator/constants.rb +61 -0
  22. data/lib/image_paradise/confree_generator/gui/gtk_confree_generator.rb +752 -0
  23. data/lib/image_paradise/confree_generator/gui/insert_button.rb +78 -0
  24. data/lib/image_paradise/confree_generator/reset.rb +61 -0
  25. data/lib/image_paradise/confree_generator/shared/shared.rb +100 -0
  26. data/lib/image_paradise/constants.rb +43 -0
  27. data/lib/image_paradise/constants/image_file_types.rb +21 -0
  28. data/lib/image_paradise/create_animated_gif.rb +47 -0
  29. data/lib/image_paradise/crop/crop.rb +302 -0
  30. data/lib/image_paradise/gm_support.rb +34 -0
  31. data/lib/image_paradise/graphs.rb +36 -0
  32. data/lib/image_paradise/graphs/accumulator_bar.rb +29 -0
  33. data/lib/image_paradise/graphs/area.rb +64 -0
  34. data/lib/image_paradise/graphs/bar.rb +117 -0
  35. data/lib/image_paradise/graphs/bar_conversion.rb +53 -0
  36. data/lib/image_paradise/graphs/base.rb +1392 -0
  37. data/lib/image_paradise/graphs/bezier.rb +45 -0
  38. data/lib/image_paradise/graphs/bullet.rb +115 -0
  39. data/lib/image_paradise/graphs/deprecated.rb +42 -0
  40. data/lib/image_paradise/graphs/dot.rb +129 -0
  41. data/lib/image_paradise/graphs/line.rb +328 -0
  42. data/lib/image_paradise/graphs/mini/bar.rb +42 -0
  43. data/lib/image_paradise/graphs/mini/legend.rb +109 -0
  44. data/lib/image_paradise/graphs/mini/pie.rb +42 -0
  45. data/lib/image_paradise/graphs/mini/side_bar.rb +41 -0
  46. data/lib/image_paradise/graphs/net.rb +133 -0
  47. data/lib/image_paradise/graphs/photo_bar.rb +106 -0
  48. data/lib/image_paradise/graphs/pie.rb +139 -0
  49. data/lib/image_paradise/graphs/scatter.rb +264 -0
  50. data/lib/image_paradise/graphs/scene.rb +216 -0
  51. data/lib/image_paradise/graphs/side_bar.rb +144 -0
  52. data/lib/image_paradise/graphs/side_stacked_bar.rb +116 -0
  53. data/lib/image_paradise/graphs/spider.rb +163 -0
  54. data/lib/image_paradise/graphs/stacked_area.rb +73 -0
  55. data/lib/image_paradise/graphs/stacked_bar.rb +68 -0
  56. data/lib/image_paradise/graphs/stacked_mixin.rb +30 -0
  57. data/lib/image_paradise/graphs/themes.rb +117 -0
  58. data/lib/image_paradise/graphviz/README.md +2 -0
  59. data/lib/image_paradise/graphviz/generate_graphviz_image.rb +274 -0
  60. data/lib/image_paradise/gui/gtk/control_panel.rb +126 -0
  61. data/lib/image_paradise/identify.rb +145 -0
  62. data/lib/image_paradise/image_border.rb +231 -0
  63. data/lib/image_paradise/image_manipulations.rb +320 -0
  64. data/lib/image_paradise/image_paradise.rb +150 -0
  65. data/lib/image_paradise/image_to_ascii/image_to_ascii.rb +187 -0
  66. data/lib/image_paradise/image_to_pdf/image_to_pdf.rb +99 -0
  67. data/lib/image_paradise/label/README.md +2 -0
  68. data/lib/image_paradise/label/simple_label.rb +206 -0
  69. data/lib/image_paradise/optimizer.rb +483 -0
  70. data/lib/image_paradise/project/project.rb +29 -0
  71. data/lib/image_paradise/random_text_to_image.rb +363 -0
  72. data/lib/image_paradise/requires/common_base_requires.rb +17 -0
  73. data/lib/image_paradise/requires/require_colours.rb +9 -0
  74. data/lib/image_paradise/requires/require_gtk_components.rb +8 -0
  75. data/lib/image_paradise/requires/require_image_to_ascii.rb +7 -0
  76. data/lib/image_paradise/requires/require_the_image_paradise_project.rb +24 -0
  77. data/lib/image_paradise/requires/require_toplevel_methods.rb +21 -0
  78. data/lib/image_paradise/rotate/README.md +2 -0
  79. data/lib/image_paradise/rotate/rotate.rb +98 -0
  80. data/lib/image_paradise/shell/interactive.rb +156 -0
  81. data/lib/image_paradise/svg/README.md +5 -0
  82. data/lib/image_paradise/svg/circle.rb +106 -0
  83. data/lib/image_paradise/svg/feature.rb +48 -0
  84. data/lib/image_paradise/svg/rectangle.rb +154 -0
  85. data/lib/image_paradise/svg/svg.rb +102 -0
  86. data/lib/image_paradise/to_gif.rb +91 -0
  87. data/lib/image_paradise/to_jpg.rb +90 -0
  88. data/lib/image_paradise/toplevel_methods/add_black_border_to_this_image.rb +56 -0
  89. data/lib/image_paradise/toplevel_methods/crop.rb +28 -0
  90. data/lib/image_paradise/toplevel_methods/e.rb +16 -0
  91. data/lib/image_paradise/toplevel_methods/esystem.rb +19 -0
  92. data/lib/image_paradise/toplevel_methods/extract_text_from_this_image.rb +56 -0
  93. data/lib/image_paradise/toplevel_methods/file_related_code.rb +25 -0
  94. data/lib/image_paradise/toplevel_methods/flip_image_left_right.rb +32 -0
  95. data/lib/image_paradise/toplevel_methods/greyscale_this_image.rb +59 -0
  96. data/lib/image_paradise/toplevel_methods/help.rb +30 -0
  97. data/lib/image_paradise/toplevel_methods/make_this_image_transparent.rb +30 -0
  98. data/lib/image_paradise/toplevel_methods/menu.rb +92 -0
  99. data/lib/image_paradise/toplevel_methods/merge_these_images.rb +49 -0
  100. data/lib/image_paradise/toplevel_methods/mirror_image.rb +28 -0
  101. data/lib/image_paradise/toplevel_methods/misc.rb +31 -0
  102. data/lib/image_paradise/toplevel_methods/png_to_svg.rb +34 -0
  103. data/lib/image_paradise/toplevel_methods/roebe.rb +17 -0
  104. data/lib/image_paradise/toplevel_methods/to_png.rb +105 -0
  105. data/lib/image_paradise/toplevel_methods/wallpaper.rb +37 -0
  106. data/lib/image_paradise/toplevel_methods/write_this_text.rb +76 -0
  107. data/lib/image_paradise/version/version.rb +19 -0
  108. data/test/16x16_red_square_image_for_testing.png +0 -0
  109. data/test/testing_confree_generator.rb +8 -0
  110. data/test/testing_crop.rb +19 -0
  111. data/test/testing_image_magick_commands.rb +39 -0
  112. data/test/testing_image_paradise.rb +49 -0
  113. data/test/testing_the_svg_component.html +261 -0
  114. data/test/testing_the_svg_component.rb +106 -0
  115. metadata +217 -0
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'image_paradise/graphs/base.rb'
6
+
7
+ module ImageParadise
8
+
9
+ class Gruff::Bezier < Gruff::Base
10
+
11
+ # ========================================================================= #
12
+ # === draw
13
+ # ========================================================================= #
14
+ def draw
15
+ super
16
+ return unless @has_data
17
+ @x_increment = @graph_width / (@column_count - 1).to_f
18
+ @norm_data.each do |data_row|
19
+ poly_points = Array.new
20
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
21
+ data_row[1].each_with_index do |data_point, index|
22
+ # Use incremented x and scaled y
23
+ new_x = @graph_left + (@x_increment * index)
24
+ new_y = @graph_top + (@graph_height - data_point * @graph_height)
25
+ if index == 0 && RUBY_PLATFORM != 'java'
26
+ poly_points << new_x
27
+ poly_points << new_y
28
+ end
29
+ poly_points << new_x
30
+ poly_points << new_y
31
+ draw_label(new_x, index)
32
+ end
33
+ @d = @d.fill_opacity 0.0
34
+ @d = @d.stroke data_row[DATA_COLOR_INDEX]
35
+ @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)
36
+ if RUBY_PLATFORM == 'java'
37
+ @d = @d.polyline(*poly_points)
38
+ else
39
+ @d = @d.bezier(*poly_points)
40
+ end
41
+ end
42
+ @d.draw(@base_image)
43
+ end
44
+
45
+ end; end
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'image_paradise/graphs/base.rb'
6
+ require 'image_paradise/graphs/themes.rb'
7
+
8
+ module ImageParadise
9
+
10
+ # http://en.wikipedia.org/wiki/Bullet_graph
11
+ class Gruff::Bullet < Gruff::Base
12
+ # === initialize
13
+ def initialize(target_width = '400x40')
14
+ if not Numeric === target_width
15
+ geometric_width, geometric_height = target_width.split('x')
16
+ @columns = geometric_width.to_f
17
+ @rows = geometric_height.to_f
18
+ else
19
+ @columns = target_width.to_f
20
+ @rows = target_width.to_f / 5.0
21
+ end
22
+
23
+ initialize_ivars
24
+
25
+ reset_themes
26
+ self.theme = Gruff::Themes::GREYSCALE
27
+ @title_font_size = 20
28
+ end
29
+
30
+ def data(value, maximum_value, options={})
31
+ @value = value.to_f
32
+ @maximum_value = maximum_value.to_f
33
+ @options = options
34
+ @options.map { |k, v| @options[k] = v.to_f if v === Numeric }
35
+ end
36
+
37
+ # def setup_drawing
38
+ # # Maybe should be done in one of the following functions for more granularity.
39
+ # unless @has_data
40
+ # draw_no_data()
41
+ # return
42
+ # end
43
+ #
44
+ # normalize()
45
+ # setup_graph_measurements()
46
+ # sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display)
47
+ #
48
+ # draw_legend()
49
+ # draw_line_markers()
50
+ # draw_axis_labels()
51
+ # draw_title
52
+ # end
53
+ # === draw
54
+ def draw
55
+ # TODO Left label
56
+ # TODO Bottom labels and markers
57
+ # @graph_bottom
58
+ # Calculations are off 800x???
59
+
60
+ @colors.reverse!
61
+
62
+ draw_title
63
+
64
+ @margin = 30.0
65
+ @thickness = @raw_rows / 6.0
66
+ @right_margin = @margin
67
+ @graph_left = @title_width * 1.3 rescue @margin # HACK Need to calculate real width
68
+ @graph_width = @raw_columns - @graph_left - @right_margin
69
+ @graph_height = @thickness * 3.0
70
+
71
+ # Background
72
+ @d = @d.fill @colors[0]
73
+ @d = @d.rectangle(@graph_left, 0, @graph_left + @graph_width, @graph_height)
74
+
75
+ [:high, :low].each_with_index do |indicator, index|
76
+ next unless @options.has_key?(indicator)
77
+ @d = @d.fill @colors[index + 1]
78
+ indicator_width_x = @graph_left + @graph_width * (@options[indicator] / @maximum_value)
79
+ @d = @d.rectangle(@graph_left, 0, indicator_width_x, @graph_height)
80
+ end
81
+ if @options.has_key?(:target)
82
+ @d = @d.fill @font_color
83
+ target_x = @graph_left + @graph_width * (@options[:target] / @maximum_value)
84
+ half_thickness = @thickness / 2.0
85
+ @d = @d.rectangle(target_x, half_thickness, target_x + half_thickness, @thickness * 2 + half_thickness)
86
+ end
87
+
88
+ # Value
89
+ @d = @d.fill @font_color
90
+ @d = @d.rectangle(@graph_left, @thickness, @graph_left + @graph_width * (@value / @maximum_value), @thickness * 2)
91
+
92
+ @d.draw(@base_image)
93
+ end
94
+
95
+ # === draw_title
96
+ def draw_title
97
+ return unless @title
98
+ @font_height = calculate_caps_height(scale_fontsize(@title_font_size))
99
+ @title_width = calculate_width(@title_font_size, @title)
100
+ @d.fill = @font_color
101
+ @d.font = @font if @font
102
+ @d.stroke('transparent')
103
+ @d.font_weight = NormalWeight
104
+ @d.pointsize = scale_fontsize(@title_font_size)
105
+ @d.gravity = NorthWestGravity
106
+ @d = @d.annotate_scaled(*[
107
+ @base_image,
108
+ 1.0, 1.0,
109
+ @font_height/2, @font_height/2,
110
+ @title,
111
+ @scale
112
+ ])
113
+ end
114
+
115
+ end; end
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # A mixin for methods that need to be deleted or have
6
+ # been replaced by cleaner code.
7
+ module ImageParadise
8
+
9
+ module Gruff
10
+
11
+ module Deprecated
12
+
13
+ def scale_measurements
14
+ setup_graph_measurements
15
+ end
16
+
17
+ def total_height
18
+ @rows + 10
19
+ end
20
+
21
+ def graph_top
22
+ @graph_top * @scale
23
+ end
24
+
25
+ def graph_height
26
+ @graph_height * @scale
27
+ end
28
+
29
+ def graph_left
30
+ @graph_left * @scale
31
+ end
32
+
33
+ def graph_width
34
+ @graph_width * @scale
35
+ end
36
+
37
+ # TODO Should be calculate_graph_height
38
+ # def setup_graph_height
39
+ # @graph_height = @graph_bottom - @graph_top
40
+ # end
41
+
42
+ end; end; end
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'image_paradise/graphs/base.rb'
6
+
7
+ ##
8
+ # Graph with dots and labels along a vertical access
9
+ # see: 'Creating More Effective Graphs' by Robbins
10
+
11
+ module ImageParadise
12
+
13
+ class Gruff::Dot < Gruff::Base
14
+
15
+ # ========================================================================= #
16
+ # === draw
17
+ def draw
18
+ @has_left_labels = true
19
+ super
20
+
21
+ return unless @has_data
22
+
23
+ # Setup spacing.
24
+ #
25
+ spacing_factor = 1.0
26
+
27
+ @items_width = @graph_height / @column_count.to_f
28
+ @item_width = @items_width * spacing_factor / @norm_data.size
29
+ @d = @d.stroke_opacity 0.0
30
+ padding = (@items_width * (1 - spacing_factor)) / 2
31
+
32
+ @norm_data.each_with_index do |data_row, row_index|
33
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
34
+ x_pos = @graph_left + (data_point * @graph_width)
35
+ y_pos = @graph_top + (@items_width * point_index) + padding + (@items_width.to_f/2.0).round
36
+
37
+ if row_index == 0
38
+ @d = @d.stroke(@marker_color)
39
+ @d = @d.fill(@marker_color)
40
+ @d = @d.stroke_width 1.0
41
+ @d = @d.stroke_opacity 0.1
42
+ @d = @d.fill_opacity 0.1
43
+ @d = @d.line(@graph_left, y_pos, @graph_left + @graph_width, y_pos)
44
+ @d = @d.fill_opacity 1
45
+ end
46
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
47
+ @d = @d.stroke('transparent')
48
+ @d = @d.circle(x_pos, y_pos, x_pos + (@item_width.to_f/3.0).round, y_pos)
49
+ draw_label(y_pos, point_index)
50
+ end
51
+
52
+ end
53
+
54
+ @d.draw(@base_image)
55
+ end
56
+ protected
57
+ # === draw_line_markers
58
+ #
59
+ # Instead of base class version, draws vertical background lines and label
60
+ def draw_line_markers
61
+ return if @hide_line_markers
62
+ @d = @d.stroke_antialias false
63
+ # Draw horizontal line markers and annotate with numbers
64
+ @d = @d.stroke(@marker_color)
65
+ @d = @d.stroke_width 1
66
+ if @y_axis_increment
67
+ increment = @y_axis_increment
68
+ number_of_lines = (@spread / @y_axis_increment).to_i
69
+ else
70
+ # Try to use a number of horizontal lines that will come out even.
71
+ #
72
+ # TODO Do the same for larger numbers...100, 75, 50, 25
73
+ if @marker_count.nil?
74
+ (3..7).each { |lines|
75
+ if @spread % lines == 0.0
76
+ @marker_count = lines
77
+ break
78
+ end
79
+ }
80
+ @marker_count ||= 5
81
+ end
82
+ # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
83
+ @increment = (@spread > 0 && @marker_count > 0) ? significant(@spread / @marker_count) : 1
84
+
85
+ number_of_lines = @marker_count
86
+ increment = @increment
87
+ end
88
+
89
+ (0..number_of_lines).each do |index|
90
+ marker_label = @minimum_value + index * increment
91
+ x = @graph_left + (marker_label - @minimum_value) * @graph_width / @spread
92
+ @d = @d.line(x, @graph_bottom, x, @graph_bottom + 0.5 * LABEL_MARGIN)
93
+ unless @hide_line_numbers
94
+ @d.fill = @font_color
95
+ @d.font = @font if @font
96
+ @d.stroke = 'transparent'
97
+ @d.pointsize = scale_fontsize(@marker_font_size)
98
+ @d.gravity = CenterGravity
99
+ # TODO Center text over line
100
+ @d = @d.annotate_scaled(@base_image,
101
+ 0, 0, # Width of box to draw text in
102
+ x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text
103
+ label(marker_label, increment), @scale)
104
+ end # unless
105
+ @d = @d.stroke_antialias true
106
+ end
107
+ end
108
+
109
+ # ========================================================================= #
110
+ # === draw_label
111
+ #
112
+ # Draw on the Y axis instead of the X
113
+ def draw_label(y_offset, index)
114
+ if !@labels[index].nil? && @labels_seen[index].nil?
115
+ @d.fill = @font_color
116
+ @d.font = @font if @font
117
+ @d.stroke = 'transparent'
118
+ @d.font_weight = NormalWeight
119
+ @d.pointsize = scale_fontsize(@marker_font_size)
120
+ @d.gravity = EastGravity
121
+ @d = @d.annotate_scaled(@base_image,
122
+ 1, 1,
123
+ -@graph_left + LABEL_MARGIN * 2.0, y_offset,
124
+ @labels[index], @scale)
125
+ @labels_seen[index] = 1
126
+ end
127
+ end
128
+
129
+ end; end
@@ -0,0 +1,328 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'image_paradise/graphs/base.rb'
6
+
7
+ module ImageParadise
8
+
9
+ ##
10
+ # Here's how to make a Line graph:
11
+ #
12
+ # g = Gruff::Line.new
13
+ # g.title = "A Line Graph"
14
+ # g.data 'Fries', [20, 23, 19, 8]
15
+ # g.data 'Hamburgers', [50, 19, 99, 29]
16
+ # g.write("test/output/line.png")
17
+ #
18
+ # There are also other options described below, such as #baseline_value, #baseline_color, #hide_dots, and #hide_lines.
19
+
20
+ class Gruff::Line < Gruff::Base
21
+
22
+ # Allow for reference lines ( which are like baseline ... just allowing for more & on both axes )
23
+ attr_accessor :reference_lines
24
+ attr_accessor :reference_line_default_color
25
+ attr_accessor :reference_line_default_width
26
+
27
+ # Allow for vertical marker lines
28
+ attr_accessor :show_vertical_markers
29
+
30
+ # Dimensions of lines and dots; calculated based on dataset size if left unspecified
31
+ attr_accessor :line_width
32
+ attr_accessor :dot_radius
33
+
34
+ # Hide parts of the graph to fit more datapoints, or for a different appearance.
35
+ attr_accessor :hide_dots, :hide_lines
36
+
37
+ #accessors for support of xy data
38
+ attr_accessor :minimum_x_value
39
+ attr_accessor :maximum_x_value
40
+ # === baseline_value
41
+ #
42
+ # Get the value if somebody has defined it.
43
+ def baseline_value
44
+ if (@reference_lines.key?(:baseline))
45
+ @reference_lines[:baseline][:value]
46
+ else
47
+ nil
48
+ end
49
+ end
50
+
51
+ # Set a value for a baseline reference line..
52
+ def baseline_value=(new_value)
53
+ @reference_lines[:baseline] ||= Hash.new
54
+ @reference_lines[:baseline][:value] = new_value
55
+ end
56
+
57
+ def baseline_color
58
+ if (@reference_lines.key?(:baseline))
59
+ @reference_lines[:baseline][:color]
60
+ else
61
+ nil
62
+ end
63
+ end
64
+ # === baseline_color=
65
+ def baseline_color=(new_value)
66
+ @reference_lines[:baseline] ||= Hash.new
67
+ @reference_lines[:baseline][:color] = new_value
68
+ end
69
+
70
+ # Call with target pixel width of graph (800, 400, 300), and/or 'false' to omit lines (points only).
71
+ #
72
+ # g = Gruff::Line.new(400) # 400px wide with lines
73
+ #
74
+ # g = Gruff::Line.new(400, false) # 400px wide, no lines (for backwards compatibility)
75
+ #
76
+ # g = Gruff::Line.new(false) # Defaults to 800px wide, no lines (for backwards compatibility)
77
+ #
78
+ # The preferred way is to call hide_dots or hide_lines instead.
79
+ def initialize(*args)
80
+ raise ArgumentError, 'Wrong number of arguments' if args.length > 2
81
+ if args.empty? || ((not Numeric === args.first) && (not String === args.first))
82
+ super()
83
+ else
84
+ super args.shift
85
+ end
86
+
87
+ @reference_lines = Hash.new
88
+ @reference_line_default_color = 'red'
89
+ @reference_line_default_width = 5
90
+
91
+ @hide_dots = @hide_lines = false
92
+ @maximum_x_value = nil
93
+ @minimum_x_value = nil
94
+ end
95
+
96
+ # This method allows one to plot a dataset with both X and Y data.
97
+ #
98
+ # Parameters are as follows:
99
+ # name: string, the title of the dataset
100
+ # x_data_points: an array containing the x data points for the graph
101
+ # y_data_points: an array containing the y data points for the graph
102
+ # color: hex number indicating the line color as an RGB triplet
103
+ #
104
+ # or
105
+ #
106
+ # name: string, the title of the dataset
107
+ # xy_data_points: an array containing both x and y data points for the graph
108
+ # color: hex number indicating the line color as an RGB triplet
109
+ #
110
+ # Notes:
111
+ # -if (x_data_points.length != y_data_points.length) an error is
112
+ # returned.
113
+ # -if the color argument is nil, the next color from the default theme will
114
+ # be used.
115
+ # -if you want to use a preset theme, you must set it before calling
116
+ # dataxy().
117
+ #
118
+ # Example:
119
+ # g = Gruff::Line.new
120
+ # g.title = "X/Y Dataset"
121
+ # g.dataxy("Apples", [1,3,4,5,6,10], [1, 2, 3, 4, 4, 3])
122
+ # g.dataxy("Bapples", [1,3,4,5,7,9], [1, 1, 2, 2, 3, 3])
123
+ # g.dataxy("Capples", [[1,1],[2,3],[3,4],[4,5],[5,7],[6,9]])
124
+ # #you can still use the old data method too if you want:
125
+ # g.data("Capples", [1, 1, 2, 2, 3, 3])
126
+ # #labels will be drawn at the x locations of the keys passed in.
127
+ # In this example the lables are drawn at x positions 2, 4, and 6:
128
+ # g.labels = {0 => '2003', 2 => '2004', 4 => '2005', 6 => '2006'}
129
+ # The 0 => '2003' label will be ignored since it is outside the chart range.
130
+ def dataxy(name, x_data_points=[], y_data_points=[], color=nil)
131
+ raise ArgumentError, 'x_data_points is nil!' if x_data_points.length == 0
132
+
133
+ if x_data_points.all? { |p| p.is_a?(Array) && p.size == 2 }
134
+ x_data_points, y_data_points = x_data_points.map { |p| p[0] }, x_data_points.map { |p| p[1] }
135
+ end
136
+
137
+ raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length
138
+
139
+ # call the existing data routine for the y data.
140
+ self.data(name, y_data_points, color)
141
+
142
+ x_data_points = Array(x_data_points) # make sure it's an array
143
+ # append the x data to the last entry that was just added in the @data member
144
+ @data.last[DATA_VALUES_X_INDEX] = x_data_points
145
+
146
+ # Update the global min/max values for the x data
147
+ x_data_points.each do |x_data_point|
148
+ next if x_data_point.nil?
149
+
150
+ # Setup max/min so spread starts at the low end of the data points
151
+ if @maximum_x_value.nil? && @minimum_x_value.nil?
152
+ @maximum_x_value = @minimum_x_value = x_data_point
153
+ end
154
+ @maximum_x_value = (x_data_point > @maximum_x_value) ?
155
+ x_data_point : @maximum_x_value
156
+ @minimum_x_value = (x_data_point < @minimum_x_value) ?
157
+ x_data_point : @minimum_x_value
158
+ end
159
+
160
+ end
161
+ # === draw_reference_line
162
+ def draw_reference_line(reference_line, left, right, top, bottom)
163
+ @d = @d.push
164
+ @d.stroke_color(reference_line[:color] || @reference_line_default_color)
165
+ @d.fill_opacity 0.0
166
+ @d.stroke_dasharray(10, 20)
167
+ @d.stroke_width(reference_line[:width] || @reference_line_default_width)
168
+ @d.line(left, top, right, bottom)
169
+ @d = @d.pop
170
+ end
171
+
172
+ def draw_horizontal_reference_line(reference_line)
173
+ level = @graph_top + (@graph_height - reference_line[:norm_value] * @graph_height)
174
+ draw_reference_line(reference_line, @graph_left, @graph_left + @graph_width, level, level)
175
+ end
176
+
177
+ def draw_vertical_reference_line(reference_line)
178
+ index = @graph_left + (@x_increment * reference_line[:index])
179
+ draw_reference_line(reference_line, index, index, @graph_top, @graph_top + @graph_height)
180
+ end
181
+
182
+ # === draw
183
+ def draw
184
+ super
185
+ return unless @has_data
186
+ # Check to see if more than one datapoint was given. NaN can result otherwise.
187
+ @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
188
+ @reference_lines.each_value do |curr_reference_line|
189
+ draw_horizontal_reference_line(curr_reference_line) if curr_reference_line.key?(:norm_value)
190
+ draw_vertical_reference_line(curr_reference_line) if curr_reference_line.key?(:index)
191
+ end
192
+
193
+ if (@show_vertical_markers)
194
+ (0..@column_count).each do |column|
195
+ x = @graph_left + @graph_width - column.to_f * @x_increment
196
+
197
+ @d = @d.fill(@marker_color)
198
+
199
+ # FIXME(uwe): Workaround for Issue #66
200
+ # https://github.com/topfunky/gruff/issues/66
201
+ # https://github.com/rmagick/rmagick/issues/82
202
+ # Remove if the issue gets fixed.
203
+ x += 0.001 unless defined?(JRUBY_VERSION)
204
+ # EMXIF
205
+
206
+ @d = @d.line(x, @graph_bottom, x, @graph_top)
207
+ #If the user specified a marker shadow color, draw a shadow just below it
208
+ unless @marker_shadow_color.nil?
209
+ @d = @d.fill(@marker_shadow_color)
210
+ @d = @d.line(x + 1, @graph_bottom, x + 1, @graph_top)
211
+ end
212
+ end
213
+ end
214
+
215
+ @norm_data.each do |data_row|
216
+ prev_x = prev_y = nil
217
+ @one_point = contains_one_point_only?(data_row)
218
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
219
+ x_data = data_row[DATA_VALUES_X_INDEX]
220
+ if x_data == nil
221
+ #use the old method: equally spaced points along the x-axis
222
+ new_x = @graph_left + (@x_increment * index)
223
+ draw_label(new_x, index)
224
+ else
225
+ new_x = get_x_coord(x_data[index], @graph_width, @graph_left)
226
+ @labels.each do |label_pos, _|
227
+ draw_label(@graph_left + ((label_pos - @minimum_x_value) * @graph_width) / (@maximum_x_value - @minimum_x_value), label_pos)
228
+ end
229
+ end
230
+ unless data_point # we can't draw a line for a null data point, we can still label the axis though
231
+ prev_x = prev_y = nil
232
+ next
233
+ end
234
+
235
+ new_y = @graph_top + (@graph_height - data_point * @graph_height)
236
+
237
+ # Reset each time to avoid thin-line errors
238
+ @d = @d.stroke data_row[DATA_COLOR_INDEX]
239
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
240
+ @d = @d.stroke_opacity 1.0
241
+ @d = @d.stroke_width line_width ||
242
+ clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0)
243
+
244
+ circle_radius = dot_radius ||
245
+ clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0)
246
+
247
+ if !@hide_lines && !prev_x.nil? && !prev_y.nil?
248
+ @d = @d.line(prev_x, prev_y, new_x, new_y)
249
+ elsif @one_point
250
+ # Show a circle if there's just one_point
251
+ @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
252
+ end
253
+ @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) unless @hide_dots
254
+
255
+ prev_x, prev_y = new_x, new_y
256
+ end
257
+ end
258
+
259
+ @d.draw(@base_image)
260
+ end
261
+
262
+ def setup_data
263
+
264
+ # Deal with horizontal reference line values that exceed the existing minimum & maximum values.
265
+ possible_maximums = [@maximum_value.to_f]
266
+ possible_minimums = [@minimum_value.to_f]
267
+
268
+ @reference_lines.each_value do |curr_reference_line|
269
+ if (curr_reference_line.key?(:value))
270
+ possible_maximums << curr_reference_line[:value].to_f
271
+ possible_minimums << curr_reference_line[:value].to_f
272
+ end
273
+ end
274
+ @maximum_value = possible_maximums.max
275
+ @minimum_value = possible_minimums.min
276
+ super
277
+ end
278
+
279
+ def normalize(force=false)
280
+ super(force)
281
+
282
+ @reference_lines.each_value do |curr_reference_line|
283
+
284
+ # We only care about horizontal markers ... for normalization.
285
+ # Vertical markers won't have a :value, they will have an :index
286
+
287
+ curr_reference_line[:norm_value] = ((curr_reference_line[:value].to_f - @minimum_value) / @spread.to_f) if (curr_reference_line.key?(:value))
288
+
289
+ end
290
+
291
+ #normalize the x data if it is specified
292
+ @data.each_with_index do |data_row, index|
293
+ norm_x_data_points = []
294
+ if data_row[DATA_VALUES_X_INDEX] != nil
295
+ data_row[DATA_VALUES_X_INDEX].each do |x_data_point|
296
+ norm_x_data_points << ((x_data_point.to_f - @minimum_x_value.to_f) /
297
+ (@maximum_x_value.to_f - @minimum_x_value.to_f))
298
+ end
299
+ @norm_data[index] << norm_x_data_points
300
+ end
301
+ end
302
+ end
303
+ # === sort_norm_data
304
+ def sort_norm_data
305
+ super unless @data.any? { |d| d[DATA_VALUES_X_INDEX] }
306
+ end
307
+ # === get_x_coord
308
+ def get_x_coord(x_data_point, width, offset)
309
+ x_data_point * width + offset
310
+ end
311
+ # === contains_one_point_only?
312
+ def contains_one_point_only?(data_row)
313
+ # Spin through data to determine if there is just one_value present.
314
+ one_point = false
315
+ data_row[DATA_VALUES_INDEX].each do |data_point|
316
+ unless data_point.nil?
317
+ if one_point
318
+ # more than one point, bail
319
+ return false
320
+ end
321
+ # there is at least one data point
322
+ one_point = true
323
+ end
324
+ end
325
+ one_point
326
+ end
327
+
328
+ end; end