20

I'm want to include two graphs mentioned below in a document.

Bar Chart Pie Chart

I'm able to draw pie charts using pgf-pie. Is there any package which is capable of generating BAR & PIE Chart both? [Also which is included in default LATEX installation] ?

Pawan Mude
  • 2,921

2 Answers2

30

For bar charts you can use pgfplots With TikZ you can setup some macros so that you can write pie charts like this:

\begin{tikzpicture}
[
    pie chart,
    slice type={comet}{blu},
    slice type={legno}{rosso},
    slice type={coltello}{giallo},
    slice type={sedia}{viola},
    slice type={caffe}{verde},
    pie values/.style={font={\small}},
    scale=2
]

    \pie{2008}{73/comet,13/legno,7/sedia,7/coltello}
    \pie[xshift=2.2cm,values of coltello/.style={pos=1.1}]%
        {2009}{52/comet,23/legno,17/sedia,3/coltello,5/caffe}
    \pie[xshift=4.4cm,values of caffe/.style={pos=1.1}]%
        {2010}{56/comet,26/legno,9/sedia,7/coltello,2/caffe}

    \legend[shift={(0cm,-1cm)}]{{Comet (Pordenone)}/comet, {Wood and furniture (Livenza)}/legno, {Knife (Maniago)}/coltello}
    \legend[shift={(3cm,-1cm)}]{{Chair (Manzano)}/sedia, {Coffee (Trieste)}/caffe}

\end{tikzpicture}

Obtaining

Pie chart example

The macros \pie and \legend and all the keys needed can be defined as follows:

\definecolor{rosso}{RGB}{220,57,18}
\definecolor{giallo}{RGB}{255,153,0}
\definecolor{blu}{RGB}{102,140,217}
\definecolor{verde}{RGB}{16,150,24}
\definecolor{viola}{RGB}{153,0,153}

\makeatletter

\tikzstyle{chart}=[
    legend label/.style={font={\scriptsize},anchor=west,align=left},
    legend box/.style={rectangle, draw, minimum size=5pt},
    axis/.style={black,semithick,->},
    axis label/.style={anchor=east,font={\tiny}},
]

\tikzstyle{bar chart}=[
    chart,
    bar width/.code={
        \pgfmathparse{##1/2}
        \global\let\bar@w\pgfmathresult
    },
    bar/.style={very thick, draw=white},
    bar label/.style={font={\bf\small},anchor=north},
    bar value/.style={font={\footnotesize}},
    bar width=.75,
]

\tikzstyle{pie chart}=[
    chart,
    slice/.style={line cap=round, line join=round, very thick,draw=white},
    pie title/.style={font={\bf}},
    slice type/.style 2 args={
        ##1/.style={fill=##2},
        values of ##1/.style={}
    }
]

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\newcommand{\pie}[3][]{
    \begin{scope}[#1]
    \pgfmathsetmacro{\curA}{90}
    \pgfmathsetmacro{\r}{1}
    \def\c{(0,0)}
    \node[pie title] at (90:1.3) {#2};
    \foreach \v/\s in{#3}{
        \pgfmathsetmacro{\deltaA}{\v/100*360}
        \pgfmathsetmacro{\nextA}{\curA + \deltaA}
        \pgfmathsetmacro{\midA}{(\curA+\nextA)/2}

        \path[slice,\s] \c
            -- +(\curA:\r)
            arc (\curA:\nextA:\r)
            -- cycle;
        \pgfmathsetmacro{\d}{max((\deltaA * -(.5/50) + 1) , .5)}

        \begin{pgfonlayer}{foreground}
        \path \c -- node[pos=\d,pie values,values of \s]{$\v\%$} +(\midA:\r);
        \end{pgfonlayer}

        \global\let\curA\nextA
    }
    \end{scope}
}

\newcommand{\legend}[2][]{
    \begin{scope}[#1]
    \path
        \foreach \n/\s in {#2}
            {
                  ++(0,-10pt) node[\s,legend box] {} +(5pt,0) node[legend label] {\n}
            }
    ;
    \end{scope}
}

This code needs polishing and is not very general but I am sharing it as I wrote it for a document I helped typeset. If you like it we can work out a better version.

azetina
  • 28,884
Bordaigorl
  • 15,135
  • Thanks for detail reply. Can you please suggest how to add legend in bar chart mentioned below:

    \begin{tikzpicture} \begin{axis}[ title=Title, xbar, xmajorgrids = true, bar width=6mm, width=12cm, height=5.5cm, enlarge y limits=0.2, xlabel={#number}, symbolic y coords={A,B,C,D}, ytick=data, nodes near coords, nodes near coords align={horizontal}, ]

    \addplot coordinates {(1,A) (7,B) (5,C)(2,D)}; \end{axis} \end{tikzpicture}

    – Pawan Mude Sep 27 '13 at 19:22
  • 1
    well, you could use my \legend macro and put it right before the \end{tikzpicture}. – Bordaigorl Sep 27 '13 at 21:40
  • 1
    By the way this was quick and dirty code I put together some time ago, if anybody has suggestions on how to make it better, I'd be interested to hear them! – Bordaigorl Sep 27 '13 at 21:42
  • follow up question: https://tex.stackexchange.com/questions/157309/pie-chart-with-values-instead-of-percentage – endo.anaconda Feb 03 '14 at 11:49
  • I have a few items with a small percentage, and the labels are overlapping. how can I adjust this? – leonardo cesar Jun 23 '14 at 19:12
  • @user42634 see how I did that for the second pie, the value of coltello is 3% and I shift the label to make it not overlapping – Bordaigorl Jun 23 '14 at 20:21
  • @bordaigorl, In this example, only the coltello label is away, in my case is 3 or more. can I paste my code here? – leonardo cesar Jun 24 '14 at 11:22
  • @user42634 I advise you to open a new question if you need more specific advice...(and in the process change your nickname to something more memorable, you might like it here =) ) – Bordaigorl Jun 24 '14 at 13:34
  • I copied the legend definition, but squares in not filled. What is the possible reason? – Gurebu Bokofu Jan 01 '15 at 07:08
  • @GurebuBokofu have you defined the slice types and imported the style definitions? – Bordaigorl Jan 01 '15 at 21:30
  • Yes, of corse. I try to do my own pie diagramm once more and got desired result, but could not find reason of previous mistake. Some kind of hard-to-find one-symbol mistake, I can suppose. – Gurebu Bokofu Jan 02 '15 at 02:07
  • I know this was a long time ago but I'm wondering if someone could help. This is a wonderful solution which I am not making use of (thanks). But I would like to make some without the values on the pie chart. How do I do that? – JSharpee Aug 25 '17 at 19:11
  • @JSharpee simply remove the part in the pgfonlayer environment. If you have more requirements write a separate question – Bordaigorl Aug 26 '17 at 05:17
  • If you want to remove 0%'s, just add \ifdim0 pt=\v pt \else \begin{pgfonlayer}{...} \fi – Alfie Apr 25 '20 at 10:57
2

With PSTricks

\documentclass[pstricks,border=3pt]{standalone}
\degrees[100]
\newcounter{counter}
\SpecialCoor
\newcommand\data[2][gray]{%
    \pswedge[fillstyle=solid,fillcolor=#1,opacity=.5](0,0){4}{!\thecounter}{!\thecounter\space #2 add}%
    \uput{2}[!#2 2 div \thecounter\space add](0,0){#2\%}%
    \addtocounter{counter}{#2}%
}

\begin{document}
\begin{pspicture}(-4.5,-4.5)(4.5,4.5)
    \data[red]{10}
    \data[orange]{40}
    \data[yellow]{30}
    \data[blue]{20}
\end{pspicture}
\end{document}

enter image description here