2

As stated in the title. I want to plot a bar which doesn't start from the y-axis(i.e., where x = 0). I try to use the bchart package but I fail to achieve this. For example, my latex code is like this:

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

and the result is as shown in the following figure. I want the bar A to start from 20, while bar B to start from 40. How should I work around it? Thank you in advance. enter image description here

  • No. With bchart they always start from x=0 unless you redefine bchart environment. Are you interested in pgfplots solution? –  Apr 11 '15 at 00:34
  • Yes, if pgfplots could do this. I am reading the documents of pgfplots, but I can't do it still. Can you give me some advice about that. Thank you. – Zongyi Zhao Apr 11 '15 at 01:02
  • @user11232: This thread is rather old, but were you offering a pgfplots solution back then? If so, then you have what I am looking for. Would you please post your approach? Thank you. – Harry Oct 03 '16 at 13:32

1 Answers1

2

Here you go. I have defined a new command \bccbar which takes an additional argument

\bccbar[text = B]{<end value>}{<Starting value>}

Code:

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

enter image description here