请问这种表格带颜色,且色条长度反映了表格数据长度,这类表格该如何绘制
跪求大神解答,或者绘制思路也行,谢谢谢谢谢谢谢谢
用 tikz 的 matrix 实现。思路是先画顶部两行表格,再画左边绿色区域的表格,最后画中间的带彩色数据条的表格,中间表格分块画,每一块都是一个 matrix. 中间表格先输入内容,然后用 foreach 加 fill 填充彩色数据条,最后给表格整体用绿色背景,然后在中间区域覆盖白色背景。中间表格我做了一部分,剩余内容和前两行的同理,有兴趣可以自己补充。
\documentclass[border=1cm,tikz]{standalone}
\usepackage{xcolor}
\usetikzlibrary{scopes,matrix,calc,backgrounds,fit}
\definecolor{blue1}{RGB}{51,204,255}
\definecolor{blue2}{RGB}{51,153,255}
\begin{document}
\begin{tikzpicture}[thick]
% 顶部表格
{[thick]
\matrix(m)[matrix of nodes,nodes={minimum width=5.4cm, minimum height=0.7cm}, ]{
\hskip-1.4cm Parameter & Ep1 $(b_p=0.04)$ & Ep2 $(b_p=0.02)$ & Ep3 $(b_p=0.01)$\\
\hskip-1.4cm Strategy & S1 \hskip3.6em S2 \hskip3.6em S3 & S1 \hskip3.6em S2 \hskip3.6em S3 & S1 \hskip3.6em S2 \hskip3.6em S3\\
};
\draw(m-1-4.south east)--++(-23.2,0);
\draw[line width=1.3pt](m-2-4.south east)--++(-23.2,0);
\draw[line width=1pt,double](m-1-4.north east)--++(-23.2,0);
\draw(m-1-1.north east)--(m-2-1.south east)--++(0,-13.6) (m-1-2.north east)--(m-2-2.south east)--++(0,-13.6) (m-1-3.north east)--(m-2-3.south east)--++(0,-13.6);
}
% 左边表格
{[shift={(-8.2,-7.4)}]
\matrix(n) [matrix of nodes,nodes={text width=2.5cm,text height=0.5cm,execute at begin node={\setlength{\baselineskip}{4ex}}, anchor=center, },align=center] {
{Efficiency \\ change [pu]} & {Avg-HPP 1\\Min-HPP 1\\Avg-Hpp 2\\Min-HPP 2} \\
{GV movement\\ {[/]}} & {Distance\\Amount} \\
{RB movement\\ {[pu]}} & {Dist.-HPP 1\\Dist.-HPP 2\\Amount-HPP 1\\Amount-HPP 2} \\
{Frequency \\ quanlity [pu]} & {HPP 1\\HPP 2} \\
{Mileage\\ {[MW]}} & {HPP 1\\HPP 2} \\
Strength [pu] & {HPP 1\\HPP 2} \\
Mileage [pu] & {HPP 1\\HPP 2} \\
Contributiuon [pu] & {HPP 1\\HPP 2} \\
};
\draw[line width=1pt,double](n-8-1.south west) ++(0,-2mm)--++(21.775,0) (n-8-1.south west) ++(0,-2mm)--++(-1.5,0);
\draw[line width=1.3pt] (n-8-1.south west) ++(0,-2mm)--(n-1-1.north west)--++(0,0.5);
\draw(n-2-1.north west)++(0,-2mm)--++(21.775,0) (n-2-1.south west)++(0,-1mm)--++(21.775,0)
(n-5-1.north west)++(0,-2mm)--++(21.775,0) (n-6-1.north west)++(0,1mm)--++(21.775,0) (n-6-1.north west)++(0,1mm)--++(-1.5,0);
\draw[line width=1.3pt](n-4-1.north west)++(0,-2mm)--++(21.775,0) (n-4-1.north west)++(0,-2mm)--++(-1.5,0);
\node[rotate=90]at ($(n-2-1.west)+(-8mm,-1.5mm)$){\large Burden};
\node[rotate=90,align=center,font=\large]at ($(n-4-1.west)+(-8mm,-9mm)$){Regulation\\performance};
\node[rotate=90]at ($(n-7-1.west)+(-8mm,-1.5mm)$){\large Payment};
}
% 中间第一行表格
{[shift={(m-2-2.south)}, yshift=-1.35cm]
\matrix(p1)[matrix of nodes, nodes={minimum width=1.8cm, minimum height=0.6cm,}, column sep=0.cm]{
-0.079\% & -0.097\% & -0.144\%\\
-0.268\% & -0.297\% & -0.304\%\\
-0.030\% & -0.061\% & -0.196\%\\
-0.435\% & -0.472\% & -0.638\%\\
};
\foreach \x/\y in{%
1-1/0.079, 1-2/0.087, 1-3/0.12,
2-1/0.19, 2-2/0.19, 2-3/0.21,
3-1/0.030, 3-2/0.041, 3-3/0.13,
4-1/0.28, 4-2/0.3, 4-3/0.35
}
{\fill[red, opacity=0.55](p1-\x.north east) rectangle ++ (-\y,-0.5);
}
}
{[shift={(m-2-3.south)}, yshift=-1.35cm]
\matrix(p1)[matrix of nodes, nodes={minimum width=1.8cm, minimum height=0.6cm,}, column sep=0.cm]{
-0.217\% & -0.249\% & -0.535\%\\
-0.493\% & -0.535\% & -0.729\%\\
-0.125\% & -0.153\% & -0.743\%\\
-0.627\% & -0.653\% & -1.369\%\\
};
\foreach \x/\y in{%
1-1/0.199, 1-2/0.207, 1-3/0.37,
2-1/0.35, 2-2/0.35, 2-3/0.43,
3-1/0.070, 3-2/0.071, 3-3/0.44,
4-1/0.38, 4-2/0.4, 4-3/0.75
}
{\fill[red, opacity=0.55](p1-\x.north east) rectangle ++ (-\y,-0.5);
}
}
{[shift={(m-2-4.south)}, yshift=-1.35cm]
\matrix(p1)[matrix of nodes, nodes={minimum width=1.8cm, minimum height=0.6cm,}, column sep=0.cm]{
-0.0726\% & -0.771\% & -1.941\%\\
-1.034 \% & -1.069\% & -2.232\%\\
-0.528\% & -0.555\% & -2.505\%\\
-1.276\% & -1.300\% & -3.182\%\\
};
\foreach \x/\y in{%
1-1/0.4, 1-2/0.42, 1-3/1.1,
2-1/0.6, 2-2/0.6, 2-3/1.3,
3-1/0.32, 3-2/0.35, 3-3/1.42,
4-1/0.68, 4-2/0.7, 4-3/1.65
}
{\fill[red, opacity=0.55](p1-\x.north east) rectangle ++ (-\y,-0.5);
}
}
% 中间第二行表格
{[shift={(m-2-2.south)}, yshift=-3.35cm]
\matrix(p1)[matrix of nodes, nodes={minimum width=1.8cm, minimum height=0.6cm,}, column sep=0.cm]{
7.610 & 7.610 & 7.610\\
2066 & 2066 & 2066\\
};
\foreach \x/\y in{%
1-1/0.4, 1-2/0.4, 1-3/0.4
}
{\fill[blue1,opacity=0.4](p1-\x.north west)++(0.04,0.05) rectangle ++ (\y,-0.6);}
\foreach \x/\y in{%
2-1/1.3, 2-2/1.3, 2-3/1.3
}
{\fill[blue!80!green, opacity=0.4](p1-\x.north west)++(0.01,-0.05) rectangle ++ (\y,-0.5);}
}
{[shift={(m-2-3.south)}, yshift=-3.35cm]
\matrix(p1)[matrix of nodes, nodes={minimum width=1.8cm, minimum height=0.6cm,}, column sep=0.cm]{
15.634 & 15.634 & 15.634\\
2343 & 2343 & 2343\\
};
\foreach \x/\y in{%
1-1/0.7, 1-2/0.7, 1-3/0.7
}
{\fill[blue1,opacity=0.4](p1-\x.north west)++(0.04,0.05) rectangle ++ (\y,-0.6);}
\foreach \x/\y in{%
2-1/1.5, 2-2/1.5, 2-3/1.5
}
{\fill[blue!80!green, opacity=0.4](p1-\x.north west)++(0.01,-0.05) rectangle ++ (\y,-0.5);}
}
{[shift={(m-2-4.south)}, yshift=-3.35cm]
\matrix(p1)[matrix of nodes, nodes={minimum width=1.8cm, minimum height=0.6cm,}, column sep=0.cm]{
31.829 & 31.829 & 31.829\\
2676 & 2676 & 2676\\
};
\foreach \x/\y in{%
1-1/1.6, 1-2/1.6, 1-3/1.6
}
{\fill[blue1,opacity=0.4](p1-\x.north west)++(0.04,0.05) rectangle ++ (\y,-0.6);}
\foreach \x/\y in{%
2-1/1.75, 2-2/1.75, 2-3/1.75
}
{\fill[blue!80!green, opacity=0.4](p1-\x.north west)++(0.01,-0.05) rectangle ++ (\y,-0.5);}
}
% 背景
{[on background layer]
\fill[green!40,opacity=0.3](current bounding box.south east)++(0.2,-0.2) rectangle +(-23.8,15.6);
\fill[white](current bounding box.south east)++(-0.3,0.25) rectangle +(-16.2,13.6);
}
\end{tikzpicture}
\end{document}

以上过程是平凡但较繁琐的。
Thanks to Jasper Habicht and GPT-6-Astra!
一个基于tabularray的functional库进行逻辑判断的实现如下:
\documentclass{article}
\usepackage[osf]{libertinus-otf}
\usepackage[landscape]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{functional, tikz}
\IgnoreSpacesOn
\tlNew \gTikzPathsTlpositive
\tlNew \gTikzPathsTlnegative
\tlNew \gTikzPathsTlzero
\clistNew \lAllDataClist
\prgNewFunction \RowMax {m} {%
\prgReturn {\fpEval {max(#1)}}
}
\prgNewFunction \RowMin {m} {%
\prgReturn {\fpEval {min(#1)}}
}
\prgNewFunction \DrawDataBar { m m } {%
\tlClear \gTikzPathsTlpositive
\tlClear \gTikzPathsTlnegative
\tlClear \gTikzPathsTlzero
\clistSet \lAllDataClist {}
\intStepOneInline {#1} {\arabic{rowcount}} {%
\intStepOneInline {#2} {\arabic{colcount}} {%
\clistPutRight \lAllDataClist {\cellGetText {##1} {####1}}
}
}
\fpSet \lTableMaxFp {\RowMax{\expValue \lAllDataClist}}
\fpSet \lTableMinFp {\RowMin{\expValue \lAllDataClist}}
\fpSet \lBoundLFp {\fpEval {min(\lTableMinFp, \cZeroFp)}}
\fpSet \lBoundRFp {\fpEval {max(\lTableMaxFp, \cZeroFp)}}
\fpCompareT {\lBoundRFp} > {\lBoundLFp} {
\fpSet \lZeroPosFp {\fpEval {( \cZeroFp - \lBoundLFp ) / ( \lBoundRFp - \lBoundLFp )}}
\intStepOneInline {#1} {\arabic{rowcount}} {%
\intStepOneInline {#2} {\arabic{colcount}} {%
\fpSet \lCurValFp {\cellGetText {##1} {####1}}
\fpSet \lCurPosFp {\fpEval {( \lCurValFp - \lBoundLFp ) / ( \lBoundRFp - \lBoundLFp )}}
\fpCompareTF {\lCurValFp} = {\cZeroFp} {
% zero
\tlPutRight \gTikzPathsTlzero {%
\evalWhole {%
([shift={(.5pt,.5pt)}]$(##1-####1.south~west)!{\fpUse \lZeroPosFp}!(##1-####1.south~east)$)
rectangle
([shift={(-.5pt,-.5pt)}]$(##1-####1.north~west)!{\fpUse \lZeroPosFp}!(##1-####1.north~east)$)
}%
}
}{
\fpCompareTF {\lCurValFp} < {\cZeroFp} {
% negative
\tlPutRight \gTikzPathsTlnegative {%
\evalWhole {%
([shift={(.5pt,.5pt)}]$(##1-####1.south~west)!{\fpUse \lCurPosFp}!(##1-####1.south~east)$)
rectangle
([shift={(-.5pt,-.5pt)}]$(##1-####1.north~west)!{\fpUse \lZeroPosFp}!(##1-####1.north~east)$)
}%
}
}{
% positive
\tlPutRight \gTikzPathsTlpositive {%
\evalWhole {%
([shift={(.5pt,.5pt)}]$(##1-####1.south~west)!{\fpUse \lZeroPosFp}!(##1-####1.south~east)$)
rectangle
([shift={(-.5pt,-.5pt)}]$(##1-####1.north~west)!{\fpUse \lCurPosFp}!(##1-####1.north~east)$)
}%
}
}
}
}
}
}
}
\IgnoreSpacesOff
\begin{document}
\begin{tblrtikzbelow}
\fill [green!40] \gTikzPathsTlpositive;
\fill [red!40] \gTikzPathsTlnegative;
\fill [gray!60] \gTikzPathsTlzero;
\end{tblrtikzbelow}
\begin{tblr}{
colspec={*{5}{Q[c,3cm]}},
hlines, vlines,
row{1} = {bg=lightgray,font=\bfseries\large},
process=\DrawDataBar{2}{1}
}
Item1 & Item2 & Item3 & Item4 & Item5 \\
0.5 & 1.5 & 2.5 & 3.5 & 4.5 \\
-1 & -2 & -3 & -4 & -5 \\
-6.5 & -5.4 & -4.3 & -3.2 & -2.1 \\
6.5 & -5.4 & 0 & -3.2 & 2.1 \\
-0.5 & -1.5 & -8.5 & -3.5 & -4.5 \\
-5.5 & -9.5 & -2.5 & -7.5 & -6.5\\
-1 & 2 & -3 & 4 & -5 \\
-3.1 & -2.1 & -1.1 & 1 & -1.1 \\
1 & 4 & 2 & 8 & 5 \\
5.5 & 6.5 & 7.5 & 8.5 & 9.5 \\
\end{tblr}
\end{document}
一个基于gradbars宏包的优雅方案:
\documentclass{article}
\usepackage[ebgaramond]{fontsetup}
\usepackage[landscape]{geometry}
\usepackage{tabularray}
\usepackage{gradbars}
\pagestyle{empty}
\gradbarssetup{
preset=presentation,
height=18pt,width=3cm,
row padding=0.5pt,
min=-9.5,max=9.5,
color=green!40,
negative color=red!40,
track color=white,
text color=black,
precision=1
}
\newcommand{\mygradbar}[1]{%
\begin{tikzpicture}[baseline=(b.base)]
\node[inner sep=0pt,outer sep=0pt] (b) {\gradbar[label=none]{#1}};
\node[inner sep=0pt] at (b.center) {#1};
\end{tikzpicture}}
\begin{document}
\begin{center}
\begin{tblr}{
colspec={*{5}{Q[c,m]}},
hlines,vlines,stretch=0,
rows={ht=18pt,rowsep=0.5pt},
row{1}={bg=lightgray,font=\bfseries\large},
row{2-Z}={cmd=\mygradbar}
}
Item1 & Item2 & Item3 & Item4 & Item5 \\
0.5 & 1.5 & 2.5 & 3.5 & 4.5 \\
-1 & -2 & -3 & -4 & -5 \\
-6.5 & -5.4 & -4.3 & -3.2 & -2.1 \\
6.5 & -5.4 & 0 & -3.2 & 2.1 \\
-0.5 & -1.5 & -8.5 & -3.5 & -4.5 \\
-5.5 & -9.5 & -2.5 & -7.5 & -6.5 \\
-1 & 2 & -3 & 4 & -5 \\
-3.1 & -2.1 & -1.1 & 1 & -1.1 \\
1 & 4 & 2 & 8 & 5 \\
5.5 & 6.5 & 7.5 & 8.5 & 9.5 \\
\end{tblr}
\end{center}
\end{document}
其实上面的代码需要手动实现:
\newcommand{\mygradbar}[1]{%
\begin{tikzpicture}[baseline=(b.base)]
\node[inner sep=0pt,outer sep=0pt] (b) {\gradbar[label=none]{#1}};
\node[inner sep=0pt] at (b.center) {#1};
\end{tikzpicture}}一点也不优雅,在v1.1.0中,支持label align=cell,详见这里的报告:
\documentclass{article}
\usepackage[ebgaramond]{fontsetup}
\usepackage[landscape]{geometry}
\usepackage{tabularray}
\usepackage{gradbars}
\pagestyle{empty}
\gradbarssetup{
preset=presentation,
height=18pt,width=3cm,
row padding=0.5pt,
min=-9.5,max=9.5,
color=green!40,
negative color=red!40,
track color=white,
text color=black,
precision=1,
% label overflow=auto, % default
label align=cell %<--- new feature in v1.1.0
}
\begin{document}
\begin{center}
\begin{tblr}{
colspec={*{5}{Q[c,m]}},
hlines,vlines,stretch=0,
rows={ht=18pt,rowsep=0.5pt},
row{1}={bg=lightgray,font=\bfseries\large},
row{2-Z}={cmd=\gradbar}
}
Item1 & Item2 & Item3 & Item4 & Item5 \\
0.5 & 1.5 & 2.5 & 3.5 & 4.5 \\
-1 & -2 & -3 & -4 & -5 \\
-6.5 & -5.4 & -4.3 & -3.2 & -2.1 \\
6.5 & -5.4 & 0 & -3.2 & 2.1 \\
-0.5 & -1.5 & -8.5 & -3.5 & -4.5 \\
-5.5 & -9.5 & -2.5 & -7.5 & -6.5 \\
-1 & 2 & -3 & 4 & -5 \\
-3.1 & -2.1 & -1.1 & 1 & -1.1 \\
1 & 4 & 2 & 8 & 5 \\
5.5 & 6.5 & 7.5 & 8.5 & 9.5 \\
\end{tblr}
\end{center}
\end{document}
嗯嗯谢谢大神
@u156710 说难也不难,可以分步绘制,先画表格,每个区块可以看作一个 node,然后填内容,数据条可以目测,如果不要求太精确是可以行得通的。有空我会试试完成一部分。
@u26254 感觉这样的话可拓展性会相对比较差...