1: # The LearningOnline Network with CAPA
2: # Dynamic plot
3: #
4: # $Id: lonplot.pm,v 1.110 2005/06/09 02:16:04 albertel 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:
29: package Apache::lonplot;
30:
31: use strict;
32: use warnings FATAL=>'all';
33: no warnings 'uninitialized';
34: use Apache::File;
35: use Apache::response;
36: use Apache::lonxml;
37: use Apache::edit;
38: use Apache::lonnet;
39:
40: use vars qw/$weboutputformat $versionstring/;
41:
42:
43:
44: BEGIN {
45: &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
46: #
47: # Determine the version of GNUPLOT
48: $weboutputformat = 'gif';
49: $versionstring = `gnuplot --version 2>/dev/null`;
50: if ($versionstring =~ /^gnuplot 4/) {
51: $weboutputformat = 'png';
52: }
53:
54: }
55:
56:
57: ##
58: ## Description of data structures:
59: ##
60: ## %plot %key %axis
61: ## --------------------------
62: ## height title color
63: ## width box xmin
64: ## bgcolor pos xmax
65: ## fgcolor ymin
66: ## transparent ymax
67: ## grid
68: ## border
69: ## font
70: ## align
71: ##
72: ## @labels: $labels[$i] = \%label
73: ## %label: text, xpos, ypos, justify
74: ##
75: ## @curves: $curves[$i] = \%curve
76: ## %curve: name, linestyle, ( function | data )
77: ##
78: ## $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
79: ## [y1,y2,y3,y4] ]
80: ##
81:
82: ###################################################################
83: ## ##
84: ## Tests used in checking the validitity of input ##
85: ## ##
86: ###################################################################
87:
88: my $max_str_len = 50; # if a label, title, xlabel, or ylabel text
89: # is longer than this, it will be truncated.
90:
91: my %linestyles =
92: (
93: lines => 2, # Maybe this will be used in the future
94: linespoints => 2, # to check on whether or not they have
95: dots => 2, # supplied enough <data></data> fields
96: points => 2, # to use the given line style. But for
97: steps => 2, # now there are more important things
98: fsteps => 2, # for me to deal with.
99: histeps => 2,
100: errorbars => 3,
101: xerrorbars => [3,4],
102: yerrorbars => [3,4],
103: xyerrorbars => [4,6],
104: boxes => 3,
105: filledcurves => 2,
106: vector => 4
107: );
108:
109: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
110: my $real_test =
111: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
112: my $pos_real_test =
113: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
114: my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-fA-F]{6}$/};
115: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
116: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
117: my $sml_test = sub {$_[0]=~/^(small|medium|large)$/};
118: my $linestyle_test = sub {exists($linestyles{$_[0]})};
119: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
120:
121: ###################################################################
122: ## ##
123: ## Attribute metadata ##
124: ## ##
125: ###################################################################
126: my @gnuplot_edit_order =
127: qw/alttag bgcolor fgcolor height width font transparent grid samples
128: border align texwidth texfont plotcolor plottype lmargin rmargin tmargin
129: bmargin major_ticscale minor_ticscale boxwidth gridlayer fillstyle
130: pattern solid/;
131:
132: my $margin_choices = ['default',0..20];
133:
134: my %gnuplot_defaults =
135: (
136: alttag => {
137: default => 'dynamically generated plot',
138: test => $words_test,
139: description => 'brief description of the plot',
140: edit_type => 'entry',
141: size => '40'
142: },
143: height => {
144: default => 300,
145: test => $int_test,
146: description => 'height of image (pixels)',
147: edit_type => 'entry',
148: size => '10'
149: },
150: width => {
151: default => 400,
152: test => $int_test,
153: description => 'width of image (pixels)',
154: edit_type => 'entry',
155: size => '10'
156: },
157: bgcolor => {
158: default => 'xffffff',
159: test => $color_test,
160: description => 'background color of image (xffffff)',
161: edit_type => 'entry',
162: size => '10'
163: },
164: fgcolor => {
165: default => 'x000000',
166: test => $color_test,
167: description => 'foreground color of image (x000000)',
168: edit_type => 'entry',
169: size => '10'
170: },
171: transparent => {
172: default => 'off',
173: test => $onoff_test,
174: description => 'Transparent image',
175: edit_type => 'onoff'
176: },
177: grid => {
178: default => 'on',
179: test => $onoff_test,
180: description => 'Display grid',
181: edit_type => 'onoff'
182: },
183: gridlayer => {
184: default => 'off',
185: test => $onoff_test,
186: description => 'Display grid front layer over filled boxes or filled curves',
187: edit_type => 'onoff'
188: },
189: box_border => {
190: default => 'noborder',
191: test => sub {$_[0]=~/^(noborder|border)$/},
192: description => 'Draw border for boxes',
193: edit_type => 'choice',
194: choices => ['border','noborder']
195: },
196: border => {
197: default => 'on',
198: test => $onoff_test,
199: description => 'Draw border around plot',
200: edit_type => 'onoff'
201: },
202: font => {
203: default => 'medium',
204: test => $sml_test,
205: description => 'Size of font to use',
206: edit_type => 'choice',
207: choices => ['small','medium','large']
208: },
209: samples => {
210: default => '100',
211: test => $int_test,
212: description => 'Number of samples for non-data plots',
213: edit_type => 'choice',
214: choices => ['100','200','500','1000','2000','5000']
215: },
216: align => {
217: default => 'center',
218: test => sub {$_[0]=~/^(left|right|center)$/},
219: description => 'alignment for image in html',
220: edit_type => 'choice',
221: choices => ['left','right','center']
222: },
223: texwidth => {
224: default => '93',
225: test => $int_test,
226: description => 'Width of plot when printed (mm)',
227: edit_type => 'entry',
228: size => '5'
229: },
230: texfont => {
231: default => '22',
232: test => $int_test,
233: description => 'Font size to use in TeX output (pts):',
234: edit_type => 'choice',
235: choices => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
236: },
237: plotcolor => {
238: default => 'monochrome',
239: test => sub {$_[0]=~/^(monochrome|color|colour)$/},
240: description => 'Color setting for printing:',
241: edit_type => 'choice',
242: choices => [qw/monochrome color colour/],
243: },
244: pattern => {
245: default => '',
246: test => $int_test,
247: description => 'pattern value for boxes:',
248: edit_type => 'choice',
249: choices => [0,1,2,3,4,5,6]
250: },
251: solid => {
252: default => 0,
253: test => $real_test,
254: description => 'The density of fill style for boxes',
255: edit_type => 'entry',
256: size => '5'
257: },
258: fillstyle => {
259: default => 'empty',
260: test => sub {$_[0]=~/^(empty|solid|pattern)$/},
261: description => 'Filled style for boxes:',
262: edit_type => 'choice',
263: choices => ['empty','solid','pattern']
264: },
265: plottype => {
266: default => 'Cartesian',
267: test => sub {$_[0]=~/^(Polar|Cartesian)$/},
268: description => 'Plot type:',
269: edit_type => 'choice',
270: choices => ['Cartesian','Polar']
271: },
272: lmargin => {
273: default => 'default',
274: test => sub {$_[0]=~/^(default|\d+)$/},
275: description => 'Left margin width (pts):',
276: edit_type => 'choice',
277: choices => $margin_choices,
278: },
279: rmargin => {
280: default => 'default',
281: test => sub {$_[0]=~/^(default|\d+)$/},
282: description => 'Right margin width (pts):',
283: edit_type => 'choice',
284: choices => $margin_choices,
285: },
286: tmargin => {
287: default => 'default',
288: test => sub {$_[0]=~/^(default|\d+)$/},
289: description => 'Top margin width (pts):',
290: edit_type => 'choice',
291: choices => $margin_choices,
292: },
293: bmargin => {
294: default => 'default',
295: test => sub {$_[0]=~/^(default|\d+)$/},
296: description => 'Bottom margin width (pts):',
297: edit_type => 'choice',
298: choices => $margin_choices,
299: },
300: boxwidth => {
301: default => '',
302: test => $real_test,
303: description => 'width of boxes default auto',
304: edit_type => 'entry',
305: size => '5'
306: },
307: major_ticscale => {
308: default => '1',
309: test => $real_test,
310: description => 'Size of major tic marks (plot coordinates)',
311: edit_type => 'entry',
312: size => '5'
313: },
314: minor_ticscale => {
315: default => '0.5',
316: test => $real_test,
317: description => 'Size of minor tic mark (plot coordinates)',
318: edit_type => 'entry',
319: size => '5'
320: },
321: );
322:
323: my %key_defaults =
324: (
325: title => {
326: default => '',
327: test => $words_test,
328: description => 'Title of key',
329: edit_type => 'entry',
330: size => '40'
331: },
332: box => {
333: default => 'off',
334: test => $onoff_test,
335: description => 'Draw a box around the key?',
336: edit_type => 'onoff'
337: },
338: pos => {
339: default => 'top right',
340: test => $key_pos_test,
341: description => 'position of the key on the plot',
342: edit_type => 'choice',
343: choices => ['top left','top right','bottom left','bottom right',
344: 'outside','below']
345: }
346: );
347:
348: my %label_defaults =
349: (
350: xpos => {
351: default => 0,
352: test => $real_test,
353: description => 'x position of label (graph coordinates)',
354: edit_type => 'entry',
355: size => '10'
356: },
357: ypos => {
358: default => 0,
359: test => $real_test,
360: description => 'y position of label (graph coordinates)',
361: edit_type => 'entry',
362: size => '10'
363: },
364: justify => {
365: default => 'left',
366: test => sub {$_[0]=~/^(left|right|center)$/},
367: description => 'justification of the label text on the plot',
368: edit_type => 'choice',
369: choices => ['left','right','center']
370: }
371: );
372:
373: my @tic_edit_order = ('location','mirror','start','increment','end',
374: 'minorfreq');
375: my %tic_defaults =
376: (
377: location => {
378: default => 'border',
379: test => sub {$_[0]=~/^(border|axis)$/},
380: description => 'Location of major tic marks',
381: edit_type => 'choice',
382: choices => ['border','axis']
383: },
384: mirror => {
385: default => 'on',
386: test => $onoff_test,
387: description => 'mirror tics on opposite axis?',
388: edit_type => 'onoff'
389: },
390: start => {
391: default => '-10.0',
392: test => $real_test,
393: description => 'Start major tics at',
394: edit_type => 'entry',
395: size => '10'
396: },
397: increment => {
398: default => '1.0',
399: test => $real_test,
400: description => 'Place a major tic every',
401: edit_type => 'entry',
402: size => '10'
403: },
404: end => {
405: default => ' 10.0',
406: test => $real_test,
407: description => 'Stop major tics at ',
408: edit_type => 'entry',
409: size => '10'
410: },
411: minorfreq => {
412: default => '0',
413: test => $int_test,
414: description => 'Number of minor tics per major tic mark',
415: edit_type => 'entry',
416: size => '10'
417: },
418: );
419:
420: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax');
421: my %axis_defaults =
422: (
423: color => {
424: default => 'x000000',
425: test => $color_test,
426: description => 'color of grid lines (x000000)',
427: edit_type => 'entry',
428: size => '10'
429: },
430: xmin => {
431: default => '-10.0',
432: test => $real_test,
433: description => 'minimum x-value shown in plot',
434: edit_type => 'entry',
435: size => '10'
436: },
437: xmax => {
438: default => ' 10.0',
439: test => $real_test,
440: description => 'maximum x-value shown in plot',
441: edit_type => 'entry',
442: size => '10'
443: },
444: ymin => {
445: default => '-10.0',
446: test => $real_test,
447: description => 'minimum y-value shown in plot',
448: edit_type => 'entry',
449: size => '10'
450: },
451: ymax => {
452: default => ' 10.0',
453: test => $real_test,
454: description => 'maximum y-value shown in plot',
455: edit_type => 'entry',
456: size => '10'
457: }
458: );
459:
460: my @curve_edit_order = ('color','name','linestyle','pointtype','pointsize','limit');
461:
462: my %curve_defaults =
463: (
464: color => {
465: default => 'x000000',
466: test => $color_test,
467: description => 'color of curve (x000000)',
468: edit_type => 'entry',
469: size => '10'
470: },
471: name => {
472: default => '',
473: test => $words_test,
474: description => 'name of curve to appear in key',
475: edit_type => 'entry',
476: size => '20'
477: },
478: linestyle => {
479: default => 'lines',
480: test => $linestyle_test,
481: description => 'Line style',
482: edit_type => 'choice',
483: choices => [keys(%linestyles)]
484: },
485: # gnuplots term=gif driver does not handle linewidth :(
486: # linewidth => {
487: # default => 1,
488: # test => $int_test,
489: # description => 'Line width (may not apply to all line styles)',
490: # edit_type => 'choice',
491: # choices => [1,2,3,4,5,6,7,8,9,10]
492: # },
493: pointsize => {
494: default => 1,
495: test => $pos_real_test,
496: description => 'point size (may not apply to all line styles)',
497: edit_type => 'entry',
498: size => '5'
499: },
500: pointtype => {
501: default => 1,
502: test => $int_test,
503: description => 'point type (may not apply to all line styles)',
504: edit_type => 'choice',
505: choices => [0,1,2,3,4,5,6]
506: },
507: limit => {
508: default => 'closed',
509: test => sub {$_[0]=~/^(closed|x1|x2|y1|y2)$/},
510: description => 'point to fill -- for filledcurves',
511: edit_type => 'choice',
512: choices => ['closed','x1','x2','y1','y2']
513: },
514: );
515:
516: ###################################################################
517: ## ##
518: ## parsing and edit rendering ##
519: ## ##
520: ###################################################################
521:
522: undef %Apache::lonplot::plot;
523: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
524:
525: sub start_gnuplot {
526: undef(%Apache::lonplot::plot); undef(%key); undef(%axis);
527: undef($title); undef($xlabel); undef($ylabel);
528: undef(@labels); undef(@curves);
529: undef(%xtics); undef(%ytics);
530: #
531: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
532: my $result='';
533: &Apache::lonxml::register('Apache::lonplot',
534: ('title','xlabel','ylabel','key','axis','label','curve',
535: 'xtics','ytics'));
536: push (@Apache::lonxml::namespace,'lonplot');
537: if ($target eq 'web' || $target eq 'tex') {
538: &get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
539: $tagstack->[-1]);
540: } elsif ($target eq 'edit') {
541: $result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
542: $result .= &edit_attributes($target,$token,\%gnuplot_defaults,
543: \@gnuplot_edit_order);
544: } elsif ($target eq 'modified') {
545: my $constructtag=&Apache::edit::get_new_args
546: ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
547: if ($constructtag) {
548: $result = &Apache::edit::rebuild_tag($token);
549: }
550: }
551: return $result;
552: }
553:
554: sub end_gnuplot {
555: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
556: pop @Apache::lonxml::namespace;
557: &Apache::lonxml::deregister('Apache::lonplot',
558: ('title','xlabel','ylabel','key','axis','label','curve'));
559: my $result = '';
560: my $randnumber;
561: # need to call rand everytime start_script would evaluate, as the
562: # safe space rand number generator and the global rand generator
563: # are not separate
564: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
565: $target eq 'answer') {
566: $randnumber=int(rand(1000));
567: }
568: if ($target eq 'web' || $target eq 'tex') {
569: &check_inputs(); # Make sure we have all the data we need
570: ##
571: ## Determine filename
572: my $tmpdir = '/home/httpd/perl/tmp/';
573: my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
574: '_'.time.'_'.$$.$randnumber.'_plot';
575: ## Write the plot description to the file
576: &write_gnuplot_file($tmpdir,$filename,$target);
577: $filename = &Apache::lonnet::escape($filename);
578: ## return image tag for the plot
579: if ($target eq 'web') {
580: $result .= <<"ENDIMAGE";
581: <img src = "/cgi-bin/plot.gif?file=$filename.data&output=$weboutputformat"
582: width = "$Apache::lonplot::plot{'width'}"
583: height = "$Apache::lonplot::plot{'height'}"
584: align = "$Apache::lonplot::plot{'align'}"
585: alt = "$Apache::lonplot::plot{'alttag'}" />
586: ENDIMAGE
587: } elsif ($target eq 'tex') {
588: &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
589: &Apache::lonxml::debug(" gnuplot ht = $Apache::lonplot::plot{'height'}");
590: #might be inside the safe space, register the URL for later
591: &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
592: $result = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'} \n";
593: $result .= '\graphicspath{{/home/httpd/perl/tmp/}}'."\n";
594: $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&Apache::lonnet::unescape($filename).'.eps}';
595: }
596: } elsif ($target eq 'edit') {
597: $result.=&Apache::edit::tag_end($target,$token);
598: }
599: return $result;
600: }
601:
602:
603: ##--------------------------------------------------------------- xtics
604: sub start_xtics {
605: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
606: my $result='';
607: if ($target eq 'web' || $target eq 'tex') {
608: &get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
609: $tagstack->[-1]);
610: } elsif ($target eq 'edit') {
611: $result .= &Apache::edit::tag_start($target,$token,'xtics');
612: $result .= &edit_attributes($target,$token,\%tic_defaults,
613: \@tic_edit_order);
614: } elsif ($target eq 'modified') {
615: my $constructtag=&Apache::edit::get_new_args
616: ($token,$parstack,$safeeval,keys(%tic_defaults));
617: if ($constructtag) {
618: $result = &Apache::edit::rebuild_tag($token);
619: }
620: }
621: return $result;
622: }
623:
624: sub end_xtics {
625: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
626: my $result = '';
627: if ($target eq 'web' || $target eq 'tex') {
628: } elsif ($target eq 'edit') {
629: $result.=&Apache::edit::tag_end($target,$token);
630: }
631: return $result;
632: }
633:
634: ##--------------------------------------------------------------- ytics
635: sub start_ytics {
636: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
637: my $result='';
638: if ($target eq 'web' || $target eq 'tex') {
639: &get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
640: $tagstack->[-1]);
641: } elsif ($target eq 'edit') {
642: $result .= &Apache::edit::tag_start($target,$token,'ytics');
643: $result .= &edit_attributes($target,$token,\%tic_defaults,
644: \@tic_edit_order);
645: } elsif ($target eq 'modified') {
646: my $constructtag=&Apache::edit::get_new_args
647: ($token,$parstack,$safeeval,keys(%tic_defaults));
648: if ($constructtag) {
649: $result = &Apache::edit::rebuild_tag($token);
650: }
651: }
652: return $result;
653: }
654:
655: sub end_ytics {
656: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
657: my $result = '';
658: if ($target eq 'web' || $target eq 'tex') {
659: } elsif ($target eq 'edit') {
660: $result.=&Apache::edit::tag_end($target,$token);
661: }
662: return $result;
663: }
664:
665:
666: ##----------------------------------------------------------------- key
667: sub start_key {
668: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
669: my $result='';
670: if ($target eq 'web' || $target eq 'tex') {
671: &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
672: $tagstack->[-1]);
673: } elsif ($target eq 'edit') {
674: $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
675: $result .= &edit_attributes($target,$token,\%key_defaults);
676: } elsif ($target eq 'modified') {
677: my $constructtag=&Apache::edit::get_new_args
678: ($token,$parstack,$safeeval,keys(%key_defaults));
679: if ($constructtag) {
680: $result = &Apache::edit::rebuild_tag($token);
681: }
682: }
683: return $result;
684: }
685:
686: sub end_key {
687: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
688: my $result = '';
689: if ($target eq 'web' || $target eq 'tex') {
690: } elsif ($target eq 'edit') {
691: $result.=&Apache::edit::tag_end($target,$token);
692: }
693: return $result;
694: }
695:
696: ##------------------------------------------------------------------- title
697: sub start_title {
698: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
699: my $result='';
700: if ($target eq 'web' || $target eq 'tex') {
701: $title = &Apache::lonxml::get_all_text("/title",$parser);
702: $title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
703: $title =~ s/\n/ /g;
704: if (length($title) > $max_str_len) {
705: $title = substr($title,0,$max_str_len);
706: }
707: } elsif ($target eq 'edit') {
708: $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
709: my $text=&Apache::lonxml::get_all_text("/title",$parser);
710: $result.=&Apache::edit::end_row().
711: &Apache::edit::start_spanning_row().
712: &Apache::edit::editline('',$text,'',60);
713: } elsif ($target eq 'modified') {
714: $result.=&Apache::edit::rebuild_tag($token);
715: $result.=&Apache::edit::modifiedfield("/title",$parser);
716: }
717: return $result;
718: }
719:
720: sub end_title {
721: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
722: my $result = '';
723: if ($target eq 'web' || $target eq 'tex') {
724: } elsif ($target eq 'edit') {
725: $result.=&Apache::edit::tag_end($target,$token);
726: }
727: return $result;
728: }
729: ##------------------------------------------------------------------- xlabel
730: sub start_xlabel {
731: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
732: my $result='';
733: if ($target eq 'web' || $target eq 'tex') {
734: $xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser);
735: $xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
736: $xlabel =~ s/\n/ /g;
737: if (length($xlabel) > $max_str_len) {
738: $xlabel = substr($xlabel,0,$max_str_len);
739: }
740: } elsif ($target eq 'edit') {
741: $result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
742: my $text=&Apache::lonxml::get_all_text("/xlabel",$parser);
743: $result.=&Apache::edit::end_row().
744: &Apache::edit::start_spanning_row().
745: &Apache::edit::editline('',$text,'',60);
746: } elsif ($target eq 'modified') {
747: $result.=&Apache::edit::rebuild_tag($token);
748: $result.=&Apache::edit::modifiedfield("/xlabel",$parser);
749: }
750: return $result;
751: }
752:
753: sub end_xlabel {
754: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
755: my $result = '';
756: if ($target eq 'web' || $target eq 'tex') {
757: } elsif ($target eq 'edit') {
758: $result.=&Apache::edit::tag_end($target,$token);
759: }
760: return $result;
761: }
762:
763: ##------------------------------------------------------------------- ylabel
764: sub start_ylabel {
765: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
766: my $result='';
767: if ($target eq 'web' || $target eq 'tex') {
768: $ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser);
769: $ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
770: $ylabel =~ s/\n/ /g;
771: if (length($ylabel) > $max_str_len) {
772: $ylabel = substr($ylabel,0,$max_str_len);
773: }
774: } elsif ($target eq 'edit') {
775: $result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
776: my $text = &Apache::lonxml::get_all_text("/ylabel",$parser);
777: $result .= &Apache::edit::end_row().
778: &Apache::edit::start_spanning_row().
779: &Apache::edit::editline('',$text,'',60);
780: } elsif ($target eq 'modified') {
781: $result.=&Apache::edit::rebuild_tag($token);
782: $result.=&Apache::edit::modifiedfield("/ylabel",$parser);
783: }
784: return $result;
785: }
786:
787: sub end_ylabel {
788: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
789: my $result = '';
790: if ($target eq 'web' || $target eq 'tex') {
791: } elsif ($target eq 'edit') {
792: $result.=&Apache::edit::tag_end($target,$token);
793: }
794: return $result;
795: }
796:
797: ##------------------------------------------------------------------- label
798: sub start_label {
799: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
800: my $result='';
801: if ($target eq 'web' || $target eq 'tex') {
802: my %label;
803: &get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
804: $tagstack->[-1]);
805: my $text = &Apache::lonxml::get_all_text("/label",$parser);
806: $text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
807: $text =~ s/\n/ /g;
808: $text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
809: $label{'text'} = $text;
810: push(@labels,\%label);
811: } elsif ($target eq 'edit') {
812: $result .= &Apache::edit::tag_start($target,$token,'Plot Label');
813: $result .= &edit_attributes($target,$token,\%label_defaults);
814: my $text = &Apache::lonxml::get_all_text("/label",$parser);
815: $result .= &Apache::edit::end_row().
816: &Apache::edit::start_spanning_row().
817: &Apache::edit::editline('',$text,'',60);
818: } elsif ($target eq 'modified') {
819: &Apache::edit::get_new_args
820: ($token,$parstack,$safeeval,keys(%label_defaults));
821: $result.=&Apache::edit::rebuild_tag($token);
822: $result.=&Apache::edit::modifiedfield("/label",$parser);
823: }
824: return $result;
825: }
826:
827: sub end_label {
828: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
829: my $result = '';
830: if ($target eq 'web' || $target eq 'tex') {
831: } elsif ($target eq 'edit') {
832: $result.=&Apache::edit::tag_end($target,$token);
833: }
834: return $result;
835: }
836:
837: ##------------------------------------------------------------------- curve
838: sub start_curve {
839: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
840: my $result='';
841: &Apache::lonxml::register('Apache::lonplot',('function','data'));
842: push (@Apache::lonxml::namespace,'curve');
843: if ($target eq 'web' || $target eq 'tex') {
844: my %curve;
845: &get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
846: $tagstack->[-1]);
847: push (@curves,\%curve);
848: } elsif ($target eq 'edit') {
849: $result .= &Apache::edit::tag_start($target,$token,'Curve');
850: $result .= &edit_attributes($target,$token,\%curve_defaults,
851: \@curve_edit_order);
852: } elsif ($target eq 'modified') {
853: my $constructtag=&Apache::edit::get_new_args
854: ($token,$parstack,$safeeval,keys(%curve_defaults));
855: if ($constructtag) {
856: $result = &Apache::edit::rebuild_tag($token);
857: $result.= &Apache::edit::handle_insert();
858: }
859: }
860: return $result;
861: }
862:
863: sub end_curve {
864: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
865: my $result = '';
866: pop @Apache::lonxml::namespace;
867: &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
868: if ($target eq 'web' || $target eq 'tex') {
869: } elsif ($target eq 'edit') {
870: $result.=&Apache::edit::tag_end($target,$token);
871: }
872: return $result;
873: }
874:
875: ##------------------------------------------------------------ curve function
876: sub start_function {
877: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
878: my $result='';
879: if ($target eq 'web' || $target eq 'tex') {
880: if (exists($curves[-1]->{'data'})) {
881: &Apache::lonxml::warning
882: ('Use of the <b>curve function</b> tag precludes use of '.
883: ' the <b>curve data</b> tag. '.
884: 'The curve data tag will be omitted in favor of the '.
885: 'curve function declaration.');
886: delete $curves[-1]->{'data'} ;
887: }
888: my $function = &Apache::lonxml::get_all_text("/function",$parser);
889: $function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
890: $curves[-1]->{'function'} = $function;
891: } elsif ($target eq 'edit') {
892: $result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
893: my $text = &Apache::lonxml::get_all_text("/function",$parser);
894: $result .= &Apache::edit::end_row().
895: &Apache::edit::start_spanning_row().
896: &Apache::edit::editline('',$text,'',60);
897: } elsif ($target eq 'modified') {
898: $result.=&Apache::edit::rebuild_tag($token);
899: $result.=&Apache::edit::modifiedfield("/function",$parser);
900: }
901: return $result;
902: }
903:
904: sub end_function {
905: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
906: my $result = '';
907: if ($target eq 'web' || $target eq 'tex') {
908: } elsif ($target eq 'edit') {
909: $result .= &Apache::edit::end_table();
910: }
911: return $result;
912: }
913:
914: ##------------------------------------------------------------ curve data
915: sub start_data {
916: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
917: my $result='';
918: if ($target eq 'web' || $target eq 'tex') {
919: if (exists($curves[-1]->{'function'})) {
920: &Apache::lonxml::warning
921: ('Use of the <b>curve function</b> tag precludes use of '.
922: ' the <b>curve data</b> tag. '.
923: 'The curve function tag will be omitted in favor of the '.
924: 'curve data declaration.');
925: delete($curves[-1]->{'function'});
926: }
927: my $datatext = &Apache::lonxml::get_all_text("/data",$parser);
928: $datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
929: # Deal with cases where we're given an array...
930: if ($datatext =~ /^\@/) {
931: $datatext = &Apache::run::run('return "'.$datatext.'"',
932: $safeeval,1);
933: }
934: $datatext =~ s/\s+/ /g;
935: # Need to do some error checking on the @data array -
936: # make sure it's all numbers and make sure each array
937: # is of the same length.
938: my @data;
939: if ($datatext =~ /,/) { # comma deliminated
940: @data = split /,/,$datatext;
941: } else { # Assume it's space separated.
942: @data = split / /,$datatext;
943: }
944: for (my $i=0;$i<=$#data;$i++) {
945: # Check that it's non-empty
946: if (! defined($data[$i])) {
947: &Apache::lonxml::warning(
948: 'undefined curve data value. Replacing with '.
949: ' pi/e = 1.15572734979092');
950: $data[$i] = 1.15572734979092;
951: }
952: # Check that it's a number
953: if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
954: &Apache::lonxml::warning(
955: 'Bad curve data value of '.$data[$i].' Replacing with '.
956: ' pi/e = 1.15572734979092');
957: $data[$i] = 1.15572734979092;
958: }
959: }
960: # complain if the number of data points is not the same as
961: # in previous sets of data.
962: if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
963: &Apache::lonxml::warning
964: ('Number of data points is not consistent with previous '.
965: 'number of data points');
966: }
967: push @{$curves[-1]->{'data'}},\@data;
968: } elsif ($target eq 'edit') {
969: $result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
970: my $text = &Apache::lonxml::get_all_text("/data",$parser);
971: $result .= &Apache::edit::end_row().
972: &Apache::edit::start_spanning_row().
973: &Apache::edit::editline('',$text,'',60);
974: } elsif ($target eq 'modified') {
975: $result.=&Apache::edit::rebuild_tag($token);
976: $result.=&Apache::edit::modifiedfield("/data",$parser);
977: }
978: return $result;
979: }
980:
981: sub end_data {
982: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
983: my $result = '';
984: if ($target eq 'web' || $target eq 'tex') {
985: } elsif ($target eq 'edit') {
986: $result .= &Apache::edit::end_table();
987: }
988: return $result;
989: }
990:
991: ##------------------------------------------------------------------- axis
992: sub start_axis {
993: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
994: my $result='';
995: if ($target eq 'web' || $target eq 'tex') {
996: &get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
997: $tagstack->[-1]);
998: } elsif ($target eq 'edit') {
999: $result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1000: $result .= &edit_attributes($target,$token,\%axis_defaults,
1001: \@axis_edit_order);
1002: } elsif ($target eq 'modified') {
1003: my $constructtag=&Apache::edit::get_new_args
1004: ($token,$parstack,$safeeval,keys(%axis_defaults));
1005: if ($constructtag) {
1006: $result = &Apache::edit::rebuild_tag($token);
1007: }
1008: }
1009: return $result;
1010: }
1011:
1012: sub end_axis {
1013: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1014: my $result = '';
1015: if ($target eq 'web' || $target eq 'tex') {
1016: } elsif ($target eq 'edit') {
1017: $result.=&Apache::edit::tag_end($target,$token);
1018: } elsif ($target eq 'modified') {
1019: }
1020: return $result;
1021: }
1022:
1023: ###################################################################
1024: ## ##
1025: ## Utility Functions ##
1026: ## ##
1027: ###################################################################
1028:
1029: ##----------------------------------------------------------- set_defaults
1030: sub set_defaults {
1031: my ($var,$defaults) = @_;
1032: my $key;
1033: foreach $key (keys(%$defaults)) {
1034: $var->{$key} = $defaults->{$key}->{'default'};
1035: }
1036: }
1037:
1038: ##------------------------------------------------------------------- misc
1039: sub get_attributes{
1040: my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1041: foreach my $attr (keys(%{$defaults})) {
1042: if ($attr eq 'texwidth' || $attr eq 'texfont') {
1043: $values->{$attr} =
1044: &Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
1045: } else {
1046: $values->{$attr} =
1047: &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1048: }
1049: if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1050: $values->{$attr} = $defaults->{$attr}->{'default'};
1051: next;
1052: }
1053: my $test = $defaults->{$attr}->{'test'};
1054: if (! &$test($values->{$attr})) {
1055: &Apache::lonxml::warning
1056: ($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1057: .$defaults->{$attr}->{'default'} );
1058: $values->{$attr} = $defaults->{$attr}->{'default'};
1059: }
1060: }
1061: return ;
1062: }
1063:
1064: ##------------------------------------------------------- write_gnuplot_file
1065: sub write_gnuplot_file {
1066: my ($tmpdir,$filename,$target)= @_;
1067: my $gnuplot_input = '';
1068: my $curve;
1069: my $pt = $Apache::lonplot::plot{'texfont'};
1070: #
1071: # Check to be sure we do not have any empty curves
1072: my @curvescopy;
1073: foreach my $curve (@curves) {
1074: if (exists($curve->{'function'})) {
1075: if ($curve->{'function'} !~ /^\s*$/) {
1076: push(@curvescopy,$curve);
1077: }
1078: } elsif (exists($curve->{'data'})) {
1079: foreach my $data (@{$curve->{'data'}}) {
1080: if (scalar(@$data) > 0) {
1081: push(@curvescopy,$curve);
1082: last;
1083: }
1084: }
1085: }
1086: }
1087: @curves = @curvescopy;
1088: # Collect all the colors
1089: my @Colors;
1090: push @Colors, $Apache::lonplot::plot{'bgcolor'};
1091: push @Colors, $Apache::lonplot::plot{'fgcolor'};
1092: push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
1093: foreach $curve (@curves) {
1094: push @Colors, ($curve->{'color'} ne '' ?
1095: $curve->{'color'} :
1096: $Apache::lonplot::plot{'fgcolor'} );
1097: }
1098: # set term
1099: if ($target eq 'web') {
1100: $gnuplot_input .= 'set term '.$weboutputformat .' ';
1101: $gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
1102: $gnuplot_input .= $Apache::lonplot::plot{'font'} . ' ';
1103: $gnuplot_input .= 'size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
1104: $gnuplot_input .= "@Colors\n";
1105: # set output
1106: $gnuplot_input .= "set output\n";
1107: } elsif ($target eq 'tex') {
1108: $gnuplot_input .= "set term postscript eps $Apache::lonplot::plot{'plotcolor'} solid \"Helvetica\" $pt \n";
1109: $gnuplot_input .= "set output \"/home/httpd/perl/tmp/".
1110: &Apache::lonnet::unescape($filename).".eps\"\n";
1111: }
1112: # cartesian or polar?
1113: if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
1114: $gnuplot_input .= 'set polar'.$/;
1115: } else {
1116: # Assume Cartesian
1117: }
1118: # solid or pattern for boxes?
1119: if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
1120: $gnuplot_input .= 'set style fill solid '.
1121: $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
1122: } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
1123: $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
1124: } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
1125: }
1126: # margin
1127: if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
1128: $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
1129: }
1130: if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
1131: $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
1132: }
1133: if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
1134: $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
1135: }
1136: if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
1137: $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
1138: }
1139: # tic scales
1140: $gnuplot_input .= 'set ticscale '.
1141: $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
1142: #boxwidth
1143: if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
1144: $gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
1145: }
1146: # gridlayer
1147: $gnuplot_input .= 'set grid noxtics noytics front '.$/
1148: if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
1149:
1150: # grid
1151: $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
1152: # border
1153: $gnuplot_input .= ($Apache::lonplot::plot{'border'} eq 'on'?
1154: 'set border'.$/ :
1155: 'set noborder'.$/ );
1156: # sampling rate for non-data curves
1157: $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
1158: # title, xlabel, ylabel
1159: # titles
1160: if ($target eq 'tex') {
1161: $gnuplot_input .= "set title \"$title\" font \"Helvetica,".$pt."pt\"\n" if (defined($title)) ;
1162: $gnuplot_input .= "set xlabel \"$xlabel\" font \"Helvetica,".$pt."pt\" \n" if (defined($xlabel));
1163: $gnuplot_input .= "set ylabel \"$ylabel\" font \"Helvetica,".$pt."pt\"\n" if (defined($ylabel));
1164: } else {
1165: $gnuplot_input .= "set title \"$title\" \n" if (defined($title)) ;
1166: $gnuplot_input .= "set xlabel \"$xlabel\" \n" if (defined($xlabel));
1167: $gnuplot_input .= "set ylabel \"$ylabel\" \n" if (defined($ylabel));
1168: }
1169: # tics
1170: if (%xtics) {
1171: $gnuplot_input .= "set xtics $xtics{'location'} ";
1172: $gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1173: $gnuplot_input .= "$xtics{'start'}, ";
1174: $gnuplot_input .= "$xtics{'increment'}, ";
1175: $gnuplot_input .= "$xtics{'end'}\n";
1176: if ($xtics{'minorfreq'} != 0) {
1177: $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
1178: }
1179: }
1180: if (%ytics) {
1181: $gnuplot_input .= "set ytics $ytics{'location'} ";
1182: $gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1183: $gnuplot_input .= "$ytics{'start'}, ";
1184: $gnuplot_input .= "$ytics{'increment'}, ";
1185: $gnuplot_input .= "$ytics{'end'}\n";
1186: if ($ytics{'minorfreq'} != 0) {
1187: $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
1188: }
1189: }
1190: # axis
1191: if (%axis) {
1192: $gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
1193: $gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1194: }
1195: # Key
1196: if (%key) {
1197: $gnuplot_input .= 'set key '.$key{'pos'}.' ';
1198: if ($key{'title'} ne '') {
1199: $gnuplot_input .= 'title "'.$key{'title'}.'" ';
1200: }
1201: $gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1202: } else {
1203: $gnuplot_input .= 'set nokey'.$/;
1204: }
1205: # labels
1206: my $label;
1207: foreach $label (@labels) {
1208: $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1209: $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'};
1210: if ($target eq 'tex') {
1211: $gnuplot_input .=' font "Helvetica,'.$pt.'pt"' ;
1212: }
1213: $gnuplot_input .= $/;
1214: }
1215: if ($target eq 'tex') {
1216: $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
1217: $gnuplot_input .="\n";
1218: }
1219: # curves
1220: $gnuplot_input .= 'plot ';
1221: for (my $i = 0;$i<=$#curves;$i++) {
1222: $curve = $curves[$i];
1223: $gnuplot_input.= ', ' if ($i > 0);
1224: if (exists($curve->{'function'})) {
1225: $gnuplot_input.=
1226: $curve->{'function'}.' title "'.
1227: $curve->{'name'}.'" with '.
1228: $curve->{'linestyle'};
1229: $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
1230: if (($curve->{'linestyle'} eq 'points') ||
1231: ($curve->{'linestyle'} eq 'linespoints') ||
1232: ($curve->{'linestyle'} eq 'errorbars') ||
1233: ($curve->{'linestyle'} eq 'xerrorbars') ||
1234: ($curve->{'linestyle'} eq 'yerrorbars') ||
1235: ($curve->{'linestyle'} eq 'xyerrorbars')) {
1236: $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1237: $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
1238: } elsif ($curve->{'linestyle'} eq 'filledcurves') {
1239: $gnuplot_input.= ' '.$curve->{'limit'};
1240: }
1241: } elsif (exists($curve->{'data'})) {
1242: # Store data values in $datatext
1243: my $datatext = '';
1244: # get new filename
1245: my $datafilename = "$tmpdir/$filename.data.$i";
1246: my $fh=Apache::File->new(">$datafilename");
1247: # Compile data
1248: my @Data = @{$curve->{'data'}};
1249: my @Data0 = @{$Data[0]};
1250: for (my $i =0; $i<=$#Data0; $i++) {
1251: my $dataset;
1252: foreach $dataset (@Data) {
1253: $datatext .= $dataset->[$i] . ' ';
1254: }
1255: $datatext .= $/;
1256: }
1257: # write file
1258: print $fh $datatext;
1259: close ($fh);
1260: # generate gnuplot text
1261: $gnuplot_input.= '"'.$datafilename.'" title "'.
1262: $curve->{'name'}.'" with '.
1263: $curve->{'linestyle'};
1264: $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
1265: if (($curve->{'linestyle'} eq 'points') ||
1266: ($curve->{'linestyle'} eq 'linespoints') ||
1267: ($curve->{'linestyle'} eq 'errorbars') ||
1268: ($curve->{'linestyle'} eq 'xerrorbars') ||
1269: ($curve->{'linestyle'} eq 'yerrorbars') ||
1270: ($curve->{'linestyle'} eq 'xyerrorbars')) {
1271: $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1272: $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
1273: } elsif ($curve->{'linestyle'} eq 'filledcurves') {
1274: $gnuplot_input.= ' '.$curve->{'limit'};
1275: }
1276: }
1277: }
1278: # Write the output to a file.
1279: my $fh=Apache::File->new(">$tmpdir$filename.data");
1280: print $fh $gnuplot_input;
1281: close($fh);
1282: # That's all folks.
1283: return ;
1284: }
1285:
1286: #---------------------------------------------- check_inputs
1287: sub check_inputs {
1288: ## Note: no inputs, no outputs - this acts only on global variables.
1289: ## Make sure we have all the input we need:
1290: if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
1291: if (! %key ) {} # No key for this plot, thats okay
1292: # if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1293: if (! defined($title )) {} # No title for this plot, thats okay
1294: if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
1295: if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
1296: if ($#labels < 0) { } # No labels for this plot, thats okay
1297: if ($#curves < 0) {
1298: &Apache::lonxml::warning("No curves specified for plot!!!!");
1299: return '';
1300: }
1301: my $curve;
1302: foreach $curve (@curves) {
1303: if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
1304: &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
1305: return '';
1306: }
1307: }
1308: }
1309:
1310: #------------------------------------------------ make_edit
1311: sub edit_attributes {
1312: my ($target,$token,$defaults,$keys) = @_;
1313: my ($result,@keys);
1314: if ($keys && ref($keys) eq 'ARRAY') {
1315: @keys = @$keys;
1316: } else {
1317: @keys = sort(keys(%$defaults));
1318: }
1319: foreach my $attr (@keys) {
1320: # append a ' ' to the description if it doesn't have one already.
1321: my $description = $defaults->{$attr}->{'description'};
1322: $description .= ' ' if ($description !~ / $/);
1323: if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1324: $result .= &Apache::edit::text_arg
1325: ($description,$attr,$token,
1326: $defaults->{$attr}->{'size'});
1327: } elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1328: $result .= &Apache::edit::select_or_text_arg
1329: ($description,$attr,$defaults->{$attr}->{'choices'},$token);
1330: } elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1331: $result .= &Apache::edit::select_or_text_arg
1332: ($description,$attr,['on','off'],$token);
1333: }
1334: $result .= '<br />';
1335: }
1336: return $result;
1337: }
1338:
1339:
1340: ###################################################################
1341: ## ##
1342: ## Insertion functions for editing plots ##
1343: ## ##
1344: ###################################################################
1345:
1346: sub insert_gnuplot {
1347: my $result = '';
1348: # plot attributes
1349: $result .= "\n<gnuplot ";
1350: foreach my $attr (keys(%gnuplot_defaults)) {
1351: $result .= "\n $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
1352: }
1353: $result .= ">";
1354: # Add the components (most are commented out for simplicity)
1355: # $result .= &insert_key();
1356: # $result .= &insert_axis();
1357: # $result .= &insert_title();
1358: # $result .= &insert_xlabel();
1359: # $result .= &insert_ylabel();
1360: $result .= &insert_curve();
1361: # close up the <gnuplot>
1362: $result .= "\n</gnuplot>";
1363: return $result;
1364: }
1365:
1366: sub insert_tics {
1367: my $result;
1368: $result .= &insert_xtics() . &insert_ytics;
1369: return $result;
1370: }
1371:
1372: sub insert_xtics {
1373: my $result;
1374: $result .= "\n <xtics ";
1375: foreach my $attr (keys(%tic_defaults)) {
1376: $result .= "\n $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1377: }
1378: $result .= "/>";
1379: return $result;
1380: }
1381:
1382: sub insert_ytics {
1383: my $result;
1384: $result .= "\n <ytics ";
1385: foreach my $attr (keys(%tic_defaults)) {
1386: $result .= "\n $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1387: }
1388: $result .= "/>";
1389: return $result;
1390: }
1391:
1392: sub insert_key {
1393: my $result;
1394: $result .= "\n <key ";
1395: foreach my $attr (keys(%key_defaults)) {
1396: $result .= "\n $attr=\"$key_defaults{$attr}->{'default'}\"";
1397: }
1398: $result .= " />";
1399: return $result;
1400: }
1401:
1402: sub insert_axis{
1403: my $result;
1404: $result .= "\n <axis ";
1405: foreach my $attr (keys(%axis_defaults)) {
1406: $result .= "\n $attr=\"$axis_defaults{$attr}->{'default'}\"";
1407: }
1408: $result .= " />";
1409: return $result;
1410: }
1411:
1412: sub insert_title { return "\n <title></title>"; }
1413: sub insert_xlabel { return "\n <xlabel></xlabel>"; }
1414: sub insert_ylabel { return "\n <ylabel></ylabel>"; }
1415:
1416: sub insert_label {
1417: my $result;
1418: $result .= "\n <label ";
1419: foreach my $attr (keys(%label_defaults)) {
1420: $result .= "\n $attr=\"".
1421: $label_defaults{$attr}->{'default'}."\"";
1422: }
1423: $result .= "></label>";
1424: return $result;
1425: }
1426:
1427: sub insert_curve {
1428: my $result;
1429: $result .= "\n <curve ";
1430: foreach my $attr (keys(%curve_defaults)) {
1431: $result .= "\n $attr=\"".
1432: $curve_defaults{$attr}->{'default'}."\"";
1433: }
1434: $result .= " >";
1435: $result .= &insert_data().&insert_data()."\n </curve>";
1436: }
1437:
1438: sub insert_function {
1439: my $result;
1440: $result .= "\n <function></function>";
1441: return $result;
1442: }
1443:
1444: sub insert_data {
1445: my $result;
1446: $result .= "\n <data></data>";
1447: return $result;
1448: }
1449:
1450: ##----------------------------------------------------------------------
1451: 1;
1452: __END__
1453:
1454:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>