| 1 |
%-------------------------------------------- |
| 2 |
% |
| 3 |
% Package pgfplots |
| 4 |
% |
| 5 |
% Provides a user-friendly interface to create function plots (normal |
| 6 |
% plots, semi-logplots and double-logplots). |
| 7 |
% |
| 8 |
% It is based on Till Tantau's PGF package. |
| 9 |
% |
| 10 |
% Copyright 2013 by Christian Feuersaenger |
| 11 |
% |
| 12 |
% This program is free software: you can redistribute it and/or modify |
| 13 |
% it under the terms of the GNU General Public License as published by |
| 14 |
% the Free Software Foundation, either version 3 of the License, or |
| 15 |
% (at your option) any later version. |
| 16 |
% |
| 17 |
% This program is distributed in the hope that it will be useful, |
| 18 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
% GNU General Public License for more details. |
| 21 |
% |
| 22 |
% You should have received a copy of the GNU General Public License |
| 23 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 |
% |
| 25 |
%-------------------------------------------- |
| 26 |
|
| 27 |
|
| 28 |
% This library adds support for high-level instructions for "fill area |
| 29 |
% between two arbitrary plots of functions". |
| 30 |
% |
| 31 |
% It activates the syntax |
| 32 |
% |
| 33 |
% \addplot fill between[of=A and B]; |
| 34 |
% |
| 35 |
% where A and B are two plots named by 'name path='. |
| 36 |
% |
| 37 |
% In fact, this here is not much more than a low-level invocation of |
| 38 |
% \pgfpathfillbetween |
| 39 |
% and a couple of styles. It could become a TikZ library because it |
| 40 |
% actually works on any two named paths, but it has its restrictions |
| 41 |
% regarding the supported input paths: both need to be plots of |
| 42 |
% functions (non-intersecting, should have at most one function value |
| 43 |
% for each canvas X coord) |
| 44 |
|
| 45 |
\pgfutil@IfUndefined{pgfplotsset}{% |
| 46 |
\pgferror{Please load pgfplots before pgfplots.fillbetween.}% |
| 47 |
\endinput |
| 48 |
}{}% |
| 49 |
|
| 50 |
\usetikzlibrary{fillbetween} |
| 51 |
\usepgfplotslibrary{decorations.softclip} |
| 52 |
|
| 53 |
% Takes a domain #1:#2 and returns a new domain \pgfplotsretval:\pgfplotsretvalb which is larger. |
| 54 |
\def\pgfplots@softclip@enlarge#1#2{% |
| 55 |
\begingroup |
| 56 |
\pgfplotscoordmath{default}{parse}{0.1*(#2-#1)}\let\pgfplots@diff=\pgfmathresult |
| 57 |
\pgfplotscoordmath{default}{if is}{\pgfplots@diff}{+}{% |
| 58 |
% ok, max > min. Should always be the case. |
| 59 |
\pgfplotscoordmath{default}{parse}{#1-\pgfplots@diff}\let\pgfplots@min=\pgfmathresult |
| 60 |
\pgfplotscoordmath{default}{parse}{#2+\pgfplots@diff}\let\pgfplots@max=\pgfmathresult |
| 61 |
}{% |
| 62 |
\pgfplotscoordmath{default}{if is}{\pgfplots@diff}{0}{% |
| 63 |
% ok, max == min. Very Strange. |
| 64 |
\pgfplotscoordmath{default}{parse}{#1-1}\let\pgfplots@min=\pgfmathresult |
| 65 |
\pgfplotscoordmath{default}{parse}{#2+1}\let\pgfplots@max=\pgfmathresult |
| 66 |
}{% |
| 67 |
% ok, max < min. Strange. |
| 68 |
\pgfplotscoordmath{default}{parse}{#1+\pgfplots@diff}\let\pgfplots@min=\pgfmathresult |
| 69 |
\pgfplotscoordmath{default}{parse}{#2-\pgfplots@diff}\let\pgfplots@max=\pgfmathresult |
| 70 |
}% |
| 71 |
}% |
| 72 |
\global\let\pgfplots@glob@TMPa=\pgfplots@min |
| 73 |
\global\let\pgfplots@glob@TMPb=\pgfplots@max |
| 74 |
\endgroup |
| 75 |
\let\pgfplotsretval=\pgfplots@glob@TMPa |
| 76 |
\let\pgfplotsretvalb=\pgfplots@glob@TMPb |
| 77 |
}% |
| 78 |
|
| 79 |
\pgfkeys{ |
| 80 |
/pgfplots/every fill between plot/.style={/pgfplots/area legend,/tikz/fill,/pgfplots/on layer=pre main}, |
| 81 |
/tikz/soft clip assign/domain/.code args={#1:#2}{% |
| 82 |
\pgfplotsifisvisualizationphase{% |
| 83 |
\ifpgfplots@yislinear |
| 84 |
\pgfplots@softclip@enlarge |
| 85 |
{min(\pgfkeysvalueof{/pgfplots/ymin},\pgfplots@data@ymin)}% |
| 86 |
{max(\pgfkeysvalueof{/pgfplots/ymax},\pgfplots@data@ymax)}% |
| 87 |
\else |
| 88 |
\pgfplots@softclip@enlarge{\pgfkeysvalueof{/pgfplots/ymin}}{\pgfkeysvalueof{/pgfplots/ymax}}% |
| 89 |
\fi |
| 90 |
\edef\pgfplots@loc@TMPa{% |
| 91 |
(axis cs:#1,\pgfplotsretval) |
| 92 |
rectangle (axis cs:#2,\pgfplotsretvalb)}% |
| 93 |
\pgfkeysalso{/tikz/soft clip assign/path/.expand once=\pgfplots@loc@TMPa}% |
| 94 |
}{}% |
| 95 |
},% |
| 96 |
/tikz/soft clip assign/y domain y/.style={/tikz/soft clip assign/domain y={#1}}, |
| 97 |
/tikz/soft clip assign/domain y/.code args={#1:#2}{% |
| 98 |
\pgfplotsifisvisualizationphase{% |
| 99 |
\ifpgfplots@yislinear |
| 100 |
\pgfplots@softclip@enlarge |
| 101 |
{min(\pgfkeysvalueof{/pgfplots/xmin},\pgfplots@data@xmin)}% |
| 102 |
{max(\pgfkeysvalueof{/pgfplots/xmax},\pgfplots@data@xmax)}% |
| 103 |
\else |
| 104 |
\pgfplots@softclip@enlarge{\pgfkeysvalueof{/pgfplots/xmin}}{\pgfkeysvalueof{/pgfplots/xmax}}% |
| 105 |
\fi |
| 106 |
\edef\pgfplots@loc@TMPa{% |
| 107 |
(axis cs:\pgfplotsretval,#1) |
| 108 |
rectangle (axis cs:\pgfplotsretvalb,#2)}% |
| 109 |
\pgfkeysalso{/tikz/soft clip assign/path/.expand once=\pgfplots@loc@TMPa}% |
| 110 |
}{}% |
| 111 |
},% |
| 112 |
% |
| 113 |
/pgfplots/execute at begin axis@@/.add={}{% |
| 114 |
\gdef\b@pgfplotsfillbetween@list@has@set@layers{0}% |
| 115 |
}, |
| 116 |
} |
| 117 |
|
| 118 |
% The options are from |
| 119 |
% \addplot[#1] fill between[#2] #3; |
| 120 |
% |
| 121 |
% This method is invoked from the global \addplot dispatching. |
| 122 |
\def\pgfplotslibraryfillbetween@addplot#1#2#3{% |
| 123 |
% |
| 124 |
\pgfplots@start@plot@with@behavioroptions{/pgfplots/every fill between plot,#1}% |
| 125 |
% |
| 126 |
% '/tikz/fill between/on layer' is not really integrated into the option processing of pgfplots. |
| 127 |
% improve it here (to some extend): |
| 128 |
\pgfkeysgetvalue{/pgfplots/on layer}\pgfplots@loc@TMPa |
| 129 |
\pgfkeyslet{/tikz/fill between/on layer}{\pgfplots@loc@TMPa}% |
| 130 |
% |
| 131 |
% FIXME: is this here an accident!? |
| 132 |
\pgfkeysvalueof{/pgfplots/execute at end survey}% |
| 133 |
\pgfplots@remember@survey@option@list |
| 134 |
% |
| 135 |
% |
| 136 |
\pgfplotslibraryfillbetweenpreparecurrentlayer |
| 137 |
% |
| 138 |
\pgfplots@addplot@enqueue@coords{% |
| 139 |
% precmd |
| 140 |
}{% |
| 141 |
% empty - this here is a TikZ instruction, not a "real" \addplot command |
| 142 |
}{% |
| 143 |
\def\b@pgfplots@fillbetween@clip@on@layer{0}% |
| 144 |
\pgfkeysgetvalue{/tikz/fill between/on layer}\pgfplots@loc@TMPa |
| 145 |
\edef\pgfplots@fillbetween@layer{\pgfplots@loc@TMPa}% |
| 146 |
\ifx\pgfplots@fillbetween@layer\pgfutil@empty |
| 147 |
\else |
| 148 |
\ifpgfplots@clip |
| 149 |
\def\b@pgfplots@fillbetween@clip@on@layer{1}% |
| 150 |
% This here is messy: we have to clip on the other |
| 151 |
% layer. I gave up to install this clip path once |
| 152 |
% per axis, that's why I do it for each individual |
| 153 |
% fillbetween - let us hope this is not too |
| 154 |
% inefficient. |
| 155 |
% |
| 156 |
% I had to give up because the result appears to |
| 157 |
% require a pattern of sorts |
| 158 |
% \onlayer{L} |
| 159 |
% \scope |
| 160 |
% \clip ... ; |
| 161 |
% \endonlayer |
| 162 |
% ... |
| 163 |
% |
| 164 |
% \onlayer{L} |
| 165 |
% \endscope |
| 166 |
% \endonlayer |
| 167 |
% |
| 168 |
% which is forbidden in TeX -- and using |
| 169 |
% \pgfsys@beginscope resulted in invalid PDF. |
| 170 |
% |
| 171 |
\pgfplotsonlayer{\pgfplots@loc@TMPa}% |
| 172 |
\scope |
| 173 |
\pgfplotspathaxisoutline |
| 174 |
\pgfusepath{clip}% |
| 175 |
\fi |
| 176 |
\fi |
| 177 |
% |
| 178 |
% the path instruction: |
| 179 |
\tikzfillbetween[#2]{% |
| 180 |
/pgfplots/.search also=/tikz,% |
| 181 |
/pgfplots/.cd,% |
| 182 |
/tikz/fill between/every last segment/.append style={/tikz/fill between/path after segment={#3}},% |
| 183 |
#1% |
| 184 |
}% |
| 185 |
% |
| 186 |
\if1\b@pgfplots@fillbetween@clip@on@layer |
| 187 |
\endscope |
| 188 |
\endpgfplotsonlayer% |
| 189 |
\fi |
| 190 |
}{% |
| 191 |
% post command. |
| 192 |
}% |
| 193 |
} |
| 194 |
|
| 195 |
% ONLY TO BE USED AS LATER OPTION! |
| 196 |
\pgfkeysdef{/pgfplots/fill between/@ensure layers}{% |
| 197 |
\ifpgfplots@layered@graphics |
| 198 |
\else |
| 199 |
\pgfplots@log3{fill between: activating layered graphics}% |
| 200 |
\pgfplotsset{set layers}% |
| 201 |
\pgfplots@set@options@of@layered@graphics |
| 202 |
\fi |
| 203 |
}% |
| 204 |
|
| 205 |
% SIDE-EFFECT: modifies \b@pgfplotsfillbetween@list@has@set@layers and |
| 206 |
\def\pgfplotslibraryfillbetweenpreparecurrentlayer{% |
| 207 |
\pgfkeysgetvalue{/tikz/fill between/on layer}\pgfplots@loc@TMPa |
| 208 |
\ifx\pgfplots@loc@TMPa\pgfutil@empty |
| 209 |
\else |
| 210 |
\if0\b@pgfplotsfillbetween@list@has@set@layers |
| 211 |
% |
| 212 |
\pgfplotssetlateoptions{fill between/@ensure layers}% |
| 213 |
\gdef\b@pgfplotsfillbetween@list@has@set@layers{1}% |
| 214 |
\fi |
| 215 |
\fi |
| 216 |
} |
| 217 |
|
| 218 |
\def\pgfplots@tikzfillbetween{\pgfutil@ifnextchar[{\pgfplots@tikzfillbetween@opt}{\pgfplots@tikzfillbetween@opt[]}} |
| 219 |
\def\pgfplots@tikzfillbetween@opt[#1]#2{% |
| 220 |
\pgfplots@addplot@enqueue@coords{% |
| 221 |
% precmd |
| 222 |
}{% |
| 223 |
% empty - this here is a TikZ instruction, not a "real" \addplot command |
| 224 |
}{% |
| 225 |
% the path instruction: |
| 226 |
\tikzfillbetween[#1]{#2}% |
| 227 |
}{% |
| 228 |
% post command. |
| 229 |
}% |
| 230 |
}% |
| 231 |
|
| 232 |
\expandafter\def\expandafter\pgfplots@replace@path@commands\expandafter{\pgfplots@replace@path@commands |
| 233 |
\let\pgfplots@orig@tikzfillbetween=\tikzfillbetween |
| 234 |
\let\tikzfillbetween=\pgfplots@tikzfillbetween |
| 235 |
}% |
| 236 |
\expandafter\def\expandafter\pgfplots@restore@path@commands\expandafter{\pgfplots@restore@path@commands |
| 237 |
\let\tikzfillbetween=\pgfplots@orig@tikzfillbetween |
| 238 |
} |
| 239 |
|
| 240 |
\endinput |