File:
[LON-CAPA] /
loncom /
xml /
lonplot.pm
Revision
1.139:
download - view:
text,
annotated -
select for diffs
Mon May 19 11:49:48 2008 UTC (15 years, 11 months ago) by
foxr
Branches:
MAIN
CVS tags:
HEAD
Added a pile more entitities.. finished the accented character set,
Latin extended A and Latin extended B..
and fleshed out the greek symbol set with some of the stranger ones.
1: # The LearningOnline Network with CAPA
2: # Dynamic plot
3: #
4: # $Id: lonplot.pm,v 1.139 2008/05/19 11:49:48 foxr 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 LONCAPA;
40:
41:
42: use vars qw/$weboutputformat $version/;
43:
44:
45:
46: BEGIN {
47: &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
48: #
49: # Determine the version of GNUPLOT
50: $weboutputformat = 'gif';
51: my $versionstring = `gnuplot --version 2>/dev/null`;
52: ($version) = ($versionstring =~ /^gnuplot ([\d.]+)/);
53: if ($version >= 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 %linetypes =
95: (
96: solid => 1,
97: dashed => 0
98: );
99:
100: my %linestyles =
101: (
102: lines => 2, # Maybe this will be used in the future
103: linespoints => 2, # to check on whether or not they have
104: dots => 2, # supplied enough <data></data> fields
105: points => 2, # to use the given line style. But for
106: steps => 2, # now there are more important things
107: fsteps => 2, # for me to deal with.
108: histeps => 2,
109: errorbars => 3,
110: xerrorbars => [3,4],
111: yerrorbars => [3,4],
112: xyerrorbars => [4,6],
113: boxes => 3,
114: filledcurves => 2,
115: vector => 4
116: );
117:
118: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
119: my $real_test =
120: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
121: my $pos_real_test =
122: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
123: my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-fA-F]{6}$/};
124: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
125: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
126: my $sml_test = sub {$_[0]=~/^(\d+|small|medium|large)$/};
127: my $linestyle_test = sub {exists($linestyles{$_[0]})};
128: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
129:
130: ###################################################################
131: ## ##
132: ## Attribute metadata ##
133: ## ##
134: ###################################################################
135: my @gnuplot_edit_order =
136: qw/alttag bgcolor fgcolor height width texwidth fontface font texfont
137: transparent grid samples
138: border align plotcolor plottype gridtype lmargin rmargin
139: tmargin bmargin major_ticscale minor_ticscale boxwidth gridlayer fillstyle
140: pattern solid/;
141:
142: my $margin_choices = ['default',0..20];
143:
144: my %gnuplot_defaults =
145: (
146: alttag => {
147: default => 'dynamically generated plot',
148: test => $words_test,
149: description => 'Brief description of the plot',
150: edit_type => 'entry',
151: size => '40'
152: },
153: height => {
154: default => 300,
155: test => $int_test,
156: description => 'Height of image (pixels)',
157: edit_type => 'entry',
158: size => '10'
159: },
160: width => {
161: default => 400,
162: test => $int_test,
163: description => 'Width of image (pixels)',
164: edit_type => 'entry',
165: size => '10'
166: },
167: bgcolor => {
168: default => 'xffffff',
169: test => $color_test,
170: description => 'Background color of image (xffffff)',
171: edit_type => 'entry',
172: size => '10'
173: },
174: fgcolor => {
175: default => 'x000000',
176: test => $color_test,
177: description => 'Foreground color of image (x000000)',
178: edit_type => 'entry',
179: size => '10'
180: },
181: transparent => {
182: default => 'off',
183: test => $onoff_test,
184: description => 'Transparent image',
185: edit_type => 'onoff'
186: },
187: grid => {
188: default => 'on',
189: test => $onoff_test,
190: description => 'Display grid',
191: edit_type => 'onoff'
192: },
193: gridlayer => {
194: default => 'off',
195: test => $onoff_test,
196: description => 'Display grid front layer over filled boxes or filled curves',
197: edit_type => 'onoff'
198: },
199: box_border => {
200: default => 'noborder',
201: test => sub {$_[0]=~/^(noborder|border)$/},
202: description => 'Draw border for boxes',
203: edit_type => 'choice',
204: choices => ['border','noborder']
205: },
206: border => {
207: default => 'on',
208: test => $onoff_test,
209: description => 'Draw border around plot',
210: edit_type => 'onoff'
211: },
212: font => {
213: default => '9',
214: test => $sml_test,
215: description => 'Font size to use in web output (pts)',
216: edit_type => 'choice',
217: choices => [['5','5 (small)'],'6','7','8',['9','9 (medium)'],'10',['11','11 (large)'],'12','15']
218: },
219: fontface => {
220: default => 'sans-serif',
221: test => sub {$_[0]=~/^(sans-serif|serif|classic)$/},
222: description => 'Type of font to use',
223: edit_type => 'choice',
224: choices => ['sans-serif','serif', 'classic']
225: },
226: samples => {
227: default => '100',
228: test => $int_test,
229: description => 'Number of samples for non-data plots',
230: edit_type => 'choice',
231: choices => ['100','200','500','1000','2000','5000']
232: },
233: align => {
234: default => 'middle',
235: test => sub {$_[0]=~/^(left|right|middle|center)$/},
236: description => 'Alignment for image in HTML',
237: edit_type => 'choice',
238: choices => ['left','right','middle']
239: },
240: texwidth => {
241: default => '93',
242: test => $int_test,
243: description => 'Width of plot when printed (mm)',
244: edit_type => 'entry',
245: size => '5'
246: },
247: texfont => {
248: default => '22',
249: test => $int_test,
250: description => 'Font size to use in TeX output (pts):',
251: edit_type => 'choice',
252: choices => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
253: },
254: plotcolor => {
255: default => 'monochrome',
256: test => sub {$_[0]=~/^(monochrome|color|colour)$/},
257: description => 'Color setting for printing:',
258: edit_type => 'choice',
259: choices => [qw/monochrome color colour/],
260: },
261: pattern => {
262: default => '',
263: test => $int_test,
264: description => 'Pattern value for boxes:',
265: edit_type => 'choice',
266: choices => [0,1,2,3,4,5,6]
267: },
268: solid => {
269: default => 0,
270: test => $real_test,
271: description => 'The density of fill style for boxes',
272: edit_type => 'entry',
273: size => '5'
274: },
275: fillstyle => {
276: default => 'empty',
277: test => sub {$_[0]=~/^(empty|solid|pattern)$/},
278: description => 'Filled style for boxes:',
279: edit_type => 'choice',
280: choices => ['empty','solid','pattern']
281: },
282: plottype => {
283: default => 'Cartesian',
284: test => sub {$_[0]=~/^(Polar|Cartesian)$/},
285: description => 'Plot type:',
286: edit_type => 'choice',
287: choices => ['Cartesian','Polar']
288: },
289: gridtype => {
290: default => 'Cartesian',
291: test => sub {$_[0]=~/^(Polar|Cartesian|Linear-Log|Log-Linear|Log-Log)$/},
292: description => 'Grid type:',
293: edit_type => 'choice',
294: choices => ['Cartesian','Polar','Linear-Log','Log-Linear','Log-Log']
295: },
296: lmargin => {
297: default => 'default',
298: test => sub {$_[0]=~/^(default|\d+)$/},
299: description => 'Left margin width (pts):',
300: edit_type => 'choice',
301: choices => $margin_choices,
302: },
303: rmargin => {
304: default => 'default',
305: test => sub {$_[0]=~/^(default|\d+)$/},
306: description => 'Right margin width (pts):',
307: edit_type => 'choice',
308: choices => $margin_choices,
309: },
310: tmargin => {
311: default => 'default',
312: test => sub {$_[0]=~/^(default|\d+)$/},
313: description => 'Top margin width (pts):',
314: edit_type => 'choice',
315: choices => $margin_choices,
316: },
317: bmargin => {
318: default => 'default',
319: test => sub {$_[0]=~/^(default|\d+)$/},
320: description => 'Bottom margin width (pts):',
321: edit_type => 'choice',
322: choices => $margin_choices,
323: },
324: boxwidth => {
325: default => '',
326: test => $real_test,
327: description => 'Width of boxes, default is auto',
328: edit_type => 'entry',
329: size => '5'
330: },
331: major_ticscale => {
332: default => '1',
333: test => $real_test,
334: description => 'Size of major tic marks (plot coordinates)',
335: edit_type => 'entry',
336: size => '5'
337: },
338: minor_ticscale => {
339: default => '0.5',
340: test => $real_test,
341: description => 'Size of minor tic mark (plot coordinates)',
342: edit_type => 'entry',
343: size => '5'
344: },
345: );
346:
347: my %key_defaults =
348: (
349: title => {
350: default => '',
351: test => $words_test,
352: description => 'Title of key',
353: edit_type => 'entry',
354: size => '40'
355: },
356: box => {
357: default => 'off',
358: test => $onoff_test,
359: description => 'Draw a box around the key?',
360: edit_type => 'onoff'
361: },
362: pos => {
363: default => 'top right',
364: test => $key_pos_test,
365: description => 'Position of the key on the plot',
366: edit_type => 'choice',
367: choices => ['top left','top right','bottom left','bottom right',
368: 'outside','below']
369: }
370: );
371:
372: my %label_defaults =
373: (
374: xpos => {
375: default => 0,
376: test => $real_test,
377: description => 'X position of label (graph coordinates)',
378: edit_type => 'entry',
379: size => '10'
380: },
381: ypos => {
382: default => 0,
383: test => $real_test,
384: description => 'Y position of label (graph coordinates)',
385: edit_type => 'entry',
386: size => '10'
387: },
388: justify => {
389: default => 'left',
390: test => sub {$_[0]=~/^(left|right|center)$/},
391: description => 'justification of the label text on the plot',
392: edit_type => 'choice',
393: choices => ['left','right','center']
394: },
395: rotate => {
396: default => 0,
397: test => $real_test,
398: description => 'Rotation of label (degrees)',
399: edit_type => 'entry',
400: size => '10',
401: }
402: );
403:
404: my @tic_edit_order = ('location','mirror','start','increment','end',
405: 'minorfreq');
406: my %tic_defaults =
407: (
408: location => {
409: default => 'border',
410: test => sub {$_[0]=~/^(border|axis)$/},
411: description => 'Location of major tic marks',
412: edit_type => 'choice',
413: choices => ['border','axis']
414: },
415: mirror => {
416: default => 'on',
417: test => $onoff_test,
418: description => 'Mirror tics on opposite axis?',
419: edit_type => 'onoff'
420: },
421: start => {
422: default => '-10.0',
423: test => $real_test,
424: description => 'Start major tics at',
425: edit_type => 'entry',
426: size => '10'
427: },
428: increment => {
429: default => '1.0',
430: test => $real_test,
431: description => 'Place a major tic every',
432: edit_type => 'entry',
433: size => '10'
434: },
435: end => {
436: default => ' 10.0',
437: test => $real_test,
438: description => 'Stop major tics at ',
439: edit_type => 'entry',
440: size => '10'
441: },
442: minorfreq => {
443: default => '0',
444: test => $int_test,
445: description => 'Number of minor tics per major tic mark',
446: edit_type => 'entry',
447: size => '10'
448: },
449: );
450:
451: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax','xformat', 'yformat');
452: my %axis_defaults =
453: (
454: color => {
455: default => 'x000000',
456: test => $color_test,
457: description => 'Color of grid lines (x000000)',
458: edit_type => 'entry',
459: size => '10'
460: },
461: xmin => {
462: default => '-10.0',
463: test => $real_test,
464: description => 'Minimum x-value shown in plot',
465: edit_type => 'entry',
466: size => '10'
467: },
468: xmax => {
469: default => ' 10.0',
470: test => $real_test,
471: description => 'Maximum x-value shown in plot',
472: edit_type => 'entry',
473: size => '10'
474: },
475: ymin => {
476: default => '-10.0',
477: test => $real_test,
478: description => 'Minimum y-value shown in plot',
479: edit_type => 'entry',
480: size => '10'
481: },
482: ymax => {
483: default => ' 10.0',
484: test => $real_test,
485: description => 'Maximum y-value shown in plot',
486: edit_type => 'entry',
487: size => '10'
488: },
489: xformat => {
490: default => 'on',
491: test => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
492: description => 'X-axis number formatting',
493: edit_type => 'choice',
494: choices => ['on', 'off', '2e', '2f'],
495: },
496: yformat => {
497: default => 'on',
498: test => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
499: description => 'X-axis number formatting',
500: edit_type => 'choice',
501: choices => ['on', 'off', '2e', '2f'],
502: },
503:
504: );
505:
506: my @curve_edit_order = ('color','name','linestyle','linewidth','linetype','pointtype','pointsize','limit');
507:
508: my %curve_defaults =
509: (
510: color => {
511: default => 'x000000',
512: test => $color_test,
513: description => 'Color of curve (x000000)',
514: edit_type => 'entry',
515: size => '10'
516: },
517: name => {
518: default => '',
519: test => $words_test,
520: description => 'Name of curve to appear in key',
521: edit_type => 'entry',
522: size => '20'
523: },
524: linestyle => {
525: default => 'lines',
526: test => $linestyle_test,
527: description => 'Plot with:',
528: edit_type => 'choice',
529: choices => [keys(%linestyles)]
530: },
531: linewidth => {
532: default => 1,
533: test => $int_test,
534: description => 'Line width (may not apply to all plot styles)',
535: edit_type => 'choice',
536: choices => [1,2,3,4,5,6,7,8,9,10]
537: },
538: linetype => {
539: default => 'solid',
540: test => sub {$_[0]=~/^(solid|dashed)$/},
541: description => 'Line type (may not apply to all plot styles)',
542: edit_type => 'choice',
543: choices => ['solid', 'dashed']
544: },
545: pointsize => {
546: default => 1,
547: test => $pos_real_test,
548: description => 'Point size (may not apply to all plot styles)',
549: edit_type => 'entry',
550: size => '5'
551: },
552: pointtype => {
553: default => 1,
554: test => $int_test,
555: description => 'Point type (may not apply to all plot styles)',
556: edit_type => 'choice',
557: choices => [0,1,2,3,4,5,6]
558: },
559: limit => {
560: default => 'closed',
561: test => sub {$_[0]=~/^(above|below|closed|x1|x2|y1|y2)$/},
562: description => 'Point to fill -- for filledcurves',
563: edit_type => 'choice',
564: choices => ['above', 'below', 'closed','x1','x2','y1','y2']
565: },
566: );
567:
568: ###################################################################
569: ## ##
570: ## parsing and edit rendering ##
571: ## ##
572: ###################################################################
573:
574: undef %Apache::lonplot::plot;
575: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
576:
577: sub start_gnuplot {
578: undef(%Apache::lonplot::plot); undef(%key); undef(%axis);
579: undef($title); undef($xlabel); undef($ylabel);
580: undef(@labels); undef(@curves);
581: undef(%xtics); undef(%ytics);
582: #
583: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
584: my $result='';
585: &Apache::lonxml::register('Apache::lonplot',
586: ('title','xlabel','ylabel','key','axis','label','curve',
587: 'xtics','ytics'));
588: push (@Apache::lonxml::namespace,'lonplot');
589: if ($target eq 'web' || $target eq 'tex') {
590: &get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
591: $tagstack->[-1]);
592: } elsif ($target eq 'edit') {
593: $result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
594: $result .= &edit_attributes($target,$token,\%gnuplot_defaults,
595: \@gnuplot_edit_order)
596: .&Apache::edit::end_row()
597: .&Apache::edit::start_spanning_row();
598: } elsif ($target eq 'modified') {
599: my $constructtag=&Apache::edit::get_new_args
600: ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
601: if ($constructtag) {
602: $result = &Apache::edit::rebuild_tag($token);
603: }
604: }
605: return $result;
606: }
607:
608: sub end_gnuplot {
609: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
610: pop @Apache::lonxml::namespace;
611: &Apache::lonxml::deregister('Apache::lonplot',
612: ('title','xlabel','ylabel','key','axis','label','curve'));
613: my $result = '';
614: my $randnumber;
615: # need to call rand everytime start_script would evaluate, as the
616: # safe space rand number generator and the global rand generator
617: # are not separate
618: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
619: $target eq 'answer') {
620: $randnumber=int(rand(1000));
621: }
622: if ($target eq 'web' || $target eq 'tex') {
623: &check_inputs(); # Make sure we have all the data we need
624: ##
625: ## Determine filename
626: my $tmpdir = '/home/httpd/perl/tmp/';
627: my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
628: '_'.time.'_'.$$.$randnumber.'_plot';
629: ## Write the plot description to the file
630: &write_gnuplot_file($tmpdir,$filename,$target);
631: $filename = &escape($filename);
632: ## return image tag for the plot
633: if ($target eq 'web') {
634: $result .= <<"ENDIMAGE";
635: <img src = "/cgi-bin/plot.$weboutputformat?file=$filename.data"
636: width = "$Apache::lonplot::plot{'width'}"
637: height = "$Apache::lonplot::plot{'height'}"
638: align = "$Apache::lonplot::plot{'align'}"
639: alt = "$Apache::lonplot::plot{'alttag'}" />
640: ENDIMAGE
641: } elsif ($target eq 'tex') {
642: &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
643: &Apache::lonxml::debug(" gnuplot ht = $Apache::lonplot::plot{'height'}");
644: #might be inside the safe space, register the URL for later
645: &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
646: $result = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'}\n";
647: $result .= '\graphicspath{{/home/httpd/perl/tmp/}}'."\n";
648: $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&unescape($filename).'.eps}';
649: }
650: } elsif ($target eq 'edit') {
651: $result.=&Apache::edit::tag_end($target,$token);
652: }
653: return $result;
654: }
655:
656:
657: ##--------------------------------------------------------------- xtics
658: sub start_xtics {
659: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
660: my $result='';
661: if ($target eq 'web' || $target eq 'tex') {
662: &get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
663: $tagstack->[-1]);
664: } elsif ($target eq 'edit') {
665: $result .= &Apache::edit::tag_start($target,$token,'xtics');
666: $result .= &edit_attributes($target,$token,\%tic_defaults,
667: \@tic_edit_order);
668: } elsif ($target eq 'modified') {
669: my $constructtag=&Apache::edit::get_new_args
670: ($token,$parstack,$safeeval,keys(%tic_defaults));
671: if ($constructtag) {
672: $result = &Apache::edit::rebuild_tag($token);
673: }
674: }
675: return $result;
676: }
677:
678: sub end_xtics {
679: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
680: my $result = '';
681: if ($target eq 'web' || $target eq 'tex') {
682: } elsif ($target eq 'edit') {
683: $result.=&Apache::edit::tag_end($target,$token);
684: }
685: return $result;
686: }
687:
688: ##--------------------------------------------------------------- ytics
689: sub start_ytics {
690: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
691: my $result='';
692: if ($target eq 'web' || $target eq 'tex') {
693: &get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
694: $tagstack->[-1]);
695: } elsif ($target eq 'edit') {
696: $result .= &Apache::edit::tag_start($target,$token,'ytics');
697: $result .= &edit_attributes($target,$token,\%tic_defaults,
698: \@tic_edit_order);
699: } elsif ($target eq 'modified') {
700: my $constructtag=&Apache::edit::get_new_args
701: ($token,$parstack,$safeeval,keys(%tic_defaults));
702: if ($constructtag) {
703: $result = &Apache::edit::rebuild_tag($token);
704: }
705: }
706: return $result;
707: }
708:
709: sub end_ytics {
710: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
711: my $result = '';
712: if ($target eq 'web' || $target eq 'tex') {
713: } elsif ($target eq 'edit') {
714: $result.=&Apache::edit::tag_end($target,$token);
715: }
716: return $result;
717: }
718:
719: ##-----------------------------------------------------------------font
720: my %font_properties =
721: (
722: 'classic' => {
723: face => 'classic',
724: file => 'DejaVuSansMono-Bold',
725: printname => 'Helvetica',
726: tex_no_file => 1,
727: },
728: 'sans-serif' => {
729: face => 'sans-serif',
730: file => 'DejaVuSans',
731: printname => 'DejaVuSans',
732: },
733: 'serif' => {
734: face => 'serif',
735: file => 'DejaVuSerif',
736: printname => 'DejaVuSerif',
737: },
738: );
739:
740: sub get_font {
741: my ($target) = @_;
742: my ($size, $selected_font);
743:
744: if ( $Apache::lonplot::plot{'font'} =~ /^(small|medium|large)/) {
745: $selected_font = $font_properties{'classic'};
746: if ( $Apache::lonplot::plot{'font'} eq 'small') {
747: $size = '5';
748: } elsif ( $Apache::lonplot::plot{'font'} eq 'medium') {
749: $size = '9';
750: } elsif ( $Apache::lonplot::plot{'font'} eq 'large') {
751: $size = '11';
752: } else {
753: $size = '9';
754: }
755: } else {
756: $size = $Apache::lonplot::plot{'font'};
757: $selected_font = $font_properties{$Apache::lonplot::plot{'fontface'}};
758: }
759: if ($target eq 'tex' && defined($Apache::lonplot::plot{'texfont'})) {
760: $size = $Apache::lonplot::plot{'texfont'};
761: }
762: return ($size, $selected_font);
763: }
764:
765: ##----------------------------------------------------------------- key
766: sub start_key {
767: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
768: my $result='';
769: if ($target eq 'web' || $target eq 'tex') {
770: &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
771: $tagstack->[-1]);
772: } elsif ($target eq 'edit') {
773: $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
774: $result .= &edit_attributes($target,$token,\%key_defaults);
775: } elsif ($target eq 'modified') {
776: my $constructtag=&Apache::edit::get_new_args
777: ($token,$parstack,$safeeval,keys(%key_defaults));
778: if ($constructtag) {
779: $result = &Apache::edit::rebuild_tag($token);
780: }
781: }
782: return $result;
783: }
784:
785: sub end_key {
786: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
787: my $result = '';
788: if ($target eq 'web' || $target eq 'tex') {
789: } elsif ($target eq 'edit') {
790: $result.=&Apache::edit::tag_end($target,$token);
791: }
792: return $result;
793: }
794:
795: sub parse_label {
796: my ($target,$text) = @_;
797: my $parser=HTML::LCParser->new(\$text);
798: my $result;
799: while (my $token=$parser->get_token) {
800: if ($token->[0] eq 'S') {
801: if ($token->[1] eq 'sub') {
802: $result .= '_{';
803: } elsif ($token->[1] eq 'sup') {
804: $result .= '^{';
805: } else {
806: $result .= $token->[4];
807: }
808: } elsif ($token->[0] eq 'E') {
809: if ($token->[1] eq 'sub'
810: || $token->[1] eq 'sup') {
811: $result .= '}';
812: } else {
813: $result .= $token->[2];
814: }
815: } elsif ($token->[0] eq 'T') {
816: $result .= &replace_entities($target,$token->[1]);
817: }
818: }
819: return $result;
820: }
821:
822:
823: my %lookup =
824: ( # Greek alphabet:
825:
826: '(Alpha|#913)' => {'tex' => '{/Symbol A}', 'web' => "\x{391}"},
827: '(Beta|#914)' => {'tex' => '{/Symbol B}', 'web' => "\x{392}"},
828: '(Chi|#935)' => {'tex' => '{/Symbol C}', 'web' => "\x{3A7}"},
829: '(Delta|#916)' => {'tex' => '{/Symbol D}', 'web' => "\x{394}"},
830: '(Epsilon|#917)' => {'tex' => '{/Symbol E}', 'web' => "\x{395}"},
831: '(Phi|#934)' => {'tex' => '{/Symbol F}', 'web' => "\x{3A6}"},
832: '(Gamma|#915)' => {'tex' => '{/Symbol G}', 'web' => "\x{393}"},
833: '(Eta|#919)' => {'tex' => '{/Symbol H}', 'web' => "\x{397}"},
834: '(Iota|#921)' => {'tex' => '{/Symbol I}', 'web' => "\x{399}"},
835: '(Kappa|#922)' => {'tex' => '{/Symbol K}', 'web' => "\x{39A}"},
836: '(Lambda|#923)' => {'tex' => '{/Symbol L}', 'web' => "\x{39B}"},
837: '(Mu|#924)' => {'tex' => '{/Symbol M}', 'web' => "\x{39C}"},
838: '(Nu|#925)' => {'tex' => '{/Symbol N}', 'web' => "\x{39D}"},
839: '(Omicron|#927)' => {'tex' => '{/Symbol O}', 'web' => "\x{39F}"},
840: '(Pi|#928)' => {'tex' => '{/Symbol P}', 'web' => "\x{3A0}"},
841: '(Theta|#920)' => {'tex' => '{/Symbol Q}', 'web' => "\x{398}"},
842: '(Rho|#929)' => {'tex' => '{/Symbol R}', 'web' => "\x{3A1}"},
843: '(Sigma|#931)' => {'tex' => '{/Symbol S}', 'web' => "\x{3A3}"},
844: '(Tau|#932)' => {'tex' => '{/Symbol T}', 'web' => "\x{3A4}"},
845: '(Upsilon|#933)' => {'tex' => '{/Symbol U}', 'web' => "\x{3A5}"},
846: '(Omega|#937)' => {'tex' => '{/Symbol W}', 'web' => "\x{3A9}"},
847: '(Xi|#926)' => {'tex' => '{/Symbol X}', 'web' => "\x{39E}"},
848: '(Psi|#936)' => {'tex' => '{/Symbol Y}', 'web' => "\x{3A8}"},
849: '(Zeta|#918)' => {'tex' => '{/Symbol Z}', 'web' => "\x{396}"},
850: '(alpha|#945)' => {'tex' => '{/Symbol a}', 'web' => "\x{3B1}"},
851: '(beta|#946)' => {'tex' => '{/Symbol b}', 'web' => "\x{3B2}"},
852: '(chi|#967)' => {'tex' => '{/Symbol c}', 'web' => "\x{3C7}"},
853: '(delta|#948)' => {'tex' => '{/Symbol d}', 'web' => "\x{3B4}"},
854: '(epsilon|#949)' => {'tex' => '{/Symbol e}', 'web' => "\x{3B5}"},
855: '(phi|#966)' => {'tex' => '{/Symbol f}', 'web' => "\x{3C6}"},
856: '(gamma|#947)' => {'tex' => '{/Symbol g}', 'web' => "\x{3B3}"},
857: '(eta|#951)' => {'tex' => '{/Symbol h}', 'web' => "\x{3B7}"},
858: '(iota|#953)' => {'tex' => '{/Symbol i}', 'web' => "\x{3B9}"},
859: '(kappa|#954)' => {'tex' => '{/Symbol k}', 'web' => "\x{3BA}"},
860: '(lambda|#955)' => {'tex' => '{/Symbol k}', 'web' => "\x{3BB}"},
861: '(mu|#956)' => {'tex' => '{/Symbol m}', 'web' => "\x{3BC}"},
862: '(nu|#957)' => {'tex' => '{/Symbol n}', 'web' => "\x{3BD}"},
863: '(omicron|#959)' => {'tex' => '{/Symbol o}', 'web' => "\x{3BF}"},
864: '(pi|#960)' => {'tex' => '{/Symbol p}', 'web' => "\x{3C0}"},
865: '(theta|#952)' => {'tex' => '{/Symbol q}', 'web' => "\x{3B8}"},
866: '(rho|#961)' => {'tex' => '{/Symbol r}', 'web' => "\x{3C1}"},
867: '(sigma|#963)' => {'tex' => '{/Symbol s}', 'web' => "\x{3C3}"},
868: '(tau|#964)' => {'tex' => '{/Symbol t}', 'web' => "\x{3C4}"},
869: '(upsilon|#965)' => {'tex' => '{/Symbol u}', 'web' => "\x{3C5}"},
870: '(omega|#969)' => {'tex' => '{/Symbol w}', 'web' => "\x{3C9}"},
871: '(xi|#958)' => {'tex' => '{/Symbol x}', 'web' => "\x{3BE}"},
872: '(psi|#968)' => {'tex' => '{/Symbol y}', 'web' => "\x{3C8}"},
873: '(zeta|#950)' => {'tex' => '{/Symbol z}', 'web' => "\x{3B6}"},
874: '(thetasym|#977)' => {'tex' => '{/Symbol \165}', 'web' => "\x{3d1}"},
875: '(upsih|#978)' => {'tex' => '{/Symbol \241}', 'web' => "\x{3d2}"},
876: '(piv|#982)' => {'tex' => '{/Symbol \166}', 'web' => "\x{3d6}"},
877:
878:
879: # Punctuation:
880:
881: '(quot|#034)' => {'tex' => '\42', 'web' => '\42'},
882: '(amp|#038)' => {'tex' => '\46', 'web' => '\46'},
883: '(lt|#060)' => {'tex' => '\74', 'web' => '\74'},
884: '(gt|#062)' => {'tex' => '\76', 'web' => '\76'},
885: '#131' => {'tex' => '{/Symbol \246}', 'web' => "\x{192}"},
886: '#132' => {'tex' => '{/Text \271}', 'web' => "\x{201e}"},
887: '#133' => {'tex' => '{/Symbol \274}', 'web'=> "\x{2026}"},
888: '#134' => {'tex' => '{/Text \262}', 'web' => "\x{2020}"},
889: '#135' => {'tex' => '{/Text \263}', 'web' => "\x{2021}"},
890: '#136' => {'tex' => '\\\\^', 'web' => '\\\\^'},
891: '#137' => {'tex' => '{/Text \275}', 'web' => "\x{2030}"},
892: '#138' => {'tex' => 'S', 'web' => "\x{160}"}, # no S-caron in ps fonts.
893: '#139' => {'tex' => '<', 'web' => '<'},
894: '#140' => {'tex' => '{/Text \352}', 'web' => "\x{152}"},
895: '#145' => {'tex' => '\140', 'web' => "\x{2018}"},
896: '#146' => {'tex' => '\47', 'web' => "\x{2019}"},
897: '#147' => {'tex' => '{/Text \252}', 'web' => "\x{201c}"},
898: '#148' => {'tex' => '{/Text \315}', 'web' => '\\"'},
899: '#149' => {'tex' => '{/Symbol \267}', 'web' => "\x{2022}"},
900: '#150' => {'tex' => '{/Text \55}', 'web' => "\x{2013}"}, #Untested here en dash
901: '#151' => {'tex' => '{/Symbol \55}', 'web' => "\x{2014}"}, # em dash
902: '#152' => {'tex' => '~', 'web' => '~'},
903: '#153' => {'tex' => '{/Text \324}', 'web' => "\x{2122}"}, # trademark
904:
905: # Accented letters, and other furreign language glyphs.
906:
907: '#154' => {'tex' => 's', 'web' => "\x{161}"}, # small s-caron no ps.
908: '#155' => {'tex' => '>', 'web' => '\76'}, # >
909: '#156' => {'tex' => '{/Text \372}', 'web' => "\x{153}"}, # oe ligature.
910: '#159', => {'tex' => 'Y', 'web' => "\x{178}"}, # Y-umlaut - can't print
911: '(nbsp|#160)' => {'tex' => ' ', 'web' => ' '}, # non breaking space.
912: '(iexcl|#161)' => {'tex' => '{/Text \241}', 'web' => "\x{a1}"}, # inverted !
913: '(cent|#162)' => {'tex' => '{/Text \242}', 'web' => "\x{a2}"}, # Cent currency.
914: '(pound|#163}' => {'tex' => '{/Text \243}', 'web' => "\x{a3}"}, # GB Pound currency.
915: '(curren|#164)' => {'tex' => '{/Text \250}', 'web' => "\x{a4}"}, # Generic currency symb.
916: '(yen|#165)' => {'tex' => '{/Text \245}', 'web' => "\x{a5}"}, # Yen currency.
917: '(brvbar|#166)' => {'tex' => '{/Symbol \174}', 'web' => "\x{a6}"}, # Broken vert bar no print.
918: '(sect|#167)' => {'tex' => '{\247}', 'web' => "\x{a7}"}, # Section symbol.
919: '(uml|#168)' => {'tex' => '{\250}', 'web' => "\x{a8}"}, # 'naked' umlaut.
920: '(copy|#169)' => {'tex' => '{/Symbol \343}', 'web' => "\x{a9}"}, # Copyright symbol.
921: '(ordf|#170)' => {'tex' => '{/Text \343}', 'web' => "\x{aa}"}, # Feminine ordinal.
922: '(laquo|#171)' => {'tex' => '{/Text \253}', 'web' => "\x{ab}"}, # << quotes.
923: '(not|#172)' => {'tex' => '\254', 'web' => "\x{ac}"}, # Logical not.
924: '(shy|#173)' => {'tex' => '-', 'web' => "\x{ad}"}, # soft hyphen.
925: '(reg|#174)' => {'tex' => '{/Symbol \342}', 'web' => "\x{ae}"}, # Registered tm.
926: '(macr|#175)' => {'tex' => '^{-}', 'web' => "\x{af}"}, # 'naked' macron (overbar).
927: '(deg|#176)' => {'tex' => '{/Text \312}', 'web' => "\x{b0}"}, # Degree symbo..
928: '(plusmn|#177)' => {'tex' => '{/Symbol \261}', 'web' => "\x{b1}"}, # +/- symbol.
929: '(sup2|#178)' => {'tex' => '^2', 'web' => "\x{b2}"}, # Superscript 2.
930: '(sup3|#179)' => {'tex' => '^3', 'web' => "\x{b3}"}, # Superscript 3.
931: '(acute|#180)' => {'tex' => '{/Text \302}', 'web' => "\x{b4}"}, # 'naked' acute accent.
932: '(micro|#181)' => {'tex' => '{/Symbol \155}', 'web' => "\x{b5}"}, # Micro (small mu).
933: '(para|#182)' => {'tex' => '{/Text \266}', 'web' => "\x{b6}"}, # Paragraph symbol.
934: '(middot|#183)' => {'tex' => '^.', 'web' => "\x{b7}"}, # middle dot (maybe text 267 is better)?
935: '(cedil|#184)' => {'tex' => '\233', 'web' => "\x{b8}"}, # 'naked' cedilla.
936: '(sup1|#185)' => {'tex' => '^1', 'web' => "\x{b9}"}, # superscript 1.
937: '(ordm|#186)' => {'tex' => '\353', 'web' => "\x{ba}"}, # masculine ordinal.
938: '(raquo|#187)', => {'tex' => '\273', 'web' => "\x{bb}"}, # Right angle quotes.
939: '(frac14|#188)' => {'tex' => '\274', 'web' => "\x{bc}"}, # 1/4.
940: '(frac12|#189)' => {'tex' => '\275', 'web' => "\x{bd}"}, # 1/2.
941: '(frac34|#190)' => {'tex' => '\276', 'web' => "\x{be}"}, # 3/4
942: '(iquest|#191)' => {'tex' => '{/Text \277}', 'web' => "\x{bf}"}, # Inverted ?
943: '(Agrave|#192)' => {'tex' => '\300', 'web' => "\x{c0}"}, # A Grave.
944: '(Aacute|#193)' => {'tex' => '\301', 'web' => "\x{c1}"}, # A Acute.
945: '(Acirc|#194)' => {'tex' => '\302', 'web' => "\x{c2}"}, # A Circumflex.
946: '(Atilde|#195)' => {'tex' => '\303', 'web' => "\x{c3}"}, # A tilde.
947: '(Auml|#196)' => {'tex' => '\304', 'web' => "\x{c4}"}, # A umlaut.
948: '(Aring|#197)' => {'tex' => '\305', 'web' => "\x{c5}"}, # A ring.
949: '(AElig|#198)' => {'tex' => '\306', 'web' => "\x{c6}"}, # AE ligature.
950: '(Ccedil|#199)' => {'tex' => '\307', 'web' => "\x{c7}"}, # C cedilla
951: '(Egrave|#200)' => {'tex' => '\310', 'web' => "\x{c8}"}, # E Accent grave.
952: '(Eacute|#201)' => {'tex' => '\311', 'web' => "\x{c9}"}, # E acute accent.
953: '(Ecirc|#202)' => {'tex' => '\312', 'web' => "\x{ca}"}, # E Circumflex.
954: '(Euml|#203)' => {'tex' => '\313', 'web' => "\x{cb}"}, # E umlaut.
955: '(Igrave|#204)' => {'tex' => '\314', 'web' => "\x{cc}"}, # I grave accent.
956: '(Iacute|#205)' => {'tex' => '\315', 'web' => "\x{cd}"}, # I acute accent.
957: '(Icirc|#206)' => {'tex' => '\316', 'web' => "\x{ce}"}, # I circumflex.
958: '(Iuml|#207)' => {'tex' => '\317', 'web' => "\x{cf}"}, # I umlaut.
959: '(ETH|#208)' => {'tex' => '\320', 'web' => "\x{d0}"}, # Icelandic Cap eth.
960: '(Ntilde|#209)' => {'tex' => '\321', 'web' => "\x{d1}"}, # Ntilde (enyan).
961: '(Ograve|#210)' => {'tex' => '\322', 'web' => "\x{d2}"}, # O accent grave.
962: '(Oacute|#211)' => {'tex' => '\323', 'web' => "\x{d3}"}, # O accent acute.
963: '(Ocirc|#212)' => {'tex' => '\324', 'web' => "\x{d4}"}, # O circumflex.
964: '(Otilde|#213)' => {'tex' => '\325', 'web' => "\x{d5}"}, # O tilde.
965: '(Ouml|#214)' => {'tex' => '\326', 'web' => "\x{d6}"}, # O umlaut.
966: '(times|#215)' => {'tex' => '\327', 'web' => "\x{d7}"}, # Times symbol.
967: '(Oslash|#216)' => {'tex' => '\330', 'web' => "\x{d8}"}, # O slash.
968: '(Ugrave|#217)' => {'tex' => '\331', 'web' => "\x{d9}"}, # U accent grave.
969: '(Uacute|#218)' => {'tex' => '\332', 'web' => "\x{da}"}, # U accent acute.
970: '(Ucirc|#219)' => {'tex' => '\333', 'web' => "\x{db}"}, # U circumflex.
971: '(Uuml|#220)' => {'tex' => '\334', 'web' => "\x{dc}"}, # U umlaut.
972: '(Yacute|#221)' => {'tex' => '\335', 'web' => "\x{dd}"}, # Y accent acute.
973: '(THORN|#222)' => {'tex' => '\336', 'web' => "\x{de}"}, # Icelandic thorn.
974: '(szlig|#223)' => {'tex' => '\337', 'web' => "\x{df}"}, # German sharfes s.
975: '(agrave|#224)' => {'tex' => '\340', 'web' => "\x{e0}"}, # a accent grave.
976: '(aacute|#225)' => {'tex' => '\341', 'web' => "\x{e1}"}, # a grave.
977: '(acirc|#226)' => {'tex' => '\342', 'web' => "\x{e2}"}, # a circumflex.
978: '(atilde|#227)' => {'tex' => '\343', 'web' => "\x{e3}"}, # a tilde.
979: '(auml|#228)' => {'tex' => '\344', 'web' => "\x{e4}"}, # a umlaut
980: '(aring|#229)' => {'tex' => '\345', 'web' => "\x{e5}"}, # a ring on top.
981: '(aelig|#230)' => {'tex' => '\346', 'web' => "\x{e6}"}, # ae ligature.
982: '{ccedil|#231)' => {'tex' => '\347', 'web' => "\x{e7}"}, # C cedilla
983: '(egrave|#232)' => {'tex' => '\350', 'web' => "\x{e8}"}, # e accent grave.
984: '(eacute|#233)' => {'tex' => '\351', 'web' => "\x{e9}"}, # e accent acute.
985: '(ecirc|#234)' => {'tex' => '\352', 'web' => "\x{ea}" }, # e circumflex.
986: '(euml|#235)' => {'tex' => '\353', 'web' => "\x{eb}"}, # e umlaut.
987: '(igrave|#236)' => {'tex' => '\354', 'web' => "\x{ec}"}, # i grave.
988: '(iacute|#237}' => {'tex' => '\355', 'web' => "\x{ed}"}, # i acute.
989: '(icirc|#238}' => {'tex' => '\356', 'web' => "\x{ee}"}, # i circumflex.
990: '(iuml|#239)' => {'tex' => '\357', 'web' => "\x{ef}"}, # i umlaut.
991: '(eth|#240)' => {'tex' => '\360', 'web' => "\x{f0}"}, # Icelandic eth.
992: '(ntilde|#241)' => {'tex' => '\361', 'web' => "\x{f1}"}, # n tilde.
993: '(ograve|#242)' => {'tex' => '\362', 'web' => "\x{f2}"}, # o grave.
994: '(oacute|#243)' => {'tex' => '\363', 'web' => "\x{f3}"}, # o acute.
995: '(ocirc'|#244)' => {'tex' => '\364', 'web' => "\x{f4}"}, # o circumflex.
996: '(otilde|#245)' => {'tex' => '\365', 'web' => "\x{f5}"}, # o tilde.
997: '(ouml|#246)' => {'tex' => '\366', 'web' => "\x{f6}"}, # o umlaut.
998: '(divide|#247)' => {'tex' => '\367', 'web' => "\x{f7}"}, # division symbol
999: '(oslash|#248)' => {'tex' => '\370', 'web' => "\x{f8}"}, # o slashed.
1000: '(ugrave|#249)' => {'tex' => '\371', 'web' => "\x{f9}"}, # u accent grave.
1001: '(uacute|#250)' => {'tex' => '\372', 'web' => "\x{fa}"}, # u acute.
1002: '(ucirc|#251)' => {'tex' => '\373', 'web' => "\x{fb}"}, # u circumflex.
1003: '(uuml|#252)' => {'tex' => '\374', 'web' => "\x{fc}"}, # u umlaut.
1004: '(yacute|#253)' => {'tex' => '\375', 'web' => "\x{fd}"}, # y acute accent.
1005: '(thorn|#254)' => {'tex' => '\376', 'web' => "\x{fe}"}, # small thorn (icelandic).
1006: '(yuml|#255)' => {'tex' => '\377', 'web' => "\x{ff}"}, # y umlaut.
1007:
1008: # Latin extended A entities:
1009:
1010: '(OElig|#338)' => {'tex' => '{/Text \352}', 'web' => "\x{152}"}, # OE ligature.
1011: '(oelig|#339)' => {'tex' => '{/Text \372}', 'web' => "\x{153}"}, # oe ligature.
1012: '(Scaron|#352)' => {'tex' => 'S', 'web' => "\x{160}"}, # S caron no printable.
1013: '(scaron|#353)' => {'tex' => 's', 'web' => "\x{161}"}, # s caron no printable.
1014: '(Yuml|#376)' => {'tex' => 'Y', 'web' => "\x{178}"}, # Y umlaut - no printable.
1015:
1016: # Latin extended B.
1017:
1018: '(fnof|#402)' => {'tex' =>'{/Symbol 246}', 'web' => "\x{192}"}, # f with little hook.
1019:
1020: # Standalon accents:
1021:
1022: '(circ|#710)' => {'tex' => '^', 'web' => '^'}, # circumflex.
1023: '(tilde|#732)' => {'tex' => '~', 'web' => '~'}, # tilde.
1024:
1025:
1026:
1027: );
1028:
1029:
1030: sub replace_entities {
1031: my ($target,$text) = @_;
1032: $text =~ s{([_^~\{\}]|\\\\)}{\\\\$1}g;
1033: while (my ($re, $replace) = each(%lookup)) {
1034: my $repl = $replace->{$target};
1035: $text =~ s/&$re;/$replace->{$target}/g;
1036: }
1037: $text =~ s{(&)}{\\\\$1}g;
1038: return $text;
1039: }
1040:
1041: ##------------------------------------------------------------------- title
1042: sub start_title {
1043: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1044: my $result='';
1045: if ($target eq 'web' || $target eq 'tex') {
1046: $title = &Apache::lonxml::get_all_text("/title",$parser,$style);
1047: $title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
1048: $title =~ s/\n/ /g;
1049: if (length($title) > $max_str_len) {
1050: $title = substr($title,0,$max_str_len);
1051: }
1052: $title = &parse_label($target,$title);
1053: } elsif ($target eq 'edit') {
1054: $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1055: my $text=&Apache::lonxml::get_all_text("/title",$parser,$style);
1056: $result.=&Apache::edit::editline('',$text,'',60);
1057: } elsif ($target eq 'modified') {
1058: $result.=&Apache::edit::rebuild_tag($token);
1059: $result.=&Apache::edit::modifiedfield("/title",$parser);
1060: }
1061: return $result;
1062: }
1063:
1064: sub end_title {
1065: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1066: my $result = '';
1067: if ($target eq 'web' || $target eq 'tex') {
1068: } elsif ($target eq 'edit') {
1069: $result.=&Apache::edit::tag_end($target,$token);
1070: }
1071: return $result;
1072: }
1073: ##------------------------------------------------------------------- xlabel
1074: sub start_xlabel {
1075: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1076: my $result='';
1077: if ($target eq 'web' || $target eq 'tex') {
1078: $xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1079: $xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
1080: $xlabel =~ s/\n/ /g;
1081: if (length($xlabel) > $max_str_len) {
1082: $xlabel = substr($xlabel,0,$max_str_len);
1083: }
1084: $xlabel = &parse_label($target,$xlabel);
1085: } elsif ($target eq 'edit') {
1086: $result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1087: my $text=&Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1088: $result.=&Apache::edit::editline('',$text,'',60);
1089: } elsif ($target eq 'modified') {
1090: $result.=&Apache::edit::rebuild_tag($token);
1091: $result.=&Apache::edit::modifiedfield("/xlabel",$parser);
1092: }
1093: return $result;
1094: }
1095:
1096: sub end_xlabel {
1097: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1098: my $result = '';
1099: if ($target eq 'web' || $target eq 'tex') {
1100: } elsif ($target eq 'edit') {
1101: $result.=&Apache::edit::tag_end($target,$token);
1102: }
1103: return $result;
1104: }
1105:
1106: ##------------------------------------------------------------------- ylabel
1107: sub start_ylabel {
1108: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1109: my $result='';
1110: if ($target eq 'web' || $target eq 'tex') {
1111: $ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1112: $ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
1113: $ylabel =~ s/\n/ /g;
1114: if (length($ylabel) > $max_str_len) {
1115: $ylabel = substr($ylabel,0,$max_str_len);
1116: }
1117: $ylabel = &parse_label($target,$ylabel);
1118: } elsif ($target eq 'edit') {
1119: $result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1120: my $text = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1121: $result .= &Apache::edit::editline('',$text,'',60);
1122: } elsif ($target eq 'modified') {
1123: $result.=&Apache::edit::rebuild_tag($token);
1124: $result.=&Apache::edit::modifiedfield("/ylabel",$parser);
1125: }
1126: return $result;
1127: }
1128:
1129: sub end_ylabel {
1130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1131: my $result = '';
1132: if ($target eq 'web' || $target eq 'tex') {
1133: } elsif ($target eq 'edit') {
1134: $result.=&Apache::edit::tag_end($target,$token);
1135: }
1136: return $result;
1137: }
1138:
1139: ##------------------------------------------------------------------- label
1140: sub start_label {
1141: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1142: my $result='';
1143: if ($target eq 'web' || $target eq 'tex') {
1144: my %label;
1145: &get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1146: $tagstack->[-1]);
1147: my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1148: $text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
1149: $text =~ s/\n/ /g;
1150: $text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
1151: $label{'text'} = &parse_label($target,$text);
1152: push(@labels,\%label);
1153: } elsif ($target eq 'edit') {
1154: $result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1155: $result .= &edit_attributes($target,$token,\%label_defaults);
1156: my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1157: $result .= &Apache::edit::end_row().
1158: &Apache::edit::start_spanning_row().
1159: &Apache::edit::editline('',$text,'',60);
1160: } elsif ($target eq 'modified') {
1161: &Apache::edit::get_new_args
1162: ($token,$parstack,$safeeval,keys(%label_defaults));
1163: $result.=&Apache::edit::rebuild_tag($token);
1164: $result.=&Apache::edit::modifiedfield("/label",$parser);
1165: }
1166: return $result;
1167: }
1168:
1169: sub end_label {
1170: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1171: my $result = '';
1172: if ($target eq 'web' || $target eq 'tex') {
1173: } elsif ($target eq 'edit') {
1174: $result.=&Apache::edit::tag_end($target,$token);
1175: }
1176: return $result;
1177: }
1178:
1179: ##------------------------------------------------------------------- curve
1180: sub start_curve {
1181: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1182: my $result='';
1183: &Apache::lonxml::register('Apache::lonplot',('function','data'));
1184: push (@Apache::lonxml::namespace,'curve');
1185: if ($target eq 'web' || $target eq 'tex') {
1186: my %curve;
1187: &get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1188: $tagstack->[-1]);
1189: push (@curves,\%curve);
1190: } elsif ($target eq 'edit') {
1191: $result .= &Apache::edit::tag_start($target,$token,'Curve');
1192: $result .= &edit_attributes($target,$token,\%curve_defaults,
1193: \@curve_edit_order)
1194: .&Apache::edit::end_row()
1195: .&Apache::edit::start_spanning_row();
1196:
1197: } elsif ($target eq 'modified') {
1198: my $constructtag=&Apache::edit::get_new_args
1199: ($token,$parstack,$safeeval,keys(%curve_defaults));
1200: if ($constructtag) {
1201: $result = &Apache::edit::rebuild_tag($token);
1202: }
1203: }
1204: return $result;
1205: }
1206:
1207: sub end_curve {
1208: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1209: my $result = '';
1210: pop @Apache::lonxml::namespace;
1211: &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1212: if ($target eq 'web' || $target eq 'tex') {
1213: } elsif ($target eq 'edit') {
1214: $result.=&Apache::edit::tag_end($target,$token);
1215: }
1216: return $result;
1217: }
1218:
1219: ##------------------------------------------------------------ curve function
1220: sub start_function {
1221: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1222: my $result='';
1223: if ($target eq 'web' || $target eq 'tex') {
1224: if (exists($curves[-1]->{'data'})) {
1225: &Apache::lonxml::warning
1226: ('Use of the <b>curve function</b> tag precludes use of '.
1227: ' the <b>curve data</b> tag. '.
1228: 'The curve data tag will be omitted in favor of the '.
1229: 'curve function declaration.');
1230: delete $curves[-1]->{'data'} ;
1231: }
1232: my $function = &Apache::lonxml::get_all_text("/function",$parser,
1233: $style);
1234: $function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
1235: $curves[-1]->{'function'} = $function;
1236: } elsif ($target eq 'edit') {
1237: $result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1238: my $text = &Apache::lonxml::get_all_text("/function",$parser,$style);
1239: $result .= &Apache::edit::editline('',$text,'',60);
1240: } elsif ($target eq 'modified') {
1241: $result.=&Apache::edit::rebuild_tag($token);
1242: $result.=&Apache::edit::modifiedfield("/function",$parser);
1243: }
1244: return $result;
1245: }
1246:
1247: sub end_function {
1248: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1249: my $result = '';
1250: if ($target eq 'web' || $target eq 'tex') {
1251: } elsif ($target eq 'edit') {
1252: $result .= &Apache::edit::end_table();
1253: }
1254: return $result;
1255: }
1256:
1257: ##------------------------------------------------------------ curve data
1258: sub start_data {
1259: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1260: my $result='';
1261: if ($target eq 'web' || $target eq 'tex') {
1262: if (exists($curves[-1]->{'function'})) {
1263: &Apache::lonxml::warning
1264: ('Use of the <b>curve function</b> tag precludes use of '.
1265: ' the <b>curve data</b> tag. '.
1266: 'The curve function tag will be omitted in favor of the '.
1267: 'curve data declaration.');
1268: delete($curves[-1]->{'function'});
1269: }
1270: my $datatext = &Apache::lonxml::get_all_text("/data",$parser,$style);
1271: $datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
1272: # Deal with cases where we're given an array...
1273: if ($datatext =~ /^\@/) {
1274: $datatext = &Apache::run::run('return "'.$datatext.'"',
1275: $safeeval,1);
1276: }
1277: $datatext =~ s/\s+/ /g;
1278: # Need to do some error checking on the @data array -
1279: # make sure it's all numbers and make sure each array
1280: # is of the same length.
1281: my @data;
1282: if ($datatext =~ /,/) { # comma deliminated
1283: @data = split /,/,$datatext;
1284: } else { # Assume it's space separated.
1285: @data = split / /,$datatext;
1286: }
1287: for (my $i=0;$i<=$#data;$i++) {
1288: # Check that it's non-empty
1289: if (! defined($data[$i])) {
1290: &Apache::lonxml::warning(
1291: 'undefined curve data value. Replacing with '.
1292: ' pi/e = 1.15572734979092');
1293: $data[$i] = 1.15572734979092;
1294: }
1295: # Check that it's a number
1296: if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
1297: &Apache::lonxml::warning(
1298: 'Bad curve data value of '.$data[$i].' Replacing with '.
1299: ' pi/e = 1.15572734979092');
1300: $data[$i] = 1.15572734979092;
1301: }
1302: }
1303: # complain if the number of data points is not the same as
1304: # in previous sets of data.
1305: if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
1306: &Apache::lonxml::warning
1307: ('Number of data points is not consistent with previous '.
1308: 'number of data points');
1309: }
1310: push @{$curves[-1]->{'data'}},\@data;
1311: } elsif ($target eq 'edit') {
1312: $result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1313: my $text = &Apache::lonxml::get_all_text("/data",$parser,$style);
1314: $result .= &Apache::edit::editline('',$text,'',60);
1315: } elsif ($target eq 'modified') {
1316: $result.=&Apache::edit::rebuild_tag($token);
1317: $result.=&Apache::edit::modifiedfield("/data",$parser);
1318: }
1319: return $result;
1320: }
1321:
1322: sub end_data {
1323: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1324: my $result = '';
1325: if ($target eq 'web' || $target eq 'tex') {
1326: } elsif ($target eq 'edit') {
1327: $result .= &Apache::edit::end_table();
1328: }
1329: return $result;
1330: }
1331:
1332: ##------------------------------------------------------------------- axis
1333: sub start_axis {
1334: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1335: my $result='';
1336: if ($target eq 'web' || $target eq 'tex') {
1337: &get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
1338: $tagstack->[-1]);
1339: } elsif ($target eq 'edit') {
1340: $result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1341: $result .= &edit_attributes($target,$token,\%axis_defaults,
1342: \@axis_edit_order);
1343: } elsif ($target eq 'modified') {
1344: my $constructtag=&Apache::edit::get_new_args
1345: ($token,$parstack,$safeeval,keys(%axis_defaults));
1346: if ($constructtag) {
1347: $result = &Apache::edit::rebuild_tag($token);
1348: }
1349: }
1350: return $result;
1351: }
1352:
1353: sub end_axis {
1354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1355: my $result = '';
1356: if ($target eq 'web' || $target eq 'tex') {
1357: } elsif ($target eq 'edit') {
1358: $result.=&Apache::edit::tag_end($target,$token);
1359: } elsif ($target eq 'modified') {
1360: }
1361: return $result;
1362: }
1363:
1364: ###################################################################
1365: ## ##
1366: ## Utility Functions ##
1367: ## ##
1368: ###################################################################
1369:
1370: ##----------------------------------------------------------- set_defaults
1371: sub set_defaults {
1372: my ($var,$defaults) = @_;
1373: my $key;
1374: foreach $key (keys(%$defaults)) {
1375: $var->{$key} = $defaults->{$key}->{'default'};
1376: }
1377: }
1378:
1379: ##------------------------------------------------------------------- misc
1380: sub get_attributes{
1381: my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1382: foreach my $attr (keys(%{$defaults})) {
1383: if ($attr eq 'texwidth' || $attr eq 'texfont') {
1384: $values->{$attr} =
1385: &Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
1386: } else {
1387: $values->{$attr} =
1388: &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1389: }
1390: if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1391: $values->{$attr} = $defaults->{$attr}->{'default'};
1392: next;
1393: }
1394: my $test = $defaults->{$attr}->{'test'};
1395: if (! &$test($values->{$attr})) {
1396: &Apache::lonxml::warning
1397: ($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1398: .$defaults->{$attr}->{'default'} );
1399: $values->{$attr} = $defaults->{$attr}->{'default'};
1400: }
1401: }
1402: return ;
1403: }
1404:
1405: ##------------------------------------------------------- write_gnuplot_file
1406: sub write_gnuplot_file {
1407: my ($tmpdir,$filename,$target)= @_;
1408: my ($fontsize, $font_properties) = &get_font($target);
1409: my $gnuplot_input = '';
1410: my $curve;
1411: #
1412: # Check to be sure we do not have any empty curves
1413: my @curvescopy;
1414: foreach my $curve (@curves) {
1415: if (exists($curve->{'function'})) {
1416: if ($curve->{'function'} !~ /^\s*$/) {
1417: push(@curvescopy,$curve);
1418: }
1419: } elsif (exists($curve->{'data'})) {
1420: foreach my $data (@{$curve->{'data'}}) {
1421: if (scalar(@$data) > 0) {
1422: push(@curvescopy,$curve);
1423: last;
1424: }
1425: }
1426: }
1427: }
1428: @curves = @curvescopy;
1429: # Collect all the colors
1430: my @Colors;
1431: push @Colors, $Apache::lonplot::plot{'bgcolor'};
1432: push @Colors, $Apache::lonplot::plot{'fgcolor'};
1433: push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
1434: foreach $curve (@curves) {
1435: push @Colors, ($curve->{'color'} ne '' ?
1436: $curve->{'color'} :
1437: $Apache::lonplot::plot{'fgcolor'} );
1438: }
1439: # set term
1440: if ($target eq 'web') {
1441: $gnuplot_input .= 'set terminal png enhanced nocrop ';
1442: $gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
1443: $gnuplot_input .= 'font "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
1444: '/'.$font_properties->{'file'}.'.ttf" ';
1445: $gnuplot_input .= $fontsize;
1446: $gnuplot_input .= ' size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
1447: $gnuplot_input .= "@Colors\n";
1448: # set output
1449: $gnuplot_input .= "set output\n";
1450: } elsif ($target eq 'tex') {
1451: $gnuplot_input .= "set term postscript eps enhanced $Apache::lonplot::plot{'plotcolor'} solid ";
1452: if (!$font_properties->{'tex_no_file'}) {
1453: $gnuplot_input .=
1454: 'fontfile "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
1455: '/'.$font_properties->{'file'}.'.pfb" ';
1456: }
1457: $gnuplot_input .= ' "'.$font_properties->{'printname'}.'" ';
1458: $gnuplot_input .= $fontsize;
1459: $gnuplot_input .= "\nset output \"/home/httpd/perl/tmp/".
1460: &unescape($filename).".eps\"\n";
1461: }
1462: # cartesian or polar plot?
1463: if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
1464: $gnuplot_input .= 'set polar'.$/;
1465: } else {
1466: # Assume Cartesian
1467: }
1468: # cartesian or polar grid?
1469: if (lc($Apache::lonplot::plot{'gridtype'}) eq 'polar') {
1470: $gnuplot_input .= 'set grid polar'.$/;
1471: } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'linear-log') {
1472: $gnuplot_input .= 'set logscale x'.$/;
1473: } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-linear') {
1474: $gnuplot_input .= 'set logscale y'.$/;
1475: } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-log') {
1476: $gnuplot_input .= 'set logscale x'.$/;
1477: $gnuplot_input .= 'set logscale y'.$/;
1478: } else {
1479: # Assume Cartesian
1480: }
1481: # solid or pattern for boxes?
1482: if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
1483: $gnuplot_input .= 'set style fill solid '.
1484: $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
1485: } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
1486: $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
1487: } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
1488: }
1489: # margin
1490: if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
1491: $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
1492: }
1493: if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
1494: $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
1495: }
1496: if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
1497: $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
1498: }
1499: if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
1500: $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
1501: }
1502:
1503: # tic scales
1504: if ($version > 4) {
1505: $gnuplot_input .= 'set tics scale '.
1506: $Apache::lonplot::plot{'major_ticscale'}.', '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
1507: } else {
1508: $gnuplot_input .= 'set ticscale '.
1509: $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
1510: }
1511: #boxwidth
1512: if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
1513: $gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
1514: }
1515: # gridlayer
1516: $gnuplot_input .= 'set grid noxtics noytics front '.$/
1517: if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
1518:
1519: # grid
1520: $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
1521: # border
1522: $gnuplot_input .= ($Apache::lonplot::plot{'border'} eq 'on'?
1523: 'set border'.$/ :
1524: 'set noborder'.$/ );
1525: # sampling rate for non-data curves
1526: $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
1527: # title, xlabel, ylabel
1528: # titles
1529: my $extra_space_x = ($xtics{'location'} eq 'axis') ? ' 0, -0.5 ' : '';
1530: my $extra_space_y = ($ytics{'location'} eq 'axis') ? ' -0.5, 0 ' : '';
1531:
1532: if ($target eq 'tex') {
1533: $gnuplot_input .= "set title \"$title\" font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($title)) ;
1534: $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($xlabel));
1535: $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($ylabel));
1536: } else {
1537: $gnuplot_input .= "set title \"$title\" \n" if (defined($title)) ;
1538: $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x \n" if (defined($xlabel));
1539: $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y \n" if (defined($ylabel));
1540: }
1541: # tics
1542: if (%xtics) {
1543: $gnuplot_input .= "set xtics $xtics{'location'} ";
1544: $gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1545: $gnuplot_input .= "$xtics{'start'}, ";
1546: $gnuplot_input .= "$xtics{'increment'}, ";
1547: $gnuplot_input .= "$xtics{'end'}\n";
1548: if ($xtics{'minorfreq'} != 0) {
1549: $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
1550: }
1551: }
1552: if (%ytics) {
1553: $gnuplot_input .= "set ytics $ytics{'location'} ";
1554: $gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1555: $gnuplot_input .= "$ytics{'start'}, ";
1556: $gnuplot_input .= "$ytics{'increment'}, ";
1557: $gnuplot_input .= "$ytics{'end'}\n";
1558: if ($ytics{'minorfreq'} != 0) {
1559: $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
1560: }
1561: }
1562: # axis
1563: if (%axis) {
1564: if ($axis{'xformat'} ne 'on') {
1565: $gnuplot_input .= "set format x ";
1566: if ($axis{'xformat'} eq 'off') {
1567: $gnuplot_input .= "\"\"\n";
1568: } else {
1569: $gnuplot_input .= "\"\%.".$axis{'xformat'}."\"\n";
1570: }
1571: }
1572: if ($axis{'yformat'} ne 'on') {
1573: $gnuplot_input .= "set format y ";
1574: if ($axis{'yformat'} eq 'off') {
1575: $gnuplot_input .= "\"\"\n";
1576: } else {
1577: $gnuplot_input .= "\"\%.".$axis{'yformat'}."\"\n";
1578: }
1579: }
1580: $gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
1581: $gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1582: }
1583: # Key
1584: if (%key) {
1585: $gnuplot_input .= 'set key '.$key{'pos'}.' ';
1586: if ($key{'title'} ne '') {
1587: $gnuplot_input .= 'title "'.$key{'title'}.'" ';
1588: }
1589: $gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1590: } else {
1591: $gnuplot_input .= 'set nokey'.$/;
1592: }
1593: # labels
1594: my $label;
1595: foreach $label (@labels) {
1596: $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1597: $label->{'xpos'}.','.$label->{'ypos'};
1598: if ($label->{'rotate'} ne '') {
1599: $gnuplot_input .= ' rotate by '.$label->{'rotate'};
1600: }
1601: $gnuplot_input .= ' '.$label->{'justify'};
1602:
1603: if ($target eq 'tex') {
1604: $gnuplot_input .=' font "'.$font_properties->{'printname'}.','.$fontsize.'pt"' ;
1605: }
1606: $gnuplot_input .= $/;
1607: }
1608: if ($target eq 'tex') {
1609: $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
1610: $gnuplot_input .="\n";
1611: }
1612: # curves
1613: $gnuplot_input .= 'plot ';
1614: for (my $i = 0;$i<=$#curves;$i++) {
1615: $curve = $curves[$i];
1616: $gnuplot_input.= ', ' if ($i > 0);
1617: if ($target eq 'tex') {
1618: $curve->{'linewidth'} *= 2;
1619: }
1620: if (exists($curve->{'function'})) {
1621: $gnuplot_input.=
1622: $curve->{'function'}.' title "'.
1623: $curve->{'name'}.'" with '.
1624: $curve->{'linestyle'};
1625:
1626: if (($curve->{'linestyle'} eq 'points') ||
1627: ($curve->{'linestyle'} eq 'linespoints') ||
1628: ($curve->{'linestyle'} eq 'errorbars') ||
1629: ($curve->{'linestyle'} eq 'xerrorbars') ||
1630: ($curve->{'linestyle'} eq 'yerrorbars') ||
1631: ($curve->{'linestyle'} eq 'xyerrorbars')) {
1632: $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1633: $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
1634: } elsif ($curve->{'linestyle'} eq 'filledcurves') {
1635: $gnuplot_input.= ' '.$curve->{'limit'};
1636: } elsif ($curve->{'linetype'} ne '' &&
1637: $curve->{'linestyle'} eq 'lines') {
1638: $gnuplot_input.= ' linetype ';
1639: $gnuplot_input.= $linetypes{$curve->{'linetype'}};
1640: $gnuplot_input.= ' linecolor rgb "';
1641: # convert color from xaaaaaa to #aaaaaa
1642: $curve->{'color'} =~ s/^x/#/;
1643: $gnuplot_input.= $curve->{'color'}.'"';
1644: }
1645: $gnuplot_input.= ' linewidth '.$curve->{'linewidth'};
1646:
1647: } elsif (exists($curve->{'data'})) {
1648: # Store data values in $datatext
1649: my $datatext = '';
1650: # get new filename
1651: my $datafilename = "$tmpdir/$filename.data.$i";
1652: my $fh=Apache::File->new(">$datafilename");
1653: # Compile data
1654: my @Data = @{$curve->{'data'}};
1655: my @Data0 = @{$Data[0]};
1656: for (my $i =0; $i<=$#Data0; $i++) {
1657: my $dataset;
1658: foreach $dataset (@Data) {
1659: $datatext .= $dataset->[$i] . ' ';
1660: }
1661: $datatext .= $/;
1662: }
1663: # write file
1664: print $fh $datatext;
1665: close($fh);
1666: # generate gnuplot text
1667: $gnuplot_input.= '"'.$datafilename.'" title "'.
1668: $curve->{'name'}.'" with '.
1669: $curve->{'linestyle'};
1670: if (($curve->{'linestyle'} eq 'points') ||
1671: ($curve->{'linestyle'} eq 'linespoints') ||
1672: ($curve->{'linestyle'} eq 'errorbars') ||
1673: ($curve->{'linestyle'} eq 'xerrorbars') ||
1674: ($curve->{'linestyle'} eq 'yerrorbars') ||
1675: ($curve->{'linestyle'} eq 'xyerrorbars')) {
1676: $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1677: $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
1678: } elsif ($curve->{'linestyle'} eq 'filledcurves') {
1679: $gnuplot_input.= ' '.$curve->{'limit'};
1680: } elsif ($curve->{'linetype'} ne '' &&
1681: $curve->{'linestyle'} eq 'lines') {
1682: $gnuplot_input.= ' linetype ';
1683: $gnuplot_input.= $linetypes{$curve->{'linetype'}};
1684: $gnuplot_input.= ' linecolor rgb "';
1685: # convert color from xaaaaaa to #aaaaaa
1686: $curve->{'color'} =~ s/^x/#/;
1687: $gnuplot_input.= $curve->{'color'}.'"';
1688: }
1689: $gnuplot_input.= ' linewidth '.$curve->{'linewidth'};
1690: }
1691: }
1692: # Write the output to a file.
1693: open (my $fh,">$tmpdir$filename.data");
1694: binmode($fh, ":utf8");
1695: print $fh $gnuplot_input;
1696: close($fh);
1697: # That's all folks.
1698: return ;
1699: }
1700:
1701: #---------------------------------------------- check_inputs
1702: sub check_inputs {
1703: ## Note: no inputs, no outputs - this acts only on global variables.
1704: ## Make sure we have all the input we need:
1705: if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
1706: if (! %key ) {} # No key for this plot, thats okay
1707: # if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1708: if (! defined($title )) {} # No title for this plot, thats okay
1709: if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
1710: if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
1711: if ($#labels < 0) { } # No labels for this plot, thats okay
1712: if ($#curves < 0) {
1713: &Apache::lonxml::warning("No curves specified for plot!!!!");
1714: return '';
1715: }
1716: my $curve;
1717: foreach $curve (@curves) {
1718: if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
1719: &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
1720: return '';
1721: }
1722: }
1723: }
1724:
1725: #------------------------------------------------ make_edit
1726: sub edit_attributes {
1727: my ($target,$token,$defaults,$keys) = @_;
1728: my ($result,@keys);
1729: if ($keys && ref($keys) eq 'ARRAY') {
1730: @keys = @$keys;
1731: } else {
1732: @keys = sort(keys(%$defaults));
1733: }
1734: foreach my $attr (@keys) {
1735: # append a ' ' to the description if it doesn't have one already.
1736: my $description = $defaults->{$attr}->{'description'};
1737: $description .= ' ' if ($description !~ / $/);
1738: if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1739: $result .= &Apache::edit::text_arg
1740: ($description,$attr,$token,
1741: $defaults->{$attr}->{'size'});
1742: } elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1743: $result .= &Apache::edit::select_or_text_arg
1744: ($description,$attr,$defaults->{$attr}->{'choices'},$token);
1745: } elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1746: $result .= &Apache::edit::select_or_text_arg
1747: ($description,$attr,['on','off'],$token);
1748: }
1749: $result .= '<br />';
1750: }
1751: return $result;
1752: }
1753:
1754:
1755: ###################################################################
1756: ## ##
1757: ## Insertion functions for editing plots ##
1758: ## ##
1759: ###################################################################
1760:
1761: sub insert_gnuplot {
1762: my $result = '';
1763: # plot attributes
1764: $result .= "\n<gnuplot ";
1765: foreach my $attr (keys(%gnuplot_defaults)) {
1766: $result .= "\n $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
1767: }
1768: $result .= ">";
1769: # Add the components (most are commented out for simplicity)
1770: # $result .= &insert_key();
1771: # $result .= &insert_axis();
1772: # $result .= &insert_title();
1773: # $result .= &insert_xlabel();
1774: # $result .= &insert_ylabel();
1775: $result .= &insert_curve();
1776: # close up the <gnuplot>
1777: $result .= "\n</gnuplot>";
1778: return $result;
1779: }
1780:
1781: sub insert_tics {
1782: my $result;
1783: $result .= &insert_xtics() . &insert_ytics;
1784: return $result;
1785: }
1786:
1787: sub insert_xtics {
1788: my $result;
1789: $result .= "\n <xtics ";
1790: foreach my $attr (keys(%tic_defaults)) {
1791: $result .= "\n $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1792: }
1793: $result .= "/>";
1794: return $result;
1795: }
1796:
1797: sub insert_ytics {
1798: my $result;
1799: $result .= "\n <ytics ";
1800: foreach my $attr (keys(%tic_defaults)) {
1801: $result .= "\n $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1802: }
1803: $result .= "/>";
1804: return $result;
1805: }
1806:
1807: sub insert_key {
1808: my $result;
1809: $result .= "\n <key ";
1810: foreach my $attr (keys(%key_defaults)) {
1811: $result .= "\n $attr=\"$key_defaults{$attr}->{'default'}\"";
1812: }
1813: $result .= " />";
1814: return $result;
1815: }
1816:
1817: sub insert_axis{
1818: my $result;
1819: $result .= "\n <axis ";
1820: foreach my $attr (keys(%axis_defaults)) {
1821: $result .= "\n $attr=\"$axis_defaults{$attr}->{'default'}\"";
1822: }
1823: $result .= " />";
1824: return $result;
1825: }
1826:
1827: sub insert_title { return "\n <title></title>"; }
1828: sub insert_xlabel { return "\n <xlabel></xlabel>"; }
1829: sub insert_ylabel { return "\n <ylabel></ylabel>"; }
1830:
1831: sub insert_label {
1832: my $result;
1833: $result .= "\n <label ";
1834: foreach my $attr (keys(%label_defaults)) {
1835: $result .= "\n $attr=\"".
1836: $label_defaults{$attr}->{'default'}."\"";
1837: }
1838: $result .= "></label>";
1839: return $result;
1840: }
1841:
1842: sub insert_curve {
1843: my $result;
1844: $result .= "\n <curve ";
1845: foreach my $attr (keys(%curve_defaults)) {
1846: $result .= "\n $attr=\"".
1847: $curve_defaults{$attr}->{'default'}."\"";
1848: }
1849: $result .= " >";
1850: $result .= &insert_data().&insert_data()."\n </curve>";
1851: }
1852:
1853: sub insert_function {
1854: my $result;
1855: $result .= "\n <function></function>";
1856: return $result;
1857: }
1858:
1859: sub insert_data {
1860: my $result;
1861: $result .= "\n <data></data>";
1862: return $result;
1863: }
1864:
1865: ##----------------------------------------------------------------------
1866: 1;
1867: __END__
1868:
1869:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>