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