Tikz:在圆的每个扇区内对齐文本

Tikz:在圆的每个扇区内对齐文本

我想要的是

  1. 我希望文本位于每个部门之内,而不是之外。
  2. 我希望文本对齐在顶部而不是在中心。

我拥有的

这是我拥有的

\documentclass[border=1cm]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{newtxtext}
\usepackage{tikz}
\usetikzlibrary{decorations.text, fadings}
\tikzfading[name=fade out,
inner color=transparent!0,
outer color=transparent!5]

\tikzset{
    figNode/.style={
        path picture={
            \node at (path picture bounding box.center) {#1};}}
}
\usepackage[OT2, OT1]{fontenc}
\begin{document}
    \pgfmathsetmacro\Nsector{15}
    \pgfmathsetmacro\Asector{360/\Nsector}
    \pgfmathsetmacro\Aorig{30}

    \begin{tikzpicture}[font=\large, main/.style = {draw, circle, text=black,  
            preaction={draw, ultra thick, transform canvas={yshift=-0.2ex}, path fading=fuzzy ring 100 percent, opacity=0.25}
        }]
        \foreach[count=\xi] \dpt/\c/\uI in {%
            Human Resurce Mgt/Cyan!70!ForestGreen!40!/App Pro \& Disc\\Training \& Welfare,
            Gen. Service/Cyan!70!Magenta!40!/Gen. Service\\ Maintainance,
            Finance/Cyan!70!ForestGreen!40!/Exp\\ Budget\\ Revenue\\ Financial \& Fiscal Reporting,
            Legal Services \& Board Affair/Cyan!40!YellowOrange!70!/Legal Services\\Board Affairs,
            Comm \& Public Relation/Magenta!70!ForestGreen!40!/Communication\\ Public Affairs,
            Information Communication Tech/Cyan!70!ForestGreen!40!/Sys Prog \& Magtm\\Data Processing \& Analysis\\ Hardware \& Networking,
            Feild Services \& and Method./Cyan!40!ForestGreen!70!/Feild Services\\Methodology,
            National Acc Energy  \& Environment/Cyan!70!ForestGreen!40!/National Account\\ Energy \& Environment,
            Corperate Planning \& Statistical Coordination/Cyan!70!/Corperate Planning \\ Statistical Coordination,
            Agric \& Biz Enter/Cyan!30!/Agric Statistics\\Biz Enterprice,
            Trade \& Prices/Cyan!130!/Trade\\Prices,
            Gender \& Social Stat/ForestGreen!30!/Gender \& Social Inclusion\\Social Stat,
            Intl Statistical Devpt/Cyan!70!YellowOrange!30!/Donor Coordination\\Intl Action Plan,
            Demo \& HH Stat/Cyan!30!YellowOrange!70!/Demographics\\Household,
            Procurement/Cyan!30!ForestGreen!30!YellowOrange!30!/Procurement Plans\\Tender Advertisement
        }
        {\draw[draw=white, fill = \c,thin] (\Aorig-\xi*\Asector:3cm)-- (\Aorig-\xi*\Asector:10cm) arc (\Aorig-\xi*\Asector:\Aorig-\xi*\Asector-\Asector:10) -- (\Aorig-\xi*\Asector-\Asector:3cm) arc (\Aorig-\xi*\Asector-\Asector:\Aorig-\xi*\Asector:3);
            \draw[decoration={text along path,
                text={|\bfseries\large|{\parbox{4em}{\centering{\large\color{red} \dpt}\\ \uI}}},text align={center}, raise=-1.2cm}, decorate] (\Aorig-\xi*\Asector:10cm) arc (\Aorig-\xi*\Asector:\Aorig-\xi*\Asector-\Asector:10);
            % or with
            %\node [align=center] at (\Aorig-\xi*\Asector-.5*\Asector:6.5cm){\dpt\\ \uI};
        }
        % inner circle
        \draw[draw=white,fill = gray!10,thick] circle[radius = 2.8cm];
        \draw[draw=white,fill = gray!40,thick] circle[radius = 2.3cm];
        \draw[draw=white,fill = gray!10,thick] circle[radius = 2cm];
    
        \pgfmathsetmacro\uAAAangle{90}
        \coordinate (uAAA) at (\uAAAangle:2.2cm);
        \draw[decoration={text along path, text={|\bfseries\large|Internal Audit},text align={center}, raise=0.2cm}, decorate] (uAAA) arc (\uAAAangle:\uAAAangle-180:2.2);
        %%Label2
        \draw[decoration={text along path, text={|\bfseries\large|Economic Intelligence Unit},text align={center}, raise=0.2cm}, decorate] (\uAAAangle-180:2.2cm) arc (\uAAAangle-180:\uAAAangle-360:2.2);
    \end{tikzpicture}
\end{document}

答案1

您需要做的是对齐\parboxes用于放置文本的 ,以便它们全部在顶部对齐。这可以使用可选参数来完成:\parbox[t][][t]{4em}

为了实现最佳放置,我进一步调整了选项的值raise,并在节点中的文本中添加了一些连字符点。

最后,我稍微优化了你的代码,使其更具可读性和更直接。

\documentclass[border=1cm]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{newtxtext}
\usepackage{tikz}
\usetikzlibrary{decorations.text, fadings}
\tikzfading[name=fade out,
inner color=transparent!0,
outer color=transparent!5]

\tikzset{
    figNode/.style={
        path picture={
            \node at (path picture bounding box.center) {#1};}}
}
\usepackage[OT2, OT1]{fontenc}
\begin{document}
    \pgfmathsetmacro\Nsector{15}
    \pgfmathsetmacro\Asector{360/\Nsector}
    \pgfmathsetmacro\Aorig{30}

    \begin{tikzpicture}[font=\large, main/.style = {draw, circle, text=black,  
            preaction={draw, ultra thick, transform canvas={yshift=-0.2ex}, path fading=fuzzy ring 100 percent, opacity=0.25}
        }]
        \foreach[count=\xi] \dpt/\c/\uI in {%
            Human Resurce Mgt/Cyan!70!ForestGreen!40!/App Pro \& Disc\\Training \& Welfare,
            Gen. Service/Cyan!70!Magenta!40!/Gen. Service\\ Main\-tainance,
            Finance/Cyan!70!ForestGreen!40!/Exp\\ Budget\\ Revenue\\ Financial \& Fiscal Reporting,
            Legal Services \& Board Affair/Cyan!40!YellowOrange!70!/Legal Services\\Board Affairs,
            Comm \& Public Relation/Magenta!70!ForestGreen!40!/Communi\-cation\\ Public Affairs,
            Information Communication Tech/Cyan!70!ForestGreen!40!/Sys Prog \& Magtm\\Data Processing \& Analysis\\ Hardware \& Net-\\working,
            Feild Services \& and Method./Cyan!40!ForestGreen!70!/Feild Services\\Methodo\-logy,
            National Acc Energy  \& Environment/Cyan!70!ForestGreen!40!/National Account\\ Energy \& Environment,
            Corperate Planning \& Statistical Coordination/Cyan!70!/Corperate Planning \\ Statistical Coordination,
            Agric \& Biz Enter/Cyan!30!/Agric Statistics\\Biz Enterprice,
            Trade \& Prices/Cyan!130!/Trade\\Prices,
            Gender \& Social Stat/ForestGreen!30!/Gender \& Social Inclusion\\Social Stat,
            Intl Statistical Devpt/Cyan!70!YellowOrange!30!/Donor Coordination\\Intl Action Plan,
            Demo \& HH Stat/Cyan!30!YellowOrange!70!/Demo\-graphics\\Household,
            Procurement/Cyan!30!ForestGreen!30!YellowOrange!30!/Procurement Plans\\Tender Advertisement
        }
        {\draw[draw=white, fill = \c,thin] (\Aorig-\xi*\Asector:3cm) -- (\Aorig-\xi*\Asector:10cm) arc (\Aorig-\xi*\Asector:\Aorig-\xi*\Asector-\Asector:10cm) -- (\Aorig-\xi*\Asector-\Asector:3cm) arc (\Aorig-\xi*\Asector-\Asector:\Aorig-\xi*\Asector:3cm);
        \draw[decoration={text along path,
            text={|\bfseries\large|{\parbox[t][][t]{5em}{\centering{\large\color{red} \dpt}\\ \uI}}},text align={center}, raise=-.9cm}, decorate] (\Aorig-\xi*\Asector:10cm) -- (\Aorig-\xi*\Asector-\Asector:10cm);
    }
        % inner circle
        \draw[draw=white,fill = gray!10,thick] circle[radius = 2.8cm];
        \draw[draw=white,fill = gray!40,thick] circle[radius = 2.3cm];
        \draw[draw=white,fill = gray!10,thick] circle[radius = 2cm];
    
        \pgfmathsetmacro\uAAAangle{90}
        \coordinate (uAAA) at (\uAAAangle:2.2cm);
        \draw[decoration={text along path, text={|\bfseries\large|Internal Audit},text align={center}, raise=0.2cm}, decorate] (uAAA) arc (\uAAAangle:\uAAAangle-180:2.2);
        %%Label2
        \draw[decoration={text along path, text={|\bfseries\large|Economic Intelligence Unit},text align={center}, raise=0.2cm}, decorate] (\uAAAangle-180:2.2cm) arc (\uAAAangle-180:\uAAAangle-360:2.2);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

好的,之后分析,为了定位文本对齐的问题,这里有一个研究解决方案概念,它将取代该decoration方法。

观察结果:虽然您为圆形文本路径建立了一个弧线,但您似乎并没有用它来弯曲文本。相反,您通过 使用了矩形\parbox。查看您的结果,相同大小的矩形似乎适合所有扇区。所以让我们寻找一个可能的tikz解决方案。此外,我没有找到将文本限制在任意形状边界内的方法,例如饼图或扇形。

开始了:我向您展示了一种基本步骤,从您的代码中,我认为您可以轻松地将其调整到您的foreach循环中。基本思想:放置和旋转\nodes;为了考虑文本颜色,使用split rectangular shape;通过极坐标定位,根据需要旋转每个文本节点。

附言:也许您应该包含一个包babel,一旦您指明了语言,它就会为您处理连字符。//肯定会将split选项移到通用样式中nd

实施概念的例子

\documentclass[10pt, border=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}% needed to draw two rectangles on top of each other

\begin{document}

    \tikz [nd/.style={
            text width=0.64cm,% max. width of text; finetune as needed
            node font=\tiny,% font size of node text
            align=center,% centered node text
            anchor=north% this is "top middle" you were missing, so to say
            }
          ] {
        
        % --- to visualize the center ------------                      
        \node [fill=red] at (0,0) {x};      
        
        % --- placing the first node -------------
        \node [nd, rectangle split, rectangle split parts=2,% 2 stacked rectangles
               rotate=-90,% rotate the node and its text
               red% text color, will be your \dpt-col
               ] at (0:3cm)% using polar coordinates
        {
            0 Some text% this text will be in [red]
            \nodepart[black]{two}% reset text color to black, lower rectangle
            more text
            and even much more text% just type ahead without \\
        };
        
        % --- next node --------------
        % --- you need to calculate the required rotation angle for the node text yourself
        \node [nd, rectangle split, rectangle split parts=2, rotate=30,  red] at (120:3cm)
        {
            120 Some text
            \nodepart[blue]{two}
            more text
            and even 
        };
        
        % --- final node ---------------------------
        \node [nd, rectangle split, rectangle split parts=2, rotate=150,  red] at (-120:3cm)
        {
            -120 Some text
            \nodepart[green]{two}
            more text
            and even 
            and much much more text
        };
        
        % --- the 3 gray beams for reference ------------------
        \draw [draw=gray] (0,0) -- (0:4cm);
        \draw [draw=gray] (0,0) -- (120:4cm);
        \draw [draw=gray] (0,0) -- (-120:4cm);
    }
    
\end{document}

相关内容