1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 77 78 79 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
$: << File.dirname(__FILE__) + '/../lib'
require "test/unit"
require "svggraph"
require "SVG/Graph/DataPoint"
class TestSvgGraphPlot < Test::Unit::TestCase
def setup
DataPoint.reset_shape_criteria
end
def teardown
DataPoint.reset_shape_criteria
end
def test_plot
projection = [
6, 11, 0, 5, 18, 7, 1, 11, 13, 9, 1, 2, 19, 0, 3, 13,
7, 9
]
actual = [
0, 18, 8, 15, 9, 4, 18, 14, 10, 2, 11, 6, 14, 12,
15, 6, 4, 17, 2, 12
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integerrs => true,
})
graph.add_data({
:data => projection,
:title => 'Projected',
})
graph.add_data({
:data => actual,
:title => 'Actual',
})
out=graph.burn()
assert(out=~/Created with SVG::Graph/)
end
def test_default_plot_emits_polyline_connecting_data_points
actual = [
0, 18, 8, 15, 9, 4, 18, 14, 10, 2, 11, 6, 14, 12,
15, 6, 4, 17, 2, 12
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integerrs => true,
})
graph.add_data({
:data => actual,
:title => 'Actual',
})
out=graph.burn()
assert_match(/path.*class='line1'/, out)
end
def test_disabling_show_lines_does_not_emit_polyline_connecting_data_points
actual = [
0, 18, 8, 15, 9, 4, 18, 14, 10, 2, 11, 6, 14, 12,
15, 6, 4, 17, 2, 12
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:show_lines => false,
})
graph.add_data({
:data => actual,
:title => 'Actual',
})
out=graph.burn()
assert_no_match(/path class='line1' d='M.* L.*'/, out)
end
def test_popup_values_round_to_integer_by_default_in_popups
actual = [
0.1, 18, 8.55, 15.1234, 9.09876765, 4,
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:add_popups => true,
})
graph.add_data({
:data => actual,
:title => 'Actual',
})
out=graph.burn()
assert_no_match(/\(0.1, 18\)/, out)
assert_match(/\(0, 18\)/, out)
assert_no_match(/\(8.55, 15.1234\)/, out)
assert_match(/\(8, 15\)/, out)
assert_no_match(/\(9.09876765, 4\)/, out)
assert_match(/\(9, 4\)/, out)
end
def test_do_not_round_popup_values_shows_decimal_values_in_popups
actual = [
0.1, 18, 8.55, 15.1234, 9.09876765, 4,
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:add_popups => true,
:round_popups => false,
})
graph.add_data({
:data => actual,
:title => 'Actual',
})
out=graph.burn()
assert_match(/\(0.1, 18\)/, out)
assert_no_match(/\(0, 18\)/, out)
assert_match(/\(8.55, 15.1234\)/, out)
assert_no_match(/\(8, 15\)/, out)
assert_match(/\(9.09876765, 4\)/, out)
assert_no_match(/\(9, 4\)/, out)
end
def test_description_is_shown_in_popups_if_provided
actual = [
8.55, 15.1234, 9.09876765, 4, 0.1, 18,
]
description = [
'first', 'second', 'third',
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:add_popups => true,
:round_popups => false,
})
graph.add_data({
:data => actual,
:title => 'Actual',
:description => description,
})
out=graph.burn()
assert_match(/\(8.55, 15.1234, first\)/, out)
assert_no_match(/\(8.55, 15.1234\)/, out)
assert_match(/\(9.09876765, 4, second\)/, out)
assert_no_match(/\(9.09876765, 4\)/, out)
assert_match(/\(0.1, 18, third\)/, out)
assert_no_match(/\(0.1, 18\)/, out)
end
def test_combine_different_shapes_based_on_description
actual = [
8.55, 15.1234, 9.09876765, 4, 2.1, 18,
]
description = [
'one is a circle', 'two is a rectangle', 'three is a rectangle with strikethrough',
]
DataPoint.configure_shape_criteria(
[/^t.*/, lambda{|x,y,line| ['polygon', {
"points" => "#{x-1.5},#{y+2.5} #{x+1.5},#{y+2.5} #{x+1.5},#{y-2.5} #{x-1.5},#{y-2.5}",
"class" => "dataPoint#{line}"
}]
}],
[/^three.*/, lambda{|x,y,line| ['line', {
"x1" => "#{x-4}",
"y1" => y.to_s,
"x2" => "#{x+4}",
"y2" => y.to_s,
"class" => "axis"
}]
}]
)
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:add_popups => true,
:round_popups => false,
})
graph.add_data({
:data => actual,
:title => 'Actual',
:description => description,
})
out=graph.burn()
assert_match(/polygon.*points/, out)
assert_match(/line.*axis/, out)
end
def test_popup_radius_is_10_by_default
actual = [
1, 1, 5, 5, 10, 10,
]
description = [
'first', 'second', 'third',
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:add_popups => true,
:round_popups => false,
})
graph.add_data({
:data => actual,
:title => 'Actual',
:description => description,
})
out=graph.burn()
assert_match(/circle\b.*\br='10'/, out)
assert_match(/circle\b.*\bonmouseover=.*/, out)
end
def test_popup_radius_is_overridable
actual = [
1, 1, 5, 5, 10, 10,
]
description = [
'first', 'second', 'third',
]
graph = SVG::Graph::Plot.new({
:height => 500,
:width => 300,
:key => true,
:scale_x_integers => true,
:scale_y_integers => true,
:add_popups => true,
:round_popups => false,
:popup_radius => 1.23
})
graph.add_data({
:data => actual,
:title => 'Actual',
:description => description,
})
out=graph.burn()
assert_match(/circle\b.*\br='1.23'/, out)
assert_match(/circle\b.*\bonmouseover=.*/, out)
end
end
|