-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_series.sp
executable file
·298 lines (255 loc) · 9.43 KB
/
graph_series.sp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/local/bin/spar
pragma annotate( summary, "graph_series.sp" )
@( description, "Plot one or more series of data points on " )
@( description, "a graph. The maximum number of series is 4. " )
@( description, "A history of 4 data points is maintained. " )
@( description, "The graph will be saved as a bmp file. If " )
@( description, "Imagemagick is installed, it will also be " )
@( description, "saved as a png file." )
@( param, "the graph name" )
@( param, "one, two, three or four data points" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
pragma software_model( shell_script );
with separate "config/contributors.inc.sp";
with separate "lib/world.inc.sp";
with separate "config/config.inc.sp";
procedure graph is
graph_name : string;
num_series : natural;
type data_arrays is array(1..30) of float;
data_file : file_type;
data_array1 : data_arrays;
data_array2 : data_arrays;
data_array3 : data_arrays;
data_array4 : data_arrays;
data_point : universal_numeric;
data_file_name : string;
canvas : pen.canvas_id;
-- For Y-axis scale, 1 = 0..100, 2 = 0..200, etc.
base_y_axis_scale : constant float := 8; -- was 3
y_axis_scale : float := base_y_axis_scale;
candidate_scale : float;
convert : limited command := "/usr/bin/convert";
-- TO X
--
-- Constrain the given horizontal coordinate to the graph.
----------------------------------------------------------------------------
function to_x( f : float ) return pen.coordinate is
x : pen.coordinate;
begin
x := pen.coordinate( f );
if x < 2 then
x := 2;
elsif x > 98 then
x := 98;
end if;
return x;
end to_x;
-- TO Y
--
-- Constrain the given vertical coordinate to the graph. Also flip
-- origin to bottom of display.
----------------------------------------------------------------------------
function to_y( f : float ) return pen.coordinate is
y : pen.coordinate;
begin
y := pen.coordinate( 100 - f );
if y < 2 then
y := 2;
elsif y > 98 then
y := 98;
end if;
return y;
end to_y;
-- DRAW GRAPH BACKGROUND
--
-- Draw the graph background and borders
----------------------------------------------------------------------------
procedure draw_graph_background is
x : pen.coordinate;
begin
pen.wait_to_reveal( canvas );
pen.set_pen_ink(canvas, 20, 20, 20);
for i in arrays.first(data_array1)..arrays.last(data_array1) loop
data_point := natural(i) - natural(arrays.first(data_array1));
data_point := 1+(100 * data_point) / float(arrays.length( data_array1) );
x := pen.coordinate( data_point );
pen.vline( canvas, x, 1, 99 );
end loop;
for i in 1..9 loop
pen.hline( canvas, 1, 99, i*10 );
end loop;
pen.set_pen_ink(canvas, pen_color_name.white );
pen.hline(canvas,1,99,1);
pen.hline(canvas,1,99,99);
pen.vline(canvas,1,1,99);
pen.vline(canvas,99,1,99);
pen.reveal( canvas );
end draw_graph_background;
-- DRAW SERIES
--
-- Plot a series of points on the graph in the given colours.
----------------------------------------------------------------------------
procedure draw_series( series : in out data_arrays; scale : float;
point_color : pen.color_name; line_color : pen.color_name ) is
x : pen.coordinate;
y : pen.coordinate;
begin
pen.wait_to_reveal( canvas );
pen.set_pen_ink(canvas, point_color);
for i in arrays.first(series)..arrays.last(series) loop
y := to_y( series(i) / scale );
data_point := natural(i) - natural(arrays.first(series));
data_point := 1+(100 * data_point) / float(arrays.length(series) );
x := to_x( data_point );
pen.move_to(canvas, x, y-0.5 );
pen.line_to(canvas, x+0.5, y)
@(canvas, x, y+0.5)
@(canvas, x-0.5, y)
@(canvas, x, y-0.5);
end loop;
pen.set_pen_ink(canvas, line_color);
for i in arrays.first(series)..arrays.last(series) loop
y := to_y( series(i) / scale );
data_point := natural(i) - natural(arrays.first(series));
data_point := 1+(100 * data_point) / float(arrays.length( series ) );
x := to_x( data_point );
if i = 1 then
pen.move_to( canvas, x, y );
end if;
pen.line_to( canvas, x, y );
end loop;
pen.reveal( canvas );
end draw_series;
-- USAGE
--
-- Show the help based on the pragma annotate documentation
----------------------------------------------------------------------------
procedure usage is
begin
help( source_info.enclosing_entity );
end usage;
begin
-- parameter handling
if $# < 2 then
put_line( "At least two parameters are required" );
command_line.set_exit_status(192);
return;
end if;
if $1 = "-h" or $1 = "--help" then
usage;
command_line.set_exit_status(192);
return;
end if;
graph_name := strings.trim( $1 );
if graph_name = "" then
put_line( "the graph name is empty" );
command_line.set_exit_status(192);
return;
end if;
num_series := $# - 1;
if num_series > 4 then
put_line( "the maximum number of series is 4" );
command_line.set_exit_status(192);
return;
end if;
-- read the history
data_file_name := graph_name & ".dat";
candidate_scale := candidate_scale;
if files.exists( "data/" & data_file_name ) then
open( data_file, in_file, "data/" & data_file_name );
for i in arrays.first(data_array1)..arrays.last(data_array1) loop
data_point := numerics.value( get_line( data_file ));
data_array1(i) := data_point;
candidate_scale := numerics.ceiling( data_point / 100 );
if candidate_scale > y_axis_scale then
y_axis_scale := candidate_scale;
end if;
data_point := numerics.value( get_line( data_file ));
data_array2(i) := data_point;
candidate_scale := numerics.ceiling( data_point / 100 );
if candidate_scale > y_axis_scale then
y_axis_scale := candidate_scale;
end if;
data_point := numerics.value( get_line( data_file ));
data_array3(i) := data_point;
candidate_scale := numerics.ceiling( data_point / 100 );
if candidate_scale > y_axis_scale then
y_axis_scale := candidate_scale;
end if;
data_point := numerics.value( get_line( data_file ));
data_array4(i) := data_point;
candidate_scale := numerics.ceiling( data_point / 100 );
if candidate_scale > y_axis_scale then
y_axis_scale := candidate_scale;
end if;
end loop;
close( data_file );
else
for i in arrays.first(data_array1)..arrays.last(data_array1) loop
data_array1(i) := 0;
data_array2(i) := 0;
data_array3(i) := 0;
data_array4(i) := 0;
end loop;
end if;
-- Record the new data points in the series
arrays.shift_left( data_array1 );
data_point := natural( numerics.value( $2 ));
data_array1( arrays.last( data_array1 ) ) := data_point;
if num_series > 1 then
arrays.shift_left( data_array2 );
data_point := natural( numerics.value( $3 ));
data_array2( arrays.last( data_array2 ) ) := data_point;
end if;
if num_series > 2 then
arrays.shift_left( data_array3 );
data_point := natural( numerics.value( $4 ));
data_array3( arrays.last( data_array3 ) ) := data_point;
end if;
if num_series > 3 then
arrays.shift_left( data_array4 );
data_point := natural( numerics.value( $5 ));
data_array4( arrays.last( data_array4 ) ) := data_point;
end if;
-- Note that off-screen canvases are broken. We will need a frame buffer.
begin
pen.new_window_canvas(512, 512, 32, canvas );
exception when others =>
put_line( "Failed to create new canvas" );
raise; -- return;
end;
-- Draw the graph
draw_graph_background;
draw_series( data_array1, y_axis_scale, pen_color_name.darkred, pen_color_name.red );
if num_series > 1 then
draw_series( data_array2, y_axis_scale, pen_color_name.darkgreen, pen_color_name.green );
end if;
if num_series > 2 then
draw_series( data_array3, y_axis_scale, pen_color_name.darkblue, pen_color_name.blue );
end if;
if num_series > 3 then
draw_series( data_array4, y_axis_scale, pen_color_name.darkblue, pen_color_name.yellow );
end if;
-- Save the graph
pen.save_canvas( graph_name & ".bmp", canvas );
pen.close_canvas( canvas );
-- This depends on imagemagick convert being installed
convert( graph_name & ".bmp", graph_name & ".png" );
mv( graph_name & ".png", string( dashboard_path ) & "/" );
chown( dashboard_file_owner & ":" & dashboard_file_group, string( dashboard_path ) & "/" & graph_name & ".png" );
chmod( "750", string( dashboard_path ) & "/" & graph_name & ".png" );
rm( graph_name & ".bmp" );
-- Save the history data
create( data_file, out_file, "data/" & data_file_name );
for i in arrays.first(data_array1)..arrays.last(data_array1) loop
put_line( data_file, strings.image( data_array1(i) ) )
@( data_file, strings.image( data_array2(i) ) )
@( data_file, strings.image( data_array3(i) ) )
@( data_file, strings.image( data_array4(i) ) );
end loop;
close( data_file );
end graph;
-- VIM editor formatting instructions
-- vim: ft=spar