-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdynamic-barchart-extract-color-fm-table-data-9000[5star].tex
97 lines (85 loc) · 3.04 KB
/
dynamic-barchart-extract-color-fm-table-data-9000[5star].tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
% Select color of a bar plot from table value
% https://tex.stackexchange.com/questions/493962/select-color-of-a-bar-plot-from-table-value
\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\usepackage[utf8]{inputenc}
% tight page
\usepackage[active,tightpage]{preview} % set tight page
\PreviewEnvironment{tikzpicture} % preview tikz
\setlength\PreviewBorder{20pt}% % gap around plot
\pgfkeys{/pgf/shapes/xbar/height/.initial=10cm,/pgf/shapes/xbar/width/.initial=6pt}
\pgfdeclareplotmark{xbar}{%
\pgfpathrectangle{\pgfpoint{0pt}{-.5*\pgfkeysvalueof{/pgf/shapes/xbar/width}}{0pt}}{%
\pgfpoint{-\pgfkeysvalueof{/pgf/shapes/xbar/height}}{\pgfkeysvalueof{/pgf/shapes/xbar/width}}}
\pgfusepath{stroke,fill}}
\begin{document}
\pgfplotstableread[col sep=comma]{
label, startyear, endyear, color, years
Washington, 1789, 1797, black!50, 8
Adams, 1797, 1801, red!30, 4
Jefferson, 1801, 1809, green!50, 8
Madison, 1801, 1809, brown!50, 8
}\loadedtable
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\loadedtable
\begin{tikzpicture}
\begin{axis}[
% xmin=1785,
% xmax=1810,
height=4cm,
/pgf/shapes/xbar/width=0.2cm,
axis lines=left,
width=\textwidth,
enlarge y limits={abs=0.5},
ytick=\empty,
scatter/@pre marker code/.code={
\pgfplotstablegetelem{\coordindex}{color}\of{\sortedtable}
\edef\mycolor{\pgfplotsretval}
\pgfplotstablegetelem{\coordindex}{startyear}\of{\sortedtable}
\edef\startyear{\pgfplotsretval}
\pgfplotstablegetelem{\coordindex}{endyear}\of{\sortedtable}
\edef\endyear{\pgfplotsretval}
\pgfplotstablegetelem{\coordindex}{years}\of{\sortedtable}
\edef\years{\pgfplotsretval}
% \pgfmathsetmacro{\myheight}{(\endyear-\startyear)*\xunit} % height
\pgfmathsetmacro{\myheight}{(\years)*\xunit} % height
\scope[fill=\mycolor,/pgf/shapes/xbar/height=\myheight pt] },
legend style={at={(1,1.5)}}
]
\addplot [
xbar ,
% draw=none,
forget plot] table [col sep=comma,x=startyear, y expr=-\coordindex]{\sortedtable};
\path let \p1=($(1786,0)-(1785,0)$) in \pgfextra{\xdef\xunit{\x1}}; % measure x unit
\addplot[
only marks,
scatter,
mark=xbar,
nodes near coords*,
forget plot,
nodes near coords align={anchor=west},
point meta=explicit symbolic,
every node near coord/.append style={black}]
table[
col sep=comma,
y expr=-\coordindex,
x expr=\thisrow{endyear},
meta=label]{\sortedtable};
% create object NumRows that we will use in foreach
\pgfplotstablegetrowsof{\sortedtable}
\pgfmathtruncatemacro{\NumRows}{\pgfplotsretval-1}
% adds a colored legend
% \pgfplotsinvokeforeach{0,...,\NumRows}{
% \pgfplotstablegetelem{#1}{color}\of{\sortedtable}
% \edef\mycolor{\pgfplotsretval}
% \pgfplotstablegetelem{#1}{label}\of{\sortedtable}
% \edef\mylabel{\pgfplotsretval}
% \edef\temp{\noexpand\addlegendimage{area legend,fill=\mycolor}
% \noexpand\addlegendentry{\mylabel}}
% \temp
% }
\end{axis}
\end{tikzpicture}
\end{document}