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