1: # The LearningOnline Network with CAPA
2: # Dynamic plot
3: #
4: # $Id: lonplot.pm,v 1.47 2002/02/05 15:05:44 matthew Exp $
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: #
28: # 12/15/01 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
31: # 01/02 01/03 01/04 01/07 01/08 01/09 Matthew
32: # 01/21 Matthew
33:
34: package Apache::lonplot;
35:
36: use strict;
37: use Apache::File;
38: use Apache::response;
39: use Apache::lonxml;
40: use Apache::edit;
41:
42: BEGIN {
43: &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
44: }
45:
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
59: ## align
60: ##
61: ## @labels: $labels[$i] = \%label
62: ## %label: text, xpos, ypos, justify
63: ##
64: ## @curves: $curves[$i] = \%curve
65: ## %curve: name, linestyle, ( function | data )
66: ##
67: ## $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
68: ## [y1,y2,y3,y4] ]
69: ##
70:
71: ###################################################################
72: ## ##
73: ## Tests used in checking the validitity of input ##
74: ## ##
75: ###################################################################
76:
77: my $max_str_len = 50; # if a label, title, xlabel, or ylabel text
78: # is longer than this, it will be truncated.
79:
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,
89: errorbars => 3,
90: xerrorbars => [3,4],
91: yerrorbars => [3,4],
92: xyerrorbars => [4,6],
93: boxes => 3,
94: # boxerrorbars => [3,4,5],
95: # boxxyerrorbars => [4,6,7],
96: # financebars => 5,
97: # candlesticks => 5,
98: vector => 4
99: );
100:
101: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
102: my $real_test =
103: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
104: my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
105: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
106: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
107: my $sml_test = sub {$_[0]=~/^(small|medium|large)$/};
108: my $linestyle_test = sub {exists($linestyles{$_[0]})};
109: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
110:
111: ###################################################################
112: ## ##
113: ## Attribute metadata ##
114: ## ##
115: ###################################################################
116: my @gnuplot_edit_order =
117: qw/bgcolor fgcolor height width font transparent grid border align/;
118: my %gnuplot_defaults =
119: (
120: height => {
121: default => 200,
122: test => $int_test,
123: description => 'height of image (pixels)',
124: edit_type => 'entry',
125: size => '10'
126: },
127: width => {
128: default => 200,
129: test => $int_test,
130: description => 'width of image (pixels)',
131: edit_type => 'entry',
132: size => '10'
133: },
134: bgcolor => {
135: default => 'xffffff',
136: test => $color_test,
137: description => 'background color of image (xffffff)',
138: edit_type => 'entry',
139: size => '10'
140: },
141: fgcolor => {
142: default => 'x000000',
143: test => $color_test,
144: description => 'foreground color of image (x000000)',
145: edit_type => 'entry',
146: size => '10'
147: },
148: transparent => {
149: default => 'off',
150: test => $onoff_test,
151: description => 'Transparent image',
152: edit_type => 'onoff'
153: },
154: grid => {
155: default => 'off',
156: test => $onoff_test,
157: description => 'Display grid',
158: edit_type => 'onoff'
159: },
160: border => {
161: default => 'on',
162: test => $onoff_test,
163: description => 'Draw border around plot',
164: edit_type => 'onoff'
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: }
180: );
181:
182: my %key_defaults =
183: (
184: title => {
185: default => '',
186: test => $words_test,
187: description => 'Title of key',
188: edit_type => 'entry',
189: size => '40'
190: },
191: box => {
192: default => 'off',
193: test => $onoff_test,
194: description => 'Draw a box around the key?',
195: edit_type => 'onoff'
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: }
205: );
206:
207: my %label_defaults =
208: (
209: xpos => {
210: default => 0,
211: test => $real_test,
212: description => 'x position of label (graph coordinates)',
213: edit_type => 'entry',
214: size => '10'
215: },
216: ypos => {
217: default => 0,
218: test => $real_test,
219: description => 'y position of label (graph coordinates)',
220: edit_type => 'entry',
221: size => '10'
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: }
230: );
231:
232: my @tic_edit_order = ('location','mirror','start','increment','end');
233: my %tic_defaults =
234: (
235: location => {
236: default => 'border',
237: test => sub {$_[0]=~/^(border|axis)$/},
238: description => 'Location of tick marks',
239: edit_type => 'choice',
240: choices => ['border','axis']
241: },
242: mirror => {
243: default => 'on',
244: test => $onoff_test,
245: description => 'mirror ticks on opposite axis?',
246: edit_type => 'onoff'
247: },
248: start => {
249: default => '-10.0',
250: test => $real_test,
251: description => 'Start ticks at',
252: edit_type => 'entry',
253: size => '10'
254: },
255: increment => {
256: default => '1.0',
257: test => $real_test,
258: description => 'Place a tick every',
259: edit_type => 'entry',
260: size => '10'
261: },
262: end => {
263: default => ' 10.0',
264: test => $real_test,
265: description => 'Stop ticks at ',
266: edit_type => 'entry',
267: size => '10'
268: },
269: );
270:
271: my %axis_defaults =
272: (
273: color => {
274: default => 'x000000',
275: test => $color_test,
276: description => 'color of axes (x000000)',
277: edit_type => 'entry',
278: size => '10'
279: },
280: xmin => {
281: default => '-10.0',
282: test => $real_test,
283: description => 'minimum x-value shown in plot',
284: edit_type => 'entry',
285: size => '10'
286: },
287: xmax => {
288: default => ' 10.0',
289: test => $real_test,
290: description => 'maximum x-value shown in plot',
291: edit_type => 'entry',
292: size => '10'
293: },
294: ymin => {
295: default => '-10.0',
296: test => $real_test,
297: description => 'minimum y-value shown in plot',
298: edit_type => 'entry',
299: size => '10'
300: },
301: ymax => {
302: default => ' 10.0',
303: test => $real_test,
304: description => 'maximum y-value shown in plot',
305: edit_type => 'entry',
306: size => '10'
307: }
308: );
309:
310: my %curve_defaults =
311: (
312: color => {
313: default => 'x000000',
314: test => $color_test,
315: description => 'color of curve (x000000)',
316: edit_type => 'entry',
317: size => '10'
318: },
319: name => {
320: default => '',
321: test => $words_test,
322: description => 'name of curve to appear in key',
323: edit_type => 'entry',
324: size => '20'
325: },
326: linestyle => {
327: default => 'lines',
328: test => $linestyle_test,
329: description => 'Line style',
330: edit_type => 'choice',
331: choices => [keys(%linestyles)]
332: }
333: );
334:
335: ###################################################################
336: ## ##
337: ## parsing and edit rendering ##
338: ## ##
339: ###################################################################
340: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
341:
342: sub start_gnuplot {
343: %plot = (); %key = (); %axis = ();
344: $title = undef; $xlabel = undef; $ylabel = undef;
345: $#labels = -1; $#curves = -1;
346: %xtics = (); %ytics = ();
347: #
348: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
349: my $result='';
350: &Apache::lonxml::register('Apache::lonplot',
351: ('title','xlabel','ylabel','key','axis','label','curve',
352: 'xtics','ytics'));
353: push (@Apache::lonxml::namespace,'lonplot');
354: if ($target eq 'web') {
355: my $inside = &Apache::lonxml::get_all_text("/gnuplot",$$parser[-1]);
356: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
357: &Apache::lonxml::newparser($parser,\$inside);
358: &get_attributes(\%plot,\%gnuplot_defaults,$parstack,$safeeval,
359: $tagstack->[-1]);
360: } elsif ($target eq 'edit') {
361: $result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
362: $result .= &edit_attributes($target,$token,\%gnuplot_defaults,
363: \@gnuplot_edit_order);
364: } elsif ($target eq 'modified') {
365: my $constructtag=&Apache::edit::get_new_args
366: ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
367: if ($constructtag) {
368: $result = &Apache::edit::rebuild_tag($token);
369: }
370: }
371: return $result;
372: }
373:
374: sub end_gnuplot {
375: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
376: pop @Apache::lonxml::namespace;
377: &Apache::lonxml::deregister('Apache::lonplot',
378: ('title','xlabel','ylabel','key','axis','label','curve'));
379: my $result = '';
380: if ($target eq 'web') {
381: &check_inputs(); # Make sure we have all the data we need
382: ##
383: ## Determine filename
384: my $tmpdir = '/home/httpd/perl/tmp/';
385: my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
386: '_'.time.'_'.$$.int(rand(1000)).'_plot.data';
387: ## Write the plot description to the file
388: &write_gnuplot_file($tmpdir,$filename);
389: ## return image tag for the plot
390: $result .= <<"ENDIMAGE";
391: <img src = "/cgi-bin/plot.gif?$filename"
392: width = "$plot{'width'}"
393: height = "$plot{'height'}"
394: align = "$plot{'align'}"
395: alt = "image should be /cgi-bin/plot.gif?$filename" />
396: ENDIMAGE
397: } elsif ($target eq 'edit') {
398: $result.=&Apache::edit::tag_end($target,$token);
399: }
400: return $result;
401: }
402:
403:
404: ##--------------------------------------------------------------- xtics
405: sub start_xtics {
406: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
407: my $result='';
408: if ($target eq 'web') {
409: &get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
410: $tagstack->[-1]);
411: } elsif ($target eq 'edit') {
412: $result .= &Apache::edit::tag_start($target,$token,'xtics');
413: $result .= &edit_attributes($target,$token,\%tic_defaults,
414: \@tic_edit_order);
415: } elsif ($target eq 'modified') {
416: my $constructtag=&Apache::edit::get_new_args
417: ($token,$parstack,$safeeval,keys(%tic_defaults));
418: if ($constructtag) {
419: $result = &Apache::edit::rebuild_tag($token);
420: }
421: }
422: return $result;
423: }
424:
425: sub end_xtics {
426: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
427: my $result = '';
428: if ($target eq 'web') {
429: } elsif ($target eq 'edit') {
430: $result.=&Apache::edit::tag_end($target,$token);
431: }
432: return $result;
433: }
434:
435: ##--------------------------------------------------------------- ytics
436: sub start_ytics {
437: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
438: my $result='';
439: if ($target eq 'web') {
440: &get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
441: $tagstack->[-1]);
442: } elsif ($target eq 'edit') {
443: $result .= &Apache::edit::tag_start($target,$token,'ytics');
444: $result .= &edit_attributes($target,$token,\%tic_defaults,
445: \@tic_edit_order);
446: } elsif ($target eq 'modified') {
447: my $constructtag=&Apache::edit::get_new_args
448: ($token,$parstack,$safeeval,keys(%tic_defaults));
449: if ($constructtag) {
450: $result = &Apache::edit::rebuild_tag($token);
451: }
452: }
453: return $result;
454: }
455:
456: sub end_ytics {
457: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
458: my $result = '';
459: if ($target eq 'web') {
460: } elsif ($target eq 'edit') {
461: $result.=&Apache::edit::tag_end($target,$token);
462: }
463: return $result;
464: }
465:
466:
467: ##----------------------------------------------------------------- key
468: sub start_key {
469: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
470: my $result='';
471: if ($target eq 'web') {
472: &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
473: $tagstack->[-1]);
474: } elsif ($target eq 'edit') {
475: $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
476: $result .= &edit_attributes($target,$token,\%key_defaults);
477: } elsif ($target eq 'modified') {
478: my $constructtag=&Apache::edit::get_new_args
479: ($token,$parstack,$safeeval,keys(%key_defaults));
480: if ($constructtag) {
481: $result = &Apache::edit::rebuild_tag($token);
482: }
483: }
484: return $result;
485: }
486:
487: sub end_key {
488: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
489: my $result = '';
490: if ($target eq 'web') {
491: } elsif ($target eq 'edit') {
492: $result.=&Apache::edit::tag_end($target,$token);
493: }
494: return $result;
495: }
496:
497: ##------------------------------------------------------------------- title
498: sub start_title {
499: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
500: my $result='';
501: if ($target eq 'web') {
502: $title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
503: if (length($title) > $max_str_len) {
504: $title = substr($title,0,$max_str_len);
505: }
506: } elsif ($target eq 'edit') {
507: $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
508: my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
509: $result.=&Apache::edit::end_row().
510: &Apache::edit::start_spanning_row().
511: &Apache::edit::editfield('',$text,'',60,1);
512: } elsif ($target eq 'modified') {
513: my $text=$$parser[-1]->get_text("/title");
514: $result.=&Apache::edit::rebuild_tag($token);
515: $result.=&Apache::edit::modifiedfield($token);
516: }
517: return $result;
518: }
519:
520: sub end_title {
521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
522: my $result = '';
523: if ($target eq 'web') {
524: } elsif ($target eq 'edit') {
525: $result.=&Apache::edit::tag_end($target,$token);
526: }
527: return $result;
528: }
529: ##------------------------------------------------------------------- xlabel
530: sub start_xlabel {
531: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
532: my $result='';
533: if ($target eq 'web') {
534: $xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
535: if (length($xlabel) > $max_str_len) {
536: $xlabel = substr($xlabel,0,$max_str_len);
537: }
538: } elsif ($target eq 'edit') {
539: $result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
540: my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
541: $result.=&Apache::edit::end_row().
542: &Apache::edit::start_spanning_row().
543: &Apache::edit::editfield('',$text,'',60,1);
544: } elsif ($target eq 'modified') {
545: my $text=$$parser[-1]->get_text("/xlabel");
546: $result.=&Apache::edit::rebuild_tag($token);
547: $result.=&Apache::edit::modifiedfield($token);
548: }
549: return $result;
550: }
551:
552: sub end_xlabel {
553: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
554: my $result = '';
555: if ($target eq 'web') {
556: } elsif ($target eq 'edit') {
557: $result.=&Apache::edit::tag_end($target,$token);
558: }
559: return $result;
560: }
561:
562: ##------------------------------------------------------------------- ylabel
563: sub start_ylabel {
564: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
565: my $result='';
566: if ($target eq 'web') {
567: $ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
568: if (length($ylabel) > $max_str_len) {
569: $ylabel = substr($ylabel,0,$max_str_len);
570: }
571: } elsif ($target eq 'edit') {
572: $result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
573: my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
574: $result .= &Apache::edit::end_row().
575: &Apache::edit::start_spanning_row().
576: &Apache::edit::editfield('',$text,'',60,1);
577: } elsif ($target eq 'modified') {
578: my $text=$$parser[-1]->get_text("/ylabel");
579: $result.=&Apache::edit::rebuild_tag($token);
580: $result.=&Apache::edit::modifiedfield($token);
581: }
582: return $result;
583: }
584:
585: sub end_ylabel {
586: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
587: my $result = '';
588: if ($target eq 'web') {
589: } elsif ($target eq 'edit') {
590: $result.=&Apache::edit::tag_end($target,$token);
591: }
592: return $result;
593: }
594:
595: ##------------------------------------------------------------------- label
596: sub start_label {
597: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
598: my $result='';
599: if ($target eq 'web') {
600: my %label;
601: &get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
602: $tagstack->[-1]);
603: my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
604: $text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
605: $label{'text'} = $text;
606: push(@labels,\%label);
607: } elsif ($target eq 'edit') {
608: $result .= &Apache::edit::tag_start($target,$token,'Plot Label');
609: $result .= &edit_attributes($target,$token,\%label_defaults);
610: my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
611: $result .= &Apache::edit::end_row().
612: &Apache::edit::start_spanning_row().
613: &Apache::edit::editfield('',$text,'',60,1);
614: } elsif ($target eq 'modified') {
615: &Apache::edit::get_new_args
616: ($token,$parstack,$safeeval,keys(%label_defaults));
617: $result.=&Apache::edit::rebuild_tag($token);
618: my $text=$$parser[-1]->get_text("/label");
619: $result.=&Apache::edit::modifiedfield($token);
620: }
621: return $result;
622: }
623:
624: sub end_label {
625: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
626: my $result = '';
627: if ($target eq 'web') {
628: } elsif ($target eq 'edit') {
629: $result.=&Apache::edit::tag_end($target,$token);
630: }
631: return $result;
632: }
633:
634: ##------------------------------------------------------------------- curve
635: sub start_curve {
636: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
637: my $result='';
638: &Apache::lonxml::register('Apache::lonplot',('function','data'));
639: push (@Apache::lonxml::namespace,'curve');
640: if ($target eq 'web') {
641: my %curve;
642: &get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
643: $tagstack->[-1]);
644: push (@curves,\%curve);
645: } elsif ($target eq 'edit') {
646: $result .= &Apache::edit::tag_start($target,$token,'Curve');
647: $result .= &edit_attributes($target,$token,\%curve_defaults);
648: } elsif ($target eq 'modified') {
649: my $constructtag=&Apache::edit::get_new_args
650: ($token,$parstack,$safeeval,keys(%curve_defaults));
651: if ($constructtag) {
652: $result = &Apache::edit::rebuild_tag($token);
653: $result.= &Apache::edit::handle_insert();
654: }
655: }
656: return $result;
657: }
658:
659: sub end_curve {
660: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
661: my $result = '';
662: pop @Apache::lonxml::namespace;
663: &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
664: if ($target eq 'web') {
665: } elsif ($target eq 'edit') {
666: $result.=&Apache::edit::tag_end($target,$token);
667: }
668: return $result;
669: }
670:
671: ##------------------------------------------------------------ curve function
672: sub start_function {
673: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
674: my $result='';
675: if ($target eq 'web') {
676: if (exists($curves[-1]->{'data'})) {
677: &Apache::lonxml::warning('Use of <function> precludes use of <data>. The <data> will be omitted in favor of the <function> declaration.');
678: delete $curves[-1]->{'data'} ;
679: }
680: $curves[-1]->{'function'} =
681: &Apache::lonxml::get_all_text("/function",$$parser[-1]);
682: } elsif ($target eq 'edit') {
683: $result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
684: my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
685: $result .= &Apache::edit::end_row().
686: &Apache::edit::start_spanning_row().
687: &Apache::edit::editfield('',$text,'',60,1);
688: } elsif ($target eq 'modified') {
689: $result.=&Apache::edit::rebuild_tag($token);
690: my $text=$$parser[-1]->get_text("/function");
691: $result.=&Apache::edit::modifiedfield($token);
692: }
693: return $result;
694: }
695:
696: sub end_function {
697: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
698: my $result = '';
699: if ($target eq 'web') {
700: } elsif ($target eq 'edit') {
701: $result .= &Apache::edit::end_table();
702: }
703: return $result;
704: }
705:
706: ##------------------------------------------------------------ curve data
707: sub start_data {
708: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
709: my $result='';
710: if ($target eq 'web') {
711: if (exists($curves[-1]->{'function'})) {
712: &Apache::lonxml::warning('Use of <data> precludes use of .'.
713: '<function>. The <function> will be omitted in favor of '.
714: 'the <data> declaration.');
715: delete($curves[-1]->{'function'});
716: }
717: my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
718: # Deal with cases where we're given an array...
719: if ($datatext =~ /^\@/) {
720: $datatext = &Apache::run::run('return "'.$datatext.'"',
721: $safeeval,1);
722: }
723: $datatext =~ s/\s+/ /g;
724: # Need to do some error checking on the @data array -
725: # make sure it's all numbers and make sure each array
726: # is of the same length.
727: my @data;
728: if ($datatext =~ /,/) { # comma deliminated
729: @data = split /,/,$datatext;
730: } else { # Assume it's space seperated.
731: @data = split / /,$datatext;
732: }
733: for (my $i=0;$i<=$#data;$i++) {
734: # Check that it's non-empty
735: if (! defined($data[$i])) {
736: &Apache::lonxml::warning(
737: 'undefined <data> value. Replacing with '.
738: ' pi/e = 1.15572734979092');
739: $data[$i] = 1.15572734979092;
740: }
741: # Check that it's a number
742: if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
743: &Apache::lonxml::warning(
744: 'Bad <data> value of '.$data[$i].' Replacing with '.
745: ' pi/e = 1.15572734979092');
746: $data[$i] = 1.15572734979092;
747: }
748: }
749: # complain if the number of data points is not the same as
750: # in previous sets of data.
751: if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
752: &Apache::lonxml::warning
753: ('Number of data points is not consistent with previous '.
754: 'number of data points');
755: }
756: push @{$curves[-1]->{'data'}},\@data;
757: } elsif ($target eq 'edit') {
758: $result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
759: my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
760: $result .= &Apache::edit::end_row().
761: &Apache::edit::start_spanning_row().
762: &Apache::edit::editfield('',$text,'',60,1);
763: } elsif ($target eq 'modified') {
764: $result.=&Apache::edit::rebuild_tag($token);
765: my $text=$$parser[-1]->get_text("/data");
766: $result.=&Apache::edit::modifiedfield($token);
767: }
768: return $result;
769: }
770:
771: sub end_data {
772: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
773: my $result = '';
774: if ($target eq 'web') {
775: } elsif ($target eq 'edit') {
776: $result .= &Apache::edit::end_table();
777: }
778: return $result;
779: }
780:
781: ##------------------------------------------------------------------- axis
782: sub start_axis {
783: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
784: my $result='';
785: if ($target eq 'web') {
786: &get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
787: $tagstack->[-1]);
788: } elsif ($target eq 'edit') {
789: $result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
790: $result .= &edit_attributes($target,$token,\%axis_defaults);
791: } elsif ($target eq 'modified') {
792: my $constructtag=&Apache::edit::get_new_args
793: ($token,$parstack,$safeeval,keys(%axis_defaults));
794: if ($constructtag) {
795: $result = &Apache::edit::rebuild_tag($token);
796: }
797: }
798: return $result;
799: }
800:
801: sub end_axis {
802: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
803: my $result = '';
804: if ($target eq 'web') {
805: } elsif ($target eq 'edit') {
806: $result.=&Apache::edit::tag_end($target,$token);
807: } elsif ($target eq 'modified') {
808: }
809: return $result;
810: }
811:
812: ###################################################################
813: ## ##
814: ## Utility Functions ##
815: ## ##
816: ###################################################################
817:
818: ##----------------------------------------------------------- set_defaults
819: sub set_defaults {
820: my ($var,$defaults) = @_;
821: my $key;
822: foreach $key (keys(%$defaults)) {
823: $var->{$key} = $defaults->{$key}->{'default'};
824: }
825: }
826:
827: ##------------------------------------------------------------------- misc
828: sub get_attributes{
829: my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
830: foreach my $attr (keys(%{$defaults})) {
831: $values->{$attr} =
832: &Apache::lonxml::get_param($attr,$parstack,$safeeval);
833: if ($values->{$attr} eq '' | !defined($values->{$attr})) {
834: $values->{$attr} = $defaults->{$attr}->{'default'};
835: next;
836: }
837: my $test = $defaults->{$attr}->{'test'};
838: if (! &$test($values->{$attr})) {
839: &Apache::lonxml::warning
840: ($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
841: .$defaults->{$attr}->{'default'} );
842: $values->{$attr} = $defaults->{$attr}->{'default'};
843: }
844: }
845: return ;
846: }
847:
848: ##------------------------------------------------------- write_gnuplot_file
849: sub write_gnuplot_file {
850: my ($tmpdir,$filename)= @_;
851: my $gnuplot_input = '';
852: my $curve;
853: # Collect all the colors
854: my @Colors;
855: push @Colors, $plot{'bgcolor'};
856: push @Colors, $plot{'fgcolor'};
857: push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
858: foreach $curve (@curves) {
859: push @Colors, ($curve->{'color'} ne '' ?
860: $curve->{'color'} :
861: $plot{'fgcolor'} );
862: }
863: # set term
864: $gnuplot_input .= 'set term gif ';
865: $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
866: $gnuplot_input .= $plot{'font'} . ' ';
867: $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
868: $gnuplot_input .= "@Colors\n";
869: # set output
870: $gnuplot_input .= "set output\n";
871: # grid
872: $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
873: # border
874: $gnuplot_input .= ($plot{'border'} eq 'on'?
875: 'set border'.$/ :
876: 'set noborder'.$/ ); # title, xlabel, ylabel
877: # titles
878: $gnuplot_input .= "set title \"$title\"\n" if (defined($title)) ;
879: $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
880: $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
881: # tics
882: if (%xtics) {
883: $gnuplot_input .= "set xtics $xtics{'location'} ";
884: $gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
885: $gnuplot_input .= "$xtics{'start'}, ";
886: $gnuplot_input .= "$xtics{'increment'}, ";
887: $gnuplot_input .= "$xtics{'end'}\n";
888: }
889: if (%ytics) {
890: $gnuplot_input .= "set ytics $ytics{'location'} ";
891: $gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
892: $gnuplot_input .= "$ytics{'start'}, ";
893: $gnuplot_input .= "$ytics{'increment'}, ";
894: $gnuplot_input .= "$ytics{'end'}\n";
895: }
896: # axis
897: if (%axis) {
898: $gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
899: $gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
900: }
901: # Key
902: if (%key) {
903: $gnuplot_input .= 'set key '.$key{'pos'}.' ';
904: if ($key{'title'} ne '') {
905: $gnuplot_input .= 'title " '.$key{'title'}.'" ';
906: }
907: $gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
908: } else {
909: $gnuplot_input .= 'set nokey'.$/;
910: }
911: # labels
912: my $label;
913: foreach $label (@labels) {
914: $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
915: $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
916: }
917: # curves
918: $gnuplot_input .= 'plot ';
919: for (my $i = 0;$i<=$#curves;$i++) {
920: $curve = $curves[$i];
921: $gnuplot_input.= ', ' if ($i > 0);
922: if (exists($curve->{'function'})) {
923: $gnuplot_input.=
924: $curve->{'function'}.' title "'.
925: $curve->{'name'}.'" with '.
926: $curve->{'linestyle'};
927: } elsif (exists($curve->{'data'})) {
928: # Store data values in $datatext
929: my $datatext = '';
930: # get new filename
931: my $datafilename = "$tmpdir/$filename.$i";
932: my $fh=Apache::File->new(">$datafilename");
933: # Compile data
934: my @Data = @{$curve->{'data'}};
935: my @Data0 = @{$Data[0]};
936: for (my $i =0; $i<=$#Data0; $i++) {
937: my $dataset;
938: foreach $dataset (@Data) {
939: $datatext .= $dataset->[$i] . ' ';
940: }
941: $datatext .= $/;
942: }
943: # write file
944: print $fh $datatext;
945: close ($fh);
946: # generate gnuplot text
947: $gnuplot_input.= '"'.$datafilename.'" title "'.
948: $curve->{'name'}.'" with '.
949: $curve->{'linestyle'};
950: }
951: }
952: # Write the output to a file.
953: my $fh=Apache::File->new(">$tmpdir$filename");
954: print $fh $gnuplot_input;
955: close($fh);
956: # That's all folks.
957: return ;
958: }
959:
960: #---------------------------------------------- check_inputs
961: sub check_inputs {
962: ## Note: no inputs, no outputs - this acts only on global variables.
963: ## Make sure we have all the input we need:
964: if (! %plot) { &set_defaults(\%plot,\%gnuplot_defaults); }
965: if (! %key ) {} # No key for this plot, thats okay
966: # if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
967: if (! defined($title )) {} # No title for this plot, thats okay
968: if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
969: if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
970: if ($#labels < 0) { } # No labels for this plot, thats okay
971: if ($#curves < 0) {
972: &Apache::lonxml::warning("No curves specified for plot!!!!");
973: return '';
974: }
975: my $curve;
976: foreach $curve (@curves) {
977: if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
978: &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
979: return '';
980: }
981: }
982: }
983:
984: #------------------------------------------------ make_edit
985: sub edit_attributes {
986: my ($target,$token,$defaults,$keys) = @_;
987: my ($result,@keys);
988: if ($keys && ref($keys) eq 'ARRAY') {
989: @keys = @$keys;
990: } else {
991: @keys = sort(keys(%$defaults));
992: }
993: foreach my $attr (@keys) {
994: # append a ' ' to the description if it doesn't have one already.
995: my $description = $defaults->{$attr}->{'description'};
996: $description .= ' ' if ($description !~ / $/);
997: if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
998: $result .= &Apache::edit::text_arg
999: ($description,$attr,$token,
1000: $defaults->{$attr}->{'size'});
1001: } elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1002: $result .= &Apache::edit::select_arg
1003: ($description,$attr,$defaults->{$attr}->{'choices'},$token);
1004: } elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1005: $result .= &Apache::edit::select_arg
1006: ($description,$attr,['on','off'],$token);
1007: }
1008: $result .= '<br />';
1009: }
1010: return $result;
1011: }
1012:
1013:
1014: ###################################################################
1015: ## ##
1016: ## Insertion functions for editing plots ##
1017: ## ##
1018: ###################################################################
1019:
1020: sub insert_gnuplot {
1021: my $result = '';
1022: # plot attributes
1023: $result .= "<plot \n";
1024: foreach my $attr (keys(%gnuplot_defaults)) {
1025: $result .= " $attr=\"$gnuplot_defaults{$attr}->{'default'}\"\n";
1026: }
1027: $result .= ">\n";
1028: # Add the components (most are commented out for simplicity)
1029: # $result .= &insert_key();
1030: # $result .= &insert_axis();
1031: # $result .= &insert_title();
1032: # $result .= &insert_xlabel();
1033: # $result .= &insert_ylabel();
1034: $result .= &insert_curve();
1035: # close up the <plot>
1036: $result .= "</plot>\n";
1037: return $result;
1038: }
1039:
1040: sub insert_tics {
1041: my $result;
1042: $result .= &insert_xtics() . &insert_ytics;
1043: return $result;
1044: }
1045:
1046: sub insert_xtics {
1047: my $result;
1048: $result .= "\n <xtics ";
1049: foreach my $attr (keys(%tic_defaults)) {
1050: $result .= "$attr=\"$tic_defaults{$attr}->{'default'}\" ";
1051: }
1052: $result .= "/>\n";
1053: return $result;
1054: }
1055:
1056: sub insert_ytics {
1057: my $result;
1058: $result .= "\n <ytics ";
1059: foreach my $attr (keys(%tic_defaults)) {
1060: $result .= "$attr=\"$tic_defaults{$attr}->{'default'}\" ";
1061: }
1062: $result .= "/>\n";
1063: return $result;
1064: }
1065:
1066: sub insert_key {
1067: my $result;
1068: $result .= "\n <key \n";
1069: foreach my $attr (keys(%key_defaults)) {
1070: $result .= " $attr=\"$key_defaults{$attr}->{'default'}\"\n";
1071: }
1072: $result .= " />\n";
1073: return $result;
1074: }
1075:
1076: sub insert_axis{
1077: my $result;
1078: $result .= "\n <axis ";
1079: foreach my $attr (keys(%axis_defaults)) {
1080: $result .= " $attr=\"$axis_defaults{$attr}->{'default'}\"\n";
1081: }
1082: $result .= " />\n";
1083: return $result;
1084: }
1085:
1086: sub insert_title { return "\n <title></title>\n"; }
1087: sub insert_xlabel { return "\n <xlabel></xlabel>\n"; }
1088: sub insert_ylabel { return "\n <ylabel></ylabel>\n"; }
1089:
1090: sub insert_label {
1091: my $result;
1092: $result .= "\n <label ";
1093: foreach my $attr (keys(%label_defaults)) {
1094: $result .= ' '.$attr.'="'.
1095: $label_defaults{$attr}->{'default'}."\"\n";
1096: }
1097: $result .= " ></label>\n";
1098: return $result;
1099: }
1100:
1101: sub insert_curve {
1102: my $result;
1103: $result .= "\n <curve ";
1104: foreach my $attr (keys(%curve_defaults)) {
1105: $result .= ' '.$attr.'="'.
1106: $curve_defaults{$attr}->{'default'}."\"\n";
1107: }
1108: $result .= " >\n";
1109: $result .= &insert_data().&insert_data()."</curve>\n";
1110: }
1111:
1112: sub insert_function {
1113: my $result;
1114: $result .= "<function></function>\n";
1115: return $result;
1116: }
1117:
1118: sub insert_data {
1119: my $result;
1120: $result .= " <data></data>\n";
1121: return $result;
1122: }
1123:
1124: ##----------------------------------------------------------------------
1125: 1;
1126: __END__
1127:
1128:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>