Finally works

This commit is contained in:
Drudoo 2014-05-20 21:36:19 +02:00
parent 18f8b6f7db
commit f696c095c1
5 changed files with 377 additions and 0 deletions

171
barchart/barchart.sty Normal file
View File

@ -0,0 +1,171 @@
% this is my first package
%
% (c) Frederik Vanggaard
%
% Inspired by bchart version 0.1.2 by Tobias Kuhn
%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
%
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{barchart}
[2014/05/19 v0.01 LaTeX package for bar charts]
\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{calc}
\newlength{\barcpos}
\newcommand{\tempWidth}{}
% barchart args:
% #1: max
% #2: min
% #3: steps
% #4: step
% #5: width
% #7: height
% #8: scale
% #9: axis label
\newcommand{\barcmin}{}
\newcommand{\barcmax}{}
\newcommand{\barcsteps}{}
\newcommand{\barcstep}{}
\newlength{\barctotalWidth}
\newcommand{\ypos}{}
\newcommand{\scale}{}
\newcommand{\barcy}{}
\makeatletter
\define@key{barchart}{min}{\renewcommand{\barcmin}{#1}}
\define@key{barchart}{max}{\renewcommand{\barcmax}{#1}}
\define@key{barchart}{steps}{\renewcommand{\barcsteps}{#1}}
\define@key{barchart}{step}{\renewcommand{\barcstep}{#1}}
\define@key{barchart}{width}{\setlength{\barctotalWidth}{#1}}
\define@key{barchart}{height}{\renewcommand{\ypos}{#1}}
\define@key{barchart}{scale}{\renewcommand{\scale}{#1}}
\define@key{barchart}{label}{\renewcommand{\barcy}{#1}}
\makeatother
%barc args:
% #1: color
% #2: width
% #3: plain (true/false)
% #4: label
% #5: rotation (true/false)
\newcommand{\barccolor}{}
\newcommand{\barcwidth}{}
\newcommand{\barcplain}{}
\newcommand{\barclabel}{}
\newcommand{\barcrotation}{}
\makeatletter
\define@key{barc}{color}{\renewcommand{\barccolor}{#1}}
\define@key{barc}{width}{\renewcommand{\barcwidth}{#1}}
\define@key{barc}{plain}[false]{\renewcommand{\barcplain}{true}}
\define@key{barc}{label}{\renewcommand{\barclabel}{#1}}
\define@key{barc}{rotation}[false]{\renewcommand{\barcrotation}{true}}
\makeatother
%skip args:
% #1: size
\newcommand{\barcskiplabel}{}
\makeatletter
\define@key{barcskip}{label}{\renewcommand{\barcskiplabel}{#1}}
\makeatother
\newenvironment{barchart}[1][]{%
\newcommand{\barc}[2][] {%
\renewcommand{\barccolor}{blue!20}
\renewcommand{\barcwidth}{30pt}
\renewcommand{\barcplain}{false}
\renewcommand{\barclabel}{}
\renewcommand{\barcrotation}{false}
\addtolength{\barcpos}{5mm}
\setkeys{barc}{##1}
%\fill[color=\barccolor,fill,draw](\barcpos,0) rectangle (\barcwidth+\barcpos,##2);
\fill[color=\barccolor, fill,draw](\barcpos,0) rectangle ($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$);
\draw (\barcpos,0) rectangle ($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$);
%\node[draw, shape=circle,fill,\barccolor] at (\barcpos+\barcwidth-5mm,0){};
%\node[draw, shape=circle,fill,\barccolor] at ($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$){##2};
\ifthenelse{\equal{\barcplain}{true}}{}{
% Write value:
%\node[anchor=east] at ($(\barcpos+(\barcwidth/3,0) + (0,##2+0.5)$){##2};
% $##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-2.5mm)$
\node[] at ($($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$)!0.5!(\barcpos,##2+0.25+\barcmax/10) $){##2};
}
\ifthenelse{\equal{\barcrotation}{true}}{false}{
\node[label={[label distance=0.5cm,text depth=-1ex,rotate=-90]right:\barclabel}] at ($($\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$)!0.5!(\barcpos,1.5) $) {};
}
\ifthenelse{\equal{\barcrotation}{false}}{true}{
\node[label={[label distance=0.5cm,text depth=-1ex,rotate=-45]right:\barclabel}] at ($($\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-10mm,0)$)!0.5!(\barcpos,1) $) {};
}
\addtolength{\barcpos}{\dimexpr(\barcwidth-20pt) \relax}
}
\newcommand{\barcskip}[2][]{
\renewcommand{\barcskiplabel}{}
\setkeys{barcskip}{##1}
\addtolength{\barcpos}{##2}
}
\renewcommand{\barcmin}{0}
\renewcommand{\barcmax}{10}
\renewcommand{\barcstep}{1}
\renewcommand{\ypos}{1em}
\renewcommand{\scale}{1}
\renewcommand{\smallskip}[1][]{\barcskip[##1]{2.5mm}}
\renewcommand{\medskip}[1][]{\barcskip[##1]{5.0mm}}
\renewcommand{\bigskip}[1][]{\barcskip[##1]{7.5mm}}
\newcommand{\barcrange}{\bcstripunit{\dimexpr\barcmax pt-\barcmin pt\relax}}
\renewcommand{\barcstep}{\barcrange}
\renewcommand{\barcsteps}{0,\barcstep,...,\barcrange}%
\setkeys{barchart}{#1}
\begin{tikzpicture}[y=\ypos, scale = \scale]
} {
\draw (0,0) -- (\barcpos+\barcwidth,0);
\draw (0,0) -- ($(0,(\barcmax-\barcmin)$);
\draw (0,0) -- (0-1mm,0);
\node[anchor=east] (n) at (0-1mm,0) {\bcstripunit{\dimexpr\barcmin pt\relax}};
\coordinate (labelpos) at (n.east);
\foreach \x in \barcsteps {
\ifthenelse{\equal{\x}{0}}{}{
\draw ($\x*(0,\barcmax/\barcrange) + (0,0)$) -- ($\x*(0,\barcmax/\barcrange) + (0-1mm,0)$);
\node[anchor=east] at (-1mm,\x){\bcstripunit{\dimexpr\barcmin pt+\x pt\relax}};
}
}
\ifthenelse{\equal{\barcy}{}}{}{
%\node[label={[label distance=0.5cm,align=center,text depth=-1ex,rotate=-90]left:\barcy}] at ($(-0.75,\barcmax) !0.5! (0,\barcmin)$){};
\node[rotate=-90] at ($(-0.75-\barcmax/100,\barcmax) !0.5! (0,\barcmin)$){\barcy};
}
\end{tikzpicture}%
}
\makeatletter
\newcommand*{\bcstripunit}[1]{\strip@pt#1}
\makeatother
%%
%% End of file `barchart.sty'.

BIN
barchart/barchart_doc.pdf Normal file

Binary file not shown.

159
barchart/barchart_doc.tex Normal file
View File

@ -0,0 +1,159 @@
\documentclass[]{article}
\usepackage{barchart}
\title{\texttt{barchart}: Easy Bar Chart in \LaTeX
\medskip\\
\large Version 0.1.0
}
\author{Frederik Vanggaard}
\date{\today}
\begin{document}
\maketitle
\section{Introduction}
\texttt{barchart} is a \LaTeX\space package for creating simple and easy to use bar-chart. The package is heavily inspired by \texttt{bchart} by Tobias Kuhn. \texttt{barchart} provides an easy way to create vertical barcharts using Tikz.
\section{Charts}
\texttt{barchart} comes with a variety of customization options for both the overall chart and for each individual bar. A simple chart can be done like this:
\begin{quote}\small
\begin{verbatim}
\begin{barchart}
\barc{5}
\barc{7}
\barc{3}
\end{barchart}
\end{verbatim}
\end{quote}
\begin{quote}\small
\begin{figure}[ht]
\begin{barchart}
\barc{5}
\barc{7}
\barc{3}
\end{barchart}
\end{figure}
\end{quote}
The only arguments that are mandatory is the bar value. The charts can be changed using a few simple options such as \texttt{min}, \texttt{max} and \texttt{step}.
\begin{quote}\small
\begin{verbatim}
\begin{barchart}[min = 0, max = 60, step = 10, height=0.25em]
\barc{30}
\barc{20}
\barc{50}
\end{barchart}
\end{verbatim}
\end{quote}
\begin{quote}\small
\begin{figure}[ht]
\begin{barchart}[min = 0, max = 60, step = 10, height=0.25em]
\barc{30}
\barc{20}
\barc{50}
\end{barchart}
\end{figure}
\end{quote}
\texttt{height} is used to set the height of the whole chart. Omitting a height on charts with large \texttt{max} values will make the chart enormous.
\section{Bars}
Each bar can be customized to your liking. This can be done using \texttt{color}, \texttt{width} and \texttt{plain}.
\texttt{color} changes the color of each bar like:
\begin{quote}\small
\begin{verbatim}
\begin{barchart}[step = 2]
\barc[color=yellow!60]{5}
\barc[color=orange!60]{7}
\barc[color=cyan!60]{3}
\end{barchart}
\end{verbatim}
\end{quote}
\begin{quote}\small
\begin{figure}[ht]
\begin{barchart}[step = 2]
\barc[color=yellow!60]{5}
\barc[color=orange!60]{7}
\barc[color=cyan!60]{3}
\end{barchart}
\end{figure}
\end{quote}
\texttt{width} changes the width of each bar:
\begin{quote}\small
\begin{verbatim}
\begin{barchart}[step = 2]
\barc[width=40pt]{5}
\barc[width=30pt]{7}
\barc[width=50pt]{3}
\barc[width=20pt]{3}
\end{barchart}
\end{verbatim}
\end{quote}
\begin{quote}\small
\begin{figure}[ht]
\begin{barchart}[step = 2]
\barc[width=40pt]{5}
\barc[width=30pt]{7}
\barc[width=50pt]{3}
\barc[width=20pt]{3}
\end{barchart}
\end{figure}
\end{quote}
Using less than \texttt{20pt} is not recommended. Instead of using pre-defined step as the examples have shown so far, \texttt{steps} can be used.
\begin{quote}\small
\begin{verbatim}
\begin{barchart}[steps = {1,3,5,7,10}]
\barc{5}
\barc{7}
\barc{3}
\end{barchart}
\end{verbatim}
\end{quote}
\begin{quote}\small
\begin{figure}[ht]
\begin{barchart}[steps = {1,3,5,7,10}]
\barc{5}
\barc{7}
\barc{3}
\end{barchart}
\end{figure}
\end{quote}
\texttt{plain} is a boolean which can either be \texttt{true} or \texttt{false} and removes the label on the bar.
\begin{quote}\small
\begin{verbatim}
\begin{barchart}
\barc{5}
\barc[plain=true]{7}
\barc[plain=true]{3}
\end{barchart}
\end{verbatim}
\end{quote}
\begin{quote}\small
\begin{figure}[ht]
\begin{barchart}
\barc{5}
\barc[plain=true]{7}
\barc[plain=true]{3}
\end{barchart}
\end{figure}
\end{quote}
\section{Skips}
\section{Labels}
\section{Scaling}
\section{Known Issues}
\end{document}

Binary file not shown.

View File

@ -0,0 +1,47 @@
\documentclass[]{article}
\usepackage{barchart}
\begin{document}
\section{Bar Chart Example}
\begin{figure}[ht]
\begin{barchart}[min = 0, max = 10, step = 2, height = 0.75em, scale = 2, label=Something nice]
\barc[label = test, rotation = true, width = 20pt, color = blue!60]{1}
\barcskip{5pt}
\barc[width = 20pt, color = cyan!60]{3}
\barcskip{5pt}
\barc[width = 20pt, color = yellow!60]{7}
\barcskip{5pt}
\barc[label = a long label, rotation = true,width = 20pt, color = orange!60]{5}
\barcskip{5pt}
\barc[width = 20pt, color = red!60]{3}
\barcskip{5pt}
\barc[color = magenta!60]{6}
\barcskip{5pt}
\barc[color = green!60]{6}
\barcskip{5pt}
\barc[label = lol,color = green!60]{9}
\barcskip{5pt}
\barc[color = green!60, plain=true]{3}
\barcskip{5pt}
\barc[color = green!60]{3}
\barcskip{5pt}
\barc[label = again?, rotation=true, color = green!60, plain=true]{3}
\barcskip{5pt}
\barc[color = green!60, label=a loooong label]{3}
\end{barchart}
\caption{A nice caption}
\end{figure}
\begin{figure}[ht]
\begin{barchart}[min = 0, max = 100, step = 10, height = 0.5ex, scale = 1, label=Something nice]
\barc[label = test, width = 20pt, color = blue!60]{50}
\barcskip{5pt}
\barc[width = 30pt, color = cyan!60]{75}
\barcskip{5pt}
\end{barchart}
\caption{A nice caption}
\end{figure}
\end{document}