forked from mpflueger/asciigraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasciiGraph
executable file
·201 lines (175 loc) · 4.7 KB
/
asciiGraph
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
#!/usr/local/bin/perl
# This is a script for generating ascii-art style graphs on the terminal
#
# Default size will be 24 rows, 80 columns
use Getopt::Long qw(:config bundling);
use Math::Round qw(nlowmult round);
use List::Util qw(max);
my $height = 24;
my $width = 80;
my $key = undef;
my $value = 1;
#TODO:
my $orientation = "vertical";
my $help = undef;
GetOptions('h=s' => \$height,
'height=s' => \$height,
'w=s'=> \$width,
'width=s' => \$width,
'k=s' => \$key,
'key=s' => \$key,
'v=s' => \$value,
'value=s' => \$value,
'help' => \$help);
my $usage = "Usage: ./asciiGraph [OPTIONS]\n"
."Data is read from STDIN\n"
."Options:\n"
." -h --height [NUM] Set the height of the graph to NUM\n"
." Default = $height\n"
." -w --width [NUM] Set the width of the graph to NUM\n"
." Default = $width\n"
." -v --value [NUM] Specify which column to use as the data\n"
." -k --key [NUM] Specify which column keys the data column\n"
." --help Show this screen\n";
if($help) {
die $usage;
}
my @buf = undef;
# Init the buffer
# NOTE: $buf[y][x]
foreach my $i (0..($height-1)) {
foreach my $j (0..($width-1)) {
$buf[$i][$j] = '.';
}
}
###################
# DRAW THE GRAPH
###################
my @keys = ();
my @values = ();
my $maxVal = 0;
my $minVal = 0;
# Read stdin to get the data
if(my $line = <>) {
$line = trim_ws($line);
my @line = split(/\s+/, $line);
push(@values, $line[($value - 1)]);
$maxVal = $line[$value-1];
$minVal = $line[$value-1];
push(@keys, $line[($key - 1)]);
}
while(my $line = <>) {
$line = trim_ws($line);
my @line = split(/\s+/, $line);
push(@values, $line[($value - 1)]);
push(@keys, $line[($key - 1)]);
# update max and min val
if($line[$value -1] > $maxVal) {
$maxVal = $line[$value-1];
}
if($line[$value -1] < $minVal) {
$minVal = $line[$value-1];
}
}
#TODO: split into horiz and vert versions
# Determine the margins
my $x0 = max(length($maxVal), length($minVal));
my $y0 = 1;
# Draw axes
my @verts = ($x0);
my @horiz = ($y0, ($y0 + 10), ($y0 + 20));
foreach (@verts) {
foreach my $i (0..($height-1)) {
$buf[$i][$_] = '|';
}
}
foreach (@horiz) {
foreach my $i (0..($width-1)) {
if($buf[$_][$i] eq '|') {
$buf[$_][$i] = '+';
} else {
$buf[$_][$i] = '-';
}
}
}
# print the scale values
my @minVal = split(//, $minVal);
for(my $i=0; $i < length($minVal); $i++) {
$buf[$y0+1][$x0 - length($minVal) + $i] = $minVal[$i];
}
my @maxVal = split(//, $maxVal);
for(my $i=0; $i < length($maxVal); $i++) {
$buf[$height-1][$x0 - length($maxVal) + $i] = $maxVal[$i];
}
# calculate chars per input unit
my $scale = ($height - $y0 - 2) / ($maxVal - $minVal);
# Draw the bars
if(scalar(@values) > 0) {
my $datWidth = nlowmult(1, ($width-$x0-1)/scalar(@values));
foreach my $i (0..(scalar(@values)-1)) {
my $xpos = $x0 + 1 + ($i * $datWidth);
my $ytop = $y0+1 + round(($values[$i] - $minVal) * $scale);
drawLine('#', $xpos, $y0+1, $xpos, $ytop);
for(my $j = 1; $j < $datWidth-1; $j++) {
drawLine('#', $xpos+$j, $y0+1, $xpos+$j, $ytop);
}
}
}
###################
#Print the buffer
# h,0 ... h,w
# . . .
# . . .
# . . .
# 0,0 ... 0,w
foreach my $i (0..($height-1)) {
$i = $height - 1 - $i;
foreach my $j (0..($width-1)) {
print STDOUT $buf[$i][$j];
}
print STDOUT "\n";
}
###################
#SUBS
###################
sub trim_ws {
my $in = shift;
chomp($in);
$in =~ s/^\s+//g;
$in =~ s/\s+$//g;
return $in;
}
sub drawLine {
my $char = shift;
my $x1 = shift;
my $y1 = shift;
my $x2 = shift;
my $y2 = shift;
# Calculate the distance of the line in each dimension
# and mark the larger dimension as the one we will
# iterate over
my $diff = $x2 - $x1;
my $smallDiff = $y2 - $y1;
my $axis = 'x';
if($diff < $smallDiff) {
my $temp = $diff;
$diff = $smallDiff;
$smallDiff = $temp;
$axis = 'y';
}
# Loop on the larger dimension to draw chars
foreach my $i (0..$diff) {
# calculate the proporitonal offset of the small dimension
my $smallI;
if($diff > 0) {
$smallI = round(($i/$diff) * $smallDiff);
} else {
$smallI = 0;
}
if($axis eq 'x') {
$buf[$y1 + $smallI][$x1 + $i] = $char;
} else {
$buf[$y1 + $i][$x1 + $smallI] = $char;
}
}
}