📄 piegraph_g.class
字号:
// piegraph_g
//
// class for pie graphs
*! version 1.0.9 20sep2004
version 8
class {
class:
graphfamily = "bar"
instance:
piegraphstyle style
radius = 0
array piestyles
array explodes
array ptext
array boxpos
array plabel
array plab_dex
array plab_radius
array plab_gap
} , inherit(graph_g)
// ----------------------------------------------------------------------------
// Uses .new from graph.cls
//
// Usage: .new statlist [if] [in] [wt] [, omit_opts graph_opts
// pie_opts]
program new , sortpreserve
capture syntax [anything(id=varlist equalok)] [if] [in] [aw fw pw] ///
[ , STYle(string) READLOG(passthru) * ]
if `"`style'"' == `""' {
local style scheme piegraph
}
.Super.new , style(`style')
if `"`readlog'"' != `""' {
local 0 `"ForceACmd_1 `0'"' // sic , for expand
local vlparse "anything(id=varlist equalok)"
}
else local vlparse "varlist(numeric ts default=none)"
// parse command
local ignored_opts noDRAW NAME(string) ///
SCHeme(passthru) COPYSCHeme REFSCHeme ///
XSIZe(passthru) YSIZe(passthru) ///
FXSIZe(passthru) FYSIZe(passthru) SAVing(string asis)
syntax [`vlparse'] [if] [in] [aw fw pw] [ , ///
CW OVER(varlist) MISSing SORT SORT1(varname) DEScending ///
ALLCategories BYIF(string asis) READLOG(passthru) ///
`ignored_opts' * ]
if ("`over'" == "") local allcategories
if ("`weight'" == "pweight") local weight iweight
// syntax errors
if `:list sizeof varlist' > 1 & "`over'" != "" {
di as error "option over() invalid with more than one variable"
exit 198
}
if "`sort'" != "" & "`sort1'" != "" {
di as error "may not combine sort and sort()"
exit 198
}
if "`readlog'" == "" {
if "`varlist'" == "" & "`over'" != "" { // counts
tempvar freq
qui gen byte `freq' = 1
label variable `freq' frequency
local varlist `freq'
}
marksample touse , novarlist // sample
if (`"`byif'"' != `""') qui replace `touse' = 0 if ! (`byif')
tempvar total angle label // pie vars
if "`over'" != "" { // over data
markout `touse' `varlist'
if ("`missing'" == "") markout `touse' `over' , strok
gettoken unused exp : exp , parse("=")
if "`weight'" == "aweight" { // care only
tempvar wvar // for labels
qui gen double `wvar' = `exp' if `touse'
sum `wvar' , meanonly
qui replace `wvar' = `wvar' / r(mean)
local wtexp "(`wvar')"
}
else {
local wtexp = cond("`exp'" == "" , "1" , "(`exp')")
}
sort `over'
qui by `over' : gen `total' = ///
sum(`touse'*`varlist'*`wtexp')
qui by `over' : replace `total' = . if _n != _N
qui by `over' : replace `touse' = min(sum(`touse'), 99)
qui by `over' : replace `touse' = 0 if _n != _N
if "`allcategories'" != "" {
qui replace `total' = 0 if !`touse'
qui by `over' : replace `touse' = 1 if _n == _N
if ("`missing'"=="") markout `touse' `over' , strok
if `"`if'"' != `""' & `"`byif'"' != `""' { // by
tempvar touse0
mark `touse0' `if'
qui by `over' : replace `touse0' = sum(`touse0')
qui by `over' : replace `touse' = 0 if !`touse0'
}
}
qui egen `label' = group(`over') if `touse' , ///
missing lname(_pie_lbl_)
}
else { // varlist data
if ("`cw'" != "") markout `touse' `varlist'
local K : list sizeof varlist
if _N <= `K' { // add obs
preserve
qui set obs `K'
}
capture label drop _T_pielbl
qui gen float `total' = .
local k 0
foreach var of local varlist {
// data
sum `var' [`weight'`exp'] if `touse' , meanonly
if "`weight'" == "aweight" {
local tot `r(sum)' * `r(N)' / `r(sum_w)'
}
else local tot `r(sum)'
qui replace `total' = `tot' in `++k'
// labels
capture local lab : variable label `var'
local ts = _rc
if (`ts' | "`lab'" == "") local lab `var'
label define _T_pielbl `k' `"`lab'"', add
}
qui replace `touse' = _n <= `K'
qui gen `label' = _n
label values `label' _T_pielbl
}
capture assert `total' >= 0
if _rc {
di as error "sum for a pie wedge is negative"
exit 459
}
sum `total' if `touse' , meanonly
qui gen `angle' = 360 * `total' / `r(sum)'
if (`r(sum)' == 0 | `r(sum)' >= .) {
di as error "sum of pie wedges is not positive"
// exit 459
}
local op = cond("`descending'" == "" , "" , "-")
if ("`sort'" != "") gsort `op'`total'
if ("`sort1'" != "") gsort `op'`sort1'
local ifspec if `touse'
}
// let graph_g.new handle readlog etc. it is given all
// the variables that need to be in the serset, most
// also retained as options so .makegraph can find them.
.parse_sersets `total' `angle' `label' `ifspec' , ///
labels `readlog' `options'
end
// ----------------------------------------------------------------------------
// Creates a bargraph given an existing sersets array.
//
// Usage: .makegraph , graph_options pie_opts
program makegraph
syntax [ , STYLE(passthru) Intensity(string) LIne(string) ///
ANGle0(string) NOCLockwise CLockwise ///
BNDOPTIONS(string) * ]
local origopts `"`options'"'
// style edits
if `"`intensity'"' != `""' {
local edits `"`edits' intensity(`intensity')"'
}
if `"`angle0'"' != `""' {
local edits `"`edits' angle0(`angle0')"'
}
if "`noclockwise'" != "" {
local edits `"`edits' clockwise(no)"'
}
if "`clockwise'" != "" {
local edits `"`edits' clockwise(yes)"'
}
if `"`line'"' != `""' {
local 0 `", `line'"'
syntax [, LColor(string) LWidth(string) LSTYle(string) ]
if (`"`lcolor'"' != `""') local lcolor `"color(`lcolor')"'
if (`"`lwidth'"' != `""') local lwidth `"width(`lwidth')"'
local line_eds `"linestyle(`lstyle' `lcolor' `lwidth')"'
local edits `"`edits' `line_eds'"'
}
if ( `"`edits'"' != `""' ) .style.editstyle `edits' editcopy
.sersets[1].set // create pie styles
forvalues i=1/0`:serset N' {
.piestyles[`i'] = .areastyle.new, style(scheme p`i'pie)
.SetIntensity `i' `intensity'
.piestyles[`i'].Declare pie_linestyle = .style.linestyle.ref
}
// "plotregion"
.insert (plotregion1 = .grid.new, subview style(scheme piegraph)) new
// add the view to be drawn it redirects
// drawing pies below to drawpie
.plotregion1.insert (piegraph = .pieview.new , piegraph(`.objkey') ///
xstretch(free) ystretch(free)) new
.ParsePies , `origopts' // piestyles
.ParsePtext , `r(rest)' // pie text
.ParsePlabel , `r(rest)'
tempname log // logged edits
.`log' = {}
_fr_legend_parse_and_log `log' `r(rest)' // legend
.parse_and_log_titles `log' "" `r(rest)' // titles
_fr_area_parse_and_log `log' "" GRAPHRegion , `r(rest)'
_fr_area_parse_and_log `log' plotregion1 PLOTRegion , `r(rest)'
_fr_merged_implicit `log' BGColor ".bgcolor.setstyle , style(X)" , ///
`r(rest)'
local 0 `", `r(rest)'"'
syntax [, FAKE_OPT_FOR_BETTER_MSG ]
_fr_runlog `log' , nologging
end
program SetIntensity
gettoken i intensity : 0
if (`.style.intensity.val' == ///
`.piestyles[`i'].shadestyle.intensity.val') ///
exit // Exit
local intensity `intensity' // sic
if `"`intensity'"' == `""' {
local intensity `.style.intensity.snm'
}
if `"`intensity'"' == `""' {
local intensity `.style.intensity.val'
}
.piestyles[`i'].editstyle shadestyle(intensity(`intensity')) editcopy
end
// ----------------------------------------------------------------------------
program ParsePies , rclass
syntax [ , Pie(string asis) * ]
while `"`pie'"' != `""' {
._parse_pie `pie'
local 0 `", `options'"'
syntax [ , Pie(string asis) * ]
}
return local rest `"`options'"'
end
program _parse_pie
syntax anything(name=wedges id=pie) [ , Color(string) ///
EXPLODE EXPLODE1(string) ]
// which wedges
if `"`wedges'"' == "_all" {
.sersets[1].set
local wedges "1/`:serset N'"
}
capture numlist `"`wedges'"' , integer range(>0)
if _rc {
di as error `"invalid wedge numbers in pie(), `wedges'"'
exit 198
}
local wedges "`r(numlist)'"
// create and apply
foreach dex of local wedges {
if `dex' > `:serset N' {
di in green "(`dex' exceeds number of pie wedges, ignored)"
continue
}
if "`.piestyles[`dex'].isa'" == "" { // safety
.piestyles[`dex'] = .areastyle.new, style(scheme p`dex')
.SetIntensity `dex'
}
if `"`color'"' != `""' { // color
.piestyles[`dex'].editstyle shadestyle(color(`color')) editcopy
}
if "`explode'`explode1'" != "" { // explosion
if ("`.style.explode_gap.stylename'" == "") {
local expstyle `.style.explode_gap.stylename'
}
else {
local expstyle `.style.explode_gap.val'
}
.explodes[`dex'].ref = .gsize.new , style(`expstyle')
if `"`explode1'"' != `""' {
.explodes[`dex'].setstyle , style(`explode1')
}
}
}
end
program ParsePlabel , rclass
syntax [ , PLabel(string asis) * ]
while `"`plabel'"' != `""' {
._parse_plabel `plabel'
local 0 `", `options'"'
syntax [ , PLabel(string asis) * ]
}
return local rest `"`options'"'
end
program _parse_plabel
syntax [ anything(name=who_what) ] [ , POSition(string) GAP(string) ///
FORmat(string) TSTYle(string) * ]
gettoken who what : who_what
local all = "`who'" == "_all" // who to label
if (`all') local dex 0
else local dex `who'
capture confirm integer number `dex'
if _rc {
di as error "invalid pie # in plabel(), `dex'"
exit 198
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -