正如标题所述。我想绘制一个不从y-axis
(即 where x = 0
)开始的条形图。我尝试使用该bchart
包,但无法实现这一点。例如,我的 latex 代码如下:
\documentclass{article}
\usepackage{bchart}
\begin{document}
\begin{bchart}[max = 100, step = 10]
\bcbar[text = A]{45}
\smallskip
\bcbar[text = B]{26}
\smallskip
\bcbar[text = C]{31}
\end{bchart}
\end{document}
结果如下图所示。我想让 A 栏从 20 开始,B 栏从 40 开始。我应该如何解决?提前谢谢。
答案1
好了。我定义了一个新命令\bccbar
,它接受一个附加参数
\bccbar[text = B]{<end value>}{<Starting value>}
代码:
\documentclass{article}
\usepackage{bchart}
%-------------------------------------------
% Bars:
\newcommand{\bccbar}[3][]{
% Set defaults:
\renewcommand{\bcbarcolor}{blue!20}
\renewcommand{\bcbartext}{}
\renewcommand{\bcbarlabel}{}
\renewcommand{\bcbarvalue}{#2\bcunit}
\renewcommand{\bcplainbar}{false}
% Read parameters:
\setkeys{bcbar}{#1}
% Draw bar:
\fill[color=\bcbarcolor,fill,draw] ([xshift=#3*(\bcwidth/\bcrange)]0,\bcpos) rectangle ([xshift=#3*(\bcwidth/\bcrange)]$#2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-5mm)$);
\draw ([xshift=#3*(\bcwidth/\bcrange)]0,\bcpos) rectangle ([xshift=#3*(\bcwidth/\bcrange)]$#2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-5mm)$);
\ifthenelse{\equal{\bcplainbar}{true}}{}{
% Write value:
\node[anchor=west] at ([xshift=#3*(\bcwidth/\bcrange)]$#2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-2.5mm)$) {\bcfontstyle\bcbarvalue};
}
% Write text:
\node[anchor=west] at ([xshift=#3*(\bcwidth/\bcrange)]0,\bcpos-2.5mm) {\bcfontstyle\bcbartext};
% Write label:
\node[anchor=east] at ([xshift=#3*(\bcwidth/\bcrange)]0,\bcpos-2.5mm) {\bcfontstyle\bcbarlabel};
% Move vertical position downward:
\addtolength{\bcpos}{-5mm}
}%
%-------------------------------------------
\begin{document}
\begin{bchart}[max = 100, step = 10]
\bccbar[text = A]{45}{20}
\smallskip
\bccbar[text = B]{26}{40}
\smallskip
\bccbar[text = C]{31}{0}
\smallskip
\bcbar[text = D]{31}
\end{bchart}
\end{document}