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