gdt-plot.lua - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/gdt-plot.lua
blob: 31d0bee06affb8cfa7161ce7045600a442b75a5e (plain)
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
local concat = table.concat
local function collate(ls)
 return concat(ls, ' ')
end
local function treat_column_refs(t, js)
 if type(js) ~= 'table' then js = {js} end
 for i = 1, #js do
 local v = js[i]
 if type(v) == 'string' then
 js[i] = t:col_index(v)
 end
 end
 return js
end
local function compare_list(a, b)
 local n = #a
 for k = 1, n do
 if a[k] ~= b[k] then return false end
 end
 return true
end
local function add_unique(ls, e)
 local n = #ls
 for i = 1, n do
 if compare_list(ls[i], e) then return i end
 end
 ls[n + 1] = e
 return n + 1
end
local function collate_factors(t, i, js)
 local c = {}
 local n = #js
 for k = 1, n do
 c[k] = t:get(i, js[k])
 end
 return c
end
local function rect_bin(t, jxs, jys, jes)
 jys = treat_column_refs(t, jys)
 jxs = treat_column_refs(t, jxs)
 jes = treat_column_refs(t, jes)
 local n = #t
 local val = {}
 local enums, labels = {}, {}
 for i = 1, n do
 local c = collate_factors(t, i, jxs)
 for p = 1, #jys do
 local e = collate_factors(t, i, jes)
 if #jys > 1 then
 e[#e+1] = t:get_header(jys[p])
 end
 local ie = add_unique(enums, e)
 local ix = add_unique(labels, c)
 if not val[ix] then val[ix] = {} end
 val[ix][ie] = t:get(i, jys[p])
 end
 end
 return labels, enums, val
end
local function gdt_table_barplot(t, jxs, jys, jes)
 local rect, webcolor = graph.rect, graph.webcolor
 local labels, enums, val = rect_bin(t, jxs, jys, jes)
 local plt = graph.plot()
 local pad = 0.1
 local dx = (1 - 2*pad) / #enums
 local cat = {}
 for p, lab in ipairs(labels) do
 for q, _ in ipairs(enums) do
 local v = val[p][q]
 if v then
 local x = (p - 1) + pad + dx * (q - 1)
 local r = rect(x, 0, x + dx, val[p][q])
 plt:add(r, webcolor(q))
 end
 end
 cat[2*p-1] = p - 0.5
 cat[2*p] = collate(lab)
 end
 plt:set_categories('x', cat)
 plt.xlab_angle = math.pi/4
 if #enums > 1 then
 for k = 1, #enums do
 plt:legend(collate(enums[k]), webcolor(k), 'square')
 end
 end
 plt:show()
 return plt
end
local function legend_symbol(sym, dx, dy)
 if sym == 'square' then
 return graph.rect(5+dx, 5+dy, 15+dx, 15+dy)
 elseif sym == 'line' then
 return graph.segment(dx, 10+dy, 20+dx, 10+dy), {{'stroke'}}
 else
 return graph.marker(10+dx, 10+dy, sym, 8)
 end
end
local function add_legend(lg, k, symspec, color, text)
 local y = -k * 20
 local sym, symtr = legend_symbol(symspec, 0, y)
 local tr = (trans and trans or symtr)
 lg:add(sym, color, tr)
 lg:add(graph.textshape(25, y + 6, text, 14), 'black')
end
local function gdt_table_lineplot(t, jxs, jys, jes)
 local path, webcolor = graph.path, graph.webcolor
 local labels, enums, val = rect_bin(t, jxs, jys, jes)
 local plt, lg = graph.plot(), graph.plot()
 plt.pad, plt.clip = true, false
 lg.units, lg.clip = false, false
 for q, en in ipairs(enums) do
 local ln = path()
 local path_method = ln.move_to
 for p, lab in ipairs(labels) do
 local y = val[p][q]
 if y then
 path_method(ln, p - 0.5, y)
 path_method = ln.line_to
 else
 path_method = ln.move_to
 end
 end
 plt:addline(ln, webcolor(q))
 plt:add(ln, webcolor(q), {{'marker', size=6, mark=q}})
 if #enums > 1 then
 local label = collate(en)
 add_legend(lg, q, 'line', webcolor(q), label)
 add_legend(lg, q, q, webcolor(q), label)
 end
 end
 plt:set_legend(lg)
 local cat = {}
 for p, lab in ipairs(labels) do
 cat[2*p-1] = p - 0.5
 cat[2*p] = collate(lab)
 end
 plt:set_categories('x', cat)
 plt.xlab_angle = math.pi/4
 plt:show()
 return plt
end
local function gdt_table_xyplot(t, jx, jys, jes, opt)
 local path, webcolor = graph.path, graph.webcolor
 local use_lines = opt and opt.lines
 local use_markers = true
 if opt then
 if opt.markers == false then use_markers = false end
 end
 jes = treat_column_refs(t, jes)
 jys = treat_column_refs(t, jys)
 local enums = {}
 local n = #t
 for i = 1, n do
 local e = collate_factors(t, i, jes)
 add_unique(enums, e)
 end
 local plt, lg = graph.plot(), graph.plot()
 plt.pad, plt.clip = true, false
 lg.units, lg.clip = false, false
 local mult = #enums * #jys
 for p = 1, #jys do
 local name = t:get_header(jys[p])
 for q, enum in ipairs(enums) do
 local ln = path()
 local path_method = ln.move_to
 for i = 1, n do
 local e = collate_factors(t, i, jes)
 if compare_list(enum, e) then
 local x, y = t:get(i, jx), t:get(i, jys[p])
 if x and y then
 path_method(ln, x, y)
 path_method = ln.line_to
 else
 path_method = ln.move_to
 end
 end
 end
 local iq = (q - 1) * #jys + p
 local ienum = collate(enum) .. " " .. name
 if mult > 1 then
 add_legend(lg, iq, iq, webcolor(iq), ienum)
 end
 if use_lines then
 plt:add(ln, webcolor(iq), {{'stroke', width=3}})
 end
 if use_markers then
 plt:add(ln, webcolor(iq), {{'marker', size=6, mark=iq}})
 end
 end
 end
 if mult > 1 then plt:set_legend(lg) end
 plt:show()
 return plt
end
gdt.barplot = gdt_table_barplot
gdt.plot = gdt_table_lineplot
gdt.xyplot = gdt_table_xyplot
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月18日 03:32:02 +0000

AltStyle によって変換されたページ (->オリジナル) /