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