bchart에 사용자 정의 범례 추가

bchart에 사용자 정의 범례 추가

다음과 같이 막대 차트에 사용자 정의 범례를 삽입하려고 합니다.

여기에 이미지 설명을 입력하세요

불행하게도 이런 유형의 차트에 사용된 범례에 대한 예를 전혀 찾지 못했습니다. 이것이 가능한가?

내 코드는 다음과 같습니다

\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} 

답변1

bchart을 사용 하므로 tikz후자를 사용하여 범례를 그릴 수 있습니다. x축 위에 범례를 오른쪽으로 정렬하려면 몇 가지 계산이 필요합니다. 또한 코드는 길이가 다른 텍스트를 설명해야 합니다.

아래 코드에는 \bclegend세 개의 인수가 있는 매크로가 정의되어 있습니다. 첫 번째는 선택 사항이며 범례의 막대 길이를 제공합니다. 기본값은 10mm입니다. 두 번째는 범례의 선 사이의 거리를 제공하며, 이는 x축과 범례 프레임 사이의 거리로도 사용됩니다. 세 번째 인수에는 색상과 텍스트가 제공됩니다. 한 줄에 한 쌍씩 있는 형식이어야 합니다 color1/text1,color2/text2,....

범례 선과 프레임 사이의 거리는 에서 정의됩니다 \bclldist. \renewcommand{\bclldist}{<some length>}(코드 뒤)를 사용하면 변경할 수 있습니다. 기본값은 1mm입니다.

\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} 

여기에 이미지 설명을 입력하세요

관련 정보