Agregar leyenda personalizada a bchart

Agregar leyenda personalizada a bchart

Estoy intentando insertar una leyenda personalizada en un gráfico de barras, como esta:

ingrese la descripción de la imagen aquí

Lamentablemente, no encuentro ningún ejemplo de leyendas utilizadas con este tipo de gráficos. es posible?

Mi código es:

\documentclass{article}
\usepackage{bchart}
\begin{document}
\begin{bchart}[steps={20,40,60,80,100,120, 140},max=140]
\bcbar[label=Median]{132} \bcskip{5pt} % you can set the seperation between bars in the argument of \bcskip
\bcbar[label=Amplitudenmaximum]{116} \bcskip{5pt}
\bcbar[label=Effektivwert]{70} \bcskip{5pt}
\bcbar[label=Standardabweichung]{66} \bcskip{5pt}
\bcbar[label=Schiefe]{60} \bcskip{5pt}
\bcbar[label=Standardabweichung, color=black!50]{59} \bcskip{5pt}
\bcbar[label=Varianz]{57} \bcskip{5pt}
\bcbar[label=Schiefe, color=black!50]{54} \bcskip{5pt}
\bcbar[label=Gleichrichtswert]{52} \bcskip{5pt}
\bcbar[label=Amplitudenminimum]{50} \bcskip{-2.1pt}
\bcxlabel{Anzahl von Anwendungen}
\end{bchart}
\end{document} 

Respuesta1

Dado que bchartse utiliza tikz, este último se puede utilizar para dibujar la leyenda. Para alinear la leyenda sobre el eje x hacia la derecha, se requieren algunos cálculos. Además, el código debe tener en cuenta textos con diferentes extensiones.

En el código siguiente \bclegendse define una macro con tres argumentos. El primero es opcional y proporciona la longitud de una barra en la leyenda. El valor predeterminado es 10 mm. El segundo da la distancia entre las líneas de la leyenda, que también se utiliza como distancia entre el eje x y el marco de la leyenda. En el tercer argumento se dan los colores y los textos. Deben estar en el formulario color1/text1,color2/text2,...con un par por línea.

La distancia entre las líneas de la leyenda y el marco se define en \bclldist. Con \renewcommand{\bclldist}{<some length>}(después del código) esto se puede cambiar. El valor predeterminado es 1 mm.

\documentclass{article}
\usepackage{bchart}

\usetikzlibrary{fit}

\makeatletter
\newdimen\legendxshift
\newdimen\legendyshift
\newcount\legendlines
% distance of frame to legend lines
\newcommand{\bclldist}{1mm}
\newcommand{\bclegend}[3][10mm]{%
    % initialize
    \legendxshift=0pt\relax
    \legendyshift=0pt\relax
    \xdef\legendnodes{}%
    % get width of longest text and number of lines
    \foreach \lcolor/\ltext [count=\ll from 1] in {#3}%
        {\global\legendlines\ll\pgftext{\setbox0\hbox{\bcfontstyle\ltext}\ifdim\wd0>\legendxshift\global\legendxshift\wd0\fi}}%
    % calculate xshift for legend; \bcwidth: from bchart package; \bclldist: from node frame, inner sep=\bclldist (see below)
    % \@tempdima: half width of bar; 0.72em: inner sep from text nodes with some manual adjustment
    \@tempdima#1\@tempdima0.5\@tempdima
    \pgftext{\bcfontstyle\global\legendxshift\dimexpr\bcwidth-\legendxshift-\bclldist-\@tempdima-0.72em}
    % calculate yshift; 5mm: heigt of bar
    \legendyshift\dimexpr5mm+#2\relax
    \legendyshift\legendlines\legendyshift
    % \bcpos-2.5mm: from bchart package; \bclldist: from node frame, inner sep=\bclldist (see below)
    \global\legendyshift\dimexpr\bcpos-2.5mm+\bclldist+\legendyshift
    % draw the legend
    \begin{scope}[shift={(\legendxshift,\legendyshift)}]
    \coordinate (lp) at (0,0);
    \foreach \lcolor/\ltext [count=\ll from 1] in {#3}%
    {
        \node[anchor=north, minimum width=#1, minimum height=5mm,fill=\lcolor] (lb\ll) at (lp) {};
        \node[anchor=west] (l\ll) at (lb\ll.east) {\bcfontstyle\ltext};
        \coordinate (lp) at ($(lp)-(0,5mm+#2)$);
        \xdef\legendnodes{\legendnodes (lb\ll)(l\ll)}
    }
    % draw the frame
    \node[draw, inner sep=\bclldist,fit=\legendnodes] (frame) {};
    \end{scope}
}
\makeatother

\begin{document}
\begin{bchart}[steps={20,40,60,80,100,120, 140},max=140]
\bcbar[label=Median]{132} \bcskip{5pt} % you can set the seperation between bars in the argument of \bcskip
\bcbar[label=Amplitudenmaximum]{116} \bcskip{5pt}
\bcbar[label=Effektivwert]{70} \bcskip{5pt}
\bcbar[label=Standardabweichung]{66} \bcskip{5pt}
\bcbar[label=Schiefe]{60} \bcskip{5pt}
\bcbar[label=Standardabweichung, color=black!50]{59} \bcskip{5pt}
\bcbar[label=Varianz]{57} \bcskip{5pt}
\bcbar[label=Schiefe, color=black!50]{54} \bcskip{5pt}
\bcbar[label=Gleichrichtswert]{52} \bcskip{5pt}
\bcbar[label=Amplitudenminimum]{50} \bcskip{-2.1pt}
\bcxlabel{Anzahl von Anwendungen}
\bclegend{5pt}{black!50/Text AA,\bcbarcolor/Text B}
\end{bchart}
\end{document} 

ingrese la descripción de la imagen aquí

información relacionada