来自 csv 表的具有可变线长度的动态图

来自 csv 表的具有可变线长度的动态图

我需要根据下表绘制动态图:

Abbreviation Percentage
A             70
S             35
C             0
R             0
E             0
I             0

它需要看起来像这样 在此处输入图片描述

到目前为止这就是我得到的

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{spy}
\usetikzlibrary{fit,calc,positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{tabulary}
\usepackage{readarray}
\usepackage{array}



\definecolor{hashc}{HTML}{00A3E5}
\definecolor{diagramgray}{HTML}{D0D0D0}
\definecolor{textdarkgray}{HTML}{585858}

\begin{filecontents*}{topatributtes.csv} 
Abbreviation, percentage 
 R, 0
 I, 0
E, 0
 A, 70
 S, 35 
C, 0
\end{filecontents*}

\begin{document}

% read table data and sort 
\pgfplotstableread[col sep=comma,columns={Abbreviation,percentage}]{topatributtes.csv}\datatableA
\pgfplotstablesort[sort key={percentage}, sort cmp=int >]{\datatablesorted}{\datatableA}
\pgfplotstablegetrowsof{\datatablesorted} 
\pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1} 

\begin{tikzpicture}
\begin{scope}
\tikzstyle{mypoligon} = [draw=none,regular polygon, rotate=-45,regular polygon sides=12]
\foreach \x/\y in {0.10625/a, 0.2125/b, 0.31875/c, 0.425/d, 0.53125/e, 0.6375/f, 0.74375/g}
{
    \node[mypoligon,minimum size=\x\textwidth] (\y) at (0,0) {};
    }
\foreach \pol in {a,b,c,d,e,f,g}{
\foreach \n [remember=\n as \lastn (initially 11)] in {1, 3, ...,  11} {
 \path[draw=diagramgray] (\pol.corner \lastn) -- (\pol.corner \n);
}
}

\draw[diagramgray] (g.corner 1) -- (0,0) -- (g.corner 11) (0,0) -- (g.corner 7) (g.corner 5) -- (0,0);
\draw[fill=blue] circle [radius=1mm] {};


% Draw abbreviation names in corners
\foreach \k/\a/\x/\y in {0/3/0/2, 1/1/2/2, 2/11/2/-1, 3/9/0/-2, 4/7/-2/-1, 5/5/-2/2}{
\pgfplotstablegetelem{\k}{Abbreviation}\of{\datatablesorted}
\draw (g.corner \a) node[xshift=\x mm,yshift=\y mm] (\pgfplotsretval)  {\footnotesize \textbf{\pgfplotsretval}};
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}

% Draw lines from center to percentage value - this goes always to the max
\pgfmathtruncatemacro{\itest}{\pgfplotsretval>0}
\ifnum\itest=1
\draw (0,0) -- (g.corner \a) ; 
\fi
}

\foreach \pol/\i in {a/10,b/20,c/30,d/40,e/50,f/60,g/70}{
    \draw (\pol.corner 3) node[yshift=0.3]{\fontsize{6pt}{10pt}\selectfont \color{textdarkgray}\textsl{\i}};
}

\end{scope}
\end{tikzpicture}

\end{document}

这就是它的样子 在此处输入图片描述

我的问题是如何缩放蓝线以使其与表中的值匹配?问题是我有 8 个多边形(节点 ag),每个缩写的值范围可以从 0 到 70。使用 ifthen 命令会花很长时间。谢谢。

答案1

像这样吗?(我必须把它做得短得多。现在所有内容都从表中读取,并且循环中没有任何硬编码。)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\definecolor{hashc}{HTML}{00A3E5}
\definecolor{diagramgray}{HTML}{D0D0D0}
\definecolor{textdarkgray}{HTML}{585858}

\begin{filecontents*}{topatributtes.csv} 
Abbreviation, percentage 
 R, 0
 I, 0
E, 0
 A, 70
 S, 35 
C, 0
\end{filecontents*}

\begin{document}

% read table data and sort 
\pgfplotstableread[col sep=comma,columns={Abbreviation,percentage}]{topatributtes.csv}\datatableA
\pgfplotstablesort[sort key={percentage}, sort cmp=int >]{\datatablesorted}{\datatableA}
\pgfplotstablegetrowsof{\datatablesorted} 
\pgfmathtruncatemacro{\RowsInTable}{\pgfplotsretval-1} 
\pgfmathtruncatemacro{\TotalRows}{\pgfplotsretval} 

\begin{tikzpicture}[mypoligon/.style={draw=none,regular polygon, rotate=-45,regular polygon
sides=12},polygrid distance/.initial=8pt] %<- controls the distance
% draw the grid based on the number of rows
 \foreach \X [evaluate=\X as \Percentage using {int(\X*10)}] in {1,...,10}
  {\node[draw,diagramgray,regular polygon,regular polygon
  sides=\TotalRows,inner sep=\X*\pgfkeysvalueof{/tikz/polygrid distance},
  rotate=90-720/\TotalRows,xscale=-1] (P-\X){}
  node[text=textdarkgray] at (P-\X.corner 1) 
  {\fontsize{6pt}{10pt}\selectfont\textsl{\Percentage}};}
% add the labels and set the bullets
 \foreach \X [evaluate=\X as \Xplus using {int(\X+1)}] in {0,...,\RowsInTable}{
 \pgfplotstablegetelem{\X}{Abbreviation}\of{\datatablesorted}
 \draw[diagramgray] (0,0) -- (P-10.corner \Xplus) node[pos=1.1,black] {\pgfplotsretval};
 \pgfplotstablegetelem{\X}{percentage}\of{\datatablesorted}
 \node [circle,fill=hashc,inner sep=1pt] (Q-\X) at ({90-\X*360/\TotalRows}:{\pgfplotsretval*\pgfkeysvalueof{/tikz/polygrid distance}/10}) {};
 }
% draw the contour 
 \draw[thick,hashc,fill=hashc,fill opacity=0.3] 
    plot[variable=\x,samples at={0,...,\RowsInTable}] (Q-\x) -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容