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