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