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