luametafun-chart.tex /size: 10 Kb    last modification: 2023-12-21 09:43
1% language=us runpath=texruns:manuals/luametafun
2
3\environment luametafun-style
4
5\startcomponent luametafun-chart
6
7\startchapter[title={Chart}]
8
9This is another example implementation but it might be handy for simple cases of
10presenting results. Of course one can debate the usefulness of certain ways of
11presenting but here we avoid that discussion. Let's start with a simple pie
12chart (\in {figure} [chart:1]).
13
14\startbuffer[1]
15\startMPcode
16    draw lmt_chart_circle [
17        samples    = { { 1, 4, 3, 2, 5, 7, 6 } },
18        percentage = true,
19        trace      = true,
20    ] ;
21\stopMPcode
22\stopbuffer
23
24\typebuffer[1][option=TEX]
25
26\startplacefigure[reference=chart:1]
27    \getbuffer[1]
28\stopplacefigure
29
30As with all these \LMTX\ extensions, you're invited to play with the parameters.
31in \in {figure} [chart:2] we see a variant that adds labels as well as one that
32has a legend.
33
34\startbuffer[2a]
35\startMPcode
36draw lmt_chart_circle [
37    height      = 4cm,
38    samples     = { { 1, 4, 3, 2, 5, 7, 6 } },
39    percentage  = true,
40    trace       = true,
41    labelcolor  = "white",
42    labelformat = "@0.1f",
43    labelstyle  = "ttxx"
44] ;
45\stopMPcode
46\stopbuffer
47
48The styling of labels and legends can be influenced independently.
49
50\typebuffer[2a][option=TEX]
51
52\startbuffer[2b]
53\startMPcode
54draw lmt_chart_circle [
55    height      = 4cm,
56    samples     =  { { 1, 4, 3, 2, 5, 7, 6 } },
57    percentage  = false,
58    trace       = true,
59    linewidth   = .125mm,
60    originsize  = 0,
61    labeloffset = 3cm,
62    labelstyle  = "bfxx",
63    legendstyle = "tfxx",
64    legend      = {
65        "first", "second", "third", "fourth",
66        "fifth", "sixths", "sevenths"
67    }
68] ;
69\stopMPcode
70\stopbuffer
71
72\typebuffer[2b][option=TEX]
73
74\startplacefigure[reference=chart:2]
75    \startcombination
76        {\getbuffer[2a]} {}
77        {\getbuffer[2b]} {}
78    \stopcombination
79\stopplacefigure
80
81A second way of rendering are histograms, and the interface is mostly the same.
82In \in {figure} [chart:3] we see two variants
83
84\startbuffer[3a]
85\startMPcode
86    draw lmt_chart_histogram [
87        samples    = { { 1, 4, 3, 2, 5, 7, 6 } },
88        percentage = true,
89        cumulative = true,
90        trace      = true,
91    ] ;
92\stopMPcode
93\stopbuffer
94
95\startbuffer[3b]
96\startMPcode
97    draw lmt_chart_histogram [
98        samples    = {
99            { 1, 4, 3, 2, 5, 7, 6 },
100            { 1, 2, 3, 4, 5, 6, 7 }
101        },
102        background = "lightgray",
103        trace      = true,
104    ] ;
105\stopMPcode
106\stopbuffer
107
108\typebuffer[3a][option=TEX]
109
110and one with two datasets:
111
112\typebuffer[3b][option=TEX]
113
114\startplacefigure[reference=chart:3]
115    \startcombination
116        {\getbuffer[3a]} {}
117        {\getbuffer[3b]} {}
118    \stopcombination
119\stopplacefigure
120
121\startbuffer[4]
122\startMPpage[offset=5mm]
123    draw lmt_chart_histogram [
124        samples         = {
125            { 1, 4, 3, 2, 5, 7, 6 },
126            { 1, 2, 3, 4, 5, 6, 7 }
127        },
128        percentage      = true,
129        cumulative      = true,
130        showlabels      = false,
131        backgroundcolor = "lightgray",
132    ] ;
133\stopMPpage
134\stopbuffer
135
136A cumulative variant is shown in \in {figure} [chart:4] where we also add a
137background (color).
138
139\typebuffer[4][option=TEX]
140
141\startplacefigure[reference=chart:4]
142    \getbuffer[4]
143\stopplacefigure
144
145A different way of using colors is shown in \in {figure} [chart:5] where each
146sample gets its own (same) color.
147
148\startbuffer[5]
149\startMPcode
150    draw lmt_chart_histogram [
151        samples    = {
152            { 1, 4, 3, 2, 5, 7, 6 },
153            { 1, 2, 3, 4, 5, 6, 7 }
154        },
155        percentage = true,
156        cumulative = true,
157        showlabels = false,
158        background = "lightgray",
159        colormode  = "local",
160    ] ;
161\stopMPcode
162\stopbuffer
163
164\typebuffer[5][option=TEX]
165
166\startplacefigure[reference=chart:5]
167    \getbuffer[5]
168\stopplacefigure
169
170As with pie charts you can add labels and a legend:
171
172\startbuffer[6a]
173\startMPcode
174    draw lmt_chart_histogram [
175        height          = 6cm,
176        samples         = { { 1, 4, 3, 2, 5, 7, 6 } },
177        percentage      = true,
178        cumulative      = true,
179        trace           = true,
180        labelstyle      = "ttxx",
181        labelanchor     = "top",
182        labelcolor      = "white",
183        backgroundcolor = "middlegray",
184    ] ;
185\stopMPcode
186\stopbuffer
187
188\typebuffer[6a][option=TEX]
189
190The previous and next examples are shown in \in {figure} [chart:6]. The height
191specified here concerns the graphic and excludes the labels,
192
193\startbuffer[6b]
194\startMPcode
195    draw lmt_chart_histogram [
196        height      = 6cm,
197        width       = 10mm,
198        samples     = { { 1, 4, 3, 2, 5, 7, 6 } },
199        trace       = true,
200        maximum     = 7.5,
201        linewidth   = 1mm,
202        originsize  = 0,
203        labelanchor = "bot",
204        labelcolor  = "black"
205        labelstyle  = "bfxx"
206        legendstyle = "tfxx",
207        labelstrut  = "yes",
208        legend      = {
209            "first", "second", "third", "fourth",
210            "fifth", "sixths", "sevenths"
211        }
212    ] ;
213\stopMPcode
214\stopbuffer
215
216\typebuffer[6b][option=TEX]
217
218\startplacefigure[reference=chart:6]
219    \startcombination
220        {\getbuffer[6a]} {}
221        {\getbuffer[6b]} {}
222    \stopcombination
223\stopplacefigure
224
225The third category concerns bar charts that run horizontal. Again we see similar
226options driving the rendering (\in {figure} [chart:7]).
227
228\startbuffer[7a]
229\startMPcode
230    draw lmt_chart_bar [
231        samples    = { { 1, 4, 3, 2, 5, 7, 6 } },
232        percentage = true,
233        cumulative = true,
234        trace      = true,
235    ] ;
236\stopMPcode
237\stopbuffer
238
239\typebuffer[7a][option=TEX]
240
241\startbuffer[7b]
242\startMPcode
243    draw lmt_chart_bar [
244        samples         = { { 1, 4, 3, 2, 5, 7, 6 } },
245        percentage      = true,
246        cumulative      = true,
247        showlabels      = false,
248        backgroundcolor = "lightgray",
249    ] ;
250\stopMPcode
251\stopbuffer
252
253\typebuffer[7b][option=TEX]
254
255Determining the offset of labels is manual work:
256
257\startbuffer[7c]
258\startMPcode
259draw lmt_chart_bar [
260    width           = 4cm,
261    height          = 5mm,
262    samples         = { { 1, 4, 3, 2, 5, 7, 6 } },
263    percentage      = true,
264    cumulative      = true,
265    trace           = true,
266    labelcolor      = "white",
267    labelstyle      = "ttxx",
268    labelanchor     = "rt",
269    labeloffset     = .25EmWidth,
270    backgroundcolor = "middlegray",
271] ;
272\stopMPcode
273\stopbuffer
274
275\typebuffer[7c][option=TEX]
276
277\startplacefigure[reference=chart:7]
278    \startcombination[3*1]
279        {\getbuffer[7a]} {}
280        {\getbuffer[7b]} {}
281        {\getbuffer[7c]} {}
282    \stopcombination
283\stopplacefigure
284
285Here is one with a legend (rendered in \in {figure} [chart:8]):
286
287\startbuffer[8]
288\startMPcode
289draw lmt_chart_bar [
290    width       = 8cm,
291    height      = 10mm,
292    samples     = { { 1, 4, 3, 2, 5, 7, 6 } },
293    trace       = true,
294    maximum     = 7.5,
295    linewidth   = 1mm,
296    originsize  = 0,
297    labelanchor = "lft",
298    labelcolor  = "black"
299    labelstyle  = "bfxx"
300    legendstyle = "tfxx",
301    labelstrut  = "yes",
302    legend      = {
303        "first", "second", "third", "fourth",
304        "fifth", "sixths", "sevenths"
305    }
306] ;
307\stopMPcode
308\stopbuffer
309
310\typebuffer[8][option=TEX]
311
312\startplacefigure[reference=chart:8]
313    \getbuffer[8]
314\stopplacefigure
315
316You can have labels per dataset as well as draw multiple datasets in
317one image, see \in {figure} [chart:9]:
318
319\startbuffer[9]
320\startMPcode
321    draw lmt_chart_bar [
322        samples = {
323            { 1, 4, 3, 2, 5, 7, 6 },
324            { 3, 2, 5, 7, 5, 6, 1 }
325        },
326        labels      = {
327            { "a1", "b1", "c1", "d1", "e1", "f1", "g1" },
328            { "a2", "b2", "c2", "d2", "e2", "f2", "g2" }
329        },
330        labeloffset = -EmWidth,
331        labelanchor = "center",
332        labelstyle  = "ttxx",
333        trace       = true,
334        center      = true,
335    ] ;
336
337    draw lmt_chart_bar [
338        samples     = {
339            { 1, 4, 3, 2, 5, 7, 6 }
340        },
341        labels      = {
342            { "a", "b", "c", "d", "e", "f", "g" }
343        },
344        labeloffset = -EmWidth,
345        labelanchor = "center",
346        trace       = true,
347        center      = true,
348    ] shifted (10cm,0) ;
349\stopMPcode
350\stopbuffer
351
352\typebuffer[9][option=TEX]
353
354\startplacefigure[reference=chart:9]
355    \getbuffer[9]
356\stopplacefigure
357
358\starttabulate[|T|T|T|p|]
359\FL
360\BC name            \BC type    \BC default \BC comment \NC \NR
361\ML
362\NC originsize      \NC numeric \NC 1mm     \NC \NC \NR
363\NC trace           \NC boolean \NC false   \NC \NC \NR
364\NC showlabels      \NC boolean \NC true    \NC \NC \NR
365\NC center          \NC boolean \NC false   \NC \NC \NR
366\ML
367\NC samples         \NC list    \NC         \NC \NC \NR
368\NC
369\NC cumulative      \NC boolean \NC false   \NC \NC \NR
370\NC percentage      \NC boolean \NC false   \NC \NC \NR
371\NC maximum         \NC numeric \NC 0       \NC \NC \NR
372\NC distance        \NC numeric \NC 1mm     \NC \NC \NR
373\ML
374\NC labels          \NC list    \NC         \NC \NC \NR
375\NC labelstyle      \NC string  \NC         \NC \NC \NR
376\NC labelformat     \NC string  \NC         \NC \NC \NR
377\NC labelstrut      \NC string  \NC auto    \NC \NC \NR
378\NC labelanchor     \NC string  \NC         \NC \NC \NR
379\NC labeloffset     \NC numeric \NC 0       \NC \NC \NR
380\NC labelfraction   \NC numeric \NC 0.8     \NC \NC \NR
381\NC labelcolor      \NC string  \NC         \NC \NC \NR
382\ML
383\NC backgroundcolor \NC string  \NC         \NC \NC \NR
384\NC drawcolor       \NC string  \NC white   \NC \NC \NR
385\NC fillcolors      \NC list    \NC         \NC primary (dark) colors \NC \NR
386\NC colormode       \NC string  \NC global  \NC \NC or \type {local} \NC \NR
387\ML
388\NC linewidth       \NC numeric \NC .25mm   \NC \NC \NR
389\ML
390\NC legendcolor     \NC string  \NC         \NC \NC \NR
391\NC legendstyle     \NC string  \NC         \NC \NC \NR
392\NC legend          \NC list    \NC         \NC \NC \NR
393\LL
394\stoptabulate
395
396Pie charts have:
397
398\starttabulate[|T|T|]
399\FL
400\BC name        \BC default \NC \NR
401\ML
402\NC height      \NC 5cm     \NC \NR
403\NC width       \NC 5mm     \NC \NR
404\NC labelanchor \NC         \NC \NR
405\NC labeloffset \NC 0       \NC \NR
406\NC labelstrut  \NC no      \NC \NR
407\LL
408\stoptabulate
409
410Histograms come with:
411
412\starttabulate[|T|T|]
413\FL
414\BC name        \BC default \NC \NR
415\ML
416\NC height      \NC 5cm     \NC \NR
417\NC width       \NC 5mm     \NC \NR
418\NC labelanchor \NC bot     \NC \NR
419\NC labeloffset \NC 1mm     \NC \NR
420\NC labelstrut  \NC auto    \NC \NR
421\LL
422\stoptabulate
423
424Bar charts use:
425
426\starttabulate[|T|T|]
427\FL
428\BC name        \BC default \NC \NR
429\ML
430\NC height      \NC 5cm     \NC \NR
431\NC width       \NC 5mm     \NC \NR
432\NC labelanchor \NC lft     \NC \NR
433\NC labeloffset \NC 1mm     \NC \NR
434\NC labelstrut  \NC no      \NC \NR
435\LL
436\stoptabulate
437
438\stopchapter
439
440\stopcomponent
441