-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinepalette.ado
375 lines (356 loc) · 11.5 KB
/
linepalette.ado
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
*! version 1.0.1 27dec2018 Ben Jann
program linepalette
version 9.2
capt _on_colon_parse `0'
if _rc==0 {
local 0 `"`s(before)'"'
local rhs `"`s(after)'"'
_parse comma lhs 0 : 0
if `"`lhs'"'!="" error 198
if `"`rhs'"'=="" local rhs default
local palettes
local palette
local space
while (`"`rhs'"'!="") {
gettoken p rhs : rhs, parse("/") quotes bind
if `"`p'"'=="/" {
local palettes `"`palettes'`"`palette'"' "'
local palette
local space
continue
}
local palette `"`palette'`space'`p'"'
local space " "
}
if `"`palette'"'!="" {
local palettes `"`palettes'`"`palette'"'"'
}
Graph2 `palettes' `0'
exit
}
Palette_Get `0'
if "`GRAPH'"=="" {
tempname hcurrent
_return hold `hcurrent'
_return restore `hcurrent', hold // make copy
Graph, `GROPTS'
_return restore `hcurrent'
}
end
/*----------------------------------------------------------------------------*/
/* retrieve palette */
/*----------------------------------------------------------------------------*/
program Palette_Get, rclass
syntax [anything(name=palette id="palette" everything equalok)] ///
[, noGRaph GRopts(str asis) TItle(passthru) LWidth(passthru) rows(passthru) ///
N(numlist max=1 integer >=1) Select(numlist integer >=1) Reverse * ]
c_local GRAPH "`graph'"
c_local GROPTS `"`rows' `title' `lwidth' `gropts'"'
// get palette
if `"`palette'"'=="" local palette default
local islist = (`: list sizeof palette'!=1)
if `islist'==0 {
capt confirm name _`palette'
if _rc local islist 1
}
if `islist'==0 {
capt _Palette_Get `palette', n(`n') `options'
if _rc==199 {
capt confirm name `palette'
if _rc { // numeric palette name: cannot be a named style
di as err `"palette `palette' not found"'
exit 198
}
local islist 1
}
else if _rc { // display error message
_Palette_Get `palette', n(`n') `options'
}
}
if `islist' {
local i 0
foreach p of local palette {
local ++i
local p`i' `"`p'"'
}
local n `i'
local palette "custom"
}
// select/order
if "`reverse'"!="" {
if "`select'"=="" {
qui numlist "`n'(-1)1"
local select `r(numlist)'
}
else {
local select0 `select'
local select
foreach s of local select0 {
local select `s' `select'
}
}
}
else if "`select'"=="" {
qui numlist "1/`n'"
local select `r(numlist)'
}
// return palette
local plist
local i 0
foreach j of local select {
if `"`p`j''"'!="" {
local ++i
local plist `"`plist'`space'`"`p`j''"'"'
local space " "
return local p`i' `"`p`j''"'
return local p`i'info `"`p`j'info'"'
}
}
local n `i'
local plist: list clean plist
return local p `"`plist'"'
return local pnote `"`note'"'
return local pname `"`palette'"'
return local ptype "line"
return scalar n = `n'
end
program _Palette_Get
gettoken palette 0 : 0, parse(" ,")
syntax [, n(numlist max=1 integer >0) * ]
linepalette_`palette', n(`n') `options'
if `"`P'"'!="" { // palettes that define P (and I)
mata: st_local("P", _parse_palette(st_local("P")))
mata: st_local("I", _parse_palette(st_local("I")))
local min 1
local max: list sizeof P
if "`n'"=="" local n `max'
local n = max(`min',min(`max',`n'))
}
else { // palettes that define P#
local min 1
while (`"`P`min''"'=="") {
local ++min
if `min'>100 {
c_local n 0
exit // emergency exit
}
}
local max `min'
while (`"`P`max''"'!="") {
local ++max
}
local --max
if "`n'"=="" local n `max'
local n = max(`min',min(`max',`n'))
local P `"`P`n''"'
mata: st_local("P", _parse_palette(st_local("P")))
mata: st_local("I", _parse_palette(st_local("I")))
}
local i 0
foreach c of local P {
gettoken info I : I
local ++i
if `i'>`n' continue, break
c_local p`i' `"`c'"'
c_local p`i'info `"`info'"'
}
c_local note `"`note'"'
c_local n `n'
end
/*----------------------------------------------------------------------------*/
/* graph of single palette */
/*----------------------------------------------------------------------------*/
program Graph
syntax [, rows(int 5) TItle(passthru) LWidth(passthru) * ]
if `"`lwidth'"'=="" local lwidth lwidth(medthick)
local n = r(n)
local c = max(3,ceil(sqrt(`n'/12*3)))
local cut = max(`rows',ceil(`n'/`c'))
local rows = max(5, `cut')
local lblgap = 10/`rows'
local infogap = 10/`rows'
local j 1
local r 0
forv i=1/`n' {
if `i'>(`cut'*`j') {
local ++j
local r 0
}
local ++r
if `"`r(p`i')'"'=="" continue
local jlab `j'
local j2 = `j' + .6
local jlab = `jlab' + .3
local plots `plots' (pci `r' `j' `r' `j2', recast(pcspike) ///
lpattern(`"`r(p`i')'"') `lwidth' lcolor(black))
local pnum `pnum' `r' `j' "`i'"
local lbl `lbl' `r' `jlab' `"`r(p`i')'"'
if `"`r(p`i'info)'"'!="" {
local info `info' `r' `jlab' `"`r(p`i'info)'"'
}
}
if `"`plots'"'=="" {
di as txt "(nothing to display)"
exit
}
if `rows'>=30 {
local pnumsize vsmall
local lblsize tiny
local infosize half_tiny
}
else if `rows'>=15 {
local pnumsize small
local lblsize vsmall
local infosize tiny
}
else if `rows'>=10 {
local pnumsize medsmall
local lblsize small
local infosize vsmall
}
else {
local pnumsize medium 3.8194
local lblsize medsmall
local infosize small
}
local pnum (scatteri `pnum', ms(i) msize(`size') mlabpos(9) ///
mlabgap(`lblgap') mlabsize(`pnumsize') mlabcolor(gray))
if `"`lbl'"'!="" {
local lbl (scatteri `lbl', ms(i) msize(`size') mlabpos(6) ///
mlabgap(`lblgap') mlabsize(`lblsize') mlabcolor(gray))
}
if `"`info'"'!="" {
local info (scatteri `info', ms(i) msize(`size') mlabpos(12) ///
mlabgap(`infogap') mlabsize(`infosize') mlabcolor(gray))
}
else local info
local l = 1/2 + 9
local r = 1/2 + 5
local b = 1/2 + 10
local t = 1/2 + 5
if `"`title'"'=="" {
if `"`r(pnote)'"'=="" local title title(`"`r(pname)'"')
else local title title(`"`r(pname)' `r(pnote)'"')
}
two `plots' `pnum' `lbl' `info' , `title' scheme(s2color) ///
legend(off) ylabel(none) graphr(color(white)) ///
xlabel(none) xscale(range(1 3) off) ///
yscale(range(1 `rows') off reverse) ///
plotr(margin(`l' `r' `b' `t')) graphr(margin(0 0 0 3)) `options'
end
/*----------------------------------------------------------------------------*/
/* graph of multiple palettes */
/*----------------------------------------------------------------------------*/
program Graph2
_parse comma palettes 0 : 0
syntax [, TItle(passthru) LABels(str asis) PLabels(str asis) ///
GRopts(str asis) LWidth(passthru) VERTical HORizontal * ]
if `"`labels'"'!="" local plabels `"`labels'"'
if `"`lwidth'"'=="" local lwidth lwidth(medthick)
local orientation `vertical' `horizontal'
if "`orientation'"=="" local orientation vertical
if "`orientation'"=="horizontal" {
local ii i
local jj j
}
else {
local ii j
local jj i
}
local N 1
local plots
local i 0
foreach p of local palettes {
local ++i
_parse comma pnm popts : p
if `"`popts'"'=="" local popts ,
Palette_Get `pnm' `popts' `options'
local n = r(n)
local N = max(`n',`N')
gettoken plab plabels : plabels
if `"`plab'"'=="" {
if `"`r(pnote)'"'=="" local plab `"`r(pname)'"'
else local plab `"`r(pname)' `r(pnote)'"'
}
local ylab `ylab' `i' `"`plab'"'
forv j=1/`n' {
local plots `plots' ///
(pci ``ii'' `=``jj''-.35' ``ii'' `=``jj''+.35', ///
recast(pcspike) lpattern(`"`r(p`j')'"') `lwidth' lcolor(black))
}
}
if `"`plots'"'=="" {
di as txt "(nothing to display)"
exit
}
if "`orientation'"=="horizontal" {
local xscale xscale(lstyle(none) range(.5 `N'.5))
local xlabel xlabel(1/`N', notick)
local yscale yscale(lstyle(none) range(.5 `i'.5) reverse)
local ylabel ylabel(`ylab', nogrid notick angle(hor))
}
else {
local xscale xscale(lstyle(none) range(.5 `i'.5) alt)
local xlabel xlabel(`ylab', notick)
local yscale yscale(lstyle(none) range(.5 `N'.5) reverse)
local ylabel ylabel(1/`N', nogrid notick angle(hor))
}
two `plots', `xscale' `xlabel' xti("") `yscale' `ylabel' yti("") ///
legend(off) graphr(margin(l=2 t=2 b=2 r=2) color(white)) ///
scheme(s2color) `title' `gropts'
end
/*----------------------------------------------------------------------------*/
/* palettes */
/*----------------------------------------------------------------------------*/
program linepalette_default
c_local P solid,dash,vshortdash,longdash_dot,longdash,dash_dot,dot, ///
shortdash_dot,tight_dot,dash_dot_dot,longdash_shortdash,dash_3dot, ///
longdash_dot_dot,shortdash_dot_dot,longdash_3dot
end
program linepalette_pplain // plotplain (p#lineplot)
c_local P solid,dash,vshortdash,dot,dash_dot_dot,tight_dot,longdash,dash_dot, ///
shortdash_dot,longdash_dot,longdash_shortdash,dash_3dot,longdash_dot_dot, ///
shortdash_dot_dot,longdash_3dot
end
/*----------------------------------------------------------------------------*/
/* mata */
/*----------------------------------------------------------------------------*/
version 9.2
mata:
mata set matastrict on
string scalar _parse_palette(string scalar p0)
{
real scalar i, j, blank
string rowvector p
p = tokens(p0, ",")
if (length(p)<1) return("")
blank = 1
j = 0
for (i=1;i<=length(p);i++) {
if (p[i]==",") {
if (blank) {
j++
p[j] = ""
}
blank = 1
continue
}
j++
p[j] = strtrim(p[i])
blank = 0
}
return(_invtokens_quoted(p[|1\j|]))
}
string scalar _invtokens_quoted(string vector In)
{
real scalar i
string scalar Out
if (length(In)<1) return("")
Out = "`" + `"""' + In[1] + `"""' + "'"
for (i=2; i<=length(In); i++) {
Out = Out + " `" + `"""' + In[i] + `"""' + "'"
}
return(Out)
}
end
exit