File:
[LON-CAPA] /
loncom /
xml /
lonplot.pm
Revision
1.183:
download - view:
text,
annotated -
select for diffs
Thu Jan 30 15:59:15 2020 UTC (4 years, 6 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Modification to changes in 1.179 and 1.180 to support display of pi symbol
to right of number in a gnuplot tic label with format: nP (n=integer) for
both web and tex targets.
1: # The LearningOnline Network with CAPA
2: # Dynamic plot
3: #
4: # $Id: lonplot.pm,v 1.183 2020/01/30 15:59:15 raeburn 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:
30:
31:
32: package Apache::lonplot;
33:
34: use strict;
35: use warnings FATAL=>'all';
36: no warnings 'uninitialized';
37: use Apache::File;
38: use Apache::response;
39: use Apache::lonxml;
40: use Apache::edit;
41: use Apache::lonnet;
42: use LONCAPA;
43:
44:
45: use vars qw/$weboutputformat $version $colorprefix/;
46:
47:
48:
49: BEGIN {
50: &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
51: #
52: # Determine the version of GNUPLOT
53: $weboutputformat = 'gif';
54: my $versionstring = `gnuplot --version 2>/dev/null`;
55: ($version) = ($versionstring =~ /^gnuplot ([\d.]+)/);
56: if ($version >= 4) {
57: $weboutputformat = 'png';
58: }
59: $colorprefix = 'x';
60: if ($version > 4.6) {
61: $colorprefix = '#';
62: }
63: }
64:
65:
66: =pod
67:
68: ##
69: ## Description of data structures:
70: ##
71: ## %plot %key %axis
72: ## --------------------------
73: ## height title color
74: ## width box xmin
75: ## bgcolor pos xmax
76: ## fgcolor ymin
77: ## transparent ymax
78: ## grid
79: ## border
80: ## font
81: ## align
82: ##
83: ## @labels: $labels[$i] = \%label
84: ## %label: text, xpos, ypos, justify, rotate, zlayer
85: ##
86: ## @curves: $curves[$i] = \%curve
87: ## %curve: name, linestyle, ( function | data )
88: ##
89: ## $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
90: ## [y1,y2,y3,y4] ]
91: ##
92:
93: ###################################################################
94: ## ##
95: ## Tests used in checking the validitity of input ##
96: ## ##
97: ###################################################################
98:
99: =cut
100:
101: my $max_str_len = 50; # if a label, title, xlabel, or ylabel text
102: # is longer than this, it will be truncated.
103:
104: my %linetypes = # For png use these linetypes.
105: (
106: solid => 1,
107: dashed => 0
108: );
109: my %ps_linetypes = # For ps the line types are different!
110: (
111: solid => 1,
112: dashed => 7
113: );
114:
115: my %linestyles =
116: (
117: lines => 2, # Maybe this will be used in the future
118: linespoints => 2, # to check on whether or not they have
119: dots => 2, # supplied enough <data></data> fields
120: points => 2, # to use the given line style. But for
121: steps => 2, # now there are more important things
122: fsteps => 2, # for me to deal with.
123: histeps => 2,
124: errorbars => 3,
125: xerrorbars => [3,4],
126: yerrorbars => [3,4],
127: xyerrorbars => [4,6],
128: boxes => 3,
129: filledcurves => 2,
130: vector => 4
131: );
132:
133: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
134: my $real_test =
135: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
136: my $pos_real_test =
137: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
138: my $color_test;
139: if ($version < 4.6) {
140: $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~s/^\#/x/;$_[0]=~/^x[\da-fA-F]{6}$/};
141: } else {
142: $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~s/^x/#/;$_[0]=~/^\#[\da-fA-F]{6}$/};
143: }
144: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
145: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
146: my $sml_test = sub {$_[0]=~/^(\d+|small|medium|large)$/};
147: my $linestyle_test = sub {exists($linestyles{$_[0]})};
148: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
149:
150: my $arrowhead_test = sub{$_[0]=~/^(nohead|head|heads| )+$/};
151: my $arrowstyle_test= sub{$_[0]=~/^(filled|empty|nofilled)+$/};
152: my $degree_test = sub{&$pos_real_test($_[0]) && ($_[0] <= 360.0)};
153:
154: ###################################################################
155: ## ##
156: ## Attribute metadata ##
157: ## ##
158: ###################################################################
159: my @gnuplot_edit_order =
160: qw/alttag bgcolor fgcolor height width texwidth fontface font texfont
161: transparent grid samples
162: border align plotcolor plottype gridtype lmargin rmargin
163: tmargin bmargin major_ticscale minor_ticscale boxwidth gridlayer fillstyle
164: pattern solid/;
165:
166: my $margin_choices = ['default',0..20];
167:
168: my %gnuplot_defaults =
169: (
170: alttag => {
171: default => 'dynamically generated plot',
172: test => $words_test,
173: description => 'Brief description of the plot',
174: edit_type => 'entry',
175: size => '40'
176: },
177: height => {
178: default => 300,
179: test => $int_test,
180: description => 'Height of image (pixels)',
181: edit_type => 'entry',
182: size => '10'
183: },
184: width => {
185: default => 400,
186: test => $int_test,
187: description => 'Width of image (pixels)',
188: edit_type => 'entry',
189: size => '10'
190: },
191: bgcolor => {
192: default => $colorprefix.'ffffff',
193: test => $color_test,
194: description => 'Background color of image ('.$colorprefix.'ffffff)',
195: edit_type => 'entry',
196: size => '10',
197: class => 'colorchooser'
198: },
199: fgcolor => {
200: default => $colorprefix.'000000',
201: test => $color_test,
202: description => 'Foreground color of image ('.$colorprefix.'000000)',
203: edit_type => 'entry',
204: size => '10',
205: class => 'colorchooser'
206: },
207: transparent => {
208: default => 'off',
209: test => $onoff_test,
210: description => 'Transparent image',
211: edit_type => 'onoff'
212: },
213: grid => {
214: default => 'on',
215: test => $onoff_test,
216: description => 'Display grid',
217: edit_type => 'onoff'
218: },
219: gridlayer => {
220: default => 'off',
221: test => $onoff_test,
222: description => 'Display grid front layer over filled boxes or filled curves',
223: edit_type => 'onoff'
224: },
225: box_border => {
226: default => 'noborder',
227: test => sub {$_[0]=~/^(noborder|border)$/},
228: description => 'Draw border for boxes',
229: edit_type => 'choice',
230: choices => ['border','noborder']
231: },
232: border => {
233: default => 'on',
234: test => $onoff_test,
235: description => 'Draw border around plot',
236: edit_type => 'onoff'
237: },
238: font => {
239: default => '9',
240: test => $sml_test,
241: description => 'Font size to use in web output (pts)',
242: edit_type => 'choice',
243: choices => [['5','5 (small)'],'6','7','8',['9','9 (medium)'],'10',['11','11 (large)'],'12','15']
244: },
245: fontface => {
246: default => 'sans-serif',
247: test => sub {$_[0]=~/^(sans-serif|serif|classic)$/},
248: description => 'Type of font to use',
249: edit_type => 'choice',
250: choices => ['sans-serif','serif', 'classic']
251: },
252: samples => {
253: default => '100',
254: test => $int_test,
255: description => 'Number of samples for non-data plots',
256: edit_type => 'choice',
257: choices => ['100','200','500','1000','2000','5000']
258: },
259: align => {
260: default => 'middle',
261: test => sub {$_[0]=~/^(left|right|middle|center)$/},
262: description => 'Alignment for image in HTML',
263: edit_type => 'choice',
264: choices => ['left','right','middle','center']
265: },
266: texwidth => {
267: default => '93',
268: test => $int_test,
269: description => 'Width of plot when printed (mm)',
270: edit_type => 'entry',
271: size => '5'
272: },
273: texfont => {
274: default => '22',
275: test => $int_test,
276: description => 'Font size to use in TeX output (pts):',
277: edit_type => 'choice',
278: choices => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
279: },
280: plotcolor => {
281: default => 'monochrome',
282: test => sub {$_[0]=~/^(monochrome|color|colour)$/},
283: description => 'Color setting for printing:',
284: edit_type => 'choice',
285: choices => [qw/monochrome color colour/],
286: },
287: pattern => {
288: default => '',
289: test => $int_test,
290: description => 'Pattern value for boxes:',
291: edit_type => 'choice',
292: choices => [0,1,2,3,4,5,6]
293: },
294: solid => {
295: default => 0,
296: test => $real_test,
297: description => 'The density of fill style for boxes',
298: edit_type => 'entry',
299: size => '5'
300: },
301: fillstyle => {
302: default => 'empty',
303: test => sub {$_[0]=~/^(empty|solid|pattern)$/},
304: description => 'Filled style for boxes:',
305: edit_type => 'choice',
306: choices => ['empty','solid','pattern']
307: },
308: plottype => {
309: default => 'Cartesian',
310: test => sub {$_[0]=~/^(Polar|Cartesian)$/},
311: description => 'Plot type:',
312: edit_type => 'choice',
313: choices => ['Cartesian','Polar']
314: },
315: gridtype => {
316: default => 'Cartesian',
317: test => sub {$_[0]=~/^(Polar|Cartesian|Linear-Log|Log-Linear|Log-Log)$/},
318: description => 'Grid type:',
319: edit_type => 'choice',
320: choices => ['Cartesian','Polar','Linear-Log','Log-Linear','Log-Log']
321: },
322: lmargin => {
323: default => 'default',
324: test => sub {$_[0]=~/^(default|\d+)$/},
325: description => 'Left margin width (pts):',
326: edit_type => 'choice',
327: choices => $margin_choices,
328: },
329: rmargin => {
330: default => 'default',
331: test => sub {$_[0]=~/^(default|\d+)$/},
332: description => 'Right margin width (pts):',
333: edit_type => 'choice',
334: choices => $margin_choices,
335: },
336: tmargin => {
337: default => 'default',
338: test => sub {$_[0]=~/^(default|\d+)$/},
339: description => 'Top margin width (pts):',
340: edit_type => 'choice',
341: choices => $margin_choices,
342: },
343: bmargin => {
344: default => 'default',
345: test => sub {$_[0]=~/^(default|\d+)$/},
346: description => 'Bottom margin width (pts):',
347: edit_type => 'choice',
348: choices => $margin_choices,
349: },
350: boxwidth => {
351: default => '',
352: test => $real_test,
353: description => 'Width of boxes, default is auto',
354: edit_type => 'entry',
355: size => '5'
356: },
357: major_ticscale => {
358: default => '1',
359: test => $real_test,
360: description => 'Size of major tic marks (plot coordinates)',
361: edit_type => 'entry',
362: size => '5'
363: },
364: minor_ticscale => {
365: default => '0.5',
366: test => $real_test,
367: description => 'Size of minor tic mark (plot coordinates)',
368: edit_type => 'entry',
369: size => '5'
370: },
371: );
372:
373:
374: my %key_defaults =
375: (
376: title => {
377: default => '',
378: test => $words_test,
379: description => 'Title of key',
380: edit_type => 'entry',
381: size => '40'
382: },
383: box => {
384: default => 'off',
385: test => $onoff_test,
386: description => 'Draw a box around the key?',
387: edit_type => 'onoff'
388: },
389: pos => {
390: default => 'top right',
391: test => $key_pos_test,
392: description => 'Position of the key on the plot',
393: edit_type => 'choice',
394: choices => ['top left','top right','bottom left','bottom right',
395: 'outside','below']
396: }
397: );
398:
399: my %label_defaults =
400: (
401: xpos => {
402: default => 0,
403: test => $real_test,
404: description => 'X position of label (graph coordinates)',
405: edit_type => 'entry',
406: size => '10'
407: },
408: ypos => {
409: default => 0,
410: test => $real_test,
411: description => 'Y position of label (graph coordinates)',
412: edit_type => 'entry',
413: size => '10'
414: },
415: justify => {
416: default => 'left',
417: test => sub {$_[0]=~/^(left|right|center)$/},
418: description => 'justification of the label text on the plot',
419: edit_type => 'choice',
420: choices => ['left','right','center']
421: },
422: rotate => {
423: default => 0,
424: test => $real_test,
425: description => 'Rotation of label (degrees)',
426: edit_type => 'entry',
427: size => '10',
428: },
429: zlayer => {
430: default => '',
431: test => sub {$_[0]=~/^(front|back)$/},
432: description => 'Z position of label',
433: edit_type => 'choice',
434: choices => ['front','back'],
435: },
436: );
437:
438: my @tic_edit_order = ('location','mirror','start','increment','end',
439: 'minorfreq');
440: my %tic_defaults =
441: (
442: location => {
443: default => 'border',
444: test => sub {$_[0]=~/^(border|axis)$/},
445: description => 'Location of major tic marks',
446: edit_type => 'choice',
447: choices => ['border','axis']
448: },
449: mirror => {
450: default => 'on',
451: test => $onoff_test,
452: description => 'Mirror tics on opposite axis?',
453: edit_type => 'onoff'
454: },
455: start => {
456: default => '-10.0',
457: test => $real_test,
458: description => 'Start major tics at',
459: edit_type => 'entry',
460: size => '10'
461: },
462: increment => {
463: default => '1.0',
464: test => $real_test,
465: description => 'Place a major tic every',
466: edit_type => 'entry',
467: size => '10'
468: },
469: end => {
470: default => ' 10.0',
471: test => $real_test,
472: description => 'Stop major tics at ',
473: edit_type => 'entry',
474: size => '10'
475: },
476: minorfreq => {
477: default => '0',
478: test => $int_test,
479: description => 'Number of minor tics per major tic mark',
480: edit_type => 'entry',
481: size => '10'
482: },
483: rotate => {
484: default => 'off',
485: test => $onoff_test,
486: description => 'Rotate tic label by 90 degrees if on',
487: edit_type => 'onoff'
488: }
489: );
490:
491: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax','xformat', 'yformat', 'xzero', 'yzero');
492: my %axis_defaults =
493: (
494: color => {
495: default => $colorprefix.'000000',
496: test => $color_test,
497: description => 'Color of grid lines ('.$colorprefix.'000000)',
498: edit_type => 'entry',
499: size => '10',
500: class => 'colorchooser'
501: },
502: xmin => {
503: default => '-10.0',
504: test => $real_test,
505: description => 'Minimum x-value shown in plot',
506: edit_type => 'entry',
507: size => '10'
508: },
509: xmax => {
510: default => ' 10.0',
511: test => $real_test,
512: description => 'Maximum x-value shown in plot',
513: edit_type => 'entry',
514: size => '10'
515: },
516: ymin => {
517: default => '-10.0',
518: test => $real_test,
519: description => 'Minimum y-value shown in plot',
520: edit_type => 'entry',
521: size => '10'
522: },
523: ymax => {
524: default => ' 10.0',
525: test => $real_test,
526: description => 'Maximum y-value shown in plot',
527: edit_type => 'entry',
528: size => '10'
529: },
530: xformat => {
531: default => 'on',
532: test => sub {$_[0]=~/^(on|off|\d+(f|F|e|E|P(|\s*[Pp][Ii])))$/},
533: description => 'X-axis number formatting',
534: edit_type => 'choice',
535: choices => ['on', 'off', '2e', '2f'],
536: },
537: yformat => {
538: default => 'on',
539: test => sub {$_[0]=~/^(on|off|\d+(f|F|e|E|P(|\s*[Pp][Ii])))$/},
540: description => 'Y-axis number formatting',
541: edit_type => 'choice',
542: choices => ['on', 'off', '2e', '2f'],
543: },
544:
545: xzero => {
546: default => 'off',
547: test => sub {$_[0]=~/^(off|line|thick-line|dotted)$/},
548: description => 'Show x-zero (y=0) axis',
549: edit_type => 'choice',
550: choices => ['off', 'line', 'thick-line', 'dotted'],
551: },
552:
553: yzero => {
554: default => 'off',
555: test => sub {$_[0]=~/^(off|line|thick-line|dotted)$/},
556: description => 'Show y-zero (x=0) axis',
557: edit_type => 'choice',
558: choices => ['off', 'line', 'thick-line', 'dotted'],
559: },
560: );
561:
562:
563: my @curve_edit_order = ('color','name','linestyle','linewidth','linetype',
564: 'pointtype','pointsize','limit', 'arrowhead', 'arrowstyle',
565: 'arrowlength', 'arrowangle', 'arrowbackangle'
566: );
567:
568: my %curve_defaults =
569: (
570: color => {
571: default => $colorprefix.'000000',
572: test => $color_test,
573: description => 'Color of curve ('.$colorprefix.'000000)',
574: edit_type => 'entry',
575: size => '10',
576: class => 'colorchooser'
577: },
578: name => {
579: default => '',
580: test => $words_test,
581: description => 'Name of curve to appear in key',
582: edit_type => 'entry',
583: size => '20'
584: },
585: linestyle => {
586: default => 'lines',
587: test => $linestyle_test,
588: description => 'Plot with:',
589: edit_type => 'choice',
590: choices => [keys(%linestyles)]
591: },
592: linewidth => {
593: default => 1,
594: test => $int_test,
595: description => 'Line width (may not apply to all plot styles)',
596: edit_type => 'choice',
597: choices => [1,2,3,4,5,6,7,8,9,10]
598: },
599: linetype => {
600: default => 'solid',
601: test => sub {$_[0]=~/^(solid|dashed)$/},
602: description => 'Line type (may not apply to all plot styles)',
603: edit_type => 'choice',
604: choices => ['solid', 'dashed']
605: },
606: pointsize => {
607: default => 1,
608: test => $pos_real_test,
609: description => 'Point size (may not apply to all plot styles)',
610: edit_type => 'entry',
611: size => '5'
612: },
613: pointtype => {
614: default => 1,
615: test => $int_test,
616: description => 'Point type (may not apply to all plot styles)',
617: edit_type => 'choice',
618: choices => [0,1,2,3,4,5,6]
619: },
620: limit => {
621: default => 'closed',
622: test => sub {$_[0]=~/^(above|below|closed|x1|x2|y1|y2)$/},
623: description => 'Point to fill -- for filledcurves',
624: edit_type => 'choice',
625: choices => ['above', 'below', 'closed','x1','x2','y1','y2']
626: },
627: arrowhead => {
628: default => 'head',
629: test => $arrowhead_test,
630: description => 'Vector arrow head type',
631: edit_type => 'choice',
632: choices => ['nohead', 'head', 'heads']
633: },
634: arrowstyle => {
635: default => 'filled',
636: test => $arrowstyle_test,
637: description => 'Vector arrow head style',
638: edit_type => 'choice',
639: choices => ['filled', 'empty', 'nofilled']
640: },
641: arrowlength => {
642: default => 0.02,
643: test => $pos_real_test,
644: description => "Length of vector arrow (only applies to vector plots)",
645: edit_type => 'entry',
646: size => '5'
647: },
648: arrowangle => {
649: default => 10.0,
650: test => $degree_test,
651: description => 'Angle of arrow branches to arrow body (only applies to vector plots)',
652: edit_type => 'entry',
653: size => '5'
654: },
655:
656: arrowbackangle => {
657: default => 90.0,
658: test => $degree_test,
659: descripton => 'Angle of arrow back lines to branches.',
660: edit_type => 'entry',
661: size => '5'
662: }
663:
664: );
665:
666: ###################################################################
667: ## ##
668: ## parsing and edit rendering ##
669: ## ##
670: ###################################################################
671:
672: undef %Apache::lonplot::plot;
673: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
674:
675: my $current_tics; # Reference to the current tick hash
676:
677: sub start_gnuplot {
678: undef(%Apache::lonplot::plot); undef(%key); undef(%axis);
679: undef($title); undef($xlabel); undef($ylabel);
680: undef(@labels); undef(@curves);
681: undef(%xtics); undef(%ytics);
682: #
683: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
684: my $result='';
685: &Apache::lonxml::register('Apache::lonplot',
686: ('title','xlabel','ylabel','key','axis','label','curve',
687: 'xtics','ytics'));
688: push (@Apache::lonxml::namespace,'lonplot');
689: if ($target eq 'web' || $target eq 'tex') {
690: &get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
691: $tagstack->[-1]);
692: } elsif ($target eq 'edit') {
693: $result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
694: $result .= &edit_attributes($target,$token,\%gnuplot_defaults,
695: \@gnuplot_edit_order)
696: .&Apache::edit::end_row()
697: .&Apache::edit::start_spanning_row();
698: } elsif ($target eq 'modified') {
699: my $constructtag=&Apache::edit::get_new_args
700: ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
701:
702: if ($constructtag) {
703: #
704: # Color chooser does not prepend x (or #) to the color values
705: # Do that here:
706: #
707: foreach my $attribute ('bgcolor', 'fgcolor') {
708: my $value = $token->[2]{$attribute};
709: if (defined $value && ($value !~ /^\Q$colorprefix\E/)) {
710: $token->[2]{$attribute} = $colorprefix . $value;
711: }
712: }
713: $result = &Apache::edit::rebuild_tag($token);
714: }
715: }
716: return $result;
717: }
718:
719: sub end_gnuplot {
720: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
721: pop @Apache::lonxml::namespace;
722: &Apache::lonxml::deregister('Apache::lonplot',
723: ('title','xlabel','ylabel','key','axis','label','curve'));
724: my $result = '';
725: my $randnumber;
726: my $tmpdir =LONCAPA::tempdir(); # Where temporary files live:
727:
728: # need to call rand everytime start_script would evaluate, as the
729: # safe space rand number generator and the global rand generator
730: # are not separate
731: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
732: $target eq 'answer') {
733: $randnumber=int(rand(1000));
734: }
735: if ($target eq 'web' || $target eq 'tex') {
736: &check_inputs(); # Make sure we have all the data we need
737: ##
738: ## Determine filename
739: my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
740: '_'.time.'_'.$$.$randnumber.'_plot';
741: ## Write the plot description to the file
742: &write_gnuplot_file($tmpdir,$filename,$target);
743: $filename = &escape($filename);
744: ## return image tag for the plot
745: if ($target eq 'web') {
746: my $srcatt = "src=\"/cgi-bin/plot.$weboutputformat?file=$filename.data\"";
747: my $widthatt = "width=\"$Apache::lonplot::plot{'width'}\"";
748: my $heightatt = "height=\"$Apache::lonplot::plot{'height'}\"";
749: my $alignatt = "align=\"$Apache::lonplot::plot{'align'}\"";
750: my $altatt = "alt=\"$Apache::lonplot::plot{'alttag'}\"";
751: if ($Apache::lonplot::plot{'align'} eq 'center') {
752: $result .= '<div style="text-align:center">'.
753: "<img $srcatt $widthatt $heightatt $altatt>".
754: "</div>\n";
755:
756: } else {
757: $result .= "<img $srcatt $widthatt $heightatt $alignatt $altatt>";
758: }
759: } elsif ($target eq 'tex') {
760: &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
761: &Apache::lonxml::debug(" gnuplot ht = $Apache::lonplot::plot{'height'}");
762: #might be inside the safe space, register the URL for later
763: &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
764: $result = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'}\n";
765: $result .= '\graphicspath{{'.$tmpdir.'}}'."\n";
766: if ($Apache::lonplot::plot{'align'} eq 'center') {
767: $result .= '\begin{center}';
768: }
769: $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&unescape($filename).'.eps}';
770: if ($Apache::lonplot::plot{'align'} eq 'center') {
771: $result .= '\end{center}';
772: }
773: }
774: } elsif ($target eq 'edit') {
775: $result.=&Apache::edit::tag_end($target,$token);
776: }
777: return $result;
778: }
779:
780:
781: ##--------------------------------------------------------------- xtics
782: sub start_xtics {
783: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
784: my $result='';
785: if ($target eq 'web' || $target eq 'tex') {
786: &get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
787: $tagstack->[-1]);
788: $current_tics = \%xtics;
789: &Apache::lonxml::register('Apache::lonplot', 'tic');
790: } elsif ($target eq 'edit') {
791: $result .= &Apache::edit::tag_start($target,$token,'xtics');
792: $result .= &edit_attributes($target,$token,\%tic_defaults,
793: \@tic_edit_order);
794: } elsif ($target eq 'modified') {
795: my $constructtag=&Apache::edit::get_new_args
796: ($token,$parstack,$safeeval,keys(%tic_defaults));
797: if ($constructtag) {
798: $result = &Apache::edit::rebuild_tag($token);
799: }
800: }
801: return $result;
802: }
803:
804: sub end_xtics {
805: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
806: my $result = '';
807: if ($target eq 'web' || $target eq 'tex') {
808: &Apache::lonxml::deregister('Apache::lonplot', 'tic');
809: } elsif ($target eq 'edit') {
810: $result.=&Apache::edit::tag_end($target,$token);
811: }
812: return $result;
813: }
814:
815: ##--------------------------------------------------------------- ytics
816: sub start_ytics {
817: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
818: my $result='';
819: if ($target eq 'web' || $target eq 'tex') {
820: &get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
821: $tagstack->[-1]);
822: $current_tics = \%ytics;
823: &Apache::lonxml::register('Apache::lonplot', 'tic');
824: } elsif ($target eq 'edit') {
825: $result .= &Apache::edit::tag_start($target,$token,'ytics');
826: $result .= &edit_attributes($target,$token,\%tic_defaults,
827: \@tic_edit_order);
828: } elsif ($target eq 'modified') {
829: my $constructtag=&Apache::edit::get_new_args
830: ($token,$parstack,$safeeval,keys(%tic_defaults));
831: if ($constructtag) {
832: $result = &Apache::edit::rebuild_tag($token);
833: }
834: }
835: return $result;
836: }
837:
838: sub end_ytics {
839: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
840: my $result = '';
841: if ($target eq 'web' || $target eq 'tex') {
842: &Apache::lonxml::deregister('Apache::lonplot', 'tic');
843: } elsif ($target eq 'edit') {
844: $result.=&Apache::edit::tag_end($target,$token);
845: }
846: return $result;
847: }
848:
849:
850: ##----------------------------------------------------------------
851: #
852: # Tic handling:
853: # The <tic> tag allows users to specify exact Tic positions and labels
854: # for each axis. In this version we only support level 0 tics (major tic).
855: # Each tic has associated with it a position and a label
856: # $current_tics is a reference to the current tick description hash.
857: # We add elements to an array in that has: ticspecs whose elements
858: # are 'pos' - the tick position and 'label' - the tic label.
859: #
860:
861:
862: sub start_tic {
863: my ($target, $token, $tagstack, $parstack, $parser, $safeeval, $style) = @_;
864:
865: my $result = '';
866: if ($target eq 'web' || $target eq 'tex') {
867: my $tic_location = &Apache::lonxml::get_param('location', $parstack, $safeeval);
868: my $tic_label = &Apache::lonxml::get_all_text('/tic', $parser);
869:
870: # Tic location must e a real:
871:
872: if (!&$real_test($tic_location)) {
873: &Apache::lonxml::warning("Tic location: $tic_location must be a real number");
874: } else {
875:
876: if (!defined $current_tics->{'ticspecs'}) {
877: $current_tics->{'ticspecs'} = [];
878: }
879: my $ticspecs = $current_tics->{'ticspecs'};
880: push (@$ticspecs, {'pos' => $tic_location, 'label' => $tic_label});
881: }
882: }
883:
884: return $result;
885: }
886:
887: sub end_tic {
888: return '';
889: }
890:
891: ##-----------------------------------------------------------------font
892: my %font_properties =
893: (
894: 'classic' => {
895: face => 'classic',
896: file => 'DejaVuSansMono-Bold',
897: printname => 'Helvetica',
898: tex_no_file => 1,
899: },
900: 'sans-serif' => {
901: face => 'sans-serif',
902: file => 'DejaVuSans',
903: printname => 'DejaVuSans',
904: },
905: 'serif' => {
906: face => 'serif',
907: file => 'DejaVuSerif',
908: printname => 'DejaVuSerif',
909: },
910: );
911:
912: sub get_font {
913: my ($target) = @_;
914: my ($size, $selected_font);
915:
916: if ( $Apache::lonplot::plot{'font'} =~ /^(small|medium|large)/) {
917: $selected_font = $font_properties{'classic'};
918: if ( $Apache::lonplot::plot{'font'} eq 'small') {
919: $size = '5';
920: } elsif ( $Apache::lonplot::plot{'font'} eq 'medium') {
921: $size = '9';
922: } elsif ( $Apache::lonplot::plot{'font'} eq 'large') {
923: $size = '11';
924: } else {
925: $size = '9';
926: }
927: } else {
928: $size = $Apache::lonplot::plot{'font'};
929: $selected_font = $font_properties{$Apache::lonplot::plot{'fontface'}};
930: }
931: if ($target eq 'tex' && defined($Apache::lonplot::plot{'texfont'})) {
932: # $selected_font = $font_properties{'classic'};
933: $size = $Apache::lonplot::plot{'texfont'};
934: }
935: return ($size, $selected_font);
936: }
937:
938: ##----------------------------------------------------------------- key
939: sub start_key {
940: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
941: my $result='';
942: if ($target eq 'web' || $target eq 'tex') {
943: &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
944: $tagstack->[-1]);
945: } elsif ($target eq 'edit') {
946: $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
947: $result .= &edit_attributes($target,$token,\%key_defaults);
948: } elsif ($target eq 'modified') {
949: my $constructtag=&Apache::edit::get_new_args
950: ($token,$parstack,$safeeval,keys(%key_defaults));
951: if ($constructtag) {
952: $result = &Apache::edit::rebuild_tag($token);
953: }
954: }
955: return $result;
956: }
957:
958: sub end_key {
959: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
960: my $result = '';
961: if ($target eq 'web' || $target eq 'tex') {
962: } elsif ($target eq 'edit') {
963: $result.=&Apache::edit::tag_end($target,$token);
964: }
965: return $result;
966: }
967:
968: sub parse_label {
969: my ($target,$text) = @_;
970: my $parser=HTML::LCParser->new(\$text);
971: my $result;
972: while (my $token=$parser->get_token) {
973: if ($token->[0] eq 'S') {
974: if ($token->[1] eq 'sub') {
975: $result .= '_{';
976: } elsif ($token->[1] eq 'sup') {
977: $result .= '^{';
978: } else {
979: $result .= $token->[4];
980: }
981: } elsif ($token->[0] eq 'E') {
982: if ($token->[1] eq 'sub'
983: || $token->[1] eq 'sup') {
984: $result .= '}';
985: } else {
986: $result .= $token->[2];
987: }
988: } elsif ($token->[0] eq 'T') {
989: $result .= &replace_entities($target,$token->[1]);
990: }
991: }
992: return $result;
993: }
994:
995: #
996: # Note that there are severe restrictions on font selection in the
997: # ps driver now. later in life Gnuplot is supposed to support
998: # utf-8 fonts in the posts script driver. When this happens,
999: # the tex entries with comments that include the word <FIX>
1000: # should be changed to print the correct glyphs rather than some
1001: # approximation or fallback of what is intended.
1002:
1003: my %lookup =
1004: ( # Greek alphabet:
1005:
1006: '(Alpha|#913)' => {'tex' => '{/Symbol A}', 'web' => "\x{391}"},
1007: '(Beta|#914)' => {'tex' => '{/Symbol B}', 'web' => "\x{392}"},
1008: '(Chi|#935)' => {'tex' => '{/Symbol C}', 'web' => "\x{3A7}"},
1009: '(Delta|#916)' => {'tex' => '{/Symbol D}', 'web' => "\x{394}"},
1010: '(Epsilon|#917)' => {'tex' => '{/Symbol E}', 'web' => "\x{395}"},
1011: '(Phi|#934)' => {'tex' => '{/Symbol F}', 'web' => "\x{3A6}"},
1012: '(Gamma|#915)' => {'tex' => '{/Symbol G}', 'web' => "\x{393}"},
1013: '(Eta|#919)' => {'tex' => '{/Symbol H}', 'web' => "\x{397}"},
1014: '(Iota|#921)' => {'tex' => '{/Symbol I}', 'web' => "\x{399}"},
1015: '(Kappa|#922)' => {'tex' => '{/Symbol K}', 'web' => "\x{39A}"},
1016: '(Lambda|#923)' => {'tex' => '{/Symbol L}', 'web' => "\x{39B}"},
1017: '(Mu|#924)' => {'tex' => '{/Symbol M}', 'web' => "\x{39C}"},
1018: '(Nu|#925)' => {'tex' => '{/Symbol N}', 'web' => "\x{39D}"},
1019: '(Omicron|#927)' => {'tex' => '{/Symbol O}', 'web' => "\x{39F}"},
1020: '(Pi|#928)' => {'tex' => '{/Symbol P}', 'web' => "\x{3A0}"},
1021: '(Theta|#920)' => {'tex' => '{/Symbol Q}', 'web' => "\x{398}"},
1022: '(Rho|#929)' => {'tex' => '{/Symbol R}', 'web' => "\x{3A1}"},
1023: '(Sigma|#931)' => {'tex' => '{/Symbol S}', 'web' => "\x{3A3}"},
1024: '(Tau|#932)' => {'tex' => '{/Symbol T}', 'web' => "\x{3A4}"},
1025: '(Upsilon|#933)' => {'tex' => '{/Symbol U}', 'web' => "\x{3A5}"},
1026: '(Omega|#937)' => {'tex' => '{/Symbol W}', 'web' => "\x{3A9}"},
1027: '(Xi|#926)' => {'tex' => '{/Symbol X}', 'web' => "\x{39E}"},
1028: '(Psi|#936)' => {'tex' => '{/Symbol Y}', 'web' => "\x{3A8}"},
1029: '(Zeta|#918)' => {'tex' => '{/Symbol Z}', 'web' => "\x{396}"},
1030: '(alpha|#945)' => {'tex' => '{/Symbol a}', 'web' => "\x{3B1}"},
1031: '(beta|#946)' => {'tex' => '{/Symbol b}', 'web' => "\x{3B2}"},
1032: '(chi|#967)' => {'tex' => '{/Symbol c}', 'web' => "\x{3C7}"},
1033: '(delta|#948)' => {'tex' => '{/Symbol d}', 'web' => "\x{3B4}"},
1034: '(epsilon|#949)' => {'tex' => '{/Symbol e}', 'web' => "\x{3B5}"},
1035: '(phi|#966)' => {'tex' => '{/Symbol f}', 'web' => "\x{3C6}"},
1036: '(gamma|#947)' => {'tex' => '{/Symbol g}', 'web' => "\x{3B3}"},
1037: '(eta|#951)' => {'tex' => '{/Symbol h}', 'web' => "\x{3B7}"},
1038: '(iota|#953)' => {'tex' => '{/Symbol i}', 'web' => "\x{3B9}"},
1039: '(kappa|#954)' => {'tex' => '{/Symbol k}', 'web' => "\x{3BA}"},
1040: '(lambda|#955)' => {'tex' => '{/Symbol k}', 'web' => "\x{3BB}"},
1041: '(mu|#956)' => {'tex' => '{/Symbol m}', 'web' => "\x{3BC}"},
1042: '(nu|#957)' => {'tex' => '{/Symbol n}', 'web' => "\x{3BD}"},
1043: '(omicron|#959)' => {'tex' => '{/Symbol o}', 'web' => "\x{3BF}"},
1044: '(pi|#960)' => {'tex' => '{/Symbol p}', 'web' => "\x{3C0}"},
1045: '(theta|#952)' => {'tex' => '{/Symbol q}', 'web' => "\x{3B8}"},
1046: '(rho|#961)' => {'tex' => '{/Symbol r}', 'web' => "\x{3C1}"},
1047: '(sigma|#963)' => {'tex' => '{/Symbol s}', 'web' => "\x{3C3}"},
1048: '(tau|#964)' => {'tex' => '{/Symbol t}', 'web' => "\x{3C4}"},
1049: '(upsilon|#965)' => {'tex' => '{/Symbol u}', 'web' => "\x{3C5}"},
1050: '(omega|#969)' => {'tex' => '{/Symbol w}', 'web' => "\x{3C9}"},
1051: '(xi|#958)' => {'tex' => '{/Symbol x}', 'web' => "\x{3BE}"},
1052: '(psi|#968)' => {'tex' => '{/Symbol y}', 'web' => "\x{3C8}"},
1053: '(zeta|#950)' => {'tex' => '{/Symbol z}', 'web' => "\x{3B6}"},
1054: '(thetasym|#977)' => {'tex' => '{/Symbol \165}', 'web' => "\x{3d1}"},
1055: '(upsih|#978)' => {'tex' => '{/Symbol \241}', 'web' => "\x{3d2}"},
1056: '(piv|#982)' => {'tex' => '{/Symbol \166}', 'web' => "\x{3d6}"},
1057:
1058:
1059: # Punctuation:
1060:
1061: '(quot|#034)' => {'tex' => '\42', 'web' => '\42'},
1062: '(amp|#038)' => {'tex' => '\46', 'web' => '\46'},
1063: '(lt|#060)' => {'tex' => '\74', 'web' => '\74'},
1064: '(gt|#062)' => {'tex' => '\76', 'web' => '\76'},
1065: '#131' => {'tex' => '{/Symbol \246}', 'web' => "\x{192}"},
1066: '#132' => {'tex' => '{/Text \271}', 'web' => "\x{201e}"},
1067: '#133' => {'tex' => '{/Symbol \274}', 'web'=> "\x{2026}"},
1068: '#134' => {'tex' => '{/Text \262}', 'web' => "\x{2020}"},
1069: '#135' => {'tex' => '{/Text \263}', 'web' => "\x{2021}"},
1070: '#136' => {'tex' => '\\\\^', 'web' => '\\\\^'},
1071: '#137' => {'tex' => '%o', 'web' => "\x{2030}"}, # Per Mille <FIX>
1072: '#138' => {'tex' => 'S', 'web' => "\x{160}"}, # S-Caron <FIX>
1073: '#139' => {'tex' => '<', 'web' => '<'},
1074: '#140' => {'tex' => 'AE', 'web' => "\x{152}"}, # AE ligature <FIX>
1075: '#145' => {'tex' => '\140', 'web' => "\x{2018}"},
1076: '#146' => {'tex' => '\47', 'web' => "\x{2019}"},
1077: '#147' => {'tex' => '\140\140', 'web' => "\x{201c}"}, # Left " <FIX>
1078: '#148' => {'tex' => '\47\47', 'web' => '\\"'}, # Right " <FIX>
1079: '#149' => {'tex' => '{/Symbol \267}', 'web' => "\x{2022}"},
1080: '#150' => {'tex' => '{/Text \55}', 'web' => "\x{2013}"}, # en dash
1081: '#151' => {'tex' => '{/Symbol \55}', 'web' => "\x{2014}"}, # em dash
1082: '#152' => {'tex' => '\\\\~', 'web' => '\\\\~'},
1083: '#153' => {'tex' => '{/Symbol \324}', 'web' => "\x{2122}"}, # trademark
1084:
1085: # Accented letters, and other furreign language glyphs.
1086:
1087: '#154' => {'tex' => 's', 'web' => "\x{161}"}, # small s-caron no ps.
1088: '#155' => {'tex' => '>', 'web' => '\76'}, # >
1089: '#156' => {'tex' => '{/Text \366}', 'web' => "\x{153}"}, # oe ligature.<FIX>
1090: '#159', => {'tex' => 'Y', 'web' => "\x{178}"}, # Y-umlaut - can't print <FIX>
1091: '(nbsp|#160)' => {'tex' => ' ', 'web' => ' '}, # non breaking space.
1092: '(iexcl|#161)' => {'tex' => '{/Text \241}', 'web' => "\x{a1}"}, # inverted !
1093: '(cent|#162)' => {'tex' => '{/Text \242}', 'web' => "\x{a2}"}, # Cent currency.
1094: '(pound|#163)' => {'tex' => '{/Text \243}', 'web' => "\x{a3}"}, # GB Pound currency.
1095: '(curren|#164)' => {'tex' => '{/ZapfDingbats \161}','web' => "\x{a4}"}, # Generic currency symb. <FIX>
1096: '(yen|#165)' => {'tex' => '{/Text \245}', 'web' => "\x{a5}"}, # Yen currency.
1097: '(brvbar|#166)' => {'tex' => '{/Symbol \174}', 'web' => "\x{a6}"}, # Broken vert bar no print.
1098: '(sect|#167)' => {'tex' => '{\247}', 'web' => "\x{a7}"}, # Section symbol.
1099: '(uml|#168)' => {'tex' => '{\250}', 'web' => "\x{a8}"}, # 'naked' umlaut.
1100: '(copy|#169)' => {'tex' => '{/Symbol \343}', 'web' => "\x{a9}"}, # Copyright symbol.
1101: '(ordf|#170)' => {'tex' => '{/Text \343}', 'web' => "\x{aa}"}, # Feminine ordinal.
1102: '(laquo|#171)' => {'tex' => '{/Text \253}', 'web' => "\x{ab}"}, # << quotes.
1103: '(not|#172)' => {'tex' => '\254', 'web' => "\x{ac}"}, # Logical not.
1104: '(shy|#173)' => {'tex' => '\255', 'web' => "\x{ad}"}, # soft hyphen.
1105: '(reg|#174)' => {'tex' => '{/Symbol \342}', 'web' => "\x{ae}"}, # Registered tm.
1106: '(macr|#175)' => {'tex' => '^{\255}', 'web' => "\x{af}"}, # 'naked' macron (overbar).
1107: '(deg|#176)' => {'tex' => '{/Text \260}', 'web' => "\x{b0}"}, # Degree symbo..`
1108: '(plusmn|#177)' => {'tex' => '{/Symbol \261}', 'web' => "\x{b1}"}, # +/- symbol.
1109: '(sup2|#178)' => {'tex' => '^2', 'web' => "\x{b2}"}, # Superscript 2.
1110: '(sup3|#179)' => {'tex' => '^3', 'web' => "\x{b3}"}, # Superscript 3.
1111: '(acute|#180)' => {'tex' => '{/Text \222}', 'web' => "\x{b4}"}, # 'naked' acute accent.
1112: '(micro|#181)' => {'tex' => '{/Symbol \155}', 'web' => "\x{b5}"}, # Micro (small mu).
1113: '(para|#182)' => {'tex' => '{/Text \266}', 'web' => "\x{b6}"}, # Paragraph symbol.
1114: '(middot|#183)' => {'tex' => '\267', 'web' => "\x{b7}"}, # middle dot
1115: '(cedil|#184)' => {'tex' => '\233', 'web' => "\x{b8}"}, # 'naked' cedilla.
1116: '(sup1|#185)' => {'tex' => '^1', 'web' => "\x{b9}"}, # superscript 1.
1117: '(ordm|#186)' => {'tex' => '{\260}', 'web' => "\x{ba}"}, # masculine ordinal.
1118: '(raquo|#187)', => {'tex' => '\273', 'web' => "\x{bb}"}, # Right angle quotes.
1119: '(frac14|#188)' => {'tex' => '\274', 'web' => "\x{bc}"}, # 1/4.
1120: '(frac12|#189)' => {'tex' => '\275', 'web' => "\x{bd}"}, # 1/2.
1121: '(frac34|#190)' => {'tex' => '\276', 'web' => "\x{be}"}, # 3/4
1122: '(iquest|#191)' => {'tex' => '{/Text \277}', 'web' => "\x{bf}"}, # Inverted ?
1123: '(Agrave|#192)' => {'tex' => '\300', 'web' => "\x{c0}"}, # A Grave.
1124: '(Aacute|#193)' => {'tex' => '\301', 'web' => "\x{c1}"}, # A Acute.
1125: '(Acirc|#194)' => {'tex' => '\302', 'web' => "\x{c2}"}, # A Circumflex.
1126: '(Atilde|#195)' => {'tex' => '\303', 'web' => "\x{c3}"}, # A tilde.
1127: '(Auml|#196)' => {'tex' => '\304', 'web' => "\x{c4}"}, # A umlaut.
1128: '(Aring|#197)' => {'tex' => '\305', 'web' => "\x{c5}"}, # A ring.
1129: '(AElig|#198)' => {'tex' => '\306', 'web' => "\x{c6}"}, # AE ligature.
1130: '(Ccedil|#199)' => {'tex' => '\307', 'web' => "\x{c7}"}, # C cedilla
1131: '(Egrave|#200)' => {'tex' => '\310', 'web' => "\x{c8}"}, # E Accent grave.
1132: '(Eacute|#201)' => {'tex' => '\311', 'web' => "\x{c9}"}, # E acute accent.
1133: '(Ecirc|#202)' => {'tex' => '\312', 'web' => "\x{ca}"}, # E Circumflex.
1134: '(Euml|#203)' => {'tex' => '\313', 'web' => "\x{cb}"}, # E umlaut.
1135: '(Igrave|#204)' => {'tex' => '\314', 'web' => "\x{cc}"}, # I grave accent.
1136: '(Iacute|#205)' => {'tex' => '\315', 'web' => "\x{cd}"}, # I acute accent.
1137: '(Icirc|#206)' => {'tex' => '\316', 'web' => "\x{ce}"}, # I circumflex.
1138: '(Iuml|#207)' => {'tex' => '\317', 'web' => "\x{cf}"}, # I umlaut.
1139: '(ETH|#208)' => {'tex' => '\320', 'web' => "\x{d0}"}, # Icelandic Cap eth.
1140: '(Ntilde|#209)' => {'tex' => '\321', 'web' => "\x{d1}"}, # Ntilde (enyan).
1141: '(Ograve|#210)' => {'tex' => '\322', 'web' => "\x{d2}"}, # O accent grave.
1142: '(Oacute|#211)' => {'tex' => '\323', 'web' => "\x{d3}"}, # O accent acute.
1143: '(Ocirc|#212)' => {'tex' => '\324', 'web' => "\x{d4}"}, # O circumflex.
1144: '(Otilde|#213)' => {'tex' => '\325', 'web' => "\x{d5}"}, # O tilde.
1145: '(Ouml|#214)' => {'tex' => '\326', 'web' => "\x{d6}"}, # O umlaut.
1146: '(times|#215)' => {'tex' => '\327', 'web' => "\x{d7}"}, # Times symbol.
1147: '(Oslash|#216)' => {'tex' => '\330', 'web' => "\x{d8}"}, # O slash.
1148: '(Ugrave|#217)' => {'tex' => '\331', 'web' => "\x{d9}"}, # U accent grave.
1149: '(Uacute|#218)' => {'tex' => '\332', 'web' => "\x{da}"}, # U accent acute.
1150: '(Ucirc|#219)' => {'tex' => '\333', 'web' => "\x{db}"}, # U circumflex.
1151: '(Uuml|#220)' => {'tex' => '\334', 'web' => "\x{dc}"}, # U umlaut.
1152: '(Yacute|#221)' => {'tex' => '\335', 'web' => "\x{dd}"}, # Y accent acute.
1153: '(THORN|#222)' => {'tex' => '\336', 'web' => "\x{de}"}, # Icelandic thorn.
1154: '(szlig|#223)' => {'tex' => '\337', 'web' => "\x{df}"}, # German sharfes s.
1155: '(agrave|#224)' => {'tex' => '\340', 'web' => "\x{e0}"}, # a accent grave.
1156: '(aacute|#225)' => {'tex' => '\341', 'web' => "\x{e1}"}, # a grave.
1157: '(acirc|#226)' => {'tex' => '\342', 'web' => "\x{e2}"}, # a circumflex.
1158: '(atilde|#227)' => {'tex' => '\343', 'web' => "\x{e3}"}, # a tilde.
1159: '(auml|#228)' => {'tex' => '\344', 'web' => "\x{e4}"}, # a umlaut
1160: '(aring|#229)' => {'tex' => '\345', 'web' => "\x{e5}"}, # a ring on top.
1161: '(aelig|#230)' => {'tex' => '\346', 'web' => "\x{e6}"}, # ae ligature.
1162: '(ccedil|#231)' => {'tex' => '\347', 'web' => "\x{e7}"}, # C cedilla
1163: '(egrave|#232)' => {'tex' => '\350', 'web' => "\x{e8}"}, # e accent grave.
1164: '(eacute|#233)' => {'tex' => '\351', 'web' => "\x{e9}"}, # e accent acute.
1165: '(ecirc|#234)' => {'tex' => '\352', 'web' => "\x{ea}" }, # e circumflex.
1166: '(euml|#235)' => {'tex' => '\353', 'web' => "\x{eb}"}, # e umlaut.
1167: '(igrave|#236)' => {'tex' => '\354', 'web' => "\x{ec}"}, # i grave.
1168: '(iacute|#237)' => {'tex' => '\355', 'web' => "\x{ed}"}, # i acute.
1169: '(icirc|#238)' => {'tex' => '\356', 'web' => "\x{ee}"}, # i circumflex.
1170: '(iuml|#239)' => {'tex' => '\357', 'web' => "\x{ef}"}, # i umlaut.
1171: '(eth|#240)' => {'tex' => '\360', 'web' => "\x{f0}"}, # Icelandic eth.
1172: '(ntilde|#241)' => {'tex' => '\361', 'web' => "\x{f1}"}, # n tilde.
1173: '(ograve|#242)' => {'tex' => '\362', 'web' => "\x{f2}"}, # o grave.
1174: '(oacute|#243)' => {'tex' => '\363', 'web' => "\x{f3}"}, # o acute.
1175: '(ocirc|#244)' => {'tex' => '\364', 'web' => "\x{f4}"}, # o circumflex.
1176: '(otilde|#245)' => {'tex' => '\365', 'web' => "\x{f5}"}, # o tilde.
1177: '(ouml|#246)' => {'tex' => '\366', 'web' => "\x{f6}"}, # o umlaut.
1178: '(divide|#247)' => {'tex' => '\367', 'web' => "\x{f7}"}, # division symbol
1179: '(oslash|#248)' => {'tex' => '\370', 'web' => "\x{f8}"}, # o slashed.
1180: '(ugrave|#249)' => {'tex' => '\371', 'web' => "\x{f9}"}, # u accent grave.
1181: '(uacute|#250)' => {'tex' => '\372', 'web' => "\x{fa}"}, # u acute.
1182: '(ucirc|#251)' => {'tex' => '\373', 'web' => "\x{fb}"}, # u circumflex.
1183: '(uuml|#252)' => {'tex' => '\374', 'web' => "\x{fc}"}, # u umlaut.
1184: '(yacute|#253)' => {'tex' => '\375', 'web' => "\x{fd}"}, # y acute accent.
1185: '(thorn|#254)' => {'tex' => '\376', 'web' => "\x{fe}"}, # small thorn (icelandic).
1186: '(yuml|#255)' => {'tex' => '\377', 'web' => "\x{ff}"}, # y umlaut.
1187:
1188: # Latin extended A entities:
1189:
1190: '(OElig|#338)' => {'tex' => '{/Text \326}', 'web' => "\x{152}"}, # OE ligature.
1191: '(oelig|#339)' => {'tex' => '{/Text \366}', 'web' => "\x{153}"}, # oe ligature.
1192: '(Scaron|#352)' => {'tex' => 'S', 'web' => "\x{160}"}, # S caron no printable.
1193: '(scaron|#353)' => {'tex' => 's', 'web' => "\x{161}"}, # s caron no printable.
1194: '(Yuml|#376)' => {'tex' => 'Y', 'web' => "\x{178}"}, # Y umlaut - no printable.
1195:
1196: # Latin extended B.
1197:
1198: '(fnof|#402)' => {'tex' =>'{/Symbol \246}', 'web' => "\x{192}"}, # f with little hook.
1199:
1200: # Standalone accents:
1201:
1202: '(circ|#710)' => {'tex' => '^', 'web' => '^'}, # circumflex.
1203: '(tilde|#732)' => {'tex' => '~', 'web' => '~'}, # tilde.
1204:
1205: # General punctuation. We're not able to make a distinction between
1206: # the various length spacings in the print version. (e.g. en/em/thin).
1207: # the various joiners will be empty strings in the print version too.
1208:
1209:
1210: '(ensp|#8194)' => {'tex' => ' ', 'web' => "\x{2002}"}, # en space.
1211: '(emsp|#8195)' => {'tex' => ' ', 'web' => "\x{2003}"}, # em space.
1212: '(thinsp|#8201)' => {'tex' => ' ', 'web' => "\x{2009}"}, # thin space.
1213: '(zwnj|#8204)' => {'tex' => ' ', 'web' => "\x{200c}"}, # Zero width non joiner.
1214: '(zwj|#8205)' => {'tex' => ' ', 'web' => "\x{200d}"}, # Zero width joiner.
1215: '(lrm|#8206)' => {'tex' => ' ', 'web' => "\x{200e}"}, # Left to right mark
1216: '(rlm|#8207)' => {'tex' => ' ', 'web' => "\x{200f}"}, # right to left mark.
1217: '(ndash|#8211)' => {'tex' => '{/Text \55}', 'web' => "\x{2013}"}, # en dash.
1218: '(mdash|#8212)' => {'tex' => '{/Symbol \55}', 'web' => "\x{2014}"}, # em dash.
1219: '(lsquo|#8216)' => {'tex' => '{/Text \140}', 'web' => "\x{2018}"}, # Left single quote.
1220: '(rsquo|#8217)' => {'tex' => '\47', 'web' => "\x{2019}"}, # Right single quote.
1221: '(sbquo|#8218)' => {'tex' => '\54', 'web' => "\x{201a}"}, # Single low-9 quote.
1222: '(ldquo|#8220)' => {'tex' => '\42', 'web' => "\x{201c}"}, # Left double quote.
1223: '(rdquo|#8221)' => {'tex' => '\42', 'web' => "\x{201d}"}, # Right double quote.
1224: '(bdquo|#8222)' => {'tex' => ',', 'web' => "\x{201e}"}, # Double low-9 quote.
1225: '(dagger|#8224)' => {'tex' => '+', 'web' => "\x{2020}"}, # Is this a dagger I see before me now?
1226: '(Dagger|#8225)' => {'tex' => '\261', 'web' => "\x{2021}"}, # it's handle pointing towards my heart?
1227: '(bull|#8226)' => {'tex' => '\267', 'web' => "\x{2022}"}, # Bullet.
1228: '(hellep|#8230)' => {'tex' => '{/Symbol \274}', 'web' => "\x{2026}"}, # Ellipses.
1229: '(permil|#8240)' => {'tex' => '%_o', 'web' => "\x{2031}"}, # Per mille.
1230: '(prime|#8242)' => {'tex' => '\264', 'web' => "\x{2032}"}, # Prime.
1231: '(Prime|#8243)' => {'tex' => '{/Symbol \262}', 'web' => "\x{2033}"}, # double prime.
1232: '(lsaquo|#8249)' => {'tex' => '<', 'web' => "\x{2039}"}, # < quote.
1233: '(rsaquo|#8250)' => {'tex' => '\74', 'web' => "\x{203a}"}, # > quote.
1234: '(oline|#8254)' => {'tex' => '{/Symbol \140}', 'web' => "\x{203e}"}, # Overline.
1235: '(frasl|#8260)' => {'tex' => '/', 'web' => "\x{2044}"}, # Fraction slash.
1236: '(euro|#8364)' => {'tex' => '{/Symbol \240}', 'web' => "\x{20ac}"}, # Euro currency.
1237:
1238: # Letter like symbols.
1239:
1240: '(weierp|#8472)' => {'tex' => '{/Symbol \303}', 'web' => "\x{2118}"}, # Power set symbol
1241: '(image|#8465)' => {'tex' => '{/Symbol \301}', 'web' => "\x{2111}"}, # Imaginary part
1242: '(real|#8476)' => {'tex' => '{/Symbol \302}', 'web' => "\x{211c}"}, # Real part.
1243: '(trade|#8482)' => {'tex' => '{/Symbol \344}', 'web' => "\x{2122}"}, # trademark symbol.
1244: '(alefsym|#8501)' => {'tex' => '{/Symbol \300}', 'web' => "\x{2135}"}, # Hebrew alef.
1245:
1246: # Arrows of various types and directions.
1247: '(larr|#8592)' => {'tex' => '{/Symbol \254}', 'web' => "\x{2190}"}, # <--
1248: '(uarr|#8593)' => {'tex' => '{/Symbol \255}', 'web' => "\x{2191}"}, # up arrow.
1249: '(rarr|#8594)' => {'tex' => '{/Symbol \256}', 'web' => "\x{2192}"}, # -->
1250: '(darr|#8595)' => {'tex' => '{/Symbol \257}', 'web' => "\x{2193}"}, # down arrow.
1251: '(harr|#8596)' => {'tex' => '{/Symbol \253}', 'web' => "\x{2194}"}, # <-->
1252: '(crarr|#8629)' => {'tex' => '{/Symbol \277}', 'web' => "\x{21b5}"}, # corner arrow down and right.
1253: '(lArr|#8656)' => {'tex' => '{/Symbol \334}', 'web' => "\x{21d0}"}, # <==
1254: '(uArr|#8657)' => {'tex' => '{/Symbol \335}', 'web' => "\x{21d1}"}, # Up double arrow.
1255: '(rArr|#8658)' => {'tex' => '{/Symbol \336}', 'web' => "\x{21d2}"}, # ==>
1256: '(dArr|#8659)' => {'tex' => '{/Symbol \337}', 'web' => "\x{21d3}"}, # Down double arrow.
1257: '(hArr|#8660)' => {'tex' => '{/Symbol \333}', 'web' => "\x{21d4}"}, # <==>
1258:
1259: # Mathematical operators. For some of these we do the best we can in printing.
1260:
1261: '(forall|#8704)' => {'tex' => '{/Symbol \42}', 'web' => "\x{2200}"}, # For all.
1262: '(part|#8706)' => {'tex' => '{/Symbol d}', 'web' => "\x{2202}"}, # partial derivative
1263: '(exist|#8707)' => {'tex' => '{/Symbol \44}', 'web' => "\x{2203}"}, # There exists.
1264: '(empty|#8709)' => {'tex' => '{/Symbol \306}', 'web' => "\x{2205}"}, # Null set.
1265: '(nabla|#8711)' => {'tex' => '{/Symbol \321}', 'web' => "\x{2207}"}, # Gradient e.g.
1266: '(isin|#8712)' => {'tex' => '{/Symbol \316}', 'web' => "\x{2208}"}, # Element of the set.
1267: '(notin|#8713)' => {'tex' => '{/Symbol \317}', 'web' => "\x{2209}"}, # Not an element of
1268: '(ni|#8715)' => {'tex' => '{/Symbol \47}', 'web' => "\x{220b}"}, # Contains as a member
1269: '(prod|#8719)' => {'tex' => '{/Symbol \325}', 'web' => "\x{220f}"}, # Product
1270: '(sum|#8721)' => {'tex' => '{/Symbol \345}', 'web' => "\x{2211}"}, # Sum of.
1271: '(minus|#8722)' => {'tex' => '{/Symbol \55}', 'web' => "\x{2212}"}, # - sign.
1272: '(lowast|#8727)' => {'tex' => '*', 'web' => "\x{2217}"}, # *
1273: '(radic|#8730)' => {'tex' => '{/Symbol \326}', 'web' => "\x{221a}"}, # Square root.
1274: '(prop|#8733)' => {'tex' => '{/Symbol \265}', 'web' => "\x{221d}"}, # Proportional to.
1275: '(infin|#8734)' => {'tex' => '{/Symbol \245}', 'web' => "\x{221e}"}, # Infinity.
1276: '(ang|#8736)' => {'tex' => '{/Symbol \320}', 'web' => "\x{2220}"}, # Angle .
1277: '(and|#8743)' => {'tex' => '{/Symbol \331}', 'web' => "\x{2227}"}, # Logical and.
1278: '(or|#8744)' => {'tex' => '{/Symbol \332}', 'web' => "\x{2228}"}, # Logical or.
1279: '(cap|#8745)' => {'tex' => '{/Symbol \307}', 'web' => "\x{2229}"}, # Set intersection.
1280: '(cup|#8746)' => {'tex' => '{/Symbol \310}', 'web' => "\x{222a}"}, # Set union.
1281: '(int|8747)' => {'tex' => '{/Symbol \362}', 'web' => "\x{222b}"}, # Integral.
1282:
1283: # Some gnuplot guru will have to explain to me why the next three
1284: # require the extra slashes... else they print very funkily.
1285:
1286: '(there4|#8756)' => {'tex' => '{/Symbol \\\134}', 'web' => "\x{2234}"}, # Therefore triple dots.
1287: '(sim|#8764)' => {'tex' => '\\\176', 'web' => "\x{223c}"}, # Simlar to.
1288: '(cong|#8773)' => {'tex' => '{/Symbol \\\100}','web' => "\x{2245}"}, # Congruent to/with.
1289:
1290: '(asymp|#8776)' => {'tex' => '{/Symbol \273}', 'web' => "\x{2248}"}, # Asymptotic to.
1291: '(ne|#8800)' => {'tex' => '{/Symbol \271}', 'web' => "\x{2260}"}, # not equal to.
1292: '(equiv|#8801)' => {'tex' => '{/Symbol \272}', 'web' => "\x{2261}"}, # Equivalent to.
1293: '(le|8804)' => {'tex' => '{/Symbol \243}', 'web' => "\x{2264}"}, # Less than or equal to.
1294: '(ge|8805)' => {'tex' => '{/Symbol \263}', 'web' => "\x{2265}"}, # Greater than or equal to
1295: '(sub|8834)' => {'tex' => '{/Symbol \314}', 'web' => "\x{2282}"}, # Subset of.
1296: '(sup|8835)' => {'tex' => '{/Symbol \311}', 'web' => "\x{2283}"}, # Super set of.
1297: '(nsub|8836)' => {'tex' => '{/Symbol \313}', 'web' => "\x{2284}"}, # not subset of.
1298: '(sube|8838)' => {'tex' => '{/Symbol \315}', 'web' => "\x{2286}"}, # Subset or equal.
1299: '(supe|8839)' => {'tex' => '{/Symbol \312}', 'web' => "\x{2287}"}, # Superset or equal
1300: '(oplus|8853)' => {'tex' => '{/Symbol \305}', 'web' => "\x{2295}"}, # O with plus inside
1301: '(otimes|8855)' => {'tex' => '{/Symbol \304}', 'web' => "\x{2297}"}, # O with times.
1302: '(perp|8869)' => {'tex' => '{/Symbol \136}', 'web' => "\x{22a5}"}, # Perpendicular.
1303: '(sdot|8901)' => {'tex' => '{/Symbol \227}', 'web' => "\x{22c5}"}, # Dot operator.
1304:
1305: # Misc. technical symbols:
1306:
1307: '(lceil|8698)' => {'tex' => '{/Symbol \351}', 'web' => "\x{2308}"}, # Left ceiling.
1308: '(rceil|8969)' => {'tex' => '{/Symbol \371}', 'web' => "\x{2309}"}, # Right ceiling.
1309: '(lfloor|8970)' => {'tex' => '{/Symbol \353}', 'web' => "\x{230a}"}, # Left floor.
1310: '(rfloor|8971)' => {'tex' => '{/Symbol \373}', 'web' => "\x{230b}"}, # Right floor.
1311:
1312: # The gnuplot png font evidently does not have the big angle brackets at
1313: # positions 0x2329, 0x232a so use ordinary brackets.
1314:
1315: '(lang|9001)' => {'tex' => '{/Symbol \341}', 'web' => '<'}, # Left angle bracket.
1316: '(rang|9002)' => {'tex' => '{/Symbol \361}', 'web' => '>'}, # Right angle bracket.
1317:
1318: # Gemoetric shapes.
1319:
1320: '(loz|9674)' => {'tex' => '{/Symbol \340}', 'web' => "\x{25ca}"}, # Lozenge.
1321:
1322: # Misc. symbols
1323:
1324: '(spades|9824)' => {'tex' => '{/Symbol \252}', 'web' => "\x{2660}"},
1325: '(clubs|9827)' => {'tex' => '{/Symbol \247}', 'web' => "\x{2663}"},
1326: '(hearts|9829)' => {'tex' => '{/Symbol \251}', 'web' => "\x{2665}"},
1327: '(diams|9830)' => {'tex' => '{/Symbol \250}', 'web' => "\x{2666}"}
1328:
1329: );
1330:
1331:
1332: sub replace_entities {
1333: my ($target,$text) = @_;
1334: $text =~ s{([_^~\{\}]|\\\\)}{\\\\$1}g;
1335: while (my ($re, $replace) = each(%lookup)) {
1336: my $repl = $replace->{$target};
1337: $text =~ s/&$re;/$replace->{$target}/g;
1338: }
1339: $text =~ s{(&)}{\\\\$1}g;
1340: return $text;
1341: }
1342:
1343: ##------------------------------------------------------------------- title
1344: sub start_title {
1345: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1346: my $result='';
1347: if ($target eq 'web' || $target eq 'tex') {
1348: $title = &Apache::lonxml::get_all_text("/title",$parser,$style);
1349: $title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
1350: $title =~ s/\n/ /g;
1351: if (length($title) > $max_str_len) {
1352: $title = substr($title,0,$max_str_len);
1353: }
1354: $title = &parse_label($target,$title);
1355: } elsif ($target eq 'edit') {
1356: $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1357: my $text=&Apache::lonxml::get_all_text("/title",$parser,$style);
1358: $result.=&Apache::edit::editline('',$text,'',60);
1359: } elsif ($target eq 'modified') {
1360: $result.=&Apache::edit::rebuild_tag($token);
1361: $result.=&Apache::edit::modifiedfield("/title",$parser);
1362: }
1363: return $result;
1364: }
1365:
1366: sub end_title {
1367: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1368: my $result = '';
1369: if ($target eq 'web' || $target eq 'tex') {
1370: } elsif ($target eq 'edit') {
1371: $result.=&Apache::edit::tag_end($target,$token);
1372: }
1373: return $result;
1374: }
1375: ##------------------------------------------------------------------- xlabel
1376: sub start_xlabel {
1377: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1378: my $result='';
1379: if ($target eq 'web' || $target eq 'tex') {
1380: $xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1381: $xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
1382: $xlabel =~ s/\n/ /g;
1383: if (length($xlabel) > $max_str_len) {
1384: $xlabel = substr($xlabel,0,$max_str_len);
1385: }
1386: $xlabel = &parse_label($target,$xlabel);
1387: } elsif ($target eq 'edit') {
1388: $result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1389: my $text=&Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1390: $result.=&Apache::edit::editline('',$text,'',60);
1391: } elsif ($target eq 'modified') {
1392: $result.=&Apache::edit::rebuild_tag($token);
1393: $result.=&Apache::edit::modifiedfield("/xlabel",$parser);
1394: }
1395: return $result;
1396: }
1397:
1398: sub end_xlabel {
1399: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1400: my $result = '';
1401: if ($target eq 'web' || $target eq 'tex') {
1402: } elsif ($target eq 'edit') {
1403: $result.=&Apache::edit::tag_end($target,$token);
1404: }
1405: return $result;
1406: }
1407:
1408: ##------------------------------------------------------------------- ylabel
1409: sub start_ylabel {
1410: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1411: my $result='';
1412: if ($target eq 'web' || $target eq 'tex') {
1413: $ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1414: $ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
1415: $ylabel =~ s/\n/ /g;
1416: if (length($ylabel) > $max_str_len) {
1417: $ylabel = substr($ylabel,0,$max_str_len);
1418: }
1419: $ylabel = &parse_label($target,$ylabel);
1420: } elsif ($target eq 'edit') {
1421: $result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1422: my $text = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1423: $result .= &Apache::edit::editline('',$text,'',60);
1424: } elsif ($target eq 'modified') {
1425: $result.=&Apache::edit::rebuild_tag($token);
1426: $result.=&Apache::edit::modifiedfield("/ylabel",$parser);
1427: }
1428: return $result;
1429: }
1430:
1431: sub end_ylabel {
1432: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1433: my $result = '';
1434: if ($target eq 'web' || $target eq 'tex') {
1435: } elsif ($target eq 'edit') {
1436: $result.=&Apache::edit::tag_end($target,$token);
1437: }
1438: return $result;
1439: }
1440:
1441: ##------------------------------------------------------------------- label
1442: sub start_label {
1443: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1444: my $result='';
1445: if ($target eq 'web' || $target eq 'tex') {
1446: my %label;
1447: &get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1448: $tagstack->[-1]);
1449: my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1450: $text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
1451: $text =~ s/\n/ /g;
1452: $text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
1453: $label{'text'} = &parse_label($target,$text);
1454: push(@labels,\%label);
1455: } elsif ($target eq 'edit') {
1456: $result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1457: $result .= &edit_attributes($target,$token,\%label_defaults);
1458: my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1459: $result .= &Apache::edit::end_row().
1460: &Apache::edit::start_spanning_row().
1461: &Apache::edit::editline('',$text,'',60);
1462: } elsif ($target eq 'modified') {
1463: &Apache::edit::get_new_args
1464: ($token,$parstack,$safeeval,keys(%label_defaults));
1465: $result.=&Apache::edit::rebuild_tag($token);
1466: $result.=&Apache::edit::modifiedfield("/label",$parser);
1467: }
1468: return $result;
1469: }
1470:
1471: sub end_label {
1472: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1473: my $result = '';
1474: if ($target eq 'web' || $target eq 'tex') {
1475: } elsif ($target eq 'edit') {
1476: $result.=&Apache::edit::tag_end($target,$token);
1477: }
1478: return $result;
1479: }
1480:
1481: ##------------------------------------------------------------------- curve
1482: sub start_curve {
1483: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1484: my $result='';
1485: &Apache::lonxml::register('Apache::lonplot',('function','data'));
1486: push (@Apache::lonxml::namespace,'curve');
1487: if ($target eq 'web' || $target eq 'tex') {
1488: my %curve;
1489: &get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1490: $tagstack->[-1]);
1491: push (@curves,\%curve);
1492: } elsif ($target eq 'edit') {
1493: $result .= &Apache::edit::tag_start($target,$token,'Curve');
1494: $result .= &edit_attributes($target,$token,\%curve_defaults,
1495: \@curve_edit_order)
1496: .&Apache::edit::end_row()
1497: .&Apache::edit::start_spanning_row();
1498:
1499: } elsif ($target eq 'modified') {
1500: my $constructtag=&Apache::edit::get_new_args
1501: ($token,$parstack,$safeeval,keys(%curve_defaults));
1502: if ($constructtag) {
1503: #
1504: # Fix up the color attribute as jcolor does not prepend an x
1505: # (or #)
1506: #
1507: my $value = $token->[2]{'color'};
1508: if (defined $value && ($value !~ /^\Q$colorprefix\E/)) {
1509: $token->[2]{'color'} = $colorprefix . $value;
1510: }
1511: $result = &Apache::edit::rebuild_tag($token);
1512: }
1513: }
1514: return $result;
1515: }
1516:
1517: sub end_curve {
1518: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1519: my $result = '';
1520: pop @Apache::lonxml::namespace;
1521: &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1522: if ($target eq 'web' || $target eq 'tex') {
1523: } elsif ($target eq 'edit') {
1524: $result.=&Apache::edit::tag_end($target,$token);
1525: }
1526: return $result;
1527: }
1528:
1529: ##------------------------------------------------------------ curve function
1530: sub start_function {
1531: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1532: my $result='';
1533: if ($target eq 'web' || $target eq 'tex') {
1534: if (exists($curves[-1]->{'data'})) {
1535: &Apache::lonxml::warning
1536: ('Use of the <b>curve function</b> tag precludes use of '.
1537: ' the <b>curve data</b> tag. '.
1538: 'The curve data tag will be omitted in favor of the '.
1539: 'curve function declaration.');
1540: delete $curves[-1]->{'data'} ;
1541: }
1542: my $function = &Apache::lonxml::get_all_text("/function",$parser,
1543: $style);
1544: $function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
1545: $function=~s/\^/\*\*/gs;
1546: $function=~ s/^\s+//; # Trim leading
1547: $function=~ s/\s+$//; # And trailing whitespace.
1548: $curves[-1]->{'function'} = $function;
1549: } elsif ($target eq 'edit') {
1550: $result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1551: my $text = &Apache::lonxml::get_all_text("/function",$parser,$style);
1552: $result .= &Apache::edit::editline('',$text,'',60);
1553: } elsif ($target eq 'modified') {
1554: $result.=&Apache::edit::rebuild_tag($token);
1555: $result.=&Apache::edit::modifiedfield("/function",$parser);
1556: }
1557: return $result;
1558: }
1559:
1560: sub end_function {
1561: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1562: my $result = '';
1563: if ($target eq 'web' || $target eq 'tex') {
1564: } elsif ($target eq 'edit') {
1565: $result .= &Apache::edit::end_table();
1566: }
1567: return $result;
1568: }
1569:
1570: ##------------------------------------------------------------ curve data
1571: sub start_data {
1572: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1573: my $result='';
1574: if ($target eq 'web' || $target eq 'tex') {
1575: if (exists($curves[-1]->{'function'})) {
1576: &Apache::lonxml::warning
1577: ('Use of the <b>curve function</b> tag precludes use of '.
1578: ' the <b>curve data</b> tag. '.
1579: 'The curve function tag will be omitted in favor of the '.
1580: 'curve data declaration.');
1581: delete($curves[-1]->{'function'});
1582: }
1583: my $datatext = &Apache::lonxml::get_all_text("/data",$parser,$style);
1584: $datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
1585: # Deal with cases where we're given an array...
1586: if ($datatext =~ /^\@/) {
1587: $datatext = &Apache::run::run('return "'.$datatext.'"',
1588: $safeeval,1);
1589: }
1590: $datatext =~ s/\s+/ /g;
1591: # Need to do some error checking on the @data array -
1592: # make sure it's all numbers and make sure each array
1593: # is of the same length.
1594: my @data;
1595: if ($datatext =~ /,/) { # comma deliminated
1596: @data = split /,/,$datatext;
1597: } else { # Assume it's space separated.
1598: @data = split / /,$datatext;
1599: }
1600: for (my $i=0;$i<=$#data;$i++) {
1601: # Check that it's non-empty
1602: if (! defined($data[$i])) {
1603: &Apache::lonxml::warning(
1604: 'undefined curve data value. Replacing with '.
1605: ' pi/e = 1.15572734979092');
1606: $data[$i] = 1.15572734979092;
1607: }
1608: # Check that it's a number
1609: if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
1610: &Apache::lonxml::warning(
1611: 'Bad curve data value of '.$data[$i].' Replacing with '.
1612: ' pi/e = 1.15572734979092');
1613: $data[$i] = 1.15572734979092;
1614: }
1615: }
1616: # complain if the number of data points is not the same as
1617: # in previous sets of data.
1618: if (($curves[-1]->{'data'}) && ($#data != $#{$curves[-1]->{'data'}->[0]})){
1619: &Apache::lonxml::warning
1620: ('Number of data points is not consistent with previous '.
1621: 'number of data points');
1622: }
1623: push @{$curves[-1]->{'data'}},\@data;
1624: } elsif ($target eq 'edit') {
1625: $result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1626: my $text = &Apache::lonxml::get_all_text("/data",$parser,$style);
1627: $result .= &Apache::edit::editline('',$text,'',60);
1628: } elsif ($target eq 'modified') {
1629: $result.=&Apache::edit::rebuild_tag($token);
1630: $result.=&Apache::edit::modifiedfield("/data",$parser);
1631: }
1632: return $result;
1633: }
1634:
1635: sub end_data {
1636: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1637: my $result = '';
1638: if ($target eq 'web' || $target eq 'tex') {
1639: } elsif ($target eq 'edit') {
1640: $result .= &Apache::edit::end_table();
1641: }
1642: return $result;
1643: }
1644:
1645: ##------------------------------------------------------------------- axis
1646: sub start_axis {
1647: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1648: my $result='';
1649: if ($target eq 'web' || $target eq 'tex') {
1650: &get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
1651: $tagstack->[-1]);
1652: } elsif ($target eq 'edit') {
1653: $result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1654: $result .= &edit_attributes($target,$token,\%axis_defaults,
1655: \@axis_edit_order);
1656: } elsif ($target eq 'modified') {
1657: my $constructtag=&Apache::edit::get_new_args
1658: ($token,$parstack,$safeeval,keys(%axis_defaults));
1659:
1660: if ($constructtag) {
1661: #
1662: # Fix up the color attribute since jchooser does not
1663: # prepend an x (or #) to the color:
1664: #
1665: my $value = $token->[2]{'color'};
1666: if (defined $value && ($value !~ /^\Q$colorprefix\E/)) {
1667: $token->[2]{'color'} = $colorprefix . $value;
1668: }
1669:
1670: $result = &Apache::edit::rebuild_tag($token);
1671: }
1672: }
1673: return $result;
1674: }
1675:
1676: sub end_axis {
1677: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1678: my $result = '';
1679: if ($target eq 'web' || $target eq 'tex') {
1680: } elsif ($target eq 'edit') {
1681: $result.=&Apache::edit::tag_end($target,$token);
1682: } elsif ($target eq 'modified') {
1683: }
1684: return $result;
1685: }
1686:
1687: ###################################################################
1688: ## ##
1689: ## Utility Functions ##
1690: ## ##
1691: ###################################################################
1692:
1693: ##----------------------------------------------------------- set_defaults
1694: sub set_defaults {
1695: my ($var,$defaults) = @_;
1696: my $key;
1697: foreach $key (keys(%$defaults)) {
1698: $var->{$key} = $defaults->{$key}->{'default'};
1699: }
1700: }
1701:
1702: ##------------------------------------------------------------------- misc
1703: sub get_attributes{
1704: my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1705: foreach my $attr (keys(%{$defaults})) {
1706: if ($attr eq 'texwidth' || $attr eq 'texfont') {
1707: $values->{$attr} =
1708: &Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
1709: } else {
1710: $values->{$attr} =
1711: &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1712: }
1713: if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1714: $values->{$attr} = $defaults->{$attr}->{'default'};
1715: next;
1716: }
1717: my $test = $defaults->{$attr}->{'test'};
1718: if (! &$test($values->{$attr})) {
1719: &Apache::lonxml::warning
1720: ($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1721: .$defaults->{$attr}->{'default'} );
1722: $values->{$attr} = $defaults->{$attr}->{'default'};
1723: }
1724: }
1725: return ;
1726: }
1727: ##
1728: # Generate tic mark specifications.
1729: #
1730: # @param type - type of tics (xtics or ytics).
1731: # @param spec - Reference to a hash that contains the tic specification.
1732: # @param target - 'tex' if hard copy target.
1733: #
1734: # @return string - the tic specification command.
1735: #
1736: sub generate_tics {
1737: my ($type, $spec, $target) = @_;
1738: my $result = '';
1739:
1740:
1741: if ((ref($spec) eq 'HASH') && (keys(%{$spec}) > 0)) {
1742:
1743:
1744:
1745: # Major tics: - If there are 'ticspecs' these override any other
1746: # specifications:
1747:
1748:
1749:
1750: $result .= "set $type $spec->{'location'} ";
1751: $result .= ($spec->{'mirror'} eq 'on') ? 'mirror ' : 'nomirror ';
1752: if ($spec->{'rotate'} eq 'on') {
1753: $result .= ' rotate ';
1754: }
1755: if (defined $spec->{'ticspecs'}) {
1756: $result .= '( ';
1757: my @ticspecs;
1758: my $ticinfo = $spec->{'ticspecs'};
1759: foreach my $tic (@$ticinfo) {
1760: push(@ticspecs, '"' . $tic->{'label'} . '" ' . $tic->{'pos'} );
1761: }
1762: $result .= join(', ', (@ticspecs));
1763: $result .= ' )';
1764: } else {
1765: $result .= "$spec->{'start'}, ";
1766: $result .= "$spec->{'increment'}, ";
1767: $result .= "$spec->{'end'} ";
1768: }
1769: if ($target eq 'tex' ) {
1770: $result .= 'font "Helvetica,22"';
1771: }
1772: $result .= "\n";
1773:
1774: # minor frequency:
1775:
1776: if ($spec->{'minorfreq'} != 0) {
1777: $result .= "set m$type $spec->{'minorfreq'}\n";
1778: }
1779: } elsif ($target eq 'tex' ) {
1780: $result .= "set $type font " . '"Helvetica,22"' ."\n";
1781: }
1782:
1783:
1784: return $result;
1785: }
1786:
1787: ##------------------------------------------------------- write_gnuplot_file
1788: sub write_gnuplot_file {
1789: my ($tmpdir,$filename,$target)= @_;
1790: my ($fontsize, $font_properties) = &get_font($target);
1791: my $gnuplot_input = '';
1792: my $curve;
1793: #
1794: # Check to be sure we do not have any empty curves
1795: my @curvescopy;
1796: foreach my $curve (@curves) {
1797: if (exists($curve->{'function'})) {
1798: if ($curve->{'function'} !~ /^\s*$/) {
1799: push(@curvescopy,$curve);
1800: }
1801: } elsif (exists($curve->{'data'})) {
1802: foreach my $data (@{$curve->{'data'}}) {
1803: if (scalar(@$data) > 0) {
1804: push(@curvescopy,$curve);
1805: last;
1806: }
1807: }
1808: }
1809: }
1810: @curves = @curvescopy;
1811: # Collect all the colors
1812: my @Colors;
1813: push @Colors, $Apache::lonplot::plot{'bgcolor'};
1814: push @Colors, $Apache::lonplot::plot{'fgcolor'};
1815: push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
1816: foreach $curve (@curves) {
1817: push @Colors, ($curve->{'color'} ne '' ?
1818: $curve->{'color'} :
1819: $Apache::lonplot::plot{'fgcolor'} );
1820: }
1821:
1822: # set term
1823: if ($target eq 'web') {
1824: $gnuplot_input .= 'set terminal png enhanced nocrop ';
1825: $gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
1826: $gnuplot_input .= 'font "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
1827: '/'.$font_properties->{'file'}.'.ttf" ';
1828: $gnuplot_input .= $fontsize;
1829: $gnuplot_input .= ' size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
1830: if ($version > 4.6) {
1831: if ($Apache::lonplot::plot{'bgcolor'}) {
1832: $gnuplot_input .= "background '$Apache::lonplot::plot{'bgcolor'}'\n";
1833: }
1834: } else {
1835: $gnuplot_input .= "@Colors\n";
1836: }
1837: # set output
1838: $gnuplot_input .= "set output\n";
1839: } elsif ($target eq 'tex') {
1840: $gnuplot_input .= "set term postscript eps enhanced $Apache::lonplot::plot{'plotcolor'} dash ";
1841: if (!$font_properties->{'tex_no_file'}) {
1842: $gnuplot_input .=
1843: 'fontfile "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
1844: '/'.$font_properties->{'file'}.'.pfb" ';
1845: }
1846: $gnuplot_input .= ' "'.$font_properties->{'printname'}.'" ';
1847: $gnuplot_input .= $fontsize;
1848: $gnuplot_input .= "\nset output \"".$tmpdir.
1849: &unescape($filename).".eps\"\n";
1850: $gnuplot_input .= "set encoding iso_8859_1\n"; # Get access to extended font.
1851:
1852: }
1853: $gnuplot_input .= "set encoding utf8\n";
1854: # cartesian or polar plot?
1855: if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
1856: $gnuplot_input .= 'set polar'.$/;
1857: } else {
1858: # Assume Cartesian
1859: }
1860: # cartesian or polar grid?
1861: if (lc($Apache::lonplot::plot{'gridtype'}) eq 'polar') {
1862: $gnuplot_input .= 'set grid polar'.$/;
1863: } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'linear-log') {
1864: $gnuplot_input .= 'set logscale x'.$/;
1865: } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-linear') {
1866: $gnuplot_input .= 'set logscale y'.$/;
1867: } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-log') {
1868: $gnuplot_input .= 'set logscale x'.$/;
1869: $gnuplot_input .= 'set logscale y'.$/;
1870: } else {
1871: # Assume Cartesian
1872: }
1873: # solid or pattern for boxes?
1874: if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
1875: $gnuplot_input .= 'set style fill solid '.
1876: $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
1877: } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
1878: $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
1879: } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
1880: }
1881: # margin
1882: if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
1883: $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
1884: }
1885: if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
1886: $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
1887: }
1888: if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
1889: $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
1890: }
1891: if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
1892: $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
1893: }
1894:
1895: # tic scales
1896: if ($version > 4) {
1897: $gnuplot_input .= 'set tics scale '.
1898: $Apache::lonplot::plot{'major_ticscale'}.', '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
1899: } else {
1900: $gnuplot_input .= 'set ticscale '.
1901: $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
1902: }
1903: #boxwidth
1904: if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
1905: $gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
1906: }
1907: # gridlayer
1908: $gnuplot_input .= 'set grid noxtics noytics front '.$/
1909: if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
1910:
1911: # grid
1912: if (($version > 4.6) && ($Apache::lonplot::plot{'fgcolor'} ne '')) {
1913: $gnuplot_input .= 'set grid linecolor "'.$Apache::lonplot::plot{'fgcolor'}.'"'.$/
1914: if ($Apache::lonplot::plot{'grid'} eq 'on');
1915: } else {
1916: $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
1917: }
1918: # border
1919: if ($Apache::lonplot::plot{'border'} eq 'on') {
1920: if (($version > 4.6) && (($axis{'color'} ne '') || ($Apache::lonplot::plot{'fgcolor'} ne ''))) {
1921: $gnuplot_input .= 'set border linecolor "'.
1922: (($axis{'color'} ne '')?$axis{'color'}:
1923: $Apache::lonplot::plot{'fgcolor'}).
1924: '" '.$/;
1925: } else {
1926: $gnuplot_input .= 'set border '.$/;
1927: }
1928: } else {
1929: $gnuplot_input .= 'set noborder '.$/;
1930: }
1931: # sampling rate for non-data curves
1932: $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
1933: # title, xlabel, ylabel
1934: # titles
1935: my $extra_space_x = ($xtics{'location'} eq 'axis') ? ' offset 0, -0.5 ' : '';
1936: my $extra_space_y = ($ytics{'location'} eq 'axis') ? ' offset -0.5, 0 ' : '';
1937:
1938: if ($target eq 'tex') {
1939: $gnuplot_input .= "set title \"$title\" font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($title)) ;
1940: $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($xlabel));
1941: $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($ylabel));
1942: } else {
1943: $gnuplot_input .= "set title \"$title\" \n" if (defined($title)) ;
1944: $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x \n" if (defined($xlabel));
1945: $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y \n" if (defined($ylabel));
1946: }
1947: # tics
1948: $gnuplot_input .= &generate_tics('xtics', \%xtics, $target);
1949:
1950: $gnuplot_input .= &generate_tics('ytics', \%ytics, $target);
1951:
1952: # axis
1953: if (%axis) {
1954: if ($axis{'xformat'} ne 'on') {
1955: $gnuplot_input .= "set format x ";
1956: if ($axis{'xformat'} eq 'off') {
1957: $gnuplot_input .= "\"\"\n";
1958: } else {
1959: if ($axis{'xformat'} =~ /^(\d+P\s*)[Pp][Ii]/) {
1960: my $xprefix = $1;
1961: if (($target eq 'web') || ($target eq 'tex')) {
1962: $axis{'xformat'} = $xprefix.$lookup{'(pi|#960)'}{$target};
1963: } else {
1964: $axis{'xformat'} = $xprefix;
1965: }
1966: }
1967: $gnuplot_input .= "\"\%.".$axis{'xformat'}."\"\n";
1968: }
1969: }
1970: if ($axis{'yformat'} ne 'on') {
1971: $gnuplot_input .= "set format y ";
1972: if ($axis{'yformat'} eq 'off') {
1973: $gnuplot_input .= "\"\"\n";
1974: } else {
1975: if ($axis{'yformat'} =~ /^(\d+P\s*)[Pp][Ii]/) {
1976: my $yprefix = $1;
1977: if (($target eq 'web') || ($target eq 'tex')) {
1978: $axis{'yformat'} = $yprefix.$lookup{'(pi|#960)'}{$target};
1979: } else {
1980: $axis{'yformat'} = $yprefix;
1981: }
1982: }
1983: $gnuplot_input .= "\"\%.".$axis{'yformat'}."\"\n";
1984: }
1985: }
1986: $gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
1987: $gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1988: if ($axis{'xzero'} ne 'off') {
1989: $gnuplot_input .= "set xzeroaxis ";
1990: if ($axis{'xzero'} eq 'line' || $axis{'xzero'} eq 'thick-line') {
1991: $gnuplot_input .= "lt -1 ";
1992: if ($axis{'xzero'} eq 'thick-line') {
1993: $gnuplot_input .= "lw 3 ";
1994: }
1995: }
1996: $gnuplot_input .= "\n";
1997: }
1998: if ($axis{'yzero'} ne 'off') {
1999: $gnuplot_input .= "set yzeroaxis ";
2000: if ($axis{'yzero'} eq 'line' || $axis{'yzero'} eq 'thick-line') {
2001: $gnuplot_input .= "lt -1 ";
2002: if ($axis{'yzero'} eq 'thick-line') {
2003: $gnuplot_input .= "lw 3 ";
2004: }
2005: }
2006: $gnuplot_input .= "\n";
2007: }
2008: }
2009: # Key
2010: if (%key) {
2011: $gnuplot_input .= 'set key '.$key{'pos'}.' ';
2012: if ($key{'title'} ne '') {
2013: $gnuplot_input .= 'title "'.$key{'title'}.'" ';
2014: }
2015: $gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
2016: } else {
2017: $gnuplot_input .= 'set nokey'.$/;
2018: }
2019: # labels
2020: my $label;
2021: foreach $label (@labels) {
2022: $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
2023: $label->{'xpos'}.','.$label->{'ypos'};
2024: if ($label->{'rotate'} ne '') {
2025: $gnuplot_input .= ' rotate by '.$label->{'rotate'};
2026: }
2027: $gnuplot_input .= ' '.$label->{'justify'};
2028:
2029: if ($target eq 'tex') {
2030: $gnuplot_input .=' font "'.$font_properties->{'printname'}.','.$fontsize.'pt"';
2031: }
2032: if (($label->{'zlayer'} eq 'front') || ($label->{'zlayer'} eq 'back')) {
2033: $gnuplot_input .= ' '.$label->{'zlayer'};
2034: }
2035: $gnuplot_input .= $/;
2036: }
2037: if ($target eq 'tex') {
2038: $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
2039: $gnuplot_input .="\n";
2040: }
2041: # curves
2042: #
2043: # Each curve will have its very own linestyle.
2044: # (This should work just fine in web rendition I think).
2045: # The line_xxx variables will hold the elements of the line style.
2046: # type (solid/dashed), color, width
2047: #
2048: my $linestyle_index = 50;
2049: my $line_width = '';
2050: my $plots = '';
2051:
2052: # If arrows are needed there will be an arrow style for each as well:
2053: #
2054:
2055: my $arrow_style_index = 50;
2056:
2057: for (my $i = 0;$i<=$#curves;$i++) {
2058: $curve = $curves[$i];
2059: my $plot_command = '';
2060: my $plot_type = '';
2061: if ($i > 0) {
2062: $plot_type = ', ';
2063: }
2064: if ($target eq 'tex') {
2065: $curve->{'linewidth'} *= 2;
2066: }
2067: $line_width = $curve->{'linewidth'};
2068: if (exists($curve->{'function'})) {
2069: $plot_type .=
2070: $curve->{'function'}.' title "'.
2071: $curve->{'name'}.'" with '.
2072: $curve->{'linestyle'};
2073: } elsif (exists($curve->{'data'})) {
2074: # Store data values in $datatext
2075: my $datatext = '';
2076: # get new filename
2077: my $datafilename = "$tmpdir/$filename.data.$i";
2078: my $fh=Apache::File->new(">$datafilename");
2079: # Compile data
2080: my @Data = @{$curve->{'data'}};
2081: my @Data0 = @{$Data[0]};
2082: for (my $i =0; $i<=$#Data0; $i++) {
2083: my $dataset;
2084: foreach $dataset (@Data) {
2085: $datatext .= $dataset->[$i] . ' ';
2086: }
2087: $datatext .= $/;
2088: }
2089: # write file
2090: print $fh $datatext;
2091: close($fh);
2092: # generate gnuplot text
2093: $plot_type .= '"'.$datafilename.'" title "'.
2094: $curve->{'name'}.'" with '.
2095: $curve->{'linestyle'};
2096: }
2097: my $pointtype = '';
2098: my $pointsize = '';
2099:
2100: # Figure out the linestyle:
2101:
2102: my $lt = $curve->{'linetype'} ne '' ? $curve->{'linetype'}
2103: : 'solid'; # Line type defaults to solid.
2104: # The mapping of lt -> the actual gnuplot line type depends on the target:
2105:
2106: if ($target eq 'tex') {
2107: $lt = $ps_linetypes{$lt};
2108: } else {
2109: $lt = $linetypes{$lt}
2110: }
2111:
2112: my $color = $curve->{'color'};
2113: $color =~ s/^x/#/; # Convert xhex color -> #hex color.
2114:
2115:
2116: if (($curve->{'linestyle'} eq 'points') ||
2117: ($curve->{'linestyle'} eq 'linespoints') ||
2118: ($curve->{'linestyle'} eq 'errorbars') ||
2119: ($curve->{'linestyle'} eq 'xerrorbars') ||
2120: ($curve->{'linestyle'} eq 'yerrorbars') ||
2121: ($curve->{'linestyle'} eq 'xyerrorbars')) {
2122:
2123: $pointtype =' pointtype '.$curve->{'pointtype'};
2124: $pointsize =' pointsize '.$curve->{'pointsize'};
2125: } elsif ($curve->{'linestyle'} eq 'filledcurves') {
2126: $plot_command.= ' '.$curve->{'limit'};
2127: } elsif ($curve->{'linestyle'} eq 'vector') {
2128:
2129: # Create the arrow head style add it to
2130: # $gnuplot_input..and ensure it gets
2131: # Selected in the plot command.
2132:
2133: $gnuplot_input .= "set style arrow $arrow_style_index ";
2134: $gnuplot_input .= ' ' . $curve->{'arrowhead'};
2135: $gnuplot_input .= ' size ' . $curve->{'arrowlength'};
2136: $gnuplot_input .= ','.$curve->{'arrowangle'};
2137: $gnuplot_input .= ',' . $curve->{'arrowbackangle'};
2138: $gnuplot_input .= ' ' . $curve->{'arrowstyle'} . " ls $linestyle_index\n";
2139:
2140:
2141: $plot_command .= " arrowstyle $arrow_style_index ";
2142: $arrow_style_index++;
2143: }
2144:
2145: my $style_command = "set style line $linestyle_index $pointtype $pointsize linetype $lt linewidth $line_width lc rgb '$color'\n";
2146: $gnuplot_input .= $style_command;
2147:
2148: # The condition below is because gnuplot lumps the linestyle in with the
2149: # arrowstyle _sigh_.
2150:
2151: if ($curve->{'linestyle'} ne 'vector') {
2152: $plot_command.= " ls $linestyle_index";
2153: }
2154:
2155: $plots .= $plot_type . ' ' . $plot_command;
2156: $linestyle_index++; # Each curve get a unique linestyle.
2157: }
2158: $gnuplot_input .= 'plot '.$plots;
2159: # Write the output to a file.
2160:
2161: # &Apache::lonnet::logthis($gnuplot_input); # uncomment to log the gnuplot input.
2162: open (my $fh, "> $tmpdir$filename.data");
2163: binmode($fh, ':utf8');
2164: print $fh $gnuplot_input;
2165: close($fh);
2166: # That's all folks.
2167: return ;
2168: }
2169:
2170: #---------------------------------------------- check_inputs
2171: sub check_inputs {
2172: ## Note: no inputs, no outputs - this acts only on global variables.
2173: ## Make sure we have all the input we need:
2174: if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
2175: if (! %key ) {} # No key for this plot, thats okay
2176: # if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
2177: if (! defined($title )) {} # No title for this plot, thats okay
2178: if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
2179: if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
2180: if ($#labels < 0) { } # No labels for this plot, thats okay
2181: if ($#curves < 0) {
2182: &Apache::lonxml::warning("No curves specified for plot!!!!");
2183: return '';
2184: }
2185: my $curve;
2186: foreach $curve (@curves) {
2187: if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
2188: &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
2189: return '';
2190: }
2191: }
2192: }
2193:
2194: #------------------------------------------------ make_edit
2195: sub edit_attributes {
2196: my ($target,$token,$defaults,$keys) = @_;
2197: my ($result,@keys);
2198: if ($keys && ref($keys) eq 'ARRAY') {
2199: @keys = @$keys;
2200: } else {
2201: @keys = sort(keys(%$defaults));
2202: }
2203: foreach my $attr (@keys) {
2204: # append a ' ' to the description if it doesn't have one already.
2205: my $description = $defaults->{$attr}->{'description'};
2206: $description .= ' ' if ($description !~ / $/);
2207: if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
2208: $result .= &Apache::edit::text_arg
2209: ($description,$attr,$token,
2210: $defaults->{$attr}->{'size'},
2211: $defaults->{$attr}->{'class'});
2212: } elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
2213: $result .= &Apache::edit::select_or_text_arg
2214: ($description,$attr,$defaults->{$attr}->{'choices'},$token);
2215: } elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
2216: $result .= &Apache::edit::select_or_text_arg
2217: ($description,$attr,['on','off'],$token);
2218: }
2219: $result .= '<br />';
2220: }
2221: return $result;
2222: }
2223:
2224:
2225: ###################################################################
2226: ## ##
2227: ## Insertion functions for editing plots ##
2228: ## ##
2229: ###################################################################
2230:
2231: sub insert_gnuplot {
2232: my $result = '';
2233: # plot attributes
2234: $result .= "\n<gnuplot ";
2235: foreach my $attr (keys(%gnuplot_defaults)) {
2236: $result .= "\n $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
2237: }
2238: $result .= ">";
2239: # Add the components (most are commented out for simplicity)
2240: # $result .= &insert_key();
2241: # $result .= &insert_axis();
2242: # $result .= &insert_title();
2243: # $result .= &insert_xlabel();
2244: # $result .= &insert_ylabel();
2245: $result .= &insert_curve();
2246: # close up the <gnuplot>
2247: $result .= "\n</gnuplot>";
2248: return $result;
2249: }
2250:
2251: sub insert_tics {
2252: my $result;
2253: $result .= &insert_xtics() . &insert_ytics;
2254: return $result;
2255: }
2256:
2257: sub insert_xtics {
2258: my $result;
2259: $result .= "\n <xtics ";
2260: foreach my $attr (keys(%tic_defaults)) {
2261: $result .= "\n $attr=\"$tic_defaults{$attr}->{'default'}\" ";
2262: }
2263: $result .= "/>";
2264: return $result;
2265: }
2266:
2267: sub insert_ytics {
2268: my $result;
2269: $result .= "\n <ytics ";
2270: foreach my $attr (keys(%tic_defaults)) {
2271: $result .= "\n $attr=\"$tic_defaults{$attr}->{'default'}\" ";
2272: }
2273: $result .= "/>";
2274: return $result;
2275: }
2276:
2277: sub insert_key {
2278: my $result;
2279: $result .= "\n <key ";
2280: foreach my $attr (keys(%key_defaults)) {
2281: $result .= "\n $attr=\"$key_defaults{$attr}->{'default'}\"";
2282: }
2283: $result .= " />";
2284: return $result;
2285: }
2286:
2287: sub insert_axis{
2288: my $result;
2289: $result .= "\n <axis ";
2290: foreach my $attr (keys(%axis_defaults)) {
2291: $result .= "\n $attr=\"$axis_defaults{$attr}->{'default'}\"";
2292: }
2293: $result .= " />";
2294: return $result;
2295: }
2296:
2297: sub insert_title { return "\n <title></title>"; }
2298: sub insert_xlabel { return "\n <xlabel></xlabel>"; }
2299: sub insert_ylabel { return "\n <ylabel></ylabel>"; }
2300:
2301: sub insert_label {
2302: my $result;
2303: $result .= "\n <label ";
2304: foreach my $attr (keys(%label_defaults)) {
2305: $result .= "\n $attr=\"".
2306: $label_defaults{$attr}->{'default'}."\"";
2307: }
2308: $result .= "></label>";
2309: return $result;
2310: }
2311:
2312: sub insert_curve {
2313: my $result;
2314: $result .= "\n <curve ";
2315: foreach my $attr (keys(%curve_defaults)) {
2316: $result .= "\n $attr=\"".
2317: $curve_defaults{$attr}->{'default'}."\"";
2318: }
2319: $result .= " >";
2320: $result .= &insert_data().&insert_data()."\n </curve>";
2321: }
2322:
2323: sub insert_function {
2324: my $result;
2325: $result .= "\n <function></function>";
2326: return $result;
2327: }
2328:
2329: sub insert_data {
2330: my $result;
2331: $result .= "\n <data></data>";
2332: return $result;
2333: }
2334:
2335: ##----------------------------------------------------------------------
2336: 1;
2337: __END__
2338:
2339:
2340: =head1 NAME
2341:
2342: Apache::lonplot.pm
2343:
2344: =head1 SYNOPSIS
2345:
2346: XML-based plotter of graphs
2347:
2348: This is part of the LearningOnline Network with CAPA project
2349: described at http://www.lon-capa.org.
2350:
2351:
2352: =head1 SUBROUTINES (parsing and edit rendering)
2353:
2354: =over
2355:
2356: =item start_gnuplot()
2357:
2358: =item end_gnuplot()
2359:
2360: =item start_xtics()
2361:
2362: =item end_xtics()
2363:
2364: =item start_ytics()
2365:
2366: =item end_ytics()
2367:
2368: =item get_font()
2369:
2370: =item start_key()
2371:
2372: =item end_key()
2373:
2374: =item parse_label()
2375:
2376: =item replace_entities()
2377:
2378: =item start_title()
2379:
2380: =item end_title()
2381:
2382: =item start_xlabel()
2383:
2384: =item end_xlabel()
2385:
2386: =item start_ylabel()
2387:
2388: =item end_label()
2389:
2390: =item start_curve()
2391:
2392: =item end_curve()
2393:
2394: =item start_function()
2395:
2396: =item end_function()
2397:
2398: =item start_data()
2399:
2400: =item end_data()
2401:
2402: =item start_axis()
2403:
2404: =item end_axis
2405:
2406: =back
2407:
2408: =head1 SUBROUTINES (Utility)
2409:
2410: =over
2411:
2412: =item set_defaults()
2413:
2414: =item get_attributes()
2415:
2416: =item write_gnuplot_file()
2417:
2418: =item check_inputs()
2419:
2420: =item edit_attributes()
2421:
2422: =back
2423:
2424: =head1 SUBROUTINES (Insertion functions for editing plots)
2425:
2426: =over
2427:
2428: =item insert_gnuplot()
2429:
2430: =item insert_tics()
2431:
2432: =item insert_xtics()
2433:
2434: =item insert_key()
2435:
2436: =item insert_axis()
2437:
2438: =item insert_title()
2439:
2440: =item insert_xlabel()
2441:
2442: =item insert_ylabel()
2443:
2444: =item insert_label()
2445:
2446: =item insert_curve()
2447:
2448: =item insert_function()
2449:
2450: =item insert_data()
2451:
2452: =back
2453:
2454: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>