% Copyright 2011 by Alain Matthes
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.   
%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %                                                               %
    %          tkz-kiviat.sty      encodage : utf8                  %
    %                                                               %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %                                                               %
    %           Créé par Alain Matthes le 07-02-2011                %
    %                                                               %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Objet :  kiviat chart or diagram

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tkz-kiviat}[2018/09/19 v0.1 b]
\ifx\e@alloc\@undefined
  \RequirePackage{etex}
\fi
\RequirePackage{tikz}
\usetikzlibrary{backgrounds}
%<--------------------------------------------------------------------------->
\makeatletter
\def\tkzutil@empty{} 
\newif\iftkz@kiv@table 
\pgfkeys{%
/kiviat/.cd,
space/.code            =  {\def\tkz@kiv@sp{#1}}, 
gap/.code              =  {\def\tkz@kiv@gap{#1}},
lattice/.code          =  {\def\tkz@kiv@lattice{#1}},
step/.code             =  {\def\tkz@kiv@step{#1}},
radial style/.style    =  {->,>=latex'},
lattice style/.style   =  {thin,lightgray},
label style/.style     =  {text width=2 cm,align=center},
label space/.code      =  {\def\tkz@kiv@space{#1}},  
/kiviat/.unknown/.code =  {\let\searchname=\pgfkeyscurrentname
                            \pgfkeysalso{\searchname/.try=#1,
                            /tikz/\searchname/.retry=#1}}  
                           } 
\def\tkzKiviatDiagram{\pgfutil@ifnextchar[{\tkz@KiviatDiagram}{%
                                            \tkz@KiviatDiagram[]}} 
\def\tkz@KiviatDiagram[#1]#2{% 

\pgfkeys{/kiviat/.cd,
gap          = .5,
lattice      = 10,
space        = .5,
step         = 1,
label space  = 1.5
}
\pgfqkeys{/kiviat}{#1}%
\begingroup
\foreach \x [count=\rang from 1] in {#2}{%
\global\let\tkz@kiv@radial\rang}% 
\foreach \x [count=\rang from 0] in {#2}{%
   \draw[/kiviat/radial style]
 (0,0)--(360/\tkz@kiv@radial*\rang:\tkz@kiv@lattice*\tkz@kiv@gap+\tkz@kiv@sp);
   \path
(0,0)--(360/\tkz@kiv@radial*\rang:\tkz@kiv@lattice*\tkz@kiv@gap+\tkz@kiv@space) node[/kiviat/label style] {\x}; 

\foreach \y in {0,1,...,\tkz@kiv@lattice}{
   \draw[/kiviat/lattice style]%
     (360/\tkz@kiv@radial*\rang:\y*\tkz@kiv@gap)--%
        (360/\tkz@kiv@radial*\rang+360/\tkz@kiv@radial:\y*\tkz@kiv@gap);
     }
}
\endgroup
}
%<--------------------------------------------------------------------------->
%<--------------------------------------------------------------------------->
\pgfkeys{/kiviatline/.cd,
fill/.code = {\global\def\tkz@kivl@fill{#1}},
opacity/.code = {\global\def\tkz@kivl@opacity{#1}}, 
/kiviatline/.unknown/.code ={\let\searchname=\pgfkeyscurrentname
                             \pgfkeysalso{\searchname/.try=#1,
                             /tikz/\searchname/.retry=#1}}   
}  
%<--------------------------------------------------------------------------->
\def\tkzKiviatLine{\pgfutil@ifnextchar[{\tkz@KiviatLine}{%
                                            \tkz@KiviatLine[]}} 
\def\tkz@KiviatLine[#1](#2,#3){% 
\begingroup
\pgfkeys{/kiviatline/.cd,
fill= {},
opacity=.5
}
\pgfqkeys{/kiviatline}{#1}%   opacity ??????
\ifx\tkzutil@empty\tkz@kivl@fill \else 
\begin{scope}[on background layer]
 \path[fill=\tkz@kivl@fill,opacity=\tkz@kivl@opacity] (360/\tkz@kiv@radial*0:#2*\tkz@kiv@gap*\tkz@kiv@step)   
\foreach \v [count=\rang from 1] in {#3}{%  
 -- (360/\tkz@kiv@radial*\rang:\v*\tkz@kiv@gap*\tkz@kiv@step) } -- (360/\tkz@kiv@radial*0:#2*\tkz@kiv@gap*\tkz@kiv@step); 
 \end{scope}
 \fi       
\draw[#1,opacity=1] (0:#2*\tkz@kiv@gap) plot coordinates {(360/\tkz@kiv@radial*0:#2*\tkz@kiv@gap*\tkz@kiv@step)}  
\foreach \v [count=\rang from 1] in {#3}{%  
 -- (360/\tkz@kiv@radial*\rang:\v*\tkz@kiv@gap*\tkz@kiv@step) plot coordinates {(360/\tkz@kiv@radial*\rang:\v*\tkz@kiv@gap*\tkz@kiv@step)}} -- (360/\tkz@kiv@radial*0:#2*\tkz@kiv@gap*\tkz@kiv@step);   
\endgroup
}%  
%<--------------------------------------------------------------------------->
%<--------------------------------------------------------------------------->
\pgfkeys{/kiviatgrad/.cd,
graduation distance/.code = \def\tkz@kiv@grad{#1},
prefix/.code  = \def\tkz@kiv@prefix{#1},
suffix/.code = \def\tkz@kiv@suffix{#1},
unity/.code = \def\tkz@kiv@unity{#1}, 
/kiviatgrad/.unknown/.code ={\let\searchname=\pgfkeyscurrentname
                             \pgfkeysalso{\searchname/.try=#1,
                             /tikz/\searchname/.retry=#1}}   
}
%<--------------------------------------------------------------------------->
\def\tkzKiviatGrad{\pgfutil@ifnextchar[{\tkz@KiviatGrad}{\tkz@KiviatGrad[]}} 
\def\tkz@KiviatGrad[#1](#2){% 
\begingroup
\pgfkeys{/kiviatgrad/.cd,
graduation distance= 0 pt,
prefix ={},
suffix={},
unity=1
 }
 \pgfqkeys{/kiviatgrad}{#1}% 
\let\tikz@label@distance@tmp\tikz@label@distance
\global\let\tikz@label@distance\tkz@kiv@grad
 \foreach \nv in {1,...,\tkz@kiv@lattice}{
 \pgfmathparse{\tkz@kiv@unity*\nv} 
 \pgfmathtruncatemacro{\result}{\pgfmathresult}
 \protected@edef\tkz@kiv@gd{\tkz@kiv@prefix$\result$\tkz@kiv@suffix}
    \path[/kiviatgrad/.cd,#1] (0:0)--(360/\tkz@kiv@radial*#2:\nv*\tkz@kiv@gap) 
       node[label=(360/\tkz@kiv@radial*#2)-90:\tkz@kiv@gd] {}; 
      }
 \let\tikz@label@distance\tikz@label@distance@tmp  
\endgroup
}%
%<--------------------------------------------------------------------------->
%<--------------------------------------------------------------------------->
\pgfkeys{%
/kiviatfile/.cd,
space/.code            =  {\def\tkz@kiv@sp{#1}}, 
gap/.code              =  {\def\tkz@kiv@gap{#1}},
lattice/.code          =  {\def\tkz@kiv@lattice{#1}},
step/.code             =  {\def\tkz@kiv@step{#1}},
radial style/.style    =  {->,>=latex'}, 
lattice style/.style   =  {thin,lightgray},
label style/.style     =  {text width=2 cm,align=center},
label space/.code      =  {\def\tkz@kiv@space{#1}},  
/kiviatfile/.unknown/.code =  {\let\searchname=\pgfkeyscurrentname
                            \pgfkeysalso{\searchname/.try=#1,
                            /tikz/\searchname/.retry=#1}}  
}
%<--------------------------------------------------------------------------->
%<---------------------------------------------------------------------------> 
\def\tkzKiviatDiagramFromFile{\pgfutil@ifnextchar[{\tkz@KiviatFile}{%
                                                    \tkz@KiviatFile[]}} 
\def\tkz@KiviatFile[#1]#2{% 
\pgfkeys{/kiviatfile/.cd,
gap          = .5,
lattice      = 10,
space        = .5,
step         = 1,
label space  = 1.5,
table        = false 
}
\pgfqkeys{/kiviatfile}{#1}%
\begingroup
\pgfplotstablevertcat{\tkz@kivdata}{#2}
\pgfplotstablegetrowsof{\tkz@kivdata}
\pgfmathsetmacro{\nbrows}{\pgfplotsretval}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\tkz@kivdata} 
\pgfmathsetmacro{\cols}{\pgfplotsretval-1}
\pgfplotstablevertcat{\tkz@kivdata}{\tkz@kivdata}
\pgfplotstablegetelem{0}{[index] 0}\of{\tkz@kivdata}
\edef\tkz@kiv@radial{\nbrows}
\foreach \x [count=\rang from 0] in {0,...,\rows}{%
 \pgfplotstablegetelem{\x}{[index] 0}\of{\tkz@kivdata} 
   \draw[/kiviatfile/radial style]
 (0,0)--(360/\tkz@kiv@radial*\rang:\tkz@kiv@lattice*\tkz@kiv@gap+\tkz@kiv@sp);
   \path
(0,0)--(360/\tkz@kiv@radial*\rang:\tkz@kiv@lattice*\tkz@kiv@gap+\tkz@kiv@space) node[/kiviatfile/label style] {\pgfplotsretval}; 

\foreach \y in {0,1,...,\tkz@kiv@lattice}{
   \draw[/kiviat/lattice style]%
     (360/\tkz@kiv@radial*\rang:\y*\tkz@kiv@gap)--%
        (360/\tkz@kiv@radial*\rang+360/\tkz@kiv@radial:\y*\tkz@kiv@gap);
     }
}
\endgroup
}   
%<--------------------------------------------------------------------------->
%<--------------------------------------------------------------------------->
\pgfkeys{/kiviatlinefile/.cd,
fill/.code = {\global\def\tkz@kivl@fill{#1}},
opacity/.code = {\global\def\tkz@kivl@opacity{#1}}, 
/kiviatlinefile/.unknown/.code ={\let\searchname=\pgfkeyscurrentname
                             \pgfkeysalso{\searchname/.try=#1,
                             /tikz/\searchname/.retry=#1}}   
}  
%<--------------------------------------------------------------------------->
\def\tkzKiviatLineFromFile{\pgfutil@ifnextchar[{\tkz@KiviatLineFile}{%
                                            \tkz@KiviatLineFile[]}} 
\def\tkz@KiviatLineFile[#1]#2#3{% 
\begingroup
\pgfkeys{/kiviatlinefile/.cd,
fill= {},
opacity=0.5
}
\pgfqkeys{/kiviatlinefile}{#1}% 
\pgfplotstablevertcat{\tkz@kivdata}{#2}
\pgfplotstablegetrowsof{\tkz@kivdata}
\pgfmathsetmacro{\nbrows}{\pgfplotsretval}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\tkz@kivdata} 
\pgfmathsetmacro{\cols}{\pgfplotsretval-1}
\pgfplotstablevertcat{\tkz@kivdata}{\tkz@kivdata}
\pgfplotstablegetelem{0}{[index] 0}\of{\tkz@kivdata}
\edef\tkz@kiv@radial{\nbrows} 
\ifx\tkzutil@empty\tkz@kivl@fill \else 
\begin{scope}[on background layer] 
      \pgfplotstablegetelem{0}{[index] #3}\of{\tkz@kivdata}
    \let\actualval\pgfplotsretval   
\path[fill=\tkz@kivl@fill,opacity=\tkz@kivl@opacity]
 (0:\actualval*\tkz@kiv@gap*\tkz@kiv@step)
 \foreach \v [count=\rang from 1] in {1,...,\nbrows}{%  
  \pgfextra{\pgfplotstablegetelem{\v}{[index] #3}\of{\tkz@kivdata}}
      -- (360/\tkz@kiv@radial*\v:\pgfplotsretval*\tkz@kiv@gap*\tkz@kiv@step)
   };   
 \end{scope}  
 \fi       
 \foreach \v [count=\rang from 1] in {0,...,\nbrows}{%  
  \pgfplotstablegetelem{\v}{[index] #3}\of{\tkz@kivdata}
  \let\actualval\pgfplotsretval
    \pgfplotstablegetelem{\rang}{[index] #3}\of{\tkz@kivdata}
  \let\nextval\pgfplotsretval 
  \draw[#1,opacity=1] (360/\tkz@kiv@radial*\v:\actualval*\tkz@kiv@gap)    
  -- (360/\tkz@kiv@radial*\rang:\nextval*\tkz@kiv@gap*\tkz@kiv@step)
   plot coordinates {(360/\tkz@kiv@radial*\rang:\nextval*\tkz@kiv@gap*\tkz@kiv@step)};  
 }   
\endgroup
}%

%<--------------------------- the end --------------------------------------->
\endinput  
