summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/pgfplotsstackedplots.code.tex
blob: 2276f571db92d16f16e84173b46a7dff907e3eae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
%--------------------------------------------
%
% Package pgfplots
%
% Provides a user-friendly interface to create function plots (normal
% plots, semi-logplots and double-logplots).
% 
% It is based on Till Tantau's PGF package.
%
% Copyright 2007/2008 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%--------------------------------------------

%
% This file contains the implementation for stacked plots.
%
% Stacked plots always keep record of the last plotted coordinates.
% Any new plot will be ADDED on top of the last plotted coordinates.
% 
% Terminology: "last plotted coordinates" are called "zero levels"
% because they actually work like shifts.
%
% Programming Structure:
%
% 1. We keep TWO lists of coordinates: a list of CURRENT zero level
% coordinates and a list of NEXT zero level coordinates.
%
% The first one will be queried whenever a zero level coordinate is
% requested.
%
% The second one will be used to form zero levels for the next plot.
%
% 2. At the beginning and end of each plot, the lists in 1.) are
% initialised properly.
%
% 3.1 While plot coordinates are processed during the survey phase, the following methods
% interact with the stacked API:
% \pgfplots@stacked@preparepoint@inmacro
% 	-> compute the 'stacked' sum.
% 	This may need to be done with floating point arithmetics because
% 	the data scaling trafo is not yet initialised
% \pgfplots@stacked@rememberzerolevelpoint@for@next@plot
% \pgfplots@stacked@getnextzerolevelpoint
%
%
% 3.2 during the final visualization phase, we have
% \pgfplots@stacked@finishpoint
% 	-> takes coordinates as they will be given to Tikz. This method is
% 	used to 
% 		- communicate zero level coordinates to Tikz
% 		- implement the 'closed paths' option (allows filled stacked plots).
%
% 4. Zero levels are communicated to Tikz by
% \pgfplots@stacked@initzerolevelhandler. This routine initialises an
% input stream for Tikz plot handlers which produces a sequence of
% zero levels. It is used by [xy]comb and [xy]bar.
%
%

\let\pgfplots@stacked@zerolevelpoint@x=\pgfutil@empty
\let\pgfplots@stacked@zerolevelpoint@y=\pgfutil@empty
\newif\ifpgfplots@stacked@isfirstplot
\newif\ifpgfplots@stacked@isinitialised

% Pre-initialisation.
% Needs to be called before the first call to
% \pgfplots@stacked@beginplot.
\def\pgfplots@stacked@initialise{%
	\gdef\pgfplots@stacked@coordcount{-1}%
	\pgfplots@stacked@isfirstplottrue
	\pgfplots@stacked@isinitialisedtrue
}%

% Cleanup method. Truncates any global variables to reduce string
% space.
\def\pgfplots@stacked@finalize{%
	\global\pgfplotslistnewempty\pgfplots@stacked@zerolevellist
	\global\pgfplotslistnewempty\pgfplots@stacked@nextzerolevellist
	\pgfplots@stacked@isinitialisedfalse
}%

% (Re)defines the macro \pgfplots@stacked@getnextzerolevelpoint
% at the beginning of each plot.
%
% The macro \pgfplots@stacked@getnextzerolevelpoint fills
% \pgfplots@stacked@zerolevelpoint@[xy].
%
% ATTENTION: call \pgfplots@stacked@initialise before the first call
% of beginplot!
\def\pgfplots@stacked@beginplot{%
%\message{pgfplots@stacked@beginplot: PLOT STARTED.}%
	\ifpgfplots@stacked@isinitialised
	\else
		\pgfplots@error{LOGIC ERROR: please call \string\pgfplots@stacked@initialise.}%
	\fi
	\global\pgfplotslistnewempty\pgfplots@stacked@PGFzerolevels
	% accumulate this command here for \closedcycle:
	\let\pgfplots@stacked@closedcycle@impl=\pgfutil@empty
	\ifpgfplots@stacked@isfirstplot
		\global\pgfplotslistnewempty\pgfplots@stacked@zerolevellist
		% only work with float if its really necessary - for
		% example if the scaling trafo which maps to pgfmath is
		% not yet initialised.
		\ifpgfplots@datascaletrafo@initialised % FIXME : should be '!ifsurvey'
			\def\pgfplots@stacked@zerolevelpoint@x{0}%
			\def\pgfplots@stacked@zerolevelpoint@y{0}%
			\def\pgfplots@stacked@zerolevelpoint@z{0}%
		\else
			\pgfplotscoordmath{x}{parsenumber}{0}%
			\let\pgfplots@stacked@zerolevelpoint@x=\pgfmathresult
			\let\pgfplots@stacked@zerolevelpoint@y=\pgfmathresult
			\let\pgfplots@stacked@zerolevelpoint@z=\pgfmathresult
			%
			\pgfmathfloatcreate{0}{0.0}{0}%
			\ifpgfplots@xislinear\else	\let\pgfplots@stacked@zerolevelpoint@x=\pgfmathresult \fi
			\ifpgfplots@yislinear\else	\let\pgfplots@stacked@zerolevelpoint@y=\pgfmathresult \fi
			\ifpgfplots@zislinear\else	\let\pgfplots@stacked@zerolevelpoint@z=\pgfmathresult \fi
		\fi
		%
		\def\pgfplots@stacked@getnextzerolevelpoint{}% will remain constant anyway.
	\else
		{\globaldefs=1
		\pgfplotslistcopy\pgfplots@stacked@nextzerolevellist\to\pgfplots@stacked@zerolevellist
		}%
		\ifpgfplots@threedim
			\def\pgfplots@stacked@getnextzerolevelpoint{%
				\pgfplotslistcheckempty\pgfplots@stacked@zerolevellist
				\ifpgfplotslistempty
					\pgfplots@stacked@wrong@count@error
				\else
					{\globaldefs=1
					\pgfplotslistpopfront\pgfplots@stacked@zerolevellist\to\pgfmathresult
					}%
					\expandafter\pgfplots@stacked@parsezerolevelpoint@threedim\pgfmathresult\relax
				\fi
			}%
		\else
			\def\pgfplots@stacked@getnextzerolevelpoint{%
				\pgfplotslistcheckempty\pgfplots@stacked@zerolevellist
				\ifpgfplotslistempty
					\pgfplots@stacked@wrong@count@error
				\else
					{\globaldefs=1
					\pgfplotslistpopfront\pgfplots@stacked@zerolevellist\to\pgfmathresult
					}%
					\expandafter\pgfplots@stacked@parsezerolevelpoint\pgfmathresult\relax
				\fi
			}%
		\fi
	\fi
	\global\pgfplotslistnewempty\pgfplots@stacked@nextzerolevellist
}%

\def\pgfplots@stacked@parsezerolevelpoint(#1,#2)\relax{%
	\def\pgfplots@stacked@zerolevelpoint@x{#1}%
	\def\pgfplots@stacked@zerolevelpoint@y{#2}%
}
\def\pgfplots@stacked@parsezerolevelpoint@threedim(#1,#2,#3)\relax{%
	\def\pgfplots@stacked@zerolevelpoint@x{#1}%
	\def\pgfplots@stacked@zerolevelpoint@y{#2}%
	\def\pgfplots@stacked@zerolevelpoint@z{#3}%
}

\def\pgfplots@stacked@wrong@count@error{%
	\pgfplots@error{Sorry, pgfplots expects stacked plots to have exactly the same number of coordinates. Unfortunately, I encountered at plot with DIFFERENT NUMBERS OF COORDINATES. Please verify that 1. no point has been dropped by coordinate filters (for example log(0) or so) and 2. all plots have the same number of coordinates.}%
}%

\def\pgfplots@stacked@endplot{%
	\ifpgfplots@stacked@isfirstplot
		\let\pgfplots@stacked@closedcycle@impl=\pgfplots@path@closed@cycle@std
	\else
		\pgfplotslistcheckempty\pgfplots@stacked@zerolevellist
		\ifpgfplotslistempty
		\else
			\pgfplots@stacked@wrong@count@error
		\fi
		\t@pgfplots@tokc=\expandafter{\pgfplots@stacked@closedcycle@impl}%
		\edef\pgfplots@stacked@closedcycle@impl{%
			[mark=none,/utils/exec=\noexpand\pgfplots@try@mirror@plot@handler]
			--plot coordinates{\the\t@pgfplots@tokc}
			--cycle
		}%
	\fi
	\global\pgfplots@stacked@isfirstplotfalse
%\message{pgfplots@stacked@endplot: PLOT ENDED}%
}%

% WARNING: when this method is called, NEITHER
% \ifpgfplots@stacked@isfirstplot NOR the zero level lists are
% initialised!
\def\pgfplots@stacked@initzerolevelhandler{%
	\if\pgfplots@stacked@dir x
		\pgfplotxzerolevelstream@@list
		\pgfplotyzerolevelstreamconstant{\pgfplots@ZERO@y}%
	\else
		\pgfplotxzerolevelstreamconstant{\pgfplots@ZERO@x}%
		\pgfplotyzerolevelstream@@list
	\fi
}%

% #1: a point as (x,y) (or (x,y,z) )
\def\pgfplots@stacked@rememberzerolevelpoint@for@next@plot#1{%
	\edef\pgfplots@loc@TMPa{#1}%
	\expandafter\pgfplotslistpushbackglobal\pgfplots@loc@TMPa\to\pgfplots@stacked@nextzerolevellist
}

% PRECONDITION:
% 	Is in invoked inside of a coord preparation routine, that means
% 	- \pgfplots@current@point@[xyz] 
% 	- \ifpgfplots@curplot@threedim
% 	are all set properly.
\def\pgfplots@stacked@finishpoint{%
	\ifpgfplots@stacked@isfirstplot
	\else
		% FIXME : This needs to be converted to logical coords.
		%
		% FIXME : 3D is not supported by the low level plot interface (yet). 
		\pgfpointxy{\pgfplots@stacked@zerolevelpoint@x}{\pgfplots@stacked@zerolevelpoint@y}%
		\if\pgfplots@stacked@dir x
			\edef\pgfplots@loc@TMPa{\the\pgf@x}%
		\else
			\edef\pgfplots@loc@TMPa{\the\pgf@y}%
		\fi
		\expandafter\pgfplotslistpushbackglobal\pgfplots@loc@TMPa\to\pgfplots@stacked@PGFzerolevels
		\t@pgfplots@toka=\expandafter{\pgfplots@stacked@closedcycle@impl}%
		\edef\pgfplots@stacked@closedcycle@impl{%
			(\pgfplots@stacked@zerolevelpoint@x,\pgfplots@stacked@zerolevelpoint@y)%
			\the\t@pgfplots@toka}%
	\fi
}%

% PRECONDITION:
% 	Is in invoked inside of a coord preparation routine, that means
% 	- \pgfplots@current@point@[xyz] 
% 	- \ifpgfplots@curplot@threedim
% 	are all set properly.
%
% POSTCONDITION:
% 	- \pgfplots@current@point@[xyz] are adjusted.
\def\pgfplots@stacked@preparepoint@inmacro{%
	\pgfplots@stacked@getnextzerolevelpoint
	%
	\ifpgfplots@stacked@plus
		\def\pgfplots@stacked@op{add}%
	\else
		\def\pgfplots@stacked@op{subtract}%
	\fi
	\pgfplots@if{pgfplots@\pgfplots@stacked@dir islinear}{%
		\pgfplotscoordmath{\pgfplots@stacked@dir}{op}{\pgfplots@stacked@op}{%
			{\csname pgfplots@stacked@zerolevelpoint@\pgfplots@stacked@dir\endcsname}%
			{\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname}}%
	}{%
		% LOG. we need to compute log(zerolevel + current):
		\pgfplotscoordmath{\pgfplots@stacked@dir}{exp}{\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname}%
		\pgfmathfloatparsenumber\pgfmathresult
		\expandafter\let\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname=\pgfmathresult
		\csname pgfmathfloat\pgfplots@stacked@op @\endcsname
			{\csname pgfplots@stacked@zerolevelpoint@\pgfplots@stacked@dir\endcsname}%
			{\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname}%
	}%
	%
	% for logs, I remember just zerolevel+current; not its log.
	\expandafter\let\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname=\pgfmathresult
	\ifpgfplots@threedim
		\ifpgfplots@curplot@threedim
			\pgfplots@stacked@rememberzerolevelpoint@for@next@plot{(\pgfplots@current@point@x,\pgfplots@current@point@y,\pgfplots@current@point@z)}%
		\else
			\pgfplots@stacked@rememberzerolevelpoint@for@next@plot{(\pgfplots@current@point@x,\pgfplots@current@point@y,\pgfplots@logical@ZERO@z)}%
		\fi
	\else
		\pgfplots@stacked@rememberzerolevelpoint@for@next@plot{(\pgfplots@current@point@x,\pgfplots@current@point@y)}%
	\fi
	%
	\pgfplots@if{pgfplots@\pgfplots@stacked@dir islinear}{%
	}{%
		\pgfplotscoordmath{\pgfplots@stacked@dir}{log}{\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname}%
		\expandafter\let\csname pgfplots@current@point@\pgfplots@stacked@dir\endcsname=\pgfmathresult
	}%
}

% This here is a re-implementation of the stored plot processing.
%
% The idea is simple, although it requires quite some work:
%
% If we stack plots on top of each other, early drawing commands
% (early plots) will be OVERDRAWN by later drawing commands (later
% plots). This is especially unfortunate if we use filled bar plots
% or comb plots.
%
% IDEA: draw plots in REVERSE order. The positioning, styles and
% whatever must not be affected, only the sequence of drawing commands
% shall change.
%
% So, this command here does all numerics which is to be done and
% assembles a NEW, REVERSED STORED PLOT LIST.
\def\pgfplots@stacked@finalize@stored@plots{%
	\pgfplotslistnewempty\pgfplots@stored@plotlist@reversed
	\begingroup
	\pgfplotslistforeachungrouped\pgfplots@stored@plotlist\as\pgfplots@loc@TMPa{%
		\expandafter\pgfplots@stored@plotlist@EXTRACTENTRY\pgfplots@loc@TMPa
		\ifx\pgfplots@stored@current@cmd\pgfutil@empty
		\else
			% Apply the data scaling transformation and compute
			% stacked coordinates, but do NOT
			% issue any drawing commands:
			\expandafter\pgfplots@stacked@finalize@stored@coords@START\pgfplots@stored@current@data
				\to
				{\pgfplots@stored@current@data}%
				{\pgfplots@stored@current@precmdappend}%
			% store preparation commands into "precmd":
			\t@pgfplots@tokb=\expandafter{\pgfplots@stored@current@precmd}%
			\t@pgfplots@toka=\expandafter{\pgfplots@stored@current@precmdappend}%
			\edef\pgfplots@stored@current@precmd{\the\t@pgfplots@tokb\the\t@pgfplots@toka}%
		\fi
		% Now, we need to insert all stored entities into the new,
		% reversed list. We only expand them ONCE.
		\t@pgfplots@toka=\expandafter{\pgfplots@stored@current@precmd}%
		\t@pgfplots@tokb=\expandafter{\pgfplots@stored@current@cmd}%
		\edef\pgfplots@loc@TMPa{%
			{\the\t@pgfplots@toka}{\the\t@pgfplots@tokb}}%
		\t@pgfplots@tokc=\expandafter{\pgfplots@loc@TMPa}%
		\t@pgfplots@toka=\expandafter{\pgfplots@stored@current@data}%
		\t@pgfplots@tokb=\expandafter{\pgfplots@stored@current@postcmd}%
		\edef\pgfplots@loc@TMPa{%
			\the\t@pgfplots@tokc
			{\the\t@pgfplots@toka}{\the\t@pgfplots@tokb}}%
		% Reverse sequence:
		\expandafter\pgfplotslistpushfront\pgfplots@loc@TMPa\to\pgfplots@stored@plotlist@reversed
	}%
	% Now, overwrite the original list:
	\global\let\pgfplots@stored@plotlist=\pgfplots@stored@plotlist@reversed
	\global\let\pgfplots@stored@plotlist@reversed=\relax
	\endgroup
}%

% This command finalizes the stored plot data. It records a coordinate
% stream into '#3' and assembles a macro '#4' which contains all
% preparation commands to ensure the functionality of
% \pgfplots@stacked@finalize@stored@plots
%
% The macro #4 will - if used as "precommand"
% befor a draw operation - restore all required options and performs
% pre-drawing.
\long\def\pgfplots@stacked@finalize@stored@coords@START normalized coordinates #1#2;\to#3#4{%
	\pgfplots@stacked@beginplot
	\pgfplots@coord@stream@INIT@finalize@stackedcoords@recordto{#3}{\pgfplots@recorded@marker@stream}%
	\pgfplots@coord@stream@foreach@NORMALIZED{#1}%
	\pgfplots@stacked@endplot
	\pgfplots@stacked@savestateto\pgfplots@loc@TMPa
	\t@pgfplots@toka=\expandafter{\pgfplots@loc@TMPa\def\pgfplots@stacked@stored@postpath{#2}}%
	\pgfplots@addplot@get@named@startendpoints@command\pgfplots@loc@TMPa
	\t@pgfplots@tokc=\expandafter{\pgfplots@loc@TMPa}%
	\ifpgfplots@record@marker@stream
		\t@pgfplots@tokb=\expandafter{\pgfplots@recorded@marker@stream}%
	\else
		\t@pgfplots@tokb={}%
	\fi
	\edef#4{%
		\the\t@pgfplots@tokc
		\noexpand\gdef\noexpand\pgfplots@recorded@marker@stream{\the\t@pgfplots@tokb}%
		\the\t@pgfplots@toka
	}%
}%

% #1: for coordinates
% #2: for markers
\def\pgfplots@coord@stream@INIT@finalize@stackedcoords@recordto#1#2{%
	\pgfplots@record@marker@streamtrue
	%
	\def\pgfplots@coord@stream@start@{%
		\gdef#1{\pgfplotstreamstart}%
		\ifpgfplots@record@marker@stream
			\pgfplotsapplistXnewempty\pgfplots@stacked@record@mark@cmds
			\pgfplotsapplistXpushback\pgfplotstreamstart\to\pgfplots@stacked@record@mark@cmds
		\fi
	}%
	\def\pgfplots@coord@stream@end@{%
		\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreamend}%
		\ifpgfplots@record@marker@stream
			\pgfplotsapplistXpushback\pgfplotstreamend\to\pgfplots@stacked@record@mark@cmds
			\pgfplotsapplistXlet\pgfplots@loc@TMPa=\pgfplots@stacked@record@mark@cmds
			\global\let#2=\pgfplots@loc@TMPa
		\fi
	}%
	%
	\begingroup
	\let\E=\noexpand
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	\pgfplots@coord@stream@INIT@finalize@storedcoords@prepare@scaletrafomacro
	% Will be inserted in one of two possible places below:
	\def\pgfplots@loc@TMPa{%
		\ifx\pgfplotspointmetainputhandler\pgfutil@empty
			% we don't have per-point meta data. Nothing special to
			% do:
			\E\edef\E\pgfmathresult{\E\noexpand\E\pgfplots@stream@{\E\the\E\c@pgfplots@coordindex}{\E\the\E\pgf@x}{\E\the\E\pgf@y}}%
			\E\expandafter\E\pgfplotsapplistXpushback\E\expandafter{\E\pgfmathresult}\E\to\E\pgfplots@stacked@record@mark@cmds%
		\else
			% Ok, then we need to process the meta data as well:
			\E\edef\E\pgfmathresult{\E\noexpand\E\pgfplots@stream@withmeta@{\E\the\E\c@pgfplots@coordindex}{\E\the\E\pgf@x}{\E\the\E\pgf@y}{\E\pgfplots@current@point@meta}}%
			\E\expandafter\E\pgfplotsapplistXpushback\E\expandafter{\E\pgfmathresult}\E\to\E\pgfplots@stacked@record@mark@cmds%
		\fi
	}%
	% This finalize command maps the logical coordinate into PGF's
	% point space. Furthermore, it collects marker coordinates
	% (properly clipped by position) if markers are required (see
	% above).
	%
	% It is prepared here to eliminate if's.
	\xdef\pgfplots@coord@stream@finalize@currentpt{%
		\ifpgfplots@curplot@threedim
			\E\pgfplotsqpointxyz{\E\pgfplots@current@point@x}{\E\pgfplots@current@point@y}{\E\pgfplots@current@point@z}%
		\else
			\ifpgfplots@threedim
				\E\pgfplotsqpointxyz{\E\pgfplots@current@point@x}{\E\pgfplots@current@point@y}{0.0}%
			\else
				\E\pgfplotsqpointxy{\E\pgfplots@current@point@x}{\E\pgfplots@current@point@y}%
			\fi
		\fi
		\E\edef\E\pgfmathresult{{\E\the\E\pgf@x}{\E\the\E\pgf@y}}%
		\E\t@pgfplots@toka=\E\expandafter{\E#1}%
		\E\t@pgfplots@tokb=\E\expandafter{\E\pgfmathresult}%
		\E\xdef\E#1{\E\the\E\t@pgfplots@toka
			\E\noexpand\E\pgfplots@stream\E\the\E\t@pgfplots@tokb}%
		%
		\ifpgfplots@record@marker@stream
			\E\pgf@xa=\E\pgfplots@current@point@x pt % FIXME : SCOPE REGISTERS!?
			\E\pgf@ya=\E\pgfplots@current@point@y pt %
			\ifpgfplots@curplot@threedim
				\E\pgf@yb=\E\pgfplots@current@point@z pt %
			\fi
			\E\ifdim\E\pgf@xa<\E\pgfplots@xmin@reg
			\E\else
				\E\ifdim\E\pgf@xa>\E\pgfplots@xmax@reg
				\E\else
					\E\ifdim\E\pgf@ya<\E\pgfplots@ymin@reg
					\E\else
						\E\ifdim\E\pgf@ya>\E\pgfplots@ymax@reg
						\E\else
							\ifpgfplots@curplot@threedim
								\E\ifdim\E\pgf@yb<\E\pgfplots@zmin@reg
								\E\else
									\E\ifdim\E\pgf@yb>\E\pgfplots@zmax@reg
									\E\else
										\pgfplots@loc@TMPa
									\E\fi
								\E\fi
							\else
								\pgfplots@loc@TMPa
							\fi
						\E\fi
					\E\fi
				\E\fi
			\E\fi
		\fi
	}%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	\endgroup
	%
	\def\pgfplots@coord@stream@coord@{%
		\pgfplotsaxisvisphasetransformcoordinate\pgfplots@current@point@x\pgfplots@current@point@y\pgfplots@current@point@z%
		\pgfplotsaxisvisphasepreparedatapoint
		\pgfplots@coord@stream@finalize@currentpt
	}%
}

% This command gets called after the sequence reversal has been done.
% It unpacks everything prepared by 
% \pgfplots@stacked@finalize@stored@coords@START
% and draws the plot (and markers).
\def\pgfplots@stacked@draw@reversed@plot{%
	\pgfplots@stored@current@cmd% [current plot style] <---- keys are already set
	\pgfextra
		\tikzset{every plot/.try}%
		\pgfplots@getcurrent@plothandler\pgfplots@basiclevel@plothandler
		\pgfplots@basiclevel@plothandler
		% this here contains a recorded plot stream, see above. 
		\pgfplots@stored@current@data
		%
		\pgfplots@assert@tikzinternal@exists{tikz@make@last@position}%
		\tikz@make@last@position{\pgfplotlastpoint}%  
	\expandafter
	\endpgfextra
	\pgfplots@stacked@stored@postpath
	;
	% this here has been assigned by the precommand as well.
	\ifx\pgfplots@recorded@marker@stream\pgfutil@empty
	\else
		\ifpgfplots@clip@marker@paths
			\scope% make sure that 'fill opacity' and 'dotted' styles remain local!
			\pgfplots@stored@current@cmd%[current plot style] <---- keys are already set
			\pgfextra 
				\pgfplots@install@plotmark@handler
				\pgfplots@recorded@marker@stream 
			\endpgfextra
			;
			\endscope
		\else
			\pgfplots@stored@REMEMBER@MARK@COMMAND
		\fi
	\fi
	\gdef\pgfplots@recorded@marker@stream{}% clear
}%


\def\pgfplots@stacked@path@closed@cycle{%
	\pgfplots@stacked@closedcycle@impl
}

% Saves the finalized variables into macro #1 such that a call to #1
% restores them.
%
% This affects pgf zero level handlers and whether this here is the
% first plot or not..
\def\pgfplots@stacked@savestateto#1{%
	\t@pgfplots@toka=\expandafter{\pgfplots@stacked@PGFzerolevels}%
	\ifpgfplots@stacked@isfirstplot
		\t@pgfplots@tokb={\pgfplots@stacked@isfirstplottrue}%
	\else
		\t@pgfplots@tokb={\pgfplots@stacked@isfirstplotfalse}%
	\fi
	\t@pgfplots@tokc=\expandafter{\pgfplots@stacked@closedcycle@impl}%
	\edef#1{%
		\noexpand\gdef\noexpand\pgfplots@stacked@PGFzerolevels{\the\t@pgfplots@toka}%
		\the\t@pgfplots@tokb
		\noexpand\def\noexpand\pgfplots@stacked@closedcycle@impl{\the\t@pgfplots@tokc}%
	}%
}

% PGF interfaces:
\def\pgfplotxzerolevelstream@@list{%
	\def\pgf@plotxzerolevelstreamstart{%
		\global\let\pgfplotxzerolevelstream@@list@@backup=\pgfplots@stacked@PGFzerolevels
		\gdef\pgf@plotxzerolevelstreamnext{%
			\pgfplotslistcheckempty\pgfplots@stacked@PGFzerolevels
			\ifpgfplotslistempty
				\global\pgf@x=\pgfplots@ZERO@x\relax
			\else
				{\globaldefs=1\relax
				\pgfplotslistpopfront\pgfplots@stacked@PGFzerolevels\to\pgfmathresult
				}%
				\global\pgf@x=\pgfmathresult\relax
			\fi
		}%
	}%
	\def\pgf@plotxzerolevelstreamend{%
		\global\let\pgfplots@stacked@PGFzerolevels=\pgfplotxzerolevelstream@@list@@backup
	}%
}%

\def\pgfplotyzerolevelstream@@list{%
	\def\pgf@plotyzerolevelstreamstart{%
		\global\let\pgfplotyzerolevelstream@@list@@backup=\pgfplots@stacked@PGFzerolevels
		\gdef\pgf@plotyzerolevelstreamnext{%
			\pgfplotslistcheckempty\pgfplots@stacked@PGFzerolevels
			\ifpgfplotslistempty
				\global\pgf@x=\pgfplots@ZERO@y\relax
			\else
				{\globaldefs=1\relax
				\pgfplotslistpopfront\pgfplots@stacked@PGFzerolevels\to\pgfmathresult
				}%
				\global\pgf@x=\pgfmathresult\relax
			\fi
		}%
	}%
	\def\pgf@plotyzerolevelstreamend{%
		\global\let\pgfplots@stacked@PGFzerolevels=\pgfplotyzerolevelstream@@list@@backup
	}%
}%