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