How can I move certain nodes near coords in a bar chart depending on their value? [duplicate]

I want to write the values of bars in a bar chart (ybar) inside each bar, except for when there is not enough space. In that case, I want to write the value above the bar.

This is my code:

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usepackage{filecontents}% write data file to make make MWE self contained
\begin{filecontents*}{test.txt}
aa  bb  cc  dd
A   7304.91 12713.3 13748.95
B   13628.96    14912.36    19002.29
C   15158.99    29120.11    31566.62
D   23459.77    26894.28    39129.52
E   23095.17    45234.5 50103.37
F   35540.84    38491.29    55900.97
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\datatable
\begin{axis}[
ybar,
nodes near coords,
every node near coord/.append style={xshift=0.05cm,rotate=90,anchor=east,/pgf/number format/.cd,fixed zerofill,precision=2},
bar width= 0.4cm,
/pgf/number format/.cd,
use comma,
width=0.9\textwidth,
height=7cm,
ymajorgrids,
xtick={data},
scaled y ticks = false,
xticklabels from table={\datatable}{aa},
ymin=0,ymax=60000,
enlarge x limits = 0.1,
]
\end{axis}
\end{tikzpicture}
\end{document}

And this is the result:

What I would like now is for the values of the 1st to 5th and 7th bar to be written above the bars, not inside them. What to do?

The following measures the width of the nodes and make its anchor depend on whether it fits into the bar or not.

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usepackage{filecontents}% write data file to make make MWE self contained
\begin{filecontents*}{test.txt}
aa  bb  cc  dd
A   7304.91 12713.3 13748.95
B   13628.96    14912.36    19002.29
C   15158.99    29120.11    31566.62
D   23459.77    26894.28    39129.52
E   23095.17    45234.5 50103.37
F   35540.84    38491.29    55900.97
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\datatable
\begin{axis}[
ybar,
nodes near coords,
node near coords style={xshift=0.05cm,
/utils/exec={\setbox0\hbox{\pgfmathprintnumber\pgfplotspointmeta}
\pgfmathfloattomacro{\pgfplotspointmeta}{\F}{\M}{\E}
\pgfmathsetmacro{\myanchor}{ifthenelse(\M*pow(10,\E-3)*2-\the\wd0>0,"east","west")}
%\typeout{\M,\E,\the\wd0,\mysign,\myanchor}
},
rotate=90,anchor=\myanchor,
/pgf/number format/.cd,fixed zerofill,precision=2},
bar width= 0.4cm,
/pgf/number format/.cd,
use comma,
width=0.9\textwidth,
height=7cm,
ymajorgrids,
xtick={data},
scaled y ticks = false,
xticklabels from table={\datatable}{aa},
ymin=0,ymax=60000,
enlarge x limits = 0.1,
]
\end{axis}
\end{tikzpicture}
\end{document}

Note that this comparison \M*pow(10,\E-3)*2-\the\wd0>0 here is (in a way) chosen by hand because during the run it is not yet known that one unit is in pt. One could compute it but then this would require an iteration.

OLDER ANSWER: You could introduce a shift (xshift since we are doing it after rotate=90) for the nodes near coordinates. If I understand you correctly, this is different for different plots so we need to add it to each \addplot separately.

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usepackage{filecontents}% write data file to make make MWE self contained
\begin{filecontents*}{test.txt}
aa  bb  cc  dd
A   7304.91 12713.3 13748.95
B   13628.96    14912.36    19002.29
C   15158.99    29120.11    31566.62
D   23459.77    26894.28    39129.52
E   23095.17    45234.5 50103.37
F   35540.84    38491.29    55900.97
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\datatable
\begin{axis}[
ybar,
nodes near coords,
node near coords style={xshift=0.05cm,
rotate=90,anchor=east,
/pgf/number format/.cd,fixed zerofill,precision=2},
bar width= 0.4cm,
/pgf/number format/.cd,
use comma,
width=0.9\textwidth,
height=7cm,
ymajorgrids,
xtick={data},
scaled y ticks = false,
xticklabels from table={\datatable}{aa},
ymin=0,ymax=60000,
enlarge x limits = 0.1,
]
every node near coord/.append style={/tikz/xshift={
ifthenelse(\coordindex<3,1,0)*1.6cm}}
] table [y=bb,x expr=\coordindex] {\datatable};