graph-bar 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graph/bar.rb +58 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5e58d816cf6b97833708fad489ee513ece24388
|
4
|
+
data.tar.gz: 9c92fc97fa846b3d2cefa165b5104f74214c7523
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e36232e0331f6382215f5a90a0ea01dbff1cde19072b9d5b15a7327e2bfa6c05ee36014619d96f61c3dcc93c3c1b6a678da0081d27dabf648f36e0127acb8967
|
7
|
+
data.tar.gz: c62e2fa6d85dcbd59eba878f771be744b7ec767f36e225e69090cc0f7d29f431031d1eccf3b6c166c4cb3b044714124eb9f4dc47b8cc3f3dfd8a7e2b6dad5f3b
|
data/lib/graph/bar.rb
CHANGED
@@ -2,6 +2,19 @@
|
|
2
2
|
# This module contains tools for analyzing and graphing data arrays
|
3
3
|
#
|
4
4
|
module Graph
|
5
|
+
##
|
6
|
+
# Modes for printing the graph
|
7
|
+
#
|
8
|
+
module PrintMode
|
9
|
+
##
|
10
|
+
# No scale factor
|
11
|
+
#
|
12
|
+
NOSCALE = 1
|
13
|
+
##
|
14
|
+
# Manually set scale factor
|
15
|
+
#
|
16
|
+
SCALE = 2
|
17
|
+
end
|
5
18
|
##
|
6
19
|
# This class allows the display of data arrays in bar graph format
|
7
20
|
#
|
@@ -10,7 +23,9 @@ module Graph
|
|
10
23
|
# Allows optionally setting the initial dataset as a parameter
|
11
24
|
#
|
12
25
|
def initialize(data = nil)
|
13
|
-
|
26
|
+
data(data)
|
27
|
+
mode(PrintMode::NOSCALE)
|
28
|
+
scale(1)
|
14
29
|
end
|
15
30
|
|
16
31
|
##
|
@@ -22,11 +37,50 @@ module Graph
|
|
22
37
|
end
|
23
38
|
|
24
39
|
##
|
25
|
-
#
|
40
|
+
# Get or Set the print mode
|
41
|
+
#
|
42
|
+
def mode(mode = nil)
|
43
|
+
@mode = mode unless mode.nil?
|
44
|
+
@mode
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Get or Set th print scale
|
49
|
+
#
|
50
|
+
def scale(scale = nil)
|
51
|
+
@scale = scale unless scale.nil?
|
52
|
+
@scale
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Print the graph to stdout, based on the print mode & scale
|
57
|
+
#
|
58
|
+
def print(mode = nil)
|
59
|
+
case mode || @mode
|
60
|
+
when PrintMode::NOSCALE
|
61
|
+
print_noscale
|
62
|
+
when PrintMode::SCALE
|
63
|
+
print_scale
|
64
|
+
else
|
65
|
+
raise ArgumentError, 'Invalid Print Mode', caller
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Print the graph without any scaling
|
71
|
+
#
|
72
|
+
def print_noscale
|
73
|
+
print_scale 1
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Prints graph with a scale. Scale of 10 means every 10 values
|
78
|
+
# is displayed as 1 character
|
26
79
|
#
|
27
|
-
def
|
80
|
+
def print_scale(scale = nil)
|
81
|
+
scale ||= @scale
|
28
82
|
@data.each_with_index do |count, index|
|
29
|
-
printf "%02d|%s\n", index, ('#' * count)
|
83
|
+
printf "%02d|%s\n", index, ('#' * (count / scale))
|
30
84
|
end
|
31
85
|
end
|
32
86
|
end
|