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