Graphics, Figures & TablesMacros for piechart, barchart, spiderweb

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
koubi
Posts: 2
Joined: Fri Sep 16, 2016 4:39 pm

Macros for piechart, barchart, spiderweb

Post by koubi »

Hi,

I spent few days trying to "macrofy" the rendering of piechart, barchart, and spiderweb from raw data input wihtin the document. I wanted some high level API like

Code: Select all

Code, edit and compile here:
\barchart
{5cm} %%label width
{ %% data (label/value)
apples/432,
banana/34,
pineapples/1223
}%%barchart
\spiderweb %% 0 to 100 grid
{4,5cm} %% radius for labels
{3,5cm} %% radius for spiderweb
{70} %% baseline score
{ %% data (label/score)
Fifty/10,
Sixty/20,
Seventy/50
}%%spiderweb
or
\def\colorCycleList{{"blue","red","yellow","green", "orange", "gray" }}
\piechart{
(Very Low,37),
(Low,23),
(Average,34),
(High,5),
(Very High,1)
}%%piechart
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The above is how I currently use the macros I implemented. I had to use xintForPair for the last one, maybe foreach can be use instead... well although the spiderweb rendering isn't that short (few seconds...), it's fine for me, and I just wanted to share.

Macro for piechart with example :

Code: Select all

Code, edit and compile here:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{fp}
\usepackage{pgfplots}
\usepackage{xintexpr}
\usepackage{xstring}
%% originaly used with babel french, which causes some issues
%% wihout the following line
\usetikzlibrary{babel}
\def\colorCycleList{{"blue","red","yellow","green", "orange", "gray" }}
%%
%% #1 = list of titles and values (not percents)
%%
%% example :
%%
%% \piechart{
%% (Very Low,37),
%% (Low,23),
%% (Average,34),
%% (High,5),
%% (Very High,1)
%% }
%%
\newcounter{totalPieChart}
\newcommand{\piechart}[1]{
%compute total in order to compute percentages
\setcounter{totalPieChart}{0}
\xintForpair ##1##2 in{#1} \do {\addtocounter{totalPieChart}{##2}}
%get the number of colors in the \colorCycleList
\pgfmathparse{int(dim(\colorCycleList)-1)}
\edef\cycleColorMax{\pgfmathresult}
\begin{tikzpicture}[nodes = {font=\sffamily}, scale=1]
\def\angle{0}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
pie-chart-1.png
pie-chart-1.png (17.6 KiB) Viewed 4085 times
pie-chart-2.png
pie-chart-2.png (35.81 KiB) Viewed 4085 times
Macro for spiderweb with example:

Code: Select all

Code, edit and compile here:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
%% originaly used with babel french, which causes some issues
%% wihout the following line
\usetikzlibrary{babel}
%%
%% #1 = radius (4cm is good)
%% #2 = radius for labels (3.5cm)
%% #3 = score baseline (blue line)
%% #4 = list of titles and values
%%
%% example
%%
%% \spiderweb{4,5cm}{3,5cm}{70}{
%% Sécurité/10,
%% Maintenabilité/30,
%% Fiabilité/50,
%% Portabilité/70,
%% Performance/90
%% }
%%
\newcommand{\spiderweb}[4]{
\newcount\numberOfDimensionsForSpiderWebCounter \numberOfDimensionsForSpiderWebCounter=0
\foreach \Title/\Score in {#4} {
\global\advance\numberOfDimensionsForSpiderWebCounter by 1 % Advance cyclecount
}
\def\numberOfDimensionsForSpiderWeb{\the\numberOfDimensionsForSpiderWebCounter} % number of dimensions (config option)
\def\numberOfScaleUnits{100} % number of scale units (config option)
\def\calculatedAngleBetweenDimAxes{360/\numberOfDimensionsForSpiderWeb} % calculated angle between dimension axes
\newdimen\R % maximal diagram radius (config option)
\newdimen\L % radius to put dimension labels (config option)
\L=#1
\R=#2
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
spiderweb-chart-1.png
spiderweb-chart-1.png (39.83 KiB) Viewed 4085 times
spiderweb-chart-2.png
spiderweb-chart-2.png (43.08 KiB) Viewed 4085 times
Macro for barchart with example:

Code: Select all

Code, edit and compile here:
\documentclass{report}
\usepackage[left=20mm,right=20mm, top=30mm, bottom=30mm,a4paper]{geometry}
\usepackage[usenames,dvipsnames,table,xcdraw]{xcolor}
\usepackage{pgfplots}
\usepackage{setspace}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{wallpaper}
\usepackage{xintexpr}
\usetikzlibrary{shadows}
%% originaly used with babel french, which causes some issues
%% wihout the following line
\usetikzlibrary{babel}
\makeatletter
\newcommand*{\getlengthincm}[1]{\strip@pt\dimexpr0.035146\dimexpr#1\relax\relax}
\makeatother
\newcommand{\nume}[1]{\num[group-separator=\text{~},output-decimal-marker={,},group-digits=integer,group-minimum-digits=4]{#1}}
%%
%% #1 = label width
%% #2 = list of titles and values
%%
%% example:
%%
%% \barchart{6cm}{
%% apples/2047,
%% pears/1244,
%% banana/1208,
%% {a fruit name with slashes / well / ...}/1055,
%% {a fruit name}/9009
%% }
%%
\newcommand{\barchart}[2]{
%% reorder list (thanks \foreach...)
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Image

Feel free to use and comment.

Best regards.
Last edited by koubi on Mon Sep 19, 2016 9:59 am, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Macros for piechart, barchart, spiderweb

Post by Stefan Kottwitz »

Hi koubi,

welcome to the forum!

Very interesting macros. Thank you for sharing! I added images to your post, so readers can immediately see the examples as compiled images too.

When I tested the bar chart example, I got

! Undefined control sequence.
\XINT_expr_scanfunc_panic ...rror:bigtroubleahead
(0\relax
l.94 }


but I must admit that I tested it on a PC with a TeX installation of 2014. I will test it again on TeX Live 2016 at home.

Stefan
LaTeX.org admin
koubi
Posts: 2
Joined: Fri Sep 16, 2016 4:39 pm

Re: Macros for piechart, barchart, spiderweb

Post by koubi »

I compiled that on archlinux with texlive from repo. It also works with latest stable miktex on windows.
Post Reply