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