PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/R2008-02-16/extra/graceplot/inst/alternatives/legend.m

#
MATLAB | 199 lines | 198 code | 1 blank | 0 comment | 1 complexity | 48702e97169ab92d9cfebf204200e059 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. ## Copyright (C) 2001 Laurent Mazet
  2. ##
  3. ## This program is free software; it is distributed in the hope that it
  4. ## will be useful, but WITHOUT ANY WARRANTY; without even the implied
  5. ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  6. ## the GNU General Public License for more details.
  7. ##
  8. ## You should have received a copy of the GNU General Public License
  9. ## along with this file; see the file COPYING. If not, see
  10. ## <http://www.gnu.org/licenses/>.
  11. ## -*- texinfo -*-
  12. ## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @var{st3}, @var{...})
  13. ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @var{st3}, @var{...}, @var{pos})
  14. ## @deftypefnx {Function File} {} legend (@var{matstr})
  15. ## @deftypefnx {Function File} {} legend (@var{matstr}, @var{pos})
  16. ## @deftypefnx {Function File} {} legend (@var{cell})
  17. ## @deftypefnx {Function File} {} legend (@var{cell}, @var{pos})
  18. ## @deftypefnx {Function File} {} legend ('@var{func}')
  19. ##
  20. ## Legend puts a legend on the current plot using the specified strings
  21. ## as labels. Use independant strings (@var{st1}, @var{st2}, @var{st3}...), a
  22. ## matrix of strings (@var{matstr}), or a cell array of strings (@var{cell}) to
  23. ## specify legends. Legend works on line graphs, bar graphs, etc...
  24. ## Be sure to call plot before calling legend.
  25. ##
  26. ## @var{pos} optionally places the legend in the specified location:
  27. ##
  28. ## @multitable @columnfractions 0.1 0.1 0.8
  29. ## @item @tab 0 @tab
  30. ## Don't move the legend box (default)
  31. ## @item @tab 1 @tab
  32. ## Upper right-hand corner
  33. ## @item @tab 2 @tab
  34. ## Upper left-hand corner
  35. ## @item @tab 3 @tab
  36. ## Lower left-hand corner
  37. ## @item @tab 4 @tab
  38. ## Lower right-hand corner
  39. ## @item @tab -1 @tab
  40. ## To the top right of the plot
  41. ## @item @tab -2 @tab
  42. ## To the bottom right of the plot
  43. ## @item @tab -3 @tab
  44. ## To the bottom of the plot
  45. ## @item @tab [@var{x}, @var{y}] @tab
  46. ## To the arbitrary postion in plot [@var{x}, @var{y}]
  47. ## @end multitable
  48. ##
  49. ## Some specific functions are directely avaliable using @var{func}:
  50. ##
  51. ## @table @code
  52. ## @item show
  53. ## Show legends from the plot
  54. ## @item hide
  55. ## @itemx off
  56. ## Hide legends from the plot
  57. ## @item boxon
  58. ## Draw a box around legends
  59. ## @item boxoff
  60. ## Withdraw the box around legends
  61. ## @item left
  62. ## Text is to the left of the keys
  63. ## @item right
  64. ## Text is to the right of the keys
  65. ## @end table
  66. ##
  67. ## REQUIRES: unix piping functionality, grep, sed and awk
  68. ## @end deftypefn
  69. ## 2001-03-31 Paul Kienzle
  70. ## * use tmpnam for temporary file name; unlink to remove
  71. ## 2001-09-28 Paul Söderlind <Paul.Soderlind@hhs.se>
  72. ## * add a pause after save request to give gnuplot time to write the file
  73. ## * add comment to call plot before legend.
  74. ## 2002-09-18 Paul Kienzle
  75. ## * make the pause check every .1 seconds
  76. ## 2003-04-1 Laurent Mazet
  77. ## * add new functions (boxon, boxoff...)
  78. ## * rebuild help message
  79. ## 2003-06-12 Quentin Spencer
  80. ## * add support for input in cell array format
  81. ## 2005-01-11 Teemu Ikonen
  82. ## * modify to support Grace
  83. ## PKG_ADD mark_as_command legend
  84. function legend (varargin)
  85. args = nargin();
  86. str = "";
  87. if (args > 0)
  88. str = varargin{1};
  89. endif;
  90. __grcmd__("legend loctype view");
  91. ## Test for strings
  92. if (ischar(str)) && (args == 1)
  93. _str = tolower(deblank(str));
  94. _replot = 1;
  95. switch _str
  96. case {"off", "hide"}
  97. __grcmd__("legend off");
  98. # __gnuplot_set__ nokey;
  99. case "show"
  100. __grcmd__("legend on");
  101. # __gnuplot_set__ key;
  102. case "boxon"
  103. __grcmd__("legend box on");
  104. # __gnuplot_set__ key box;
  105. case "boxoff"
  106. __grcmd__("legend box off");
  107. # __gnuplot_set__ key nobox;
  108. case "left"
  109. __grcmd__("legend 0.15,0.85");
  110. # __gnuplot_set__ key Right noreverse;
  111. case "right"
  112. __grcmd__("legend 0.7,0.85");
  113. # __gnuplot_set__ key Left reverse;
  114. otherwise
  115. _replot = 0;
  116. endswitch
  117. if _replot
  118. __grcmd__("redraw");
  119. return;
  120. endif
  121. endif
  122. if(ischar(str) && size(str,1) > 1)
  123. data = cellstr(str);
  124. args--;
  125. elseif(iscell(str))
  126. data = str;
  127. args--;
  128. else
  129. data = varargin;
  130. args = 0;
  131. endif
  132. nb_data = length(data);
  133. pos_leg = NaN;
  134. i = 1;
  135. while(i <= nb_data && ischar(data{i}))
  136. __grcmd__(sprintf("s%d legend \"%s\"", i-1, data{i}));
  137. i++;
  138. endwhile
  139. if(i <= nb_data && isreal(data{i}) )
  140. pos_leg = data{i};
  141. endif
  142. if(args > 0 && isreal(varargin{end}))
  143. pos_leg = varargin{end};
  144. endif
  145. if (isnan(pos_leg))
  146. ;
  147. elseif (isscalar (pos_leg) && isreal(pos_leg))
  148. switch (pos_leg)
  149. case 1
  150. __grcmd__("legend 0.7,0.85");
  151. # __gnuplot_set__ key right top;
  152. case 2
  153. __grcmd__("legend 0.15,0.85");
  154. # __gnuplot_set__ key left top;
  155. case 3
  156. __grcmd__("legend 0.15,0.20");
  157. # __gnuplot_set__ key left bottom;
  158. case 4
  159. __grcmd__("legend 0.7,0.20");
  160. # __gnuplot_set__ key right bottom;
  161. case -1
  162. __grcmd__("legend 0.7,0.95");
  163. # __gnuplot_set__ key right top outside;
  164. case -2
  165. __grcmd__("legend 0.7,0.10");
  166. # __gnuplot_set__ key right bottom outside;
  167. case -3
  168. __grcmd__("legend 0.15,0.10");
  169. # __gnuplot_set__ key below;
  170. otherwise
  171. warning ("incorrect pos");
  172. endswitch;
  173. elseif (isvector (pos_leg)) && (length (pos_leg) == 2) && \
  174. (all(isreal(pos_leg)))
  175. __grcmd__(sprintf("legend %f,%f", pos_leg(1), pos_leg(2)));
  176. else
  177. warning ("pos must be a scalar");
  178. endif;
  179. ## Regenerate the plot
  180. __grcmd__("redraw");
  181. endfunction;