File:  [LON-CAPA] / loncom / html / adm / help / tex / Authoring_Dynamic_Plot.tex
Revision 1.6: download - view: text, annotated - select for diffs
Thu Aug 18 11:36:48 2016 UTC (8 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, HEAD
- Use LaTeX-style single quotes.

\label{Authoring_Dynamic_Plot}
Dynamically generated plots are used to produce graphs which will be
different for each student who views them. The plots are produced by
calling `gnuplot', and in fact the xml tag for the plots is <gnuplot>.

Dynamically generated plots should be used in conjunction with a perl
script block which generates the data to be plotted. If you are using
static data a dynamically generated plot is not appropriate because of
the overhead associated with generating the plot. In that case, use a
picture (see \ref{Authoring_Adding_Pictures}).

There are a great deal of parameters that can be set for a plot. These
parameters are accessed by including various sub-tags. By default only
the <gnuplot > tag and <curve > tag are present in a plot. The
sub-tags allow you to define the axes of the plot, the presence of a
key, the placement of gridlines, and the title and legends on the
plot. The example given below shows the use of the <axis > sub-tag to
set the domain and range shown in the graph. 

Below is an example which produces a simple plot. To use it, create a
new problem and \textbf{Edit XML}. Remove all of the text and replace 
it with the text below: 

\begin{verbatim}

 <problem >
 <script type="loncapa/perl" >
 $amplitude = &random(3,5,1);
 for ($x=-6.0; $x<=6.0; $x+=0.05) {
     push @X,$x;
     push @Y, $amplitude * sin($x);
 }
 </script >
 <gnuplot font="medium" width="400" grid="on" 
      height="300" border="on" fgcolor="x000000" 
      alttag="dynamically generated plot" align="center"  
      bgcolor="xffffff" transparent="off" >
     <axis ymin="-6.0" ymax="6.0" xmin="-5.0" 
           xmax="5.0" color="x000000" /> 
     <curve 
          linestyle="lines"
          pointtype="1"
          pointsize="1"
          name=""
          color="x000000" >
         <data >@X</data >
         <data >@Y</data >
     </curve >
 </gnuplot >
 <startouttext /><br />
 What is the amplitude of this function?
 <endouttext />
 <numericalresponse answer="$amplitude" >
 <responseparam type="tolerance" default="5%" name="tol" 
      description="Numerical Tolerance" />
 <responseparam name="sig" type="int_range,0-16" default="0,15"  
      description="Significant Figures" />
 </numericalresponse >
 </problem >

\end{verbatim}

Below is a screenshot (in authoring space) of the resulting plot.

\includegraphics{dynamic_plot}

It's likely that the above plot doesn't quite look right. It would be
nice to have the gridlines drawn every 1 unit instead of every 2
units. A title, and labels on the axes, would be a nice
addition. Although it's overkill for this example, we can also add a
key for the plot. We can accomplish these changes by inserting
sub-tags into the <gnuplot > tag. 

Gridlines can be set using the <xtics > and <ytics > tags. The names
of these tags correspond to the names of the commands used in
gnuplot. We specify the beginning, end, and increment of the tick
marks. Gnuplot only puts gridlines on the tick marks. 

Inserting the <title >, <xlabel >, and <ylabel > commands allows us to
set the title and axes labels as one would expect. Inserting a <key >
tag, but not changing any of the information in it, signals gnuplot to
place a key in the graph. If we decide we don't want the key, deleting
the <key > tag will remove it from the graph. 

These changes in the xml are shown below and a screenshot of the new
plot is provided as well. 

\begin{verbatim}

 <problem >
 <script type="loncapa/perl" >
 $amplitude = &random(3,5,1);
 for ($x=-6.0; $x<=6.0; $x+=0.05) {
     push @X,$x;
     push @Y, $amplitude * sin($x);
 }
 </script >
 <gnuplot font="medium" width="400" grid="on" height="300" 
       border="on" fgcolor="x000000" 
       alttag="dynamically generated plot" 
       align="center" bgcolor="xffffff" transparent="off" >
     <key title="" pos="top right" box="off" />
     <ylabel >Y</ylabel >
     <xlabel >X</xlabel >
     <title >A sample plot</title >
     <xtics end="5.0" location="border" start="-5.0" 
            increment="1.0" mirror="on" />
     <ytics end="6.0" location="border" start="-6.0" 
            increment="1.0" mirror="on" />
     <axis ymin="-6.0" ymax="6.0" xmin="-5.0" 
            xmax="5.0" color="x000000" /> 
     <curve linestyle="lines" pointtype="1" pointsize="1" name="f(x)"   
         color="x000000">
         <data >@X</data >
         <data >@Y</data >
     </curve >
 </gnuplot >
 <startouttext />
 <br />
 What is the amplitude of this function?
 <endouttext />
 <numericalresponse answer="$amplitude" >
 <responseparam type="tolerance" default="5%" name="tol" 
      description="Numerical Tolerance" />
 <responseparam name="sig" type="int_range,0-16" default="0,15"  
      description="Significant Figures" />
 </numericalresponse > 
 </problem >

\end{verbatim}

\includegraphics{dynamic_plot2}

It is also possible to override the attributes of the <xtics > and <ytics > by
nesting <tic > tags inside those tags.   For each <tic > tag, the ``location''
attribute allows you to specify the location of the tic mark and the contents
of the tag allow you to specify the tic mark label.  The following
fragment of XML demonstrates an X axis that is labeled with the months of
the year.  Note that the ``rotate'' attribute of the <xtics > tag will rotate
the text of the tic marks if the output device supports rotated text:

\begin{verbatim}
...
<gnuplot ...

<xtics rotate='on'>
   <tic location='1'>January</tic>
   <tic location='2'>February</tic>
   <tic location='3'>March</tic>
   <tic location='4'>April</tic>
   <tic location='5'>May</tic>
   <tic location='6'>June</tic>
   <tic location='7'>July</tic>
   <tic location='8'>August</tic>
   <tic location='9'>September</tic>
   <tic location='10'>October</tic>
   <tic location='11'>November</tic>
   <tic location='12'>December</tic>
</xtics>
<ytics end="6.0" location="border" start="-6.0" 
       increment="1.0" mirror="on" />

...
</gnuplot>

\end{verbatim}

The values of location can be arbitrary and need not even be at regular
intervals.

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>