-
Notifications
You must be signed in to change notification settings - Fork 3
/
l3charts.sty
582 lines (543 loc) · 24.5 KB
/
l3charts.sty
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{l3charts}[2022/08/01 0.7.0 Customizable tikz charts using latex3]
\RequirePackage{expl3}
\RequirePackage{xparse}
\RequirePackage{tikz}
\usetikzlibrary{shapes, shapes.misc, backgrounds, calc, fit, positioning}
\usepackage{tcolorbox}
\ExplSyntaxOn
\makeatletter % to access latex2e variables
% utility cs
\cs_set:Npn \identity #1 {#1} % identity cs to return the first argument as is
\cs_set:Npn \nop #1 {} % nop cs to remove the first argument from input
\cs_set:Npn \percent #1 {#1\%} % nop cs to remove the first argument from input
% Draw an arc from a center
% #1: TikZ options to draw the arc with
% #2: center (x,y)
% #3: initial angle
% #4: final angle
% #5: radius
\def\centerarc[#1](#2)(#3,#4,#5){
\draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3\c_colon_str#4\c_colon_str#5)
}
% error messages
\msg_new:nnn {l3charts} {unknown-choice} {#1\ key value should\ be\ among\ #2\ but\ "#3"\ was\ given}
\msg_new:nnn {l3charts} {debug} {debug: #1}
% Environment to draw kiviat charts
% Inside kiviatchar environment at least one dims environment must be defined
% followed by one or more set environment
\NewDocumentEnvironment {kiviatchart} {o} {
\prop_clear_new:N \l_tikz_prop % properties for tikzpicture
\clist_clear_new:N \l_tikz_clist % properties for tikzpicture as keyval string
% keyval options for the environment
\keys_define:nn {kiviatchart} {
radius .tl_set:N = \R, % maximal diagram radius
units .int_set:N = \U, % number of scale units
% forward all other options to tikzpicture
unknown .code:n = {\prop_put:Nxx \l_tikz_prop {\l_keys_key_str} {##1}},
rounded .bool_set:N = \l_rounded_bool, % use circles and curves instead of polygons
}
% set default environment options
\keys_set:nn {kiviatchart} {
radius = 3.5cm,
units = 5,
rounded = false
}
% override with given options if any
\IfValueT {#1} {\keys_set:nn {kiviatchart} {#1}}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_tikz_prop {
\clist_put_right:Nn \l_tikz_clist {##1=##2}
}
\str_set:Nx \l_tikz_str {[\clist_use:Nn \l_tikz_clist {,}]}
% Command to set dimentions and values
\DeclareDocumentCommand \value {om} {
% when used inside set environment
\tl_if_eq:NnTF \@currenvir {set} {
% add new value
\seq_put_right:Nn \l_values_seq {##2}
} {}
% when used inside dims environment
\tl_if_eq:NnTF \@currenvir {dims} {
% add new dimension with its options
\IfValueTF {##1} {
\seq_put_right:Nn \l_labeloptions_seq {##1}
} {
% replace novalue with nothing
\seq_put_right:Nn \l_labeloptions_seq {}
}
\seq_put_right:Nn \l_labels_seq {##2}
} {}
}
% Environment for defining dimensions
% draw the kiviat chart axis
\NewDocumentEnvironment {dims} {o} {
% keyval options for the environment
\keys_define:nn {kiviatchart_dims} {
radius .tl_set:N = \L, % radius to put dimension labels
label-on .int_set:N = \l_labelon_int, % which dim axis to put labels on
dim-options .tl_set:N = \l_dimoptions_tl, % options for dimensions axis
unit-options .tl_set:N = \l_unitoptions_tl, % options for unit polygons
label-options .tl_set:N = \l_labeloptions_tl, % options for unit labels
label-cs .str_set:N = \l_labelcs_str, % name of the cs used to format label
unit-cs .str_set:N = \l_unitcs_str, % name of the cs used to format label
angle .fp_set:N = \l_angle_fp % angle of first dimension
}
\cs_set:Npn \tinytt ####1 {\texttt{\tiny ####1}} % cs to format unit labels
% set default environment options
\keys_set:nn {kiviatchart_dims} {
radius = \R,
label-on = 1,
dim-options = {opacity=0.8},
unit-options = {opacity=0.3},
label-options = {opacity=0.5,above,xshift=1.5mm},
label-cs = identity,
unit-cs = tinytt,
angle = 90
}
% override with given options if any
\IfValueT {##1} {\keys_set:nn {kiviatchart_dims} {##1}}
\seq_clear_new:N \l_labels_seq % sequence to keep labels from \value cs
\seq_clear_new:N \l_labeloptions_seq % sequence to keep label options from \value cs
} {
% now that we have all the dimensions we can draw the picture
\tl_set:Nx \labels {\seq_use:Nn \l_labels_seq {,}} % , separated list of dimensions
\tl_set:Nx \D {\seq_count:N \l_labels_seq} % number of dimensions
\foreach \Y in {0,...,\U} {
% save positions as (D\X-\Y) with \X dimension in [1,\D] and \Y value in [0,\U]
\foreach \X in {1,...,\D} {
\tl_set:Nx \l_tmpa_tl {\fp_to_decimal:n {\l_angle_fp+360-((\X-1)*360/\D)}}
\path (\l_tmpa_tl \c_colon_str \Y*\R/\U) coordinate (D\X-\Y);
% \fill (D\X-\Y) circle (1pt); % intersection points on the web
}
\bool_if:nTF {\l_rounded_bool} {
\exp_last_unbraced:Ne \draw {[\l_unitoptions_tl]} (0,0) circle (\Y*\R/\U);
} {
% draw units polygons
\exp_last_unbraced:Ne \draw {[\l_unitoptions_tl]} (D1-\Y) \foreach \X in {1,...,\D} {
-- (D\X-\Y)
} -- cycle;
}
}
% draw the dimensions axis
\foreach \X in {1,...,\D} {
\tl_set:Nx \l_tmpa_tl {\int_use:N \U}
\exp_last_unbraced:Ne \draw {[\l_dimoptions_tl]} (D\X-0) -- (D\X-\l_tmpa_tl);
}
% draw units labels
\bool_if:nTF {\int_compare_p:n {\l_labelon_int >= 1} && \int_compare_p:n {\l_labelon_int <= \D}} {
\foreach \Y in {1,...,\U} {
\tl_set:Nx \l_tmpa_tl {\int_eval:n {\Y-1}}
\exp_last_unbraced:Ne \node {[\l_labeloptions_tl]} at (D\int_use:N \l_labelon_int-\l_tmpa_tl) {\use:c {\l_unitcs_str} {\Y}};
}
} {}
% draw labels for each dimension axis
\foreach \l [count=\X from 1] in \labels {
\tl_set:Nx \l_tmpa_tl {\fp_to_decimal:n {\l_angle_fp+360-((\X-1)*360/\D)}}
\tl_set:Nx \l_tmpb_tl {\seq_item:Nn \l_labeloptions_seq {\X}}
\path (\l_tmpa_tl \c_colon_str \L) node [\l_tmpb_tl] {\use:c {\l_labelcs_str} {\l}};
}
}
% Environment for values set
% draw set polygon
\NewDocumentEnvironment {set} {o} {
\seq_clear_new:N \l_values_seq % store the values of a set
\prop_clear_new:N \l_setoptions_prop % prop to store keyval options
\clist_clear_new:N \l_setoptions_clist % clist to get prop back to keyval str
% keyval options for the environment
\keys_define:nn {kiviatchart_set} {
dot-options .tl_set:N = \l_dotoptions_tl, % options for polygon node
% forward all other options to tikz
unknown .code:n = {\prop_put:Nxx \l_setoptions_prop {\l_keys_key_str} {####1}}
}
% set default options (exp_args to expand \c_space_tl)
\exp_args:Nnx \keys_set:nn {kiviatchart_set} {
dot-options = {fill,circle,inner~sep=1pt},
% default options forwarded to tikzpicture
color = black,
line~width = 1.5pt,
opacity = 1,
fill~opacity = 0.3,
fill = gray
}
% override with given options if any
\IfValueT {##1} {\keys_set:nn {kiviatchart_set} {##1}}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_setoptions_prop {
\clist_put_right:Nn \l_setoptions_clist {####1=####2}
}
\str_set:Nx \l_setoptions_str {\clist_use:Nn \l_setoptions_clist {,}}
}{
% now that we have all the values, we can draw the points and the polygons
\tl_set:Nx \values {\seq_use:Nn \l_values_seq {,}}
\seq_get_left:NN \l_values_seq \firstval
% dots on the dim axis
\foreach \v [count=\i from 1] in \values {
\exp_last_unbraced:Ne \node {[\l_dotoptions_tl]} at (D\i-\v) {};
}
\bool_if:nTF {\l_rounded_bool} {
% closed curve going through all the coordinates
% construct the coordinates string (space separated list of defined coordinates)
\str_new:N \l_coordinates_str
\seq_map_indexed_inline:Nn \l_values_seq {\str_put_right:Nn \l_coordinates_str {(D####1-####2)}}
% \msg_term:nnx {l3charts} {debug} {\l_coordinates_str}
\exp_last_unbraced:Ne \draw {[\l_setoptions_str]} plot [smooth~cycle] coordinates {\l_coordinates_str};
} {
% closed polygon going through all the coordinates
\exp_last_unbraced:Ne \draw {[\l_setoptions_str]} (D1-\firstval) foreach \v [count=\i from 1] in \values {-- (D\i-\v)} -- cycle;
}
}
% start a picture (expand option before begin)
\exp_last_unbraced:Nno \begin {tikzpicture} {\l_tikz_str}
}{
% close the picture
\end{tikzpicture}
}
% Environment to hold a ball chart
\NewDocumentEnvironment {ballchart} {o} {
\prop_clear_new:N \l_tikz_prop % properties for tikzpicture
\clist_clear_new:N \l_tikz_clist % properties for tikzpicture as keyval string
\keys_define:nn {ballchart} {
n .int_set:N = \l_n_int, % number of circles
gap .dim_set:N = \l_gap_dim, % gap between bars
cgap .dim_set:N = \l_cgap_dim, % gap between circles
radius .dim_set:N = \l_radius_dim, % radius
fill-options .tl_set:N = \l_filloptions_tl, % TikZ options to fill balls with
draw-options .tl_set:N = \l_drawoptions_tl, % TikZ options to draw balls with
label-options .tl_set:N = \l_labeloptions_tl, % TikZ options for dimensions axis
label-cs .str_set:N = \l_labelcs_str, % cs name to format labels with
label-pos .choices:nn =
{left, right, above, below, above right, above left, below right, below left}
{\str_set:Nx \l_labelpos_str {\l_keys_choice_tl}},
label-pos / unknown .code:n = {
\msg_error:nnxxx { l3charts } { unknown-choice }
{ label-pos } % Name of choice key
{left, right, above, below, above right, above left, below right, below left} % Valid choices
{ ##1 } % Invalid choice given
},
value-cs .str_set:N = \l_valuecs_str, % cs name to format values with
% forward all other options to tikzpicture
unknown .code:n = {\prop_put:Nxx \l_tikz_prop {\l_keys_key_str} {##1}}
}
% set default options
\keys_set:nn {ballchart} {
n = 5,
gap = 1ex,
cgap = 1pt,
radius = 2.5mm,
fill-options = {fill=black},
draw-options = {draw=none},
label-options = {},
label-cs = identity,
label-pos = left,
value-cs = nop,
}
% override with given options if any
\IfValueT {#1} {\keys_set:nn {ballchart} {#1}}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_tikz_prop {
\clist_put_right:Nn \l_tikz_clist {##1=##2}
}
\str_set:Nx \l_tikz_str {[\clist_use:Nn \l_tikz_clist {,}]}
\tl_set:Nn \l_width_tl {\dim_to_decimal_in_unit:nn {(2\l_radius_dim + \l_cgap_dim) * \l_n_int} {1cm}}
\int_zero_new:N \l_count_int
% Command to add a new ballbar
% #1: label
% #2: percentage
\DeclareDocumentCommand \value {mm} {
\tl_set:Nx \l_perc_tl {\fp_to_decimal:n {##2 / 100}}
\tl_set:Nx \l_y_tl {\dim_to_decimal_in_unit:nn {(2\l_radius_dim + \l_gap_dim) * \l_count_int} {1cm}}
\tl_set:Nx \l_m_tl {\int_eval:n {\l_n_int - 1}}
\exp_last_unbraced:Ne \draw {[\l_drawoptions_tl, fill~fraction={\l_perc_tl}]} foreach \i in {0,...,\l_m_tl} {(2\l_radius_dim * \i + \l_cgap_dim * \i, -\l_y_tl) circle (\l_radius_dim)};
\exp_last_unbraced:Ne \node {(ball\int_use:N \l_count_int)} at (\l_width_tl/2, -\l_y_tl) {\makebox[\l_width_tl cm][c]{\use:c {\l_valuecs_str} {##2}}};
\exp_last_unbraced:Ne \node {[\l_labelpos_str=of~ball\int_use:N \l_count_int,\l_labeloptions_tl]} {\use:c {\l_labelcs_str} {##1}};
\int_incr:N \l_count_int
}
\exp_last_unbraced:Nno \begin {tikzpicture} {\l_tikz_str}
\tikzset{fill~fraction/.style~n~args={1}{
path~picture={
\exp_last_unbraced:Ne \fill {[\l_filloptions_tl]} (path~picture~bounding~box.south~west) rectangle
($(path~picture~bounding~box.north~west)!##1!(path~picture~bounding~box.north~east)$);
}
}
}
}{
\end{tikzpicture}
}
% % Environment to hold a bar chart
\NewDocumentEnvironment {barchart} {o} {
\prop_clear_new:N \l_tikz_prop % properties for tikzpicture
\clist_clear_new:N \l_tikz_clist % properties for tikzpicture as keyval string
% keyval options for the environment
\keys_define:nn {barchart} {
width .dim_set:N = \l_w_dim, % maximum bar width
height .dim_set:N = \l_h_dim, % bar height
gap .dim_set:N = \l_gap_dim, % gap between bars
fill-options .tl_set:N = \l_filloptions_str, % TikZ options to fill background with
draw-options .tl_set:N = \l_drawoptions_str, % TikZ options to draw bar with
label-options .tl_set:N = \l_labeloptions_tl, % TikZ options to draw the label with
label-cs .str_set:N = \l_labelcs_str, % cs name to format labels with
label-pos .choices:nn =
{left, right, above, below, above right, above left, below right, below left}
{\str_set:Nx \l_labelpos_str {\l_keys_choice_tl}},
label-pos / unknown .code:n = {
\msg_error:nnxxx { l3charts } { unknown-choice }
{ label-pos } % Name of choice key
{left, right, above, below, above right, above left, below right, below left} % Valid choices
{ ##1 } % Invalid choice given
},
value-cs .str_set:N = \l_valuecs_str, % cs name to format values with
% forward all other options to tikzpicture
unknown .code:n = {\prop_put:Nxx \l_tikz_prop {\l_keys_key_str} {##1}}
}
% set default options
\keys_set:nn {barchart} {
width = 3cm,
height = 3.5mm,
gap = 1ex,
fill-options = {fill=none},
draw-options = {fill=black},
label-options = {},
label-cs = identity,
label-pos = left,
value-cs = nop
}
% override with given options if any
\IfValueT {#1} {\keys_set:nn {barchart} {#1}}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_tikz_prop {
\clist_put_right:Nn \l_tikz_clist {##1=##2}
}
\str_set:Nx \l_tikz_str {[\clist_use:Nn \l_tikz_clist {,}]}
\int_zero_new:N \l_count_int
% Command to add a bar to the bar chart
% #1: bar label
% #2: percentage the current bar should take up of the total width
\DeclareDocumentCommand \value {mm} {
\tl_set:Nn \l_x_tl {0}
\tl_set:Nx \l_y_tl {\dim_to_decimal_in_unit:nn {(\l_h_dim + \l_gap_dim) * \l_count_int} {1cm}}
\tl_set:Nx \l_barx_tl {\dim_to_decimal_in_unit:nn {##2\l_w_dim / 100} {1cm}}
\tl_set:Nx \l_bary_tl {\dim_to_decimal_in_unit:nn {((\l_h_dim + \l_gap_dim) * \l_count_int) + \l_h_dim} {1cm}}
\tl_set:Nx \l_cx_tl {\dim_to_decimal_in_unit:nn {0.5\l_w_dim} {1cm}}
\tl_set:Nx \l_cy_tl {\dim_to_decimal_in_unit:nn {((\l_h_dim + \l_gap_dim) * \l_count_int) + 0.5\l_h_dim} {1cm}}
\exp_last_unbraced:Ne \fill {[\l_filloptions_str]} (\l_x_tl,-\l_y_tl) rectangle (\l_w_dim, -\l_bary_tl);
\exp_last_unbraced:Ne \draw {[\l_drawoptions_str]} (\l_x_tl,-\l_y_tl) rectangle (\l_barx_tl, -\l_bary_tl);
\exp_last_unbraced:Ne \node {(bar\int_use:N \l_count_int)} at (\l_cx_tl,-\l_cy_tl) {\makebox[\l_w_dim][c]{\use:c {\l_valuecs_str} {##2}}};
\exp_last_unbraced:Ne \node {[\l_labelpos_str=of~bar\int_use:N \l_count_int,\l_labeloptions_tl]} {\use:c {\l_labelcs_str} {##1}};
\int_incr:N \l_count_int
}
% start a picture (expand option before begin)
\exp_last_unbraced:Nno \begin {tikzpicture} {\l_tikz_str}
}{
\end{tikzpicture}
}
% Environment to hold a bubble chart
\NewDocumentEnvironment {bubblechart} {o} {
\prop_clear_new:N \l_tikz_prop % properties for tikzpicture
\clist_clear_new:N \l_tikz_clist % properties for tikzpicture as keyval string
% keyval options for the environment
\keys_define:nn {bubblechart} {
radius .dim_set:N = \l_radius_dim, % max radius
gap .dim_set:N = \l_gap_dim, % gap between bubbles
fill-options .tl_set:N = \l_filloptions_str, % TikZ options to draw the background with
draw-options .tl_set:N = \l_drawoptions_str, % TikZ options to draw the bubble with
label-options .tl_set:N = \l_labeloptions_tl, % TikZ options to draw the label with
label-cs .str_set:N = \l_labelcs_str, % cs name to format labels with
label-pos .choices:nn =
{left, right, above, below, above right, above left, below right, below left}
{\str_set:Nx \l_labelpos_str {\l_keys_choice_tl}},
label-pos / unknown .code:n = {
\msg_error:nnxxx { l3charts } { unknown-choice }
{ label-pos } % Name of choice key
{left, right, above, below, above right, above left, below right, below left} % Valid choices
{ ##1 } % Invalid choice given
},
value-cs .str_set:N = \l_valuecs_str, % cs name to format values with
vertical .bool_set:N = \l_vertical_bool, % stack bubbles vertically
% forward all other options to tikzpicture
unknown .code:n = {\prop_put:Nxx \l_tikz_prop {\l_keys_key_str} {##1}}
}
% set default options
\keys_set:nn {bubblechart} {
radius = 1cm,
gap = 1ex,
fill-options = {fill=none,draw=none},
draw-options = {fill=black},
label-options = {},
label-cs = identity,
label-pos = above,
value-cs = nop,
vertical = false
}
% override with given options if any
\IfValueT {#1} {\keys_set:nn {bubblechart} {#1}}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_tikz_prop {
\clist_put_right:Nn \l_tikz_clist {##1=##2}
}
\str_set:Nx \l_tikz_str {[\clist_use:Nn \l_tikz_clist {,}]}
\int_zero_new:N \l_count_int
% Command to add a bubble
% #1: label
% #2: percent
\DeclareDocumentCommand \value {mm} {
\tl_set:Nx \l_bubbleradius_tl {\dim_to_decimal_in_unit:nn {##2\l_radius_dim / 100} {1cm}}
\bool_if:nTF {\l_vertical_bool} {
\tl_set:Nn \l_x_tl {0}
\tl_set:Nx \l_y_tl {-\dim_to_decimal_in_unit:nn {(2\l_radius_dim + \l_gap_dim) * \l_count_int} {1cm}}
}{
\tl_set:Nx \l_x_tl {\dim_to_decimal_in_unit:nn {(2\l_radius_dim + \l_gap_dim) * \l_count_int} {1cm}}
\tl_set:Nn \l_y_tl {0}
}
% \tl_set:Nn \l_diam_tl {\fp_to_decimal:n {\l_radius_dim * 2}cm} % diameter for makebox
\exp_last_unbraced:Ne \fill {[\l_filloptions_str]} (\l_x_tl, \l_y_tl) circle (\l_radius_dim);
\exp_last_unbraced:Ne \draw {[\l_drawoptions_str]} (\l_x_tl, \l_y_tl) circle (\l_bubbleradius_tl);
\exp_last_unbraced:Ne \node {(bubble\int_use:N \l_count_int)} at (\l_x_tl,\l_y_tl) {\makebox[2\l_radius_dim][c]{\use:c {\l_valuecs_str} {##2}}};
\exp_last_unbraced:Ne \node {[\l_labelpos_str=of~bubble\int_use:N \l_count_int,\l_labeloptions_tl]} {\use:c {\l_labelcs_str} {##1}};
\int_incr:N \l_count_int
}
% start a picture (expand option before begin)
\exp_last_unbraced:Nno \begin {tikzpicture} {\l_tikz_str}
}{
\end{tikzpicture}
}
% Environment to hold a radial chart
\NewDocumentEnvironment {radialchart} {o} {
\prop_clear_new:N \l_tikz_prop % properties for tikzpicture
\clist_clear_new:N \l_tikz_clist % properties for tikzpicture as keyval string
% keyval options for the environment
\keys_define:nn {radialchart} {
radius .dim_set:N = \l_radius_dim, % max radius
gap .dim_set:N = \l_gap_dim, % gap between radials
line~width .dim_set:N = \l_linewidth_dim, % line width
fill-options .tl_set:N = \l_filloptions_tl, % TikZ options to fill/draw the center of the radial with
draw-options .tl_set:N = \l_drawoptions_tl, % TikZ options to draw the radial with
label-options .tl_set:N = \l_labeloptions_tl, % TikZ options to draw the label with
label-cs .str_set:N = \l_labelcs_str, % cs name to format labels with
label-pos .choices:nn =
{left, right, above, below, above right, above left, below right, below left}
{\str_set:Nx \l_labelpos_str {\l_keys_choice_tl}},
label-pos / unknown .code:n = {
\msg_error:nnxxx { l3charts } { unknown-choice }
{ label-pos } % Name of choice key
{left, right, above, below, above right, above left, below right, below left} % Valid choices
{ ##1 } % Invalid choice given
},
value-cs .str_set:N = \l_valuecs_str, % cs name to format values with
vertical .bool_set:N = \l_vertical_bool, % stack radials vertically
% forward all other options to tikzpicture
unknown .code:n = {\prop_put:Nxx \l_tikz_prop {\l_keys_key_str} {##1}}
}
% set default options
\keys_set:nx {radialchart} {
radius = 1cm,
gap = 2.5ex,
line~width = 3mm,
fill-options = {fill=none,draw=black!10},
draw-options = black,
label-options = {},
label-cs = identity,
label-pos = above,
value-cs = percent,
vertical = false,
% default options forwarded to tikzpicture
line~cap = round
}
% override with given options if any
\IfValueT {#1} {\keys_set:nn {radialchart} {#1}}
% set tikz 'line width' option
\prop_put:Nxx \l_tikz_prop {line~width} {\dim_use:N \l_linewidth_dim}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_tikz_prop {
\clist_put_right:Nn \l_tikz_clist {##1=##2}
}
\str_set:Nx \l_tikz_str {[\clist_use:Nn \l_tikz_clist {,}]}
\int_zero_new:N \l_count_int
% Command to add a radial
% #1: label
% #2: percent
\DeclareDocumentCommand \value {mm} {
\tl_set:Nx \l_tmpa_tl {\fp_to_decimal:n {90 - 360 * ##2 / 100}}
\bool_if:nTF {\l_vertical_bool} {
\tl_set:Nn \l_x_tl {0}
\tl_set:Nx \l_y_tl {-\dim_to_decimal_in_unit:nn {(2\l_radius_dim + \l_gap_dim) * \l_count_int} {1cm}}
}{
\tl_set:Nx \l_x_tl {\dim_to_decimal_in_unit:nn {(2\l_radius_dim + \l_gap_dim) * \l_count_int} {1cm}}
\tl_set:Nn \l_y_tl {0}
}
\exp_last_unbraced:Ne \fill {[\l_filloptions_tl]} (\l_x_tl,\l_y_tl) circle (\l_radius_dim);
\exp_last_unbraced:Ne \centerarc {[\l_drawoptions_tl]} (\l_x_tl,\l_y_tl) (90,\l_tmpa_tl,\l_radius_dim);
\exp_last_unbraced:Ne \node {(radial\int_use:N \l_count_int)} at (\l_x_tl,\l_y_tl) {\makebox[2\l_radius_dim][c]{\use:c {\l_valuecs_str} {##2}}};
\exp_last_unbraced:Ne \node {[\l_labelpos_str=of~radial\int_use:N \l_count_int,\l_labeloptions_tl]} {\use:c {\l_labelcs_str} {##1}};
\int_incr:N \l_count_int
}
% start a picture (expand option before begin)
\exp_last_unbraced:Nno \begin {tikzpicture} {\l_tikz_str}
}{
\end{tikzpicture}
}
% Environment to hold a arc chart
\NewDocumentEnvironment {arcchart} {o} {
\prop_clear_new:N \l_tikz_prop % properties for tikzpicture
\clist_clear_new:N \l_tikz_clist % properties for tikzpicture as keyval string
% keyval options for the environment
\keys_define:nn {arcchart} {
radius .dim_set:N = \l_radius_dim, % radius of outer arc
gap .dim_set:N = \l_gap_dim, % gap between arcs
line~width .dim_set:N = \l_linewidth_dim, % line width
fill-options .tl_set:N = \l_filloptions_tl, % TikZ options to fill/draw background of the arcs with
draw-options .tl_set:N = \l_drawoptions_tl, % TikZ options to draw the arcs with
label-options .tl_set:N = \l_labeloptions_tl, % TikZ options to draw the label with
label-cs .str_set:N = \l_labelcs_str, % cs name to format labels with
value-options .tl_set:N = \l_valueoptions_tl, % TikZ options to draw values with
value-cs .str_set:N = \l_valuecs_str, % cs name to format values with
value-angle .fp_set:N = \l_valueangle_fp, % value angle
% forward all other options to tikzpicture
unknown .code:n = {\prop_put:Nxx \l_tikz_prop {\l_keys_key_str} {##1}}
}
% set default options
\keys_set:nx {arcchart} {
radius = 2cm,
gap = 1mm,
line~width = 4mm,
fill-options = {fill=none,draw=black!10},
draw-options = black,
label-options = {left},
label-cs = identity,
value-options = {},
value-cs = nop,
value-angle = 90,
% default options forwarded to tikzpicture
line~cap = round
}
% override with given options if any
\IfValueT {#1} {\keys_set:nn {arcchart} {#1}}
% set tikz 'line width' option
\prop_put:Nxx \l_tikz_prop {line~width} {\dim_use:N \l_linewidth_dim}
% turn the tikz prop into a keyval string
\prop_map_inline:Nn \l_tikz_prop {
\clist_put_right:Nn \l_tikz_clist {##1=##2}
}
\str_set:Nx \l_tikz_str {[\clist_use:Nn \l_tikz_clist {,}]}
\int_zero_new:N \l_count_int
% Command to add a radial
% #1: TikZ drawing options
% #2: label
% #3: percent
\DeclareDocumentCommand \value {O{}mm} {
% 270 is 100%
\tl_set:Nx \l_tmpa_tl {\fp_to_decimal:n {90 - 270 * ##3 / 100}}
% radius is smaller at each step
\dim_set:Nn \l_tmpa_dim {\l_radius_dim - \l_count_int\l_linewidth_dim - \l_count_int\l_gap_dim}
\exp_last_unbraced:Ne \centerarc {[\l_filloptions_tl]} (0,0) (90,90-270,\l_tmpa_dim);
\exp_last_unbraced:Ne \centerarc {[##1]} (0,0) (90,\l_tmpa_tl,\l_tmpa_dim);
\exp_last_unbraced:Ne \node {[\l_valueoptions_tl]} at (\fp_use:N \l_valueangle_fp \c_colon_str \l_tmpa_dim) {\fontsize{0.5\l_linewidth_dim}{0.5\l_linewidth_dim}\selectfont\use:c {\l_valuecs_str} {##3}};
\exp_last_unbraced:Ne \node {[\l_labeloptions_tl]} at (90 \c_colon_str \l_tmpa_dim) {\fontsize{\l_linewidth_dim}{\l_linewidth_dim}\selectfont\use:c {\l_labelcs_str} {##2}};
\int_incr:N \l_count_int
}
% start a picture (expand option before begin)
\exp_last_unbraced:Nno \begin {tikzpicture} {\l_tikz_str}
}{
\end{tikzpicture}
}
\ExplSyntaxOff