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