5

I am looking for an example of a TikZ picture of an OLAP cube with dimension hierarchy. More-less something like this cube:

enter image description here

I know how to draw a cube based on What is the easiest way to draw 3D cube with TikZ? and Sudoku 3D cube example. But I wonder how to put nicely also dimensions' hierarchies, e.g., with trees tikz library.

Skarab
  • 1,731
  • I would strongly suggest you use the tikz-3dplot package! It adds another dimension to the tikz drawings and calculates the projection based on the angle of view. I use it myself to do about every drawing I need! – romeovs Jun 09 '11 at 21:57

2 Answers2

6

It's not exactly an answer at your question because I don't use the library "trees" but if you make a macro, it's possible to simplify the code : I don't put "Shipping dates" and "Product" because it's easy to work on the main plane

\documentclass[10pt]{article}
\usepackage{tikz}

\newcommand\pgfmathsinandcos[3]{% 
  \pgfmathsetmacro#1{sin(#3)} 
  \pgfmathsetmacro#2{cos(#3)}}

\begin{document}
\pgfmathsetmacro\angFuite{30}
\pgfmathsetmacro\coeffReduc{0.5}

\begin{tikzpicture}[scale=.75]
\pgfmathsinandcos\sint\cost{\angFuite} %  
  \draw (0,0)--(8,0)--(8,8)--(0,8)--(0,0)
        (4,0)--(4,8) (0,4)--(8,4);
          \path[coordinate] (8,4) coordinate (A);

\tikzset{current plane/.estyle={%
  cm={1,0,\coeffReduc*\cost,\coeffReduc*\sint,(0,0)}}}
\begin{scope}[current plane]
\draw  (8,0)--(8,8);
\end{scope}

\tikzset{current plane/.estyle={%
cm={1,0,\coeffReduc*\cost,\coeffReduc*\sint,(0,8)}}}
\begin{scope}[current plane]
   \draw (4,0)--(4,8) (0,4)--(8,4) (4,0)--(4,8);
   \draw (8,0)--(8,8) 
         (0,8)--(0,0) 
         (-3.5,0)--(-4,0)--(-4,8)--(-3.5,8) node[right] {Los Angeles};  
          \node[right] at (-3.5,0){New York}; 
   \draw (-4,4)--(-4.5,4) node [left]{\textbf{location}}; 
    \path[coordinate] (8,4) coordinate (C);   
\end{scope} 

\tikzset{current plane/.estyle={%
cm={1,0,0,1,(8*\coeffReduc*\cost,8*\coeffReduc*\sint)}}}
\begin{scope}[current plane]
  \draw (8,0)--(8,8)--(0,8);
  \path[coordinate] (8,4) coordinate (B);
\end{scope}

\draw (A)--(B);
\draw (C)--++(0,-8); 
\end{tikzpicture}

\end{document}

enter image description here

Alain Matthes
  • 95,075
  • The code is not perfect and there is lot of space for improvement – Alain Matthes Jun 09 '11 at 20:38
  • Another problem : the cube is in "perpective cavalière" french term I don't know the english term (sides are parallel) and this is not the case in your example. – Alain Matthes Jun 09 '11 at 20:42
  • According to Wikipedia the same term exists in English: http://en.wikipedia.org/wiki/Oblique_projection#Cavalier_projection – Caramdir Jun 09 '11 at 21:20
  • @Caramdir thanks! because I try to find this in wikipedia but the french wikipedia does not give the equivalent page in english – Alain Matthes Jun 10 '11 at 06:18
5

This is what I got after playing around with the tikz-3dplot package (I'm tired, so my code shows a bit of brute force lazyness):

\documentclass{article}
\usepackage[]{tikz}
\usepackage[]{tikz-3dplot}

\begin{document}

\tdplotsetmaincoords{70}{110} %set the viewing angle
\begin{tikzpicture}[tdplot_main_coords,every node/.style={font=\tiny,outer sep=0pt,inner sep=2pt},scale=1.3]    

%front  
\draw[thick](0,0,0)--++(0,2,0)--++(0,0,2)--++(0,-2,0)--cycle;   
\draw[]     (0,0,0)--++(0,1,0)--++(0,0,1)--++(0,-1,0)--cycle;   
\draw[]     (0,1,1)--++(0,1,0)--++(0,0,1)--++(0,-1,0)--cycle;

%side   
\draw[thick]( 0,2,0)--++(-2,0,0)--++(0,0,2)--++(2,0,0)--cycle;  
\draw[]     ( 0,2,0)--++(-1,0,0)--++(0,0,1)--++(1,0,0)--cycle;  
\draw[]     (-1,2,1)--++(-1,0,0)--++(0,0,1)--++(1,0,0)--cycle;

%top    
\draw[thick]( 0,0,2)--++(0,2,0)--++(-2,0,0)--++(0,-2,0)--cycle;     
\draw[]     ( 0,0,2)--++(0,1,0)--++(-1,0,0)--++(0,-1,0)--cycle;     
\draw[]     (-1,1,2)--++(0,1,0)--++(-1,0,0)--++(0,-1,0)--cycle;

\def\A{1}   
\def\B{1}   
\draw(-0.1,0,2)++(0,-\A,0)--node[above,sloped,at start]{Los Angeles}++(0,-\B,0)--++(-1.8,0,0)--node[below,sloped]{New York}++(0,\B,0)coordinate(NY);    
\draw(-1,-\A-\B,2)--node[left,sloped,at end]{\bf location}++(0,-0.3,0);

\draw(0,0,0.1)++(0,-\A,0)--node[above,sloped,near start]{Tennis racquets}++(0,-\B,0)--++(0,0,1.8)--node[below,sloped,at end]{Soccer balls}++(0,\B,0);   
\draw(0,-\A-\B,1)--node[left,near end,sloped]{\bf product}++(0,-0.3,0);

\def\B{1.3}     
\def\A{1.2}     
\draw(\A,0.1,0)--node[below,sloped,at start]{January}++(\B,0,0)--++(0,1.8,0)--node[above,sloped,near end]{February}++(-\B,0,0);     \draw(\A+\B,1,0)--++(0.3,0,0)coordinate(SD);    
\draw[draw=none](SD)--node[sloped,below]{\bf Shipping Date}++(0,0.1,0);
\end{tikzpicture}

\end{document}

enter image description here

Text Readability

Ok this example hasn't got any sloped text: \documentclass{article} \usepackage[]{tikz} \usepackage[]{tikz-3dplot}

\begin{document}

\tdplotsetmaincoords{76}{103}
\begin{tikzpicture}[tdplot_main_coords,every node/.style={font=\tiny,outer sep=0pt,inner sep=2pt},scale=1.3]

%front
\draw[thick](0,0,0)--++(0,2,0)--++(0,0,2)--++(0,-2,0)--cycle;
\draw[]   (0,0,0)--++(0,1,0)--++(0,0,1)--++(0,-1,0)--cycle;
\draw[]   (0,1,1)--++(0,1,0)--++(0,0,1)--++(0,-1,0)--cycle;

%side
\draw[thick]( 0,2,0)--++(-2,0,0)--++(0,0,2)--++(2,0,0)--cycle;
\draw[]   ( 0,2,0)--++(-1,0,0)--++(0,0,1)--++(1,0,0)--cycle;
\draw[]     (-1,2,1)--++(-1,0,0)--++(0,0,1)--++(1,0,0)--cycle;

%top
\draw[thick]( 0,0,2)--++(0,2,0)--++(-2,0,0)--++(0,-2,0)--cycle;
\draw[]   ( 0,0,2)--++(0,1,0)--++(-1,0,0)--++(0,-1,0)--cycle;
\draw[]     (-1,1,2)--++(0,1,0)--++(-1,0,0)--++(0,-1,0)--cycle;

\def\A{0.5}
\def\B{1.3}
\draw(-0.1,0,2)++(0,-\A,0)--node[above,near start]{Los Angeles}++(0,-\B,0)--++(-1.8,0,0)--node[below,near end]{New York}++(0,\B,0)coordinate(NY);
\draw(-1,-\A-\B,2)--++(0,-0.3,0)node[left,anchor=south east]{\bf location};




\draw(0,0,0.1)++(0,-\A,0)--node[above,near start]{Tennis racquets}++(0,-\B,0)--++(0,0,1.8)--node[below,near end]{Soccer balls}++(0,\B,0);
\draw(0,-\A-\B,1)--++(0,-0.3,0)node[left,anchor=east]{\bf product};


\def\B{1.3}
\def\A{1.2}
\draw(\A,0.1,0)--node[right,near start]{January}++(\B,0,0)--++(0,1.8,0)--node[left,near end]{February}++(-\B,0,0);
\draw(\A+\B,1,0)--++(0.3,0,0)node[below left]{\bf Shipping Date};
\end{tikzpicture}

\end{document}

enter image description here

romeovs
  • 9,102
  • The problem I think in this case, its' that the texts are sloped and sometimes, it's not very easy to read these texts. – Alain Matthes Jun 10 '11 at 03:51
  • It looks very good, any ideas how to fix the issue with the text labels? – Skarab Jun 10 '11 at 06:58
  • added another example that fixes this.. – romeovs Jun 10 '11 at 08:40
  • Is there an easy way to fix the lines connecting the dimensions with the concepts? I mean, to have them in the same orientation as text labels. – Skarab Jun 14 '11 at 15:02
  • I have finally chosen the first solution, because it was much more easier for me to adapt to my needs. When I learn more about tikz I will definitely try tikz3d again. – Skarab Jun 15 '11 at 06:47