Annotation of loncom/xml/lonplot.pm, revision 1.30
1.1 matthew 1: # The LearningOnline Network with CAPA
2: # Dynamic plot
3: #
1.30 ! matthew 4: # $Id: lonplot.pm,v 1.29 2002/01/01 19:34:25 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.27 matthew 29: # 12/17 12/18 12/19 12/20 12/21 12/27 12/28 12/30 Matthew
1.1 matthew 30: package Apache::lonplot;
1.10 matthew 31:
1.1 matthew 32: use strict;
1.10 matthew 33: use Apache::File;
1.1 matthew 34: use Apache::response;
1.2 matthew 35: use Apache::lonxml;
1.20 matthew 36: use Apache::edit;
1.10 matthew 37:
1.1 matthew 38: sub BEGIN {
39: &Apache::lonxml::register('Apache::lonplot',('plot'));
40: }
41:
1.10 matthew 42: ##
43: ## Description of data structures:
44: ##
45: ## %plot %key %axis
46: ## --------------------------
47: ## height title color
48: ## width box xmin
49: ## bgcolor pos xmax
50: ## fgcolor ymin
51: ## transparent ymax
52: ## grid
53: ## border
54: ## font
1.19 matthew 55: ## align
1.10 matthew 56: ##
57: ## @labels: $labels[$i] = \%label
58: ## %label: text, xpos, ypos, justify
1.14 matthew 59: ##
1.10 matthew 60: ## @curves: $curves[$i] = \%curve
1.14 matthew 61: ## %curve: name, linestyle, ( function | data )
1.10 matthew 62: ##
63: ## $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
64: ## [y1,y2,y3,y4] ]
65: ##
1.21 matthew 66:
67: ###################################################################
68: ## ##
69: ## Tests used in checking the validitity of input ##
70: ## ##
71: ###################################################################
1.29 matthew 72:
73: my %linestyles =
74: (
75: lines => 2, # Maybe this will be used in the future
76: linespoints => 2, # to check on whether or not they have
77: dots => 2, # supplied enough <data></data> fields
78: points => 2, # to use the given line style. But for
79: steps => 2, # now there are more important things
80: fsteps => 2, # for me to deal with.
81: histeps => 2,
82: errorbars => 2,
83: xerrorbars => 2,
84: yerrorbars => 2,
85: xyerrorbars => 2,
86: boxes => 2,
87: boxerrorbars => 2,
88: boxxyerrorbars => 2,
89: financebars => 2,
90: candlesticks => 2,
91: vector => 2
92: );
93:
1.11 matthew 94: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19 matthew 95: my $real_test =
96: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.11 matthew 97: my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
1.1 matthew 98: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
1.15 matthew 99: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1 matthew 100: my $sml_test = sub {$_[0]=~/^(small|medium|large)$/};
1.29 matthew 101: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.15 matthew 102: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
1.21 matthew 103:
104: ###################################################################
105: ## ##
106: ## Attribute metadata ##
107: ## ##
108: ###################################################################
1.1 matthew 109: my %plot_defaults =
110: (
1.20 matthew 111: height => {
112: default => 200,
113: test => $int_test,
1.29 matthew 114: description => 'height of image (pixels)',
1.20 matthew 115: edit_type => 'entry'
116: },
117: width => {
118: default => 200,
119: test => $int_test,
1.29 matthew 120: description => 'width of image (pixels)',
1.20 matthew 121: edit_type => 'entry'
122: },
123: bgcolor => {
124: default => 'xffffff',
125: test => $color_test,
126: description => 'background color of image (xffffff)',
127: edit_type => 'entry'
128: },
129: fgcolor => {
130: default => 'x000000',
131: test => $color_test,
132: description => 'foreground color of image (x000000)',
133: edit_type => 'entry'
134: },
135: transparent => {
136: default => 'off',
137: test => $onoff_test,
138: description => '',
139: edit_type => 'on_off'
140: },
141: grid => {
142: default => 'off',
143: test => $onoff_test,
144: description => '',
145: edit_type => 'on_off'
146: },
147: border => {
148: default => 'on',
149: test => $onoff_test,
150: description => '',
151: edit_type => 'on_off'
152: },
153: font => {
154: default => 'medium',
155: test => $sml_test,
156: description => 'Size of font to use',
157: edit_type => 'choice',
158: choices => ['small','medium','large']
159: },
160: align => {
161: default => 'left',
162: test => sub {$_[0]=~/^(left|right|center)$/},
163: description => 'alignment for image in html',
164: edit_type => 'choice',
165: choices => ['left','right','center']
166: }
1.1 matthew 167: );
168:
169: my %key_defaults =
170: (
1.20 matthew 171: title => {
172: default => '',
173: test => $words_test,
174: description => 'Title of key',
175: edit_type => 'entry'
176: },
177: box => {
178: default => 'off',
179: test => $onoff_test,
180: description => 'Draw a box around the key?',
181: edit_type => 'on_off'
182: },
183: pos => {
184: default => 'top right',
185: test => $key_pos_test,
186: description => 'position of the key on the plot',
187: edit_type => 'choice',
188: choices => ['top left','top right','bottom left','bottom right',
189: 'outside','below']
190: }
1.1 matthew 191: );
192:
193: my %label_defaults =
194: (
1.20 matthew 195: xpos => {
196: default => 0,
197: test => $real_test,
198: description => 'x position of label (graph coordinates)',
199: edit_type => 'entry'
200: },
201: ypos => {
202: default => 0,
203: test => $real_test,
204: description => 'y position of label (graph coordinates)',
205: edit_type => 'entry'
206: },
207: justify => {
208: default => 'left',
209: test => sub {$_[0]=~/^(left|right|center)$/},
210: description => 'justification of the label text on the plot',
211: edit_type => 'choice',
212: choices => ['left','right','center']
213: }
1.1 matthew 214: );
215:
216: my %axis_defaults =
217: (
1.28 matthew 218: color => {
1.20 matthew 219: default => 'x000000',
220: test => $color_test,
221: description => 'color of axes (x000000)',
222: edit_type => 'entry'
223: },
224: xmin => {
225: default => '-10.0',
226: test => $real_test,
227: description => 'minimum x-value shown in plot',
228: edit_type => 'entry'
229: },
230: xmax => {
231: default => ' 10.0',
232: test => $real_test,
233: description => 'maximum x-value shown in plot',
234: edit_type => 'entry'
235: },
236: ymin => {
237: default => '-10.0',
238: test => $real_test,
239: description => 'minimum y-value shown in plot',
240: edit_type => 'entry'
241: },
242: ymax => {
243: default => ' 10.0',
244: test => $real_test,
245: description => 'maximum y-value shown in plot',
246: edit_type => 'entry'
247: }
1.1 matthew 248: );
249:
250: my %curve_defaults =
251: (
1.20 matthew 252: color => {
253: default => 'x000000',
254: test => $color_test,
255: description => 'color of curve (x000000)',
256: edit_type => 'entry'
257: },
258: name => {
259: default => '',
260: test => $words_test,
261: description => 'name of curve to appear in key',
262: edit_type => 'entry'
263: },
264: linestyle => {
265: default => 'lines',
266: test => $linestyle_test,
267: description => 'Style of the axis lines',
268: edit_type => 'choice',
1.29 matthew 269: choices => ['lines','linespoints','dots','points','steps',
270: 'fsteps','histeps','errorbars','xerrorbars',
271: 'yerrorbars','xyerrorbars','boxes','boxerrorbars',
272: 'boxxyerrorbars','financebars','candlesticks',
273: 'vector']
1.20 matthew 274: }
1.1 matthew 275: );
276:
1.21 matthew 277: ###################################################################
278: ## ##
279: ## parsing and edit rendering ##
280: ## ##
281: ###################################################################
1.1 matthew 282: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
283:
284: sub start_plot {
1.23 matthew 285: %plot = (); %key = (); %axis = ();
1.10 matthew 286: $title = undef; $xlabel = undef; $ylabel = undef;
287: $#labels = -1; $#curves = -1;
1.6 matthew 288: #
1.1 matthew 289: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
290: my $result='';
1.25 matthew 291: &Apache::lonxml::register('Apache::lonplot',
292: ('title','xlabel','ylabel','key','axis','label','curve'));
1.29 matthew 293: push (@Apache::lonxml::namespace,'lonplot');
1.4 matthew 294: if ($target eq 'web') {
1.29 matthew 295: my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
1.17 matthew 296: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
1.29 matthew 297: &Apache::lonxml::newparser($parser,\$inside);
1.17 matthew 298: &get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
299: $tagstack->[-1]);
1.20 matthew 300: } elsif ($target eq 'edit') {
1.25 matthew 301: $result .= &Apache::edit::tag_start($target,$token,'Plot');
1.21 matthew 302: $result .= &edit_attributes($target,$token,\%plot_defaults);
1.20 matthew 303: } elsif ($target eq 'modified') {
304: my $constructtag=&Apache::edit::get_new_args
1.24 matthew 305: ($token,$parstack,$safeeval,keys(%plot_defaults));
1.20 matthew 306: if ($constructtag) {
307: $result = &Apache::edit::rebuild_tag($token);
1.26 matthew 308: # $result.= &Apache::edit::handle_insert();
1.20 matthew 309: }
1.4 matthew 310: }
1.21 matthew 311: return $result;
1.1 matthew 312: }
313:
314: sub end_plot {
315: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25 matthew 316:
1.1 matthew 317: pop @Apache::lonxml::namespace;
1.4 matthew 318: &Apache::lonxml::deregister('Apache::lonplot',
319: ('title','xlabel','ylabel','key','axis','label','curve'));
320: my $result = '';
321: if ($target eq 'web') {
1.21 matthew 322: &check_inputs(); # Make sure we have all the data we need
1.13 matthew 323: ##
324: ## Determine filename
1.4 matthew 325: my $tmpdir = '/home/httpd/perl/tmp/';
1.12 matthew 326: my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.29 matthew 327: '_'.time.'_'.$$.int(rand(1000)).'_plot.data';
1.4 matthew 328: ## Write the plot description to the file
1.12 matthew 329: my $fh=Apache::File->new(">$tmpdir$filename");
330: print $fh &write_gnuplot_file();
1.14 matthew 331: close($fh);
1.4 matthew 332: ## return image tag for the plot
1.12 matthew 333: $result .= <<"ENDIMAGE";
1.16 matthew 334: <img src = "/cgi-bin/plot.gif?$filename"
335: width = "$plot{'width'}"
336: height = "$plot{'height'}"
337: align = "$plot{'align'}"
338: alt = "/cgi-bin/plot.gif?$filename" />
1.12 matthew 339: ENDIMAGE
1.20 matthew 340: } elsif ($target eq 'edit') {
1.21 matthew 341: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 342: }
1.1 matthew 343: return $result;
344: }
1.2 matthew 345:
1.1 matthew 346: ##----------------------------------------------------------------- key
347: sub start_key {
348: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
349: my $result='';
1.17 matthew 350: if ($target eq 'web') {
351: &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11 matthew 352: $tagstack->[-1]);
1.20 matthew 353: } elsif ($target eq 'edit') {
1.25 matthew 354: $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21 matthew 355: $result .= &edit_attributes($target,$token,\%key_defaults);
1.20 matthew 356: } elsif ($target eq 'modified') {
357: my $constructtag=&Apache::edit::get_new_args
1.24 matthew 358: ($token,$parstack,$safeeval,keys(%key_defaults));
1.20 matthew 359: if ($constructtag) {
360: $result = &Apache::edit::rebuild_tag($token);
361: $result.= &Apache::edit::handle_insert();
362: }
1.4 matthew 363: }
1.1 matthew 364: return $result;
365: }
366:
367: sub end_key {
368: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
369: my $result = '';
1.4 matthew 370: if ($target eq 'web') {
1.20 matthew 371: } elsif ($target eq 'edit') {
1.21 matthew 372: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 373: }
1.1 matthew 374: return $result;
375: }
1.21 matthew 376:
1.1 matthew 377: ##------------------------------------------------------------------- title
378: sub start_title {
379: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
380: my $result='';
1.4 matthew 381: if ($target eq 'web') {
1.17 matthew 382: $title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.20 matthew 383: } elsif ($target eq 'edit') {
1.25 matthew 384: $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.22 matthew 385: my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
386: $result.='</td></tr><tr><td colspan="3">'.
1.30 ! matthew 387: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 388: } elsif ($target eq 'modified') {
1.29 matthew 389: my $text=$$parser[-1]->get_text("/title");
1.21 matthew 390: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 391: }
1.1 matthew 392: return $result;
393: }
394:
395: sub end_title {
396: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
397: my $result = '';
1.4 matthew 398: if ($target eq 'web') {
1.20 matthew 399: } elsif ($target eq 'edit') {
1.27 matthew 400: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 401: }
1.1 matthew 402: return $result;
403: }
404: ##------------------------------------------------------------------- xlabel
405: sub start_xlabel {
406: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
407: my $result='';
1.4 matthew 408: if ($target eq 'web') {
1.17 matthew 409: $xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.20 matthew 410: } elsif ($target eq 'edit') {
1.25 matthew 411: $result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.22 matthew 412: my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
413: $result.='</td></tr><tr><td colspan="3">'.
1.30 ! matthew 414: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 415: } elsif ($target eq 'modified') {
1.29 matthew 416: my $text=$$parser[-1]->get_text("/xlabel");
1.21 matthew 417: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 418: }
1.1 matthew 419: return $result;
420: }
421:
422: sub end_xlabel {
423: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
424: my $result = '';
1.4 matthew 425: if ($target eq 'web') {
1.20 matthew 426: } elsif ($target eq 'edit') {
1.27 matthew 427: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 428: }
1.1 matthew 429: return $result;
430: }
1.21 matthew 431:
1.1 matthew 432: ##------------------------------------------------------------------- ylabel
433: sub start_ylabel {
434: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
435: my $result='';
1.4 matthew 436: if ($target eq 'web') {
1.17 matthew 437: $ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.20 matthew 438: } elsif ($target eq 'edit') {
1.25 matthew 439: $result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.22 matthew 440: my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
441: $result .= '</td></tr><tr><td colspan="3">'.
1.30 ! matthew 442: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 443: } elsif ($target eq 'modified') {
1.29 matthew 444: my $text=$$parser[-1]->get_text("/ylabel");
1.21 matthew 445: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 446: }
1.1 matthew 447: return $result;
448: }
449:
450: sub end_ylabel {
451: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
452: my $result = '';
1.4 matthew 453: if ($target eq 'web') {
1.20 matthew 454: } elsif ($target eq 'edit') {
1.27 matthew 455: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 456: }
1.1 matthew 457: return $result;
458: }
1.21 matthew 459:
1.1 matthew 460: ##------------------------------------------------------------------- label
461: sub start_label {
462: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
463: my $result='';
1.17 matthew 464: if ($target eq 'web') {
465: my %label;
466: &get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11 matthew 467: $tagstack->[-1]);
1.17 matthew 468: $label{'text'} = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
469: if (! &$words_test($label{'text'})) {
470: # I should probably warn about it, too.
471: $label{'text'} = 'Illegal text';
472: }
473: push(@labels,\%label);
1.20 matthew 474: } elsif ($target eq 'edit') {
1.25 matthew 475: $result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21 matthew 476: $result .= &edit_attributes($target,$token,\%label_defaults);
1.22 matthew 477: my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
478: $result .= '</td></tr><tr><td colspan="3">'.
1.30 ! matthew 479: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 480: } elsif ($target eq 'modified') {
481: my $constructtag=&Apache::edit::get_new_args
1.24 matthew 482: ($token,$parstack,$safeeval,keys(%label_defaults));
1.20 matthew 483: if ($constructtag) {
484: $result = &Apache::edit::rebuild_tag($token);
485: $result.= &Apache::edit::handle_insert();
486: }
1.22 matthew 487: my $text=$$parser[-1]->get_text("/label");
1.21 matthew 488: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 489: }
1.1 matthew 490: return $result;
491: }
492:
493: sub end_label {
494: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
495: my $result = '';
1.4 matthew 496: if ($target eq 'web') {
1.20 matthew 497: } elsif ($target eq 'edit') {
1.21 matthew 498: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 499: }
1.1 matthew 500: return $result;
501: }
502:
503: ##------------------------------------------------------------------- curve
504: sub start_curve {
505: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
506: my $result='';
1.25 matthew 507: &Apache::lonxml::register('Apache::lonplot',('function','data'));
508: push (@Apache::lonxml::namespace,'curve');
1.17 matthew 509: if ($target eq 'web') {
510: my %curve;
511: &get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11 matthew 512: $tagstack->[-1]);
1.17 matthew 513: push (@curves,\%curve);
1.20 matthew 514: } elsif ($target eq 'edit') {
1.26 matthew 515: $result .= &Apache::edit::tag_start($target,$token,'Curve');
1.21 matthew 516: $result .= &edit_attributes($target,$token,\%curve_defaults);
1.20 matthew 517: } elsif ($target eq 'modified') {
518: my $constructtag=&Apache::edit::get_new_args
1.24 matthew 519: ($token,$parstack,$safeeval,keys(%label_defaults));
1.20 matthew 520: if ($constructtag) {
521: $result = &Apache::edit::rebuild_tag($token);
522: $result.= &Apache::edit::handle_insert();
523: }
1.4 matthew 524: }
1.1 matthew 525: return $result;
526: }
527:
528: sub end_curve {
529: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
530: my $result = '';
1.25 matthew 531: pop @Apache::lonxml::namespace;
532: &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.4 matthew 533: if ($target eq 'web') {
1.20 matthew 534: } elsif ($target eq 'edit') {
1.21 matthew 535: $result.=&Apache::edit::tag_end($target,$token);
1.4 matthew 536: }
1.1 matthew 537: return $result;
538: }
1.21 matthew 539:
1.1 matthew 540: ##------------------------------------------------------------ curve function
541: sub start_function {
542: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
543: my $result='';
1.4 matthew 544: if ($target eq 'web') {
1.17 matthew 545: if (exists($curves[-1]->{'data'})) {
546: &Apache::lonxml::warning('Use of <function> precludes use of <data>. The <data> will be omitted in favor of the <function> declaration.');
547: delete $curves[-1]->{'data'} ;
548: }
549: $curves[-1]->{'function'} =
550: &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.20 matthew 551: } elsif ($target eq 'edit') {
1.25 matthew 552: $result .= &Apache::edit::tag_start($target,$token,'Curve Function');
1.22 matthew 553: my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
554: $result .= '</td></tr><tr><td colspan="3">'.
1.30 ! matthew 555: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 556: } elsif ($target eq 'modified') {
557: # Why do I do this?
558: my $text=$$parser[-1]->get_text("/function");
559: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 560: }
1.1 matthew 561: return $result;
562: }
563:
564: sub end_function {
565: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
566: my $result = '';
1.4 matthew 567: if ($target eq 'web') {
1.20 matthew 568: } elsif ($target eq 'edit') {
1.26 matthew 569: $result .= &Apache::edit::end_table();
1.4 matthew 570: }
1.1 matthew 571: return $result;
572: }
1.21 matthew 573:
1.1 matthew 574: ##------------------------------------------------------------ curve data
575: sub start_data {
576: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
577: my $result='';
1.4 matthew 578: if ($target eq 'web') {
1.17 matthew 579: if (exists($curves[-1]->{'function'})) {
580: &Apache::lonxml::warning('Use of <data> precludes use of .'.
581: '<function>. The <function> will be omitted in favor of '.
582: 'the <data> declaration.');
583: delete($curves[-1]->{'function'});
584: }
585: my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
586: $datatext =~ s/\s+/ /g;
587: # Need to do some error checking on the @data array -
588: # make sure it's all numbers and make sure each array
589: # is of the same length.
590: my @data;
591: if ($datatext =~ /,/) {
592: @data = split /,/,$datatext;
593: } else { # Assume it's space seperated.
594: @data = split / /,$datatext;
595: }
596: for (my $i=0;$i<=$#data;$i++) {
597: # Check that it's non-empty
1.19 matthew 598: if (! defined($data[$i])) {
599: &Apache::lonxml::warning(
600: 'undefined <data> value. Replacing with '.
601: ' pi/e = 1.15572734979092');
602: $data[$i] = 1.15572734979092;
603: }
1.17 matthew 604: # Check that it's a number
1.19 matthew 605: if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
606: &Apache::lonxml::warning(
607: 'Bad <data> value of '.$data[$i].' Replacing with '.
608: ' pi/e = 1.15572734979092');
609: $data[$i] = 1.15572734979092;
610: }
1.17 matthew 611: }
612: push @{$curves[-1]->{'data'}},\@data;
1.20 matthew 613: } elsif ($target eq 'edit') {
1.25 matthew 614: $result .= &Apache::edit::tag_start($target,$token,'Curve Data');
1.22 matthew 615: my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
616: $result .= '</td></tr><tr><td colspan="3">'.
1.30 ! matthew 617: &Apache::edit::editfield('',$text,'',60,1);
1.20 matthew 618: } elsif ($target eq 'modified') {
1.21 matthew 619: my $text=$$parser[-1]->get_text("/data");
620: $result.=&Apache::edit::modifiedfield($token);
1.4 matthew 621: }
1.1 matthew 622: return $result;
623: }
624:
625: sub end_data {
626: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
627: my $result = '';
1.4 matthew 628: if ($target eq 'web') {
1.20 matthew 629: } elsif ($target eq 'edit') {
1.26 matthew 630: $result .= &Apache::edit::end_table();
1.4 matthew 631: }
1.1 matthew 632: return $result;
633: }
634:
635: ##------------------------------------------------------------------- axis
636: sub start_axis {
637: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
638: my $result='';
1.4 matthew 639: if ($target eq 'web') {
1.17 matthew 640: &get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
641: $tagstack->[-1]);
1.20 matthew 642: } elsif ($target eq 'edit') {
1.25 matthew 643: $result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.21 matthew 644: $result .= &edit_attributes($target,$token,\%axis_defaults);
1.20 matthew 645: } elsif ($target eq 'modified') {
1.29 matthew 646: my $constructtag=&Apache::edit::get_new_args
647: ($token,$parstack,$safeeval,keys(%axis_defaults));
648: if ($constructtag) {
649: $result = &Apache::edit::rebuild_tag($token);
650: $result.= &Apache::edit::handle_insert();
651: }
1.4 matthew 652: }
1.1 matthew 653: return $result;
654: }
655:
656: sub end_axis {
657: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
658: my $result = '';
1.4 matthew 659: if ($target eq 'web') {
1.20 matthew 660: } elsif ($target eq 'edit') {
1.21 matthew 661: $result.=&Apache::edit::tag_end($target,$token);
1.20 matthew 662: } elsif ($target eq 'modified') {
1.4 matthew 663: }
1.1 matthew 664: return $result;
665: }
666:
1.21 matthew 667: ###################################################################
668: ## ##
669: ## Utility Functions ##
670: ## ##
671: ###################################################################
672:
1.13 matthew 673: ##----------------------------------------------------------- set_defaults
674: sub set_defaults {
1.21 matthew 675: my ($var,$defaults) = @_;
1.13 matthew 676: my $key;
1.24 matthew 677: foreach $key (keys(%$defaults)) {
1.13 matthew 678: $var->{$key} = $defaults->{$key}->{'default'};
679: }
680: }
681:
1.1 matthew 682: ##------------------------------------------------------------------- misc
1.2 matthew 683: sub get_attributes{
1.21 matthew 684: my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24 matthew 685: foreach my $attr (keys(%{$defaults})) {
1.10 matthew 686: $values->{$attr} =
1.15 matthew 687: &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10 matthew 688: if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11 matthew 689: $values->{$attr} = $defaults->{$attr}->{'default'};
1.6 matthew 690: next;
691: }
1.10 matthew 692: my $test = $defaults->{$attr}->{'test'};
693: if (! &$test($values->{$attr})) {
1.6 matthew 694: &Apache::lonxml::warning
695: ($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11 matthew 696: .$defaults->{$attr}->{'default'} );
697: $values->{$attr} = $defaults->{$attr}->{'default'};
1.10 matthew 698: }
1.2 matthew 699: }
1.11 matthew 700: return ;
1.6 matthew 701: }
1.15 matthew 702: ##------------------------------------------------------- write_gnuplot_file
1.6 matthew 703: sub write_gnuplot_file {
704: my $gnuplot_input = '';
1.10 matthew 705: my $curve;
1.6 matthew 706: # Collect all the colors
707: my @Colors;
708: push @Colors, $plot{'bgcolor'};
709: push @Colors, $plot{'fgcolor'};
1.13 matthew 710: push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9 matthew 711: foreach $curve (@curves) {
712: push @Colors, ($curve->{'color'} ne '' ?
713: $curve->{'color'} :
1.13 matthew 714: $plot{'fgcolor'} );
1.6 matthew 715: }
716: # set term
717: $gnuplot_input .= 'set term gif ';
718: $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
719: $gnuplot_input .= $plot{'font'} . ' ';
1.10 matthew 720: $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
1.6 matthew 721: $gnuplot_input .= "@Colors\n";
1.7 matthew 722: # grid
1.10 matthew 723: $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7 matthew 724: # border
1.9 matthew 725: $gnuplot_input .= ($plot{'border'} eq 'on'?
726: 'set border'.$/ :
727: 'set noborder'.$/ ); # title, xlabel, ylabel
1.13 matthew 728: $gnuplot_input .= "set output\n";
729: $gnuplot_input .= "set title \"$title\"\n" if (defined($title)) ;
730: $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
731: $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
1.23 matthew 732: if (%axis) {
1.13 matthew 733: $gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
734: $gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6 matthew 735: }
736: # Key
1.23 matthew 737: if (%key) {
1.9 matthew 738: $gnuplot_input .= 'set key '.$key{'pos'}.' ';
739: if ($key{'title'} ne '') {
1.11 matthew 740: $gnuplot_input .= 'title "'.$key{'title'}.'" ';
741: }
742: $gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6 matthew 743: } else {
1.9 matthew 744: $gnuplot_input .= 'set nokey'.$/;
1.13 matthew 745: }
1.6 matthew 746: # labels
1.10 matthew 747: my $label;
1.6 matthew 748: foreach $label (@labels) {
749: $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9 matthew 750: $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6 matthew 751: }
752: # curves
753: $gnuplot_input .= 'plot ';
754: my $datatext = '';
1.9 matthew 755: for (my $i = 0;$i<=$#curves;$i++) {
756: $curve = $curves[$i];
757: $gnuplot_input.= ', ' if ($i > 0);
1.6 matthew 758: if (exists($curve->{'function'})) {
1.9 matthew 759: $gnuplot_input.=
760: $curve->{'function'}.' title "'.
761: $curve->{'name'}.'" with '.
762: $curve->{'linestyle'};
1.6 matthew 763: } elsif (exists($curve->{'data'})) {
1.9 matthew 764: $gnuplot_input.= '\'-\' title "'.
765: $curve->{'name'}.'" with '.
766: $curve->{'linestyle'};
1.6 matthew 767: my @Data = @{$curve->{'data'}};
1.9 matthew 768: my @Data0 = @{$Data[0]};
769: for (my $i =0; $i<=$#Data0; $i++) {
1.10 matthew 770: my $dataset;
1.6 matthew 771: foreach $dataset (@Data) {
1.9 matthew 772: $datatext .= $dataset->[$i] . ' ';
1.6 matthew 773: }
1.9 matthew 774: $datatext .= $/;
1.6 matthew 775: }
1.9 matthew 776: $datatext .=$/;
1.6 matthew 777: }
778: }
1.9 matthew 779: $gnuplot_input .= $/.$datatext;
1.10 matthew 780: return $gnuplot_input;
1.2 matthew 781: }
1.21 matthew 782:
783: #---------------------------------------------- check_inputs
784: sub check_inputs {
785: ## Note: no inputs, no outputs - this acts only on global variables.
786: ## Make sure we have all the input we need:
1.23 matthew 787: if (! %plot) { &set_defaults(\%plot,\%plot_defaults); }
788: if (! %key ) {} # No key for this plot, thats okay
789: if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21 matthew 790: if (! defined($title )) {} # No title for this plot, thats okay
791: if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
792: if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
793: if ($#labels < 0) { } # No labels for this plot, thats okay
794: if ($#curves < 0) {
795: &Apache::lonxml::warning("No curves specified for plot!!!!");
796: return '';
797: }
798: my $curve;
799: foreach $curve (@curves) {
800: if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
801: &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
802: return '';
803: }
804: }
805: }
806:
1.20 matthew 807: #------------------------------------------------ make_edit
808: sub edit_attributes {
1.21 matthew 809: my ($target,$token,$defaults) = @_;
1.20 matthew 810: my $result;
1.25 matthew 811: foreach my $attr (sort keys(%$defaults)) {
1.20 matthew 812: if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
813: $result .= &Apache::edit::text_arg(
814: $defaults->{$attr}->{'description'},
815: $attr,
816: $token);
817: } elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
818: $result .= &Apache::edit::select_arg(
819: $defaults->{$attr}->{'description'},
820: $attr,
821: $defaults->{$attr}->{'choices'},
822: $token);
823: }
1.25 matthew 824: $result .= '<br />';
1.20 matthew 825: }
826: return $result;
827: }
1.1 matthew 828:
1.21 matthew 829:
830: ###################################################################
831: ## ##
832: ## Insertion functions for editing plots ##
833: ## ##
834: ###################################################################
835:
1.20 matthew 836: #------------------------------------------------ insert_xxxxxxx
837: sub insert_plot {
1.29 matthew 838: my $result = '';
1.20 matthew 839: # plot attributes
1.29 matthew 840: $result .= "<plot \n";
1.30 ! matthew 841: foreach my $attr (keys(%plot_defaults)) {
1.29 matthew 842: $result .= " $attr=\"$plot_defaults{$attr}->{'default'}\"\n";
1.20 matthew 843: }
844: $result .= ">\n";
845: # Add the components
846: $result .= &insert_key();
847: $result .= &insert_axis();
1.29 matthew 848: $result .= &insert_title();
849: $result .= &insert_xlabel();
850: $result .= &insert_ylabel();
1.20 matthew 851: $result .= &insert_curve();
852: # close up the <plot>
853: $result .= "</plot>\n";
854: return $result;
855: }
856:
857: sub insert_key {
858: my $result;
1.29 matthew 859: $result .= " <key \n";
1.30 ! matthew 860: foreach my $attr (keys(%key_defaults)) {
1.29 matthew 861: $result .= " $attr=\"$key_defaults{$attr}->{'default'}\"\n";
1.20 matthew 862: }
863: $result .= " />\n";
864: return $result;
865: }
866:
867: sub insert_axis{
868: my $result;
869: $result .= ' <axis ';
1.30 ! matthew 870: foreach my $attr (keys(%axis_defaults)) {
1.29 matthew 871: $result .= " $attr=\"$axis_defaults{$attr}->{'default'}\"\n";
1.20 matthew 872: }
873: $result .= " />\n";
874: return $result;
875: }
1.28 matthew 876:
877: sub insert_title { return " <title></title>\n"; }
1.29 matthew 878: sub insert_xlabel { return " <xlabel></xlabel>\n"; }
879: sub insert_ylabel { return " <ylabel></ylabel>\n"; }
1.20 matthew 880:
881: sub insert_label {
882: my $result;
883: $result .= ' <label ';
1.30 ! matthew 884: foreach my $attr (keys(%label_defaults)) {
1.27 matthew 885: $result .= ' '.$attr.'="'.
1.20 matthew 886: $label_defaults{$attr}->{'default'}."\"\n";
887: }
888: $result .= " ></label>\n";
889: return $result;
890: }
891:
892: sub insert_curve {
893: my $result;
894: $result .= ' <curve ';
1.30 ! matthew 895: foreach my $attr (keys(%curve_defaults)) {
1.27 matthew 896: $result .= ' '.$attr.'="'.
1.20 matthew 897: $curve_defaults{$attr}->{'default'}."\"\n";
898: }
1.29 matthew 899: $result .= " ></curve>\n";
1.20 matthew 900: }
1.4 matthew 901:
1.20 matthew 902: sub insert_function {
903: my $result;
904: $result .= "<function></function>\n";
905: return $result;
906: }
1.4 matthew 907:
1.20 matthew 908: sub insert_data {
909: my $result;
910: $result .= " <data></data>\n";
911: return $result;
912: }
1.4 matthew 913:
1.21 matthew 914: ##----------------------------------------------------------------------
1.20 matthew 915: 1;
916: __END__
1.4 matthew 917:
918:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>