Annotation of loncom/xml/lonplot.pm, revision 1.42
1.1 matthew 1: # The LearningOnline Network with CAPA
2: # Dynamic plot
3: #
1.42 ! matthew 4: # $Id: lonplot.pm,v 1.41 2002/01/21 15:20:28 matthew Exp $
1.1 matthew 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.3 matthew 28: # 12/15/01 Matthew
1.31 matthew 29: # 12/17 12/18 12/19 12/20 12/21 12/27 12/28 12/30 12/31 Matthew
30: # 01/01/02 Matthew
1.35 matthew 31: # 01/02 01/03 01/04 01/07 01/08 01/09 Matthew
1.41 matthew 32: # 01/21 Matthew
1.35 matthew 33:
1.1 matthew 34: package Apache::lonplot;
1.10 matthew 35:
1.1 matthew 36: use strict;
1.10 matthew 37: use Apache::File;
1.1 matthew 38: use Apache::response;
1.2 matthew 39: use Apache::lonxml;
1.20 matthew 40: use Apache::edit;
1.10 matthew 41:
1.33 harris41 42: BEGIN {
1.1 matthew 43: &Apache::lonxml::register('Apache::lonplot',('plot'));
44: }
45:
1.10 matthew 46: ##
47: ## Description of data structures:
48: ##
49: ## %plot %key %axis
50: ## --------------------------
51: ## height title color
52: ## width box xmin
53: ## bgcolor pos xmax
54: ## fgcolor ymin
55: ## transparent ymax
56: ## grid
57: ## border
58: ## font
1.19 matthew 59: ## align
1.10 matthew 60: ##
61: ## @labels: $labels[$i] = \%label
62: ## %label: text, xpos, ypos, justify
1.14 matthew 63: ##
1.10 matthew 64: ## @curves: $curves[$i] = \%curve
1.14 matthew 65: ## %curve: name, linestyle, ( function | data )
1.10 matthew 66: ##
67: ## $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
68: ## [y1,y2,y3,y4] ]
69: ##
1.21 matthew 70:
71: ###################################################################
72: ## ##
73: ## Tests used in checking the validitity of input ##
74: ## ##
75: ###################################################################
1.29 matthew 76:
1.32 matthew 77: my $max_str_len = 50; # if a label, title, xlabel, or ylabel text
78: # is longer than this, it will be truncated.
79:
1.29 matthew 80: my %linestyles =
81: (
82: lines => 2, # Maybe this will be used in the future
83: linespoints => 2, # to check on whether or not they have
84: dots => 2, # supplied enough <data></data> fields
85: points => 2, # to use the given line style. But for
86: steps => 2, # now there are more important things
87: fsteps => 2, # for me to deal with.
88: histeps => 2,
1.34 matthew 89: errorbars => 3,
90: xerrorbars => [3,4],
91: yerrorbars => [3,4],
1.35 matthew 92: xyerrorbars => [4,6],
1.34 matthew 93: boxes => 3,
1.35 matthew 94: # boxerrorbars => [3,4,5],
95: # boxxyerrorbars => [4,6,7],
96: # financebars => 5,
97: # candlesticks => 5,
98: vector => 4
1.29 matthew 99: );
100:
1.11 matthew 101: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19 matthew 102: my $real_test =
103: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.11 matthew 104: my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
1.1 matthew 105: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
1.15 matthew 106: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1 matthew 107: my $sml_test = sub {$_[0]=~/^(small|medium|large)$/};
1.29 matthew 108: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.15 matthew 109: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
1.21 matthew 110:
111: ###################################################################
112: ## ##
113: ## Attribute metadata ##
114: ## ##
115: ###################################################################
1.34 matthew 116: my @plot_edit_order =
1.37 matthew 117: qw/bgcolor fgcolor height width font transparent grid border align/;
1.1 matthew 118: my %plot_defaults =
119: (
1.20 matthew 120: height => {
121: default => 200,
122: test => $int_test,
1.29 matthew 123: description => 'height of image (pixels)',
1.38 matthew 124: edit_type => 'entry',
125: size => '10'
1.20 matthew 126: },
127: width => {
128: default => 200,
129: test => $int_test,
1.29 matthew 130: description => 'width of image (pixels)',
1.38 matthew 131: edit_type => 'entry',
132: size => '10'
1.20 matthew 133: },
134: bgcolor => {
135: default => 'xffffff',
136: test => $color_test,
137: description => 'background color of image (xffffff)',
1.38 matthew 138: edit_type => 'entry',
139: size => '10'
1.20 matthew 140: },
141: fgcolor => {
142: default => 'x000000',
143: test => $color_test,
144: description => 'foreground color of image (x000000)',
1.38 matthew 145: edit_type => 'entry',
146: size => '10'
1.20 matthew 147: },
148: transparent => {
149: default => 'off',
150: test => $onoff_test,
1.34 matthew 151: description => 'Transparent image',
1.20 matthew 152: edit_type => 'on_off'
153: },
154: grid => {
155: default => 'off',
156: test => $onoff_test,
1.34 matthew 157: description => 'Display grid',
1.20 matthew 158: edit_type => 'on_off'
159: },
160: border => {
161: default => 'on',
162: test => $onoff_test,
1.34 matthew 163: description => 'Draw border around plot',
1.20 matthew 164: edit_type => 'on_off'
165: },
166: font => {
167: default => 'medium',
168: test => $sml_test,
169: description => 'Size of font to use',
170: edit_type => 'choice',
171: choices => ['small','medium','large']
172: },
173: align => {
174: default => 'left',
175: test => sub {$_[0]=~/^(left|right|center)$/},
176: description => 'alignment for image in html',
177: edit_type => 'choice',
178: choices => ['left','right','center']
179: }
1.1 matthew 180: );
181:
182: my %key_defaults =
183: (
1.20 matthew 184: title => {
185: default => '',
186: test => $words_test,
187: description => 'Title of key',
1.38 matthew 188: edit_type => 'entry',
189: size => '40'
1.20 matthew 190: },
191: box => {
192: default => 'off',
193: test => $onoff_test,
194: description => 'Draw a box around the key?',
195: edit_type => 'on_off'
196: },
197: pos => {
198: default => 'top right',
199: test => $key_pos_test,
200: description => 'position of the key on the plot',
201: edit_type => 'choice',
202: choices => ['top left','top right','bottom left','bottom right',
203: 'outside','below']
204: }
1.1 matthew 205: );
206:
207: my %label_defaults =
208: (
1.20 matthew 209: xpos => {
210: default => 0,
211: test => $real_test,
212: description => 'x position of label (graph coordinates)',
1.38 matthew 213: edit_type => 'entry',
214: size => '10'
1.20 matthew 215: },
216: ypos => {
217: default => 0,
218: test => $real_test,
219: description => 'y position of label (graph coordinates)',
1.38 matthew 220: edit_type => 'entry',
221: size => '10'
1.20 matthew 222: },
223: justify => {
224: default => 'left',
225: test => sub {$_[0]=~/^(left|right|center)$/},
226: description => 'justification of the label text on the plot',
227: edit_type => 'choice',
228: choices => ['left','right','center']
229: }
1.1 matthew 230: );
231:
232: my %axis_defaults =
233: (
1.28 matthew 234: color => {
1.20 matthew 235: default => 'x000000',
236: test => $color_test,
237: description => 'color of axes (x000000)',
1.38 matthew 238: edit_type => 'entry',
239: size => '10'
1.20 matthew 240: },
241: xmin => {
242: default => '-10.0',
243: test => $real_test,
244: description => 'minimum x-value shown in plot',
1.38 matthew 245: edit_type => 'entry',
246: size => '10'
1.20 matthew 247: },
248: xmax => {
249: default => ' 10.0',
250: test => $real_test,
251: description => 'maximum x-value shown in plot',
1.38 matthew 252: edit_type => 'entry',
253: size => '10'
1.20 matthew 254: },
255: ymin => {
256: default => '-10.0',
257: test => $real_test,
258: description => 'minimum y-value shown in plot',
1.38 matthew 259: edit_type => 'entry',
260: size => '10'
1.20 matthew 261: },
262: ymax => {
263: default => ' 10.0',
264: test => $real_test,
265: description => 'maximum y-value shown in plot',
1.38 matthew 266: edit_type => 'entry',
267: size => '10'
1.20 matthew 268: }
1.1 matthew 269: );
270:
271: my %curve_defaults =
272: (
1.20 matthew 273: color => {
274: default => 'x000000',
275: test => $color_test,
276: description => 'color of curve (x000000)',
1.38 matthew 277: edit_type => 'entry',
278: size => '10'
1.20 matthew 279: },
280: name => {
281: default => '',
282: test => $words_test,
283: description => 'name of curve to appear in key',
1.38 matthew 284: edit_type => 'entry',
285: size => '20'
1.20 matthew 286: },
287: linestyle => {
288: default => 'lines',
289: test => $linestyle_test,
1.35 matthew 290: description => 'Line style',
1.20 matthew 291: edit_type => 'choice',
1.38 matthew 292: choices => [keys(%linestyles)]
1.20 matthew 293: }
1.1 matthew 294: );
295:
1.21 matthew 296: ###################################################################
297: ## ##
298: ## parsing and edit rendering ##
299: ## ##
300: ###################################################################
1.1 matthew 301: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
302:
303: sub start_plot {
1.23 matthew 304: %plot = (); %key = (); %axis = ();
1.10 matthew 305: $title = undef; $xlabel = undef; $ylabel = undef;
306: $#labels = -1; $#curves = -1;
1.6 matthew 307: #
1.1 matthew 308: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
309: my $result='';
1.25 matthew 310: &Apache::lonxml::register('Apache::lonplot',
311: ('title','xlabel','ylabel','key','axis','label','curve'));
1.29 matthew 312: push (@Apache::lonxml::namespace,'lonplot');
1.4 matthew 313: if ($target eq 'web') {
1.29 matthew 314: my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
1.17 matthew 315: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
1.29 matthew 316: &Apache::lonxml::newparser($parser,\$inside);
1.17 matthew 317: &get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
318: $tagstack->[-1]);
1.20 matthew 319: } elsif ($target eq 'edit') {
1.25 matthew 320: $result .= &Apache::edit::tag_start($target,$token,'Plot');
1.34 matthew 321: $result .= &edit_attributes($target,$token,\%plot_defaults,
322: \@plot_edit_order);
1.20 matthew 323: } elsif ($target eq 'modified') {
324: my $constructtag=&Apache::edit::get_new_args
1.24 matthew 325: ($token,$parstack,$safeeval,keys(%plot_defaults));
1.20 matthew 326: if ($constructtag) {
327: $result = &Apache::edit::rebuild_tag($token);
328: }
1.4 matthew 329: }
1.21 matthew 330: return $result;
1.1 matthew 331: }
332:
333: sub end_plot {
334: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25 matthew 335:
1.1 matthew 336: pop @Apache::lonxml::namespace;
1.4 matthew 337: &Apache::lonxml::deregister('Apache::lonplot',
338: ('title','xlabel','ylabel','key','axis','label','curve'));
339: my $result = '';
340: if ($target eq 'web') {
1.21 matthew 341: &check_inputs(); # Make sure we have all the data we need
1.13 matthew 342: ##
343: ## Determine filename
1.4 matthew 344: my $tmpdir = '/home/httpd/perl/tmp/';
1.12 matthew 345: my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.29 matthew 346: '_'.time.'_'.$$.int(rand(1000)).'_plot.data';
1.4 matthew 347: ## Write the plot description to the file
1.40 matthew 348: &write_gnuplot_file($tmpdir,$filename);
1.4 matthew 349: ## return image tag for the plot
1.12 matthew 350: $result .= <<"ENDIMAGE";
1.16 matthew 351: <img src = "/cgi-bin/plot.gif?$filename"
352: width = "$plot{'width'}"
353: height = "$plot{'height'}"
354: align = "$plot{'align'}"
355: alt = "/cgi-bin/plot.gif?$filename" />
1.12 matthew 356: ENDIMAGE
1.20 matthew 357: } elsif ($target eq 'edit') {
1.21 matthew 358: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 359: }
1.1 matthew 360: return $result;
361: }
1.2 matthew 362:
1.1 matthew 363: ##----------------------------------------------------------------- key
364: sub start_key {
365: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
366: my $result='';
1.17 matthew 367: if ($target eq 'web') {
368: &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11 matthew 369: $tagstack->[-1]);
1.20 matthew 370: } elsif ($target eq 'edit') {
1.25 matthew 371: $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21 matthew 372: $result .= &edit_attributes($target,$token,\%key_defaults);
1.20 matthew 373: } elsif ($target eq 'modified') {
374: my $constructtag=&Apache::edit::get_new_args
1.24 matthew 375: ($token,$parstack,$safeeval,keys(%key_defaults));
1.20 matthew 376: if ($constructtag) {
377: $result = &Apache::edit::rebuild_tag($token);
378: }
1.4 matthew 379: }
1.1 matthew 380: return $result;
381: }
382:
383: sub end_key {
384: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
385: my $result = '';
1.4 matthew 386: if ($target eq 'web') {
1.20 matthew 387: } elsif ($target eq 'edit') {
1.21 matthew 388: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 389: }
1.1 matthew 390: return $result;
391: }
1.21 matthew 392:
1.1 matthew 393: ##------------------------------------------------------------------- title
394: sub start_title {
395: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
396: my $result='';
1.4 matthew 397: if ($target eq 'web') {
1.17 matthew 398: $title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.32 matthew 399: if (length($title) > $max_str_len) {
400: $title = substr($title,0,$max_str_len);
401: }
1.20 matthew 402: } elsif ($target eq 'edit') {
1.25 matthew 403: $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.22 matthew 404: my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.39 matthew 405: $result.=&Apache::edit::end_row().
406: &Apache::edit::start_spanning_row().
1.30 matthew 407: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 408: } elsif ($target eq 'modified') {
1.29 matthew 409: my $text=$$parser[-1]->get_text("/title");
1.42 ! matthew 410: $result.=&Apache::edit::rebuild_tag($token);
1.21 matthew 411: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 412: }
1.1 matthew 413: return $result;
414: }
415:
416: sub end_title {
417: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
418: my $result = '';
1.4 matthew 419: if ($target eq 'web') {
1.20 matthew 420: } elsif ($target eq 'edit') {
1.27 matthew 421: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 422: }
1.1 matthew 423: return $result;
424: }
425: ##------------------------------------------------------------------- xlabel
426: sub start_xlabel {
427: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
428: my $result='';
1.4 matthew 429: if ($target eq 'web') {
1.17 matthew 430: $xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.32 matthew 431: if (length($xlabel) > $max_str_len) {
432: $xlabel = substr($xlabel,0,$max_str_len);
433: }
1.20 matthew 434: } elsif ($target eq 'edit') {
1.25 matthew 435: $result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.22 matthew 436: my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.39 matthew 437: $result.=&Apache::edit::end_row().
438: &Apache::edit::start_spanning_row().
1.30 matthew 439: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 440: } elsif ($target eq 'modified') {
1.29 matthew 441: my $text=$$parser[-1]->get_text("/xlabel");
1.42 ! matthew 442: $result.=&Apache::edit::rebuild_tag($token);
1.21 matthew 443: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 444: }
1.1 matthew 445: return $result;
446: }
447:
448: sub end_xlabel {
449: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
450: my $result = '';
1.4 matthew 451: if ($target eq 'web') {
1.20 matthew 452: } elsif ($target eq 'edit') {
1.27 matthew 453: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 454: }
1.1 matthew 455: return $result;
456: }
1.21 matthew 457:
1.1 matthew 458: ##------------------------------------------------------------------- ylabel
459: sub start_ylabel {
460: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
461: my $result='';
1.4 matthew 462: if ($target eq 'web') {
1.17 matthew 463: $ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.32 matthew 464: if (length($ylabel) > $max_str_len) {
465: $ylabel = substr($ylabel,0,$max_str_len);
466: }
1.20 matthew 467: } elsif ($target eq 'edit') {
1.25 matthew 468: $result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.22 matthew 469: my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.39 matthew 470: $result .= &Apache::edit::end_row().
471: &Apache::edit::start_spanning_row().
1.30 matthew 472: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 473: } elsif ($target eq 'modified') {
1.29 matthew 474: my $text=$$parser[-1]->get_text("/ylabel");
1.42 ! matthew 475: $result.=&Apache::edit::rebuild_tag($token);
1.21 matthew 476: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 477: }
1.1 matthew 478: return $result;
479: }
480:
481: sub end_ylabel {
482: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
483: my $result = '';
1.4 matthew 484: if ($target eq 'web') {
1.20 matthew 485: } elsif ($target eq 'edit') {
1.27 matthew 486: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 487: }
1.1 matthew 488: return $result;
489: }
1.21 matthew 490:
1.1 matthew 491: ##------------------------------------------------------------------- label
492: sub start_label {
493: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
494: my $result='';
1.17 matthew 495: if ($target eq 'web') {
496: my %label;
497: &get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11 matthew 498: $tagstack->[-1]);
1.32 matthew 499: my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
500: $text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
501: $label{'text'} = $text;
1.17 matthew 502: push(@labels,\%label);
1.20 matthew 503: } elsif ($target eq 'edit') {
1.25 matthew 504: $result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21 matthew 505: $result .= &edit_attributes($target,$token,\%label_defaults);
1.22 matthew 506: my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
1.39 matthew 507: $result .= &Apache::edit::end_row().
508: &Apache::edit::start_spanning_row().
1.30 matthew 509: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 510: } elsif ($target eq 'modified') {
1.42 ! matthew 511: &Apache::edit::get_new_args
1.24 matthew 512: ($token,$parstack,$safeeval,keys(%label_defaults));
1.42 ! matthew 513: $result.=&Apache::edit::rebuild_tag($token);
1.22 matthew 514: my $text=$$parser[-1]->get_text("/label");
1.21 matthew 515: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 516: }
1.1 matthew 517: return $result;
518: }
519:
520: sub end_label {
521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
522: my $result = '';
1.4 matthew 523: if ($target eq 'web') {
1.20 matthew 524: } elsif ($target eq 'edit') {
1.21 matthew 525: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 526: }
1.1 matthew 527: return $result;
528: }
529:
530: ##------------------------------------------------------------------- curve
531: sub start_curve {
532: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
533: my $result='';
1.25 matthew 534: &Apache::lonxml::register('Apache::lonplot',('function','data'));
535: push (@Apache::lonxml::namespace,'curve');
1.17 matthew 536: if ($target eq 'web') {
537: my %curve;
538: &get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11 matthew 539: $tagstack->[-1]);
1.17 matthew 540: push (@curves,\%curve);
1.20 matthew 541: } elsif ($target eq 'edit') {
1.26 matthew 542: $result .= &Apache::edit::tag_start($target,$token,'Curve');
1.21 matthew 543: $result .= &edit_attributes($target,$token,\%curve_defaults);
1.20 matthew 544: } elsif ($target eq 'modified') {
545: my $constructtag=&Apache::edit::get_new_args
1.35 matthew 546: ($token,$parstack,$safeeval,keys(%curve_defaults));
1.20 matthew 547: if ($constructtag) {
548: $result = &Apache::edit::rebuild_tag($token);
549: $result.= &Apache::edit::handle_insert();
550: }
1.4 matthew 551: }
1.1 matthew 552: return $result;
553: }
554:
555: sub end_curve {
556: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
557: my $result = '';
1.25 matthew 558: pop @Apache::lonxml::namespace;
559: &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.4 matthew 560: if ($target eq 'web') {
1.20 matthew 561: } elsif ($target eq 'edit') {
1.21 matthew 562: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 563: }
1.1 matthew 564: return $result;
565: }
1.21 matthew 566:
1.1 matthew 567: ##------------------------------------------------------------ curve function
568: sub start_function {
569: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
570: my $result='';
1.4 matthew 571: if ($target eq 'web') {
1.17 matthew 572: if (exists($curves[-1]->{'data'})) {
573: &Apache::lonxml::warning('Use of <function> precludes use of <data>. The <data> will be omitted in favor of the <function> declaration.');
574: delete $curves[-1]->{'data'} ;
575: }
576: $curves[-1]->{'function'} =
577: &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.20 matthew 578: } elsif ($target eq 'edit') {
1.37 matthew 579: $result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1.22 matthew 580: my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.39 matthew 581: $result .= &Apache::edit::end_row().
582: &Apache::edit::start_spanning_row().
1.30 matthew 583: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 584: } elsif ($target eq 'modified') {
1.42 ! matthew 585: $result.=&Apache::edit::rebuild_tag($token);
1.20 matthew 586: my $text=$$parser[-1]->get_text("/function");
587: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 588: }
1.1 matthew 589: return $result;
590: }
591:
592: sub end_function {
593: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
594: my $result = '';
1.4 matthew 595: if ($target eq 'web') {
1.20 matthew 596: } elsif ($target eq 'edit') {
1.26 matthew 597: $result .= &Apache::edit::end_table();
1.4 matthew 598: }
1.1 matthew 599: return $result;
600: }
1.21 matthew 601:
1.1 matthew 602: ##------------------------------------------------------------ curve data
603: sub start_data {
604: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
605: my $result='';
1.4 matthew 606: if ($target eq 'web') {
1.17 matthew 607: if (exists($curves[-1]->{'function'})) {
608: &Apache::lonxml::warning('Use of <data> precludes use of .'.
609: '<function>. The <function> will be omitted in favor of '.
610: 'the <data> declaration.');
611: delete($curves[-1]->{'function'});
612: }
613: my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
1.40 matthew 614: # Deal with cases where we're given an array...
615: if ($datatext =~ /^\@/) {
616: $datatext = &Apache::run::run('return "'.$datatext.'"',
617: $safeeval,1);
618: }
1.17 matthew 619: $datatext =~ s/\s+/ /g;
620: # Need to do some error checking on the @data array -
621: # make sure it's all numbers and make sure each array
622: # is of the same length.
623: my @data;
1.35 matthew 624: if ($datatext =~ /,/) { # comma deliminated
1.17 matthew 625: @data = split /,/,$datatext;
626: } else { # Assume it's space seperated.
627: @data = split / /,$datatext;
628: }
629: for (my $i=0;$i<=$#data;$i++) {
630: # Check that it's non-empty
1.19 matthew 631: if (! defined($data[$i])) {
632: &Apache::lonxml::warning(
633: 'undefined <data> value. Replacing with '.
634: ' pi/e = 1.15572734979092');
635: $data[$i] = 1.15572734979092;
636: }
1.17 matthew 637: # Check that it's a number
1.19 matthew 638: if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
639: &Apache::lonxml::warning(
640: 'Bad <data> value of '.$data[$i].' Replacing with '.
641: ' pi/e = 1.15572734979092');
642: $data[$i] = 1.15572734979092;
643: }
1.17 matthew 644: }
1.35 matthew 645: # complain if the number of data points is not the same as
646: # in previous sets of data.
1.36 matthew 647: if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
1.35 matthew 648: &Apache::lonxml::warning
649: ('Number of data points is not consistent with previous '.
650: 'number of data points');
651: }
1.17 matthew 652: push @{$curves[-1]->{'data'}},\@data;
1.20 matthew 653: } elsif ($target eq 'edit') {
1.37 matthew 654: $result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1.22 matthew 655: my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
1.39 matthew 656: $result .= &Apache::edit::end_row().
657: &Apache::edit::start_spanning_row().
1.30 matthew 658: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 659: } elsif ($target eq 'modified') {
1.42 ! matthew 660: $result.=&Apache::edit::rebuild_tag($token);
1.21 matthew 661: my $text=$$parser[-1]->get_text("/data");
662: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 663: }
1.1 matthew 664: return $result;
665: }
666:
667: sub end_data {
668: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
669: my $result = '';
1.4 matthew 670: if ($target eq 'web') {
1.20 matthew 671: } elsif ($target eq 'edit') {
1.26 matthew 672: $result .= &Apache::edit::end_table();
1.4 matthew 673: }
1.1 matthew 674: return $result;
675: }
676:
677: ##------------------------------------------------------------------- axis
678: sub start_axis {
679: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
680: my $result='';
1.4 matthew 681: if ($target eq 'web') {
1.17 matthew 682: &get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
683: $tagstack->[-1]);
1.20 matthew 684: } elsif ($target eq 'edit') {
1.25 matthew 685: $result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.21 matthew 686: $result .= &edit_attributes($target,$token,\%axis_defaults);
1.20 matthew 687: } elsif ($target eq 'modified') {
1.29 matthew 688: my $constructtag=&Apache::edit::get_new_args
689: ($token,$parstack,$safeeval,keys(%axis_defaults));
690: if ($constructtag) {
691: $result = &Apache::edit::rebuild_tag($token);
692: }
1.4 matthew 693: }
1.1 matthew 694: return $result;
695: }
696:
697: sub end_axis {
698: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
699: my $result = '';
1.4 matthew 700: if ($target eq 'web') {
1.20 matthew 701: } elsif ($target eq 'edit') {
1.21 matthew 702: $result.=&Apache::edit::tag_end($target,$token);
1.20 matthew 703: } elsif ($target eq 'modified') {
1.4 matthew 704: }
1.1 matthew 705: return $result;
706: }
707:
1.21 matthew 708: ###################################################################
709: ## ##
710: ## Utility Functions ##
711: ## ##
712: ###################################################################
713:
1.13 matthew 714: ##----------------------------------------------------------- set_defaults
715: sub set_defaults {
1.21 matthew 716: my ($var,$defaults) = @_;
1.13 matthew 717: my $key;
1.24 matthew 718: foreach $key (keys(%$defaults)) {
1.13 matthew 719: $var->{$key} = $defaults->{$key}->{'default'};
720: }
721: }
722:
1.1 matthew 723: ##------------------------------------------------------------------- misc
1.2 matthew 724: sub get_attributes{
1.21 matthew 725: my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24 matthew 726: foreach my $attr (keys(%{$defaults})) {
1.10 matthew 727: $values->{$attr} =
1.15 matthew 728: &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10 matthew 729: if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11 matthew 730: $values->{$attr} = $defaults->{$attr}->{'default'};
1.6 matthew 731: next;
732: }
1.10 matthew 733: my $test = $defaults->{$attr}->{'test'};
734: if (! &$test($values->{$attr})) {
1.6 matthew 735: &Apache::lonxml::warning
736: ($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11 matthew 737: .$defaults->{$attr}->{'default'} );
738: $values->{$attr} = $defaults->{$attr}->{'default'};
1.10 matthew 739: }
1.2 matthew 740: }
1.11 matthew 741: return ;
1.6 matthew 742: }
1.40 matthew 743:
1.15 matthew 744: ##------------------------------------------------------- write_gnuplot_file
1.6 matthew 745: sub write_gnuplot_file {
1.40 matthew 746: my ($tmpdir,$filename)= @_;
1.6 matthew 747: my $gnuplot_input = '';
1.10 matthew 748: my $curve;
1.6 matthew 749: # Collect all the colors
750: my @Colors;
751: push @Colors, $plot{'bgcolor'};
752: push @Colors, $plot{'fgcolor'};
1.13 matthew 753: push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9 matthew 754: foreach $curve (@curves) {
755: push @Colors, ($curve->{'color'} ne '' ?
756: $curve->{'color'} :
1.13 matthew 757: $plot{'fgcolor'} );
1.6 matthew 758: }
759: # set term
760: $gnuplot_input .= 'set term gif ';
761: $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
762: $gnuplot_input .= $plot{'font'} . ' ';
1.10 matthew 763: $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
1.6 matthew 764: $gnuplot_input .= "@Colors\n";
1.7 matthew 765: # grid
1.10 matthew 766: $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7 matthew 767: # border
1.9 matthew 768: $gnuplot_input .= ($plot{'border'} eq 'on'?
769: 'set border'.$/ :
770: 'set noborder'.$/ ); # title, xlabel, ylabel
1.13 matthew 771: $gnuplot_input .= "set output\n";
772: $gnuplot_input .= "set title \"$title\"\n" if (defined($title)) ;
773: $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
774: $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
1.23 matthew 775: if (%axis) {
1.13 matthew 776: $gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
777: $gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6 matthew 778: }
779: # Key
1.23 matthew 780: if (%key) {
1.9 matthew 781: $gnuplot_input .= 'set key '.$key{'pos'}.' ';
782: if ($key{'title'} ne '') {
1.11 matthew 783: $gnuplot_input .= 'title "'.$key{'title'}.'" ';
784: }
785: $gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6 matthew 786: } else {
1.9 matthew 787: $gnuplot_input .= 'set nokey'.$/;
1.13 matthew 788: }
1.6 matthew 789: # labels
1.10 matthew 790: my $label;
1.6 matthew 791: foreach $label (@labels) {
792: $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9 matthew 793: $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6 matthew 794: }
795: # curves
796: $gnuplot_input .= 'plot ';
1.9 matthew 797: for (my $i = 0;$i<=$#curves;$i++) {
798: $curve = $curves[$i];
799: $gnuplot_input.= ', ' if ($i > 0);
1.6 matthew 800: if (exists($curve->{'function'})) {
1.9 matthew 801: $gnuplot_input.=
802: $curve->{'function'}.' title "'.
803: $curve->{'name'}.'" with '.
804: $curve->{'linestyle'};
1.6 matthew 805: } elsif (exists($curve->{'data'})) {
1.40 matthew 806: # Store data values in $datatext
807: my $datatext = '';
808: # get new filename
809: my $datafilename = "$tmpdir/$filename.$i";
810: my $fh=Apache::File->new(">$datafilename");
811: # Compile data
1.6 matthew 812: my @Data = @{$curve->{'data'}};
1.9 matthew 813: my @Data0 = @{$Data[0]};
814: for (my $i =0; $i<=$#Data0; $i++) {
1.10 matthew 815: my $dataset;
1.6 matthew 816: foreach $dataset (@Data) {
1.9 matthew 817: $datatext .= $dataset->[$i] . ' ';
1.6 matthew 818: }
1.9 matthew 819: $datatext .= $/;
1.6 matthew 820: }
1.40 matthew 821: # write file
822: print $fh $datatext;
823: close ($fh);
824: # generate gnuplot text
825: $gnuplot_input.= '"'.$datafilename.'" title "'.
826: $curve->{'name'}.'" with '.
827: $curve->{'linestyle'};
1.6 matthew 828: }
829: }
1.40 matthew 830: # Write the output to a file.
831: my $fh=Apache::File->new(">$tmpdir$filename");
832: print $fh $gnuplot_input;
833: close($fh);
834: # That's all folks.
835: return ;
1.2 matthew 836: }
1.21 matthew 837:
838: #---------------------------------------------- check_inputs
839: sub check_inputs {
840: ## Note: no inputs, no outputs - this acts only on global variables.
841: ## Make sure we have all the input we need:
1.23 matthew 842: if (! %plot) { &set_defaults(\%plot,\%plot_defaults); }
843: if (! %key ) {} # No key for this plot, thats okay
1.34 matthew 844: # if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21 matthew 845: if (! defined($title )) {} # No title for this plot, thats okay
846: if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
847: if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
848: if ($#labels < 0) { } # No labels for this plot, thats okay
849: if ($#curves < 0) {
850: &Apache::lonxml::warning("No curves specified for plot!!!!");
851: return '';
852: }
853: my $curve;
854: foreach $curve (@curves) {
855: if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
856: &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
857: return '';
858: }
859: }
860: }
861:
1.20 matthew 862: #------------------------------------------------ make_edit
863: sub edit_attributes {
1.34 matthew 864: my ($target,$token,$defaults,$keys) = @_;
865: my ($result,@keys);
866: if ($keys && ref($keys) eq 'ARRAY') {
867: @keys = @$keys;
868: } else {
869: @keys = sort(keys(%$defaults));
870: }
871: foreach my $attr (@keys) {
1.35 matthew 872: # append a ' ' to the description if it doesn't have one already.
873: my $description = $defaults->{$attr}->{'description'};
874: $description .= ' ' if ($description !~ / $/);
1.20 matthew 875: if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1.35 matthew 876: $result .= &Apache::edit::text_arg
1.38 matthew 877: ($description,$attr,$token,
878: $defaults->{$attr}->{'size'});
1.20 matthew 879: } elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1.35 matthew 880: $result .= &Apache::edit::select_arg
881: ($description,$attr,$defaults->{$attr}->{'choices'},$token);
1.34 matthew 882: } elsif ($defaults->{$attr}->{'edit_type'} eq 'on_off') {
1.35 matthew 883: $result .= &Apache::edit::select_arg
884: ($description,$attr,['on','off'],$token);
1.20 matthew 885: }
1.25 matthew 886: $result .= '<br />';
1.20 matthew 887: }
888: return $result;
889: }
1.1 matthew 890:
1.21 matthew 891:
892: ###################################################################
893: ## ##
894: ## Insertion functions for editing plots ##
895: ## ##
896: ###################################################################
897:
1.20 matthew 898: #------------------------------------------------ insert_xxxxxxx
899: sub insert_plot {
1.29 matthew 900: my $result = '';
1.20 matthew 901: # plot attributes
1.29 matthew 902: $result .= "<plot \n";
1.30 matthew 903: foreach my $attr (keys(%plot_defaults)) {
1.29 matthew 904: $result .= " $attr=\"$plot_defaults{$attr}->{'default'}\"\n";
1.20 matthew 905: }
906: $result .= ">\n";
907: # Add the components
908: $result .= &insert_key();
909: $result .= &insert_axis();
1.29 matthew 910: $result .= &insert_title();
911: $result .= &insert_xlabel();
912: $result .= &insert_ylabel();
1.20 matthew 913: $result .= &insert_curve();
914: # close up the <plot>
915: $result .= "</plot>\n";
916: return $result;
917: }
918:
919: sub insert_key {
920: my $result;
1.29 matthew 921: $result .= " <key \n";
1.30 matthew 922: foreach my $attr (keys(%key_defaults)) {
1.29 matthew 923: $result .= " $attr=\"$key_defaults{$attr}->{'default'}\"\n";
1.20 matthew 924: }
925: $result .= " />\n";
926: return $result;
927: }
928:
929: sub insert_axis{
930: my $result;
931: $result .= ' <axis ';
1.30 matthew 932: foreach my $attr (keys(%axis_defaults)) {
1.29 matthew 933: $result .= " $attr=\"$axis_defaults{$attr}->{'default'}\"\n";
1.20 matthew 934: }
935: $result .= " />\n";
936: return $result;
937: }
1.28 matthew 938:
939: sub insert_title { return " <title></title>\n"; }
1.29 matthew 940: sub insert_xlabel { return " <xlabel></xlabel>\n"; }
941: sub insert_ylabel { return " <ylabel></ylabel>\n"; }
1.20 matthew 942:
943: sub insert_label {
944: my $result;
945: $result .= ' <label ';
1.30 matthew 946: foreach my $attr (keys(%label_defaults)) {
1.27 matthew 947: $result .= ' '.$attr.'="'.
1.20 matthew 948: $label_defaults{$attr}->{'default'}."\"\n";
949: }
950: $result .= " ></label>\n";
951: return $result;
952: }
953:
954: sub insert_curve {
955: my $result;
1.41 matthew 956: $result .= "\n <curve ";
1.30 matthew 957: foreach my $attr (keys(%curve_defaults)) {
1.27 matthew 958: $result .= ' '.$attr.'="'.
1.20 matthew 959: $curve_defaults{$attr}->{'default'}."\"\n";
960: }
1.41 matthew 961: $result .= " >\n";
962: $result .= &insert_data().&insert_data()."</curve>\n";
1.20 matthew 963: }
1.4 matthew 964:
1.20 matthew 965: sub insert_function {
966: my $result;
967: $result .= "<function></function>\n";
968: return $result;
969: }
1.4 matthew 970:
1.20 matthew 971: sub insert_data {
972: my $result;
973: $result .= " <data></data>\n";
974: return $result;
975: }
1.4 matthew 976:
1.21 matthew 977: ##----------------------------------------------------------------------
1.20 matthew 978: 1;
979: __END__
1.4 matthew 980:
981:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>