![键值接口](https://linux22.com/image/319246/%E9%94%AE%E5%80%BC%E6%8E%A5%E5%8F%A3.png)
我想画一个带有两种颜色边框的圆圈。一种颜色应为基色,第二种颜色应填充边框至一定百分比。目前我只有这一段代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt, thick] (char) {#1};}}
\begin{document}
\circled{85\%}
\end{document}
在文本周围画一个圆圈:
我想要实现的是:
答案1
\documentclass{standalone}
\usepackage{tikz}
\usepackage{calc}
\newlength{\outerradius}
\newlength{\innerradius}
\setlength{\outerradius}{2cm}
\setlength{\innerradius}{1.5cm}
\newcommand{\progresscircle}[1]{
\begin{tikzpicture}
\fill[black!50] (0,0) circle (\outerradius);
\fill[violet!70] (0,0) -- (0, \outerradius)
arc (90:90-3.6*#1:\outerradius) -- (0,0);
\fill[white] (0,0) circle (\innerradius);
\node (0,0) {\Huge\sffamily #1\%};
\end{tikzpicture}
}
\begin{document}
\progresscircle{85.34}
\progresscircle{41.57}
\end{document}
生产
答案2
像这样吗?
\documentclass{article}
\usepackage{tikz,calc}
\newlength\charwidth
\newlength\chwidth
\newcommand*\circled[1]{%
\settowidth\charwidth{#1\,\%}%
\settototalheight\chwidth{#1\,\%}%
\ifdim\chwidth>\charwidth\let\charwidth\chwidth\fi
\addtolength\charwidth{5pt}% twice inner sep plus half line width
\tikz[baseline=(char.base)]{%
\node [inner sep=2pt, circle] (char) {#1\,\%};
\draw [line width=2pt, color=basecol] (char.north) arc (90:90-#1*3.6:.5\charwidth) coordinate (a);
\draw [line width=2pt, color=othercol] (a) arc (90-#1*3.6:-270:.5\charwidth);
}%
}
\colorlet{basecol}{purple}
\colorlet{othercol}{purple!25}
\begin{document}
\circled{87.34}
\circled{34}
\circled{20.89}
\circled{0} \circled{25} \circled{50} \circled{75} \circled{100}
\end{document}
键值接口
如果您希望能够更大程度地配置事物,这里还有另一种方法:
\documentclass{article}
\usepackage{tikz,calc}
\makeatletter
\tikzset{
circled/.code={
\tikzset{%
/circled/.cd,
#1
}
},
circled defaults/.code={
\tikzset{%
circled={%
base=blue!50!magenta,
other=gray!25,
sep=2pt,
width=2pt,
}
}
},
/circled/.cd,
base/.store in=\circled@basecol,
other/.store in=\circled@othercol,
sep/.store in=\circled@sep,
width/.store in=\circled@width,
/tikz/circled defaults,
}
\newlength\charwidth
\newlength\chwidth
\newdimen\circled@cw
\newdimen\circled@cs
\newcommand*\circled[2][]{%
\tikzset{%
circled defaults,
circled={#1}
}%
\settowidth\charwidth{#2\,\%}%
\settototalheight\chwidth{#2\,\%}%
\ifdim\chwidth>\charwidth\let\charwidth\chwidth\fi
\circled@cw=\circled@width
\circled@cs=\circled@sep
\addtolength{\charwidth}{2\circled@cs+.5\circled@cw}% twice inner sep plus half line width
\tikz[baseline=(char.base)]{%
\node [inner sep=\circled@sep, circle] (char) {#2\,\%};
\draw [line width=\circled@width, color=\circled@basecol] (char.north) arc (90:90-#2*3.6:.5\charwidth) coordinate (a);
\draw [line width=\circled@width, color=\circled@othercol] (a) arc (90-#2*3.6:-270:.5\charwidth);
}%
}
\makeatother
\begin{document}
\circled{87.34} \circled[base=red, other=green, sep=10.2pt, width=5.5pt]{34} \circled[base=green!50!blue!50, width=3pt]{20.89}
\end{document}
编辑
我不确定对齐的问题是什么:我的圆圈按预期向左对齐。可能您真正想要的是大小相同的圆圈,而不管内容如何。
此版本修改了上述代码以允许这样做,并且示例显示了动态尺寸的圆和采用标准尺寸的圆之间的对比。
\documentclass{article}
\usepackage{tikz,calc}
\makeatletter
\newif\ifcircled@makesize
\tikzset{
circled/.code={
\tikzset{%
/circled/.cd,
#1
}
},
circled defaults/.code={
\tikzset{%
circled={%
base=blue!50!magenta,
other=gray!25,
sep=2pt,
width=2pt,
}
}
},
circled size/.style={%
circled={%
make size=true,
size=#1,
}
},
/circled/.cd,
base/.store in=\circled@basecol,
other/.store in=\circled@othercol,
sep/.store in=\circled@sep,
width/.store in=\circled@width,
size/.store in=\circled@size,
make size/.is if=circled@makesize,
make size=false,
size=0pt,
/tikz/circled defaults,
}
\newlength\charwidth
\newlength\chwidth
\newdimen\circled@cw
\newdimen\circled@cs
\newcommand*\circled[2][]{%
\tikzset{%
circled defaults,
circled={#1}
}%
\circled@cw=\circled@width
\ifcircled@makesize
\circled@cs=\circled@size
\setlength\charwidth{\circled@cs}%
\addtolength{\charwidth}{.5\circled@cw}% half line width
\tikz[baseline=(char.base)]{%
\node [text width=\circled@cs, align=center] (char) {#2\,\%};
\draw [line width=\circled@width, color=\circled@basecol] ([yshift=.5\charwidth]char.center) arc (90:90-#2*3.6:.5\charwidth) coordinate (a);
\draw [line width=\circled@width, color=\circled@othercol] (a) arc (90-#2*3.6:-270:.5\charwidth);
}%
\else
\settowidth\charwidth{#2\,\%}%
\settototalheight\chwidth{#2\,\%}%
\ifdim\chwidth>\charwidth\let\charwidth\chwidth\fi
\circled@cs=\circled@sep
\addtolength{\charwidth}{2\circled@cs+.5\circled@cw}% twice inner sep plus half line width
\tikz[baseline=(char.base)]{%
\node [inner sep=\circled@sep, circle] (char) {#2\,\%};
\draw [line width=\circled@width, color=\circled@basecol] (char.north) arc (90:90-#2*3.6:.5\charwidth) coordinate (a);
\draw [line width=\circled@width, color=\circled@othercol] (a) arc (90-#2*3.6:-270:.5\charwidth);
}%
\fi
}
\makeatother
\begin{document}
\noindent\circled{87.34} \circled[base=red, other=green, sep=10.2pt, width=5.5pt]{34} \circled[base=green!50!blue!50, width=3pt]{20.89}
\noindent\circled{0} \circled{25} \circled{50} \circled{75} \circled{100}
\tikzset{circled size=40pt}% switch to circles of standard size
\noindent\circled{87.34}
\noindent\circled{34}
\noindent\circled[base=green!50!blue!50]{20.89}
\noindent\circled{0} \circled{25} \circled{50} \circled{75} \circled{100}
\end{document}
如果使用标准尺寸,则sep=<dimension>
没有效果。但是,line width=<dimension>
和颜色选项仍然有效。但是,如果仅更改某些圆圈的线宽,则会破坏标准尺寸,因此在这种情况下,您可能只想根据每个圆圈更改颜色。
请注意,设置足够大的尺寸非常重要,因为如果内容太大,圆圈将不会调整:无论内容的宽度和高度如何,圆圈都会保持您设置的大小。
答案3
\documentclass[pstricks,margin=5mm]{standalone}
\degrees[100]
\def\progress#1{%
\begin{pspicture}[linewidth=5mm,dimen=m](-1,-1)(1,1)
\pscircle[linecolor=gray]{1}
\psarcn[linecolor=red](0,0){1}{25}{!25 #1 sub}
\rput(0,0){#1\%}
\end{pspicture}}
\begin{document}
\foreach \x in {0,10.5,25,50,60,75,90,100}{\progress{\x}}
\end{document}
答案4
这轮图我写的包,可以使用。
百分比由 定义,\n
并放在中间,键为middle
。
该值由第一个变量给出,该变量\n
用于第一个切片和100-\n
第二个切片。
\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\def\n{85.34}
\wheelchart[
data=,
middle={\Huge\qty{\n}{\percent}},
radius={1.5}{2}
]{%
\n/violet!70,
{100-\n}/black!50%
}
\end{tikzpicture}
\end{document}