📄 bygraph_g.class
字号:
// bygraph_g
//
// Base class for by graphs
*! version 1.0.9 30nov2004
version 8
class {
class:
graphfamily = "by"
instance:
bygraphstyle style
n = 0 // number of subgraphs
rows = 0
cols = 0
set_rows = 0 // from user
set_cols = 0 // from user
set_colfirst = 0 // from user
has_total = .yesno.new , style(no)
holes = "" // list of holes
xlst = "" // list of xaxes
ylst = "" // list of yaxes
prlst = "" // list of plotregions
array graphs // array of bygraphs
draw_legend = .yesno.new // is legend drawn
at_legend = .bylegend_g.new // only if at a hole
legend_hole = 0 // hole for legend
} , inherit(graph_g)
// ---------------------------------------------------------------------------
program new
syntax [ , STYLE(string) SUBVIEW READLOG(string) * ]
if `"`style'"' == `""' {
local style scheme bygraph
}
.Super.new , style(`style') `subview' `options'
._scheme.ref = .`c(curscm)'.ref // declare local scheme
if "`readlog'" != "" { // reading from a file
.runfromfile `readlog'
exit
}
end
program parse_make , sortpreserve
gettoken do cmd : 0
gettoken by cmd : cmd
gettoken glob_if cmd : cmd
gettoken glob_in cmd : cmd
gettoken glob_op cmd : cmd
local 0 `by' // parse by
syntax [varlist] [ , ///
TOTal MISSing ///
Rows(numlist integer >0 min=0 max=1) COLFirst ///
Cols(numlist integer >0 min=0 max=1) ///
SCALE(string) ISCALE(string) ///
LEGend(string) LEGLOC(string) ///
* ]
if `:word count `rows' `cols'' > 1 {
di as error "may not specify both rows() and cols()"
exit 198
}
tempname log
.`log' = {}
.`log'.Arrpush .set_rows = 0`rows'
.`log'.Arrpush .set_cols = 0`cols'
.`log'.Arrpush .set_colfirst = "`colfirst'" != ""
_fr_runlog `log'
.`log' = {}
.parse_settings `log' , `options' // by graph settings
local options `"`r(rest)'"'
_fr_runlog `log'
.`log' = {}
if "`missing'" == "" {
tempname missvar
gen byte `missvar' = 1
markout `missvar' `varlist' , strok
}
// create by graphs,
// fill graphs array
tempname obsnum
gen long `obsnum' = _n
local sortedby (`:sortedby' `obsnum')
bysort `varlist' `sortedby': RunGraph `do' `"`glob_if'"' ///
`"`glob_in'"' `"`glob_op'"' "`missvar'" "`commonscheme'" `cmd'
if "`total'" != "" {
tempname one lblone
qui gen byte `one' = 1
label define `lblone' 1 Total
label values `one' `lblone'
bysort `one' `sortedby' : RunGraph `do' `"`glob_if'"' ///
`"`glob_in'"' `"`glob_op'"' "`missvar'" ///
"`commonscheme'" `cmd'
label drop `lblone'
.`log'.Arrpush .has_total.set_true
}
if 0`.graphs.arrnels' == 0 {
di as error "nothing to graph"
exit 198
}
.n = 0`.graphs.arrnels'
.`log'.Arrpush .n = 0`.graphs.arrnels'
.`log'.Arrpush .MapGraphs
// "plotregion1" is a grid
.`log'.Arrpush .insert (plotregion1 = .graph_g.new, ///
style(\`.style.plotregionstyle.snm')) new
.`log'.Arrpush .SetLegend , `legend' `legloc' // create the legend
.`log'.Arrpush .set_placement // place by graphs
// set styles to subs
.`log'.Arrpush .set_axes_titles
.`log'.Arrpush .set_axes_shown
.`log'.Arrpush .set_axes_labels // if style/scheme sets off
.`log'.Arrpush .set_axes_ticks // must reset to get anywhere
.`log'.Arrpush .set_rescaled // set rescaled
.`log'.Arrpush .set_shrink // set shrinkage
.`log'.Arrpush .set_scale `scale'
.`log'.Arrpush .set_iscale `iscale'
.`log'.Arrpush .set_outer_axes
// .`log'.Arrpush .set_outer_axes_edges_only
if 0`.style.indiv_as_whole.istrue' {
.`log'.Arrpush .set_margins
}
_fr_runlog `log'
.`log' = {}
/*
local laxes `.graphs[1].list_axes left'
local raxes `.graphs[1].list_axes right'
local taxes `.graphs[1].list_axes above'
local baxes `.graphs[1].list_axes below'
// x/yalt errors
if "`taxes'" != "" & "`xalternate'`xsalternate'" != "" {
local xalternate
local xsalternate
di in green "note, graphs have top axes, `xalternate'"
"`xsalternate' ignored"
}
if "`raxes'" != "" & "`yalternate'`ysalternate'" != "" {
local yalternate
local yalternate
di in green "note, graphs have right axes, `yalternate'"
"`ysalternate' ignored"
}
*/
// put our titles at the front
// of options so they can be
// supseeded by typed options
SetByList bylist : `varlist' // title
local options `"note("Graphs by `bylist'") `options'"'
foreach d in x y { // "axis" titles
if 0`.style.outer_`d'titles.istrue' {
local text `"`.graphs[1].get_axis_title `d''"'
local tsty \`.graphs[1].get_axis_title_styleref `d''
if `"`text'"' != `""' {
local p = cond("`d'"=="x", "b", "l")
local tsty`p' `macval(tsty)'
local options `"`p'1title(`text') `options'"'
/*
local options ///
`"`p'1title(`text', style(`tsty')) `options'"'
*/
}
}
}
// standard areastyles
_fr_area_parse_and_log `log' "" GRAPHRegion , `options'
_fr_area_parse_and_log `log' plotregion1 PLOTRegion , `r(rest)'
.parse_and_log_titles `log' "" `r(rest)' // standard titles
.parse_and_log_spacers `log' "" `r(rest)' // spacers
// bgcolor
_fr_merged_implicit `log' BGColor ///
".bgcolor.setstyle , style(X)" , `r(rest)'
local 0 `", `r(rest)'"'
syntax [, FAKE_OPT_FOR_BETTER_MSG ]
_fr_runlog `log'
foreach p in b l { // axis title styles
if "tsty`p'" != "" { // extra work because
.`p'1title.setstyle , style(`tsty`p'') // tsty a reference
.__LOG.Arrpush .`p'1title.setstyle , style(`macval(tsty`p')')
}
}
end
// ---------------------------------------------------------------------------
// Pushes log entries to map the graphs when reading from disk.
program MapGraphs
forvalues i = 1/`.n' {
.__LOG.Arrpush __NOLOG__ ///
.graphs[`i'] =.__Map.`.graphs[`i'].uname'.ref
}
end
// ---------------------------------------------------------------------------
// Parse and set bygraph settings
program parse_settings , rclass
gettoken log 0 : 0
syntax [ , COMpact ///
STYLE(passthru) ///
NORescale Rescale ///
NOXRescale NOYRescale XRescale YRescale ///
HOLes(numlist integer >0 max=300) ///
NOEDGElabel EDGElabel ///
NOIXaxes NOIYaxes IXaxes IYaxes ///
NOXTitles XTitles NOYTitles YTitles ///
NOIXLabels NOIYLabels IXLabels IYLabels ///
NOIXTicks NOIYTicks IXTicks IYTicks ///
NOIXTitles NOIYTitles IXTitles IYTitles ///
NOINDividual INDividual ///
ILEGends ///
IMargin(string) ///
NOSHRink NOISHRink NOOVERALLSHRink ///
SHRink ISHRink OVERALLSHRink ///
* ]
// XAlternate XSalternate YAlternate YSalternate
local holes : list uniq holes
.`log'.Arrpush .holes = "`holes'"
// overall options
if "`style'" == "" & "`compact'" != "" {
local style style(compact)
}
if "`style'" != "" {
.`log'.Arrpush .style.setstyle , `style'
}
// outer axis titles
if "`xtitles'`noxtitles'" != "" {
.`log'.Arrpush ._set_xor outer_xtitles "`xtitles'" "`noxtitles'"
}
if "`ytitles'`noytitles'" != "" {
.`log'.Arrpush ._set_xor outer_ytitles "`ytitles'" "`noytitles'"
}
// edge labels
if "`edgelabel'`noedgelabel'" != "" {
.`log'.Arrpush ._set_xor edgelabel "`edgelabel'" "`noedgelabel'"
}
// rip apart or not
if "`individual'`noindividual'" != "" {
.`log'.Arrpush ._set_xor indiv_as_whole ///
"`individual'" "`noindividual'"
}
if "`ilegends'" != "" {
.`log'.Arrpush ._set_xor indiv_legends "`ilegends'"
}
// shrinking
if "`shrink'`noshrink'" != "" {
.`log'.Arrpush ._set_xor shrink_indiv "`shrink'" "`noshrink'"
.`log'.Arrpush ._set_xor shrink_plotregion ///
"`shrink'" "`noshrink'"
}
if "`ishrink'`noishrink'" != "" {
.`log'.Arrpush ._set_xor shrink_indiv "`ishrink'" "`noishrink'"
}
if "`overallshrink'`nooverallshrink'" != "" {
.`log'.Arrpush ._set_xor shrink_plotregion ///
"`overallshrink'" "`nooverallshrink'"
}
// individual/by graphs
if "`ixaxes'`noixaxes'" != "" {
.`log'.Arrpush ._set_xor indiv_xaxes "`ixaxes'" "`noixaxes'"
}
if "`iyaxes'`noiyaxes'" != "" {
.`log'.Arrpush ._set_xor indiv_yaxes "`iyaxes'" "`noiyaxes'"
}
if "`ixtitles'`noixtitles'" != "" {
.`log'.Arrpush ._set_xor indiv_xtitles "`ixtitles'" "`noixtitles'"
}
if "`iytitles'`noiytitles'" != "" {
.`log'.Arrpush ._set_xor indiv_ytitles "`iytitles'" "`noiytitles'"
}
if "`ixlabels'`noixlabels'" != "" {
.`log'.Arrpush ._set_xor indiv_xlabels ///
"`ixlabels'" "`noixlabels'"
}
if "`iylabels'`noiylabels'" != "" {
.`log'.Arrpush ._set_xor indiv_ylabels ///
"`iylabels'" "`noiylabels'"
}
if "`ixticks'`noixticks'" != "" {
.`log'.Arrpush ._set_xor indiv_xticks "`ixticks'" "`noixticks'"
}
if "`iyticks'`noiyticks'" != "" {
.`log'.Arrpush ._set_xor indiv_yticks "`iyticks'" "`noiyticks'"
}
// rescaling
if "`rescale'" != "" {
local xrescale xrescale // implies x and y
local yrescale yrescale
}
else if "`norescale'" != "" {
local noxrescale noxrescale // implies x and y
local noyrescale noyrescale
}
if "`xrescale'`noxrescale'" != "" {
.`log'.Arrpush ._set_xor indiv_xrescale "`xrescale'" "`noxrescale'"
if "`xrescale'" != "" {
.`log'.Arrpush .style.editstyle indiv_xaxes(yes) editcopy
.`log'.Arrpush .style.editstyle outer_xaxes(yes) editcopy
}
}
if "`yrescale'`noyrescale'" != "" {
.`log'.Arrpush ._set_xor indiv_yrescale "`yrescale'" "`noyrescale'"
if "`yrescale'" != "" {
.`log'.Arrpush .style.editstyle indiv_yaxes(yes) editcopy
.`log'.Arrpush .style.editstyle outer_yaxes(yes) editcopy
}
}
if `"`imargin'"' != `""' {
.`log'.Arrpush .style.editstyle indiv_margin(`imargin') editcopy
}
return local rest `"`options'"'
end
// ---------------------------------------------------------------------------
program define RunGraph , byable(recall, noheader)
gettoken do cmd_1 : 0
gettoken glob_if cmd_1 : cmd_1
gettoken glob_in cmd_1 : cmd_1
gettoken glob_op cmd_1 : cmd_1
gettoken missvar cmd_1 : cmd_1
gettoken uniq_schm cmd_1 : cmd_1
// by titles
tempname n
gen long `n' = _n
sum `n' if `_byindex' == _byindex() , meanonly
local obs `r(min)'
foreach var of local _byvars {
if substr("`:type `var''", 1, 3) == "str" {
local vtitl `"`=`var'[`obs']'"'
if `"`vtitl'"' == `""' {
local vtitl (missing)
}
}
else {
local vtitl `"`: label (`var') `=`var'[`obs']''"'
if `"`vtitl'"' == `"."' {
local vtitl (missing)
}
}
local title `"`title'`sep'`vtitl'"'
local sep ", "
}
// by options
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -