📄 legend.pro
字号:
items = replicate('',n) ; DEFAULT BLANK ARRAYendif else begin if size(items,/TNAME) NE 'STRING' then message, $ 'First parameter must be a string array. For help, type legend,/help.' if ni ne n then message,'Must have number of items equal to '+strnendelsesymline = (np ne 0) or (nl ne 0) ; FLAG TO PLOT SYM/LINE if (np ne 0) and (np ne n) and (np NE 1) then message, $ 'Must have 0, 1 or '+strn+' elements in PSYM array.' if (nl ne 0) and (nl ne n) and (nl NE 1) then message, $ 'Must have 0, 1 or '+strn+' elements in LINESTYLE array.' if (nth ne 0) and (nth ne n) and (nth NE 1) then message, $ 'Must have 0, 1 or '+strn+' elements in THICK array.' case nl of 0: linestyle = intarr(n) ;Default = solid 1: linestyle = intarr(n) + linestylei else: linestyle = linestylei endcase case nth of 0: thick = replicate(!p.thick,n) ;Default = !P.THICK 1: thick = intarr(n) + thicki else: thick = thicki endcase case np of ;Get symbols 0: psym = intarr(n) ;Default = solid 1: psym = intarr(n) + psymi else: psym = psymi endcase case nv of 0: vectorfont = replicate('',n) 1: vectorfont = replicate(vectorfonti,n) else: vectorfont = vectorfonti endcase ;; =====>> CHOOSE VERTICAL OR HORIZONTAL ORIENTATION.;if n_elements(horizontal) eq 0 then begin ; D=VERTICAL if n_elements(vertical) eq 0 then vertical = 1endif else begin if n_elements(vertical) eq 0 then vertical = not horizontalendelse;; =====>> SET DEFAULTS FOR OTHER OPTIONS.;if n_elements(box) eq 0 then box = 1if n_elements(clear) eq 0 then clear = 0if n_elements(margin) eq 0 then margin = 0.5if n_elements(delimiter) eq 0 then delimiter = ''if n_elements(charsize) eq 0 then charsize = !p.charsizeif n_elements(charthick) eq 0 then charthick = !p.charthickif charsize eq 0 then charsize = 1if (n_elements (symsize) eq 0) then symsize= charsize + intarr(n)if n_elements(number) eq 0 then number = 1 case N_elements(colorsi) of 0: colors = replicate(!P.color,n) ;Default is !P.COLOR 1: colors = replicate(colorsi,n) else: colors = colorsi endcase case N_elements(textcolorsi) of 0: textcolors = replicate(!P.color,n) ;Default is !P.COLOR 1: textcolors = replicate(textcolorsi,n) else: textcolors = textcolorsi endcase fill = keyword_set(fill)if n_elements(usersym) eq 1 then usersym = 2*[[0,0],[0,1],[1,1],[1,0],[0,0]]-1if n_elements(outline_color) EQ 0 then outline_color = !P.Color;; =====>> INITIALIZE SPACING;if n_elements(spacing) eq 0 then spacing = 1.2*charsizeif n_elements(pspacing) eq 0 then pspacing = 3*charsizexspacing = !d.x_ch_size/float(!d.x_size) * (spacing > charsize)yspacing = !d.y_ch_size/float(!d.y_size) * (spacing > charsize)ltor = 1 ; flag for left-to-rightif n_elements(left) eq 1 then ltor = left eq 1if n_elements(right) eq 1 then ltor = right ne 1ttob = 1 ; flag for top-to-bottomif n_elements(top) eq 1 then ttob = top eq 1if n_elements(bottom) eq 1 then ttob = bottom ne 1xalign = ltor ne 1 ; x alignment: 1 or 0yalign = -0.5*ttob + 1 ; y alignment: 0.5 or 1xsign = 2*ltor - 1 ; xspacing direction: 1 or -1ysign = 2*ttob - 1 ; yspacing direction: 1 or -1if not ttob then yspacing = -yspacingif not ltor then xspacing = -xspacing;; =====>> INITIALIZE POSITIONS: FIRST CALCULATE X OFFSET FOR TEXT;xt = 0if nlpv gt 0 then begin ; SKIP IF TEXT ITEMS ONLY.if vertical then begin ; CALC OFFSET FOR TEXT START for i = 0,n-1 do begin if (psym[i] eq 0) and (vectorfont[i] eq '') then num = (number + 1) > 3 else num = number if psym[i] lt 0 then num = number > 2 ; TO SHOW CONNECTING LINE if psym[i] eq 0 then expand = 1 else expand = 2 thisxt = (expand*pspacing*(num-1)*xspacing) if ltor then xt = thisxt > xt else xt = thisxt < xt endforendif ; NOW xt IS AN X OFFSET TO ALIGN ALL TEXT ENTRIES.endif;; =====>> INITIALIZE POSITIONS: SECOND LOCATE BORDER;if !x.window[0] eq !x.window[1] then begin plot,/nodata,xstyle=4,ystyle=4,[0],/noeraseendif; next line takes care of weirdness with small windowspos = [min(!x.window),min(!y.window),max(!x.window),max(!y.window)]case n_elements(position) of 0: begin if ltor then px = pos[0] else px = pos[2] if ttob then py = pos[3] else py = pos[1] if keyword_set(center) then begin if not keyword_set(right) and not keyword_set(left) then $ px = (pos[0] + pos[2])/2. - xt if not keyword_set(top) and not keyword_set(bottom) then $ py = (pos[1] + pos[3])/2. + n*yspacing endif nposition = [px,py] + [xspacing,-yspacing] end 1: begin ; interactive message,/inform,'Place mouse at upper left corner and click any mouse button.' cursor,x,y,/normal nposition = [x,y] end 2: begin ; convert upper left corner to normal coordinates if keyword_set(data) then $ nposition = convert_coord(position,/to_norm) $ else if keyword_set(device) then $ nposition = convert_coord(position,/to_norm,/device) $ else if not keyword_set(normal) then $ nposition = convert_coord(position,/to_norm) else nposition= position end else: message,'Position keyword can have 0, 1, or 2 elements only. Try legend,/help.'endcaseyoff = 0.25*yspacing*ysign ; VERT. OFFSET FOR SYM/LINE.x0 = nposition[0] + (margin)*xspacing ; INITIAL X & Y POSITIONSy0 = nposition[1] - margin*yspacing + yalign*yspacing ; WELL, THIS WORKS!;; =====>> OUTPUT TEXT FOR LEGEND, ITEM BY ITEM.; =====>> FOR EACH ITEM, PLACE SYM/LINE, THEN DELIMITER,; =====>> THEN TEXT---UPDATING X & Y POSITIONS EACH TIME.; =====>> THERE ARE A NUMBER OF EXCEPTIONS DONE WITH IF STATEMENTS.;for iclr = 0,clear do begin y = y0 ; STARTING X & Y POSITIONS x = x0 if ltor then xend = 0 else xend = 1 ; SAVED WIDTH FOR DRAWING BOX if ttob then ii = [0,n-1,1] else ii = [n-1,0,-1] for i = ii[0],ii[1],ii[2] do begin if vertical then x = x0 else y = y0 ; RESET EITHER X OR Y x = x + xspacing ; UPDATE X & Y POSITIONS y = y - yspacing if nlpv eq 0 then goto,TEXT_ONLY ; FLAG FOR TEXT ONLY if (psym[i] eq 0) and (vectorfont[i] eq '') then num = (number + 1) > 3 else num = number if psym[i] lt 0 then num = number > 2 ; TO SHOW CONNECTING LINE if psym[i] eq 0 then expand = 1 else expand = 2 xp = x + expand*pspacing*indgen(num)*xspacing if (psym[i] gt 0) and (num eq 1) and vertical then xp = x + xt/2. yp = y + intarr(num) if vectorfont[i] eq '' then yp = yp + yoff if psym[i] eq 0 then begin xp = [min(xp),max(xp)] ; TO EXPOSE LINESTYLES yp = [min(yp),max(yp)] ; DITTO endif if (psym[i] eq 8) and (N_elements(usersym) GT 1) then $ usersym,usersym,fill=fill,color=colors[i];; extra by djseed .. psym=88 means use the already defined usersymbol if psym[i] eq 88 then psym[i] =8 if vectorfont[i] ne '' then begin; if (num eq 1) and vertical then xp = x + xt/2 ; IF 1, CENTERED. xyouts,xp,yp,vectorfont[i],width=width,color=colors[i] $ ,size=charsize,align=xalign,charthick = charthick,/norm,font=font xt = xt > width xp = xp + width/2. endif else begin if symline and (linestyle[i] ge 0) then plots,xp,yp,color=colors[i] $ ,/normal,linestyle=linestyle[i],psym=psym[i],symsize=symsize[i], $ thick=thick[i] endelse if vertical then x = x + xt else if ltor then x = max(xp) else x = min(xp) if symline then x = x + xspacing TEXT_ONLY: if vertical and (vectorfont[i] eq '') and symline and (linestyle[i] eq -99) then x=x0 + xspacing xyouts,x,y,delimiter,width=width,/norm,color=textcolors[i], $ size=charsize,align=xalign,charthick = charthick,font=font x = x + width*xsign if width ne 0 then x = x + 0.5*xspacing xyouts,x,y,items[i],width=width,/norm,color=textcolors[i],size=charsize, $ align=xalign,charthick=charthick,font=font x = x + width*xsign if not vertical and (i lt (n-1)) then x = x+2*xspacing; ADD INTER-ITEM SPACE xfinal = (x + xspacing*margin) if ltor then xend = xfinal > xend else xend = xfinal < xend ; UPDATE END X endfor if (iclr lt clear ) then begin; =====>> CLEAR AREA x = nposition[0] y = nposition[1] if vertical then bottom = n else bottom = 1 ywidth = - (2*margin+bottom-0.5)*yspacing corners = [x,y+ywidth,xend,y] polyfill,[x,xend,xend,x,x],y + [0,0,ywidth,ywidth,0],/norm,color=-1; plots,[x,xend,xend,x,x],y + [0,0,ywidth,ywidth,0],thick=2 endif else begin;; =====>> OUTPUT BORDER; x = nposition[0] y = nposition[1] if vertical then bottom = n else bottom = 1 ywidth = - (2*margin+bottom-0.5)*yspacing corners = [x,y+ywidth,xend,y] if box then plots,[x,xend,xend,x,x],y + [0,0,ywidth,ywidth,0],/norm, $ color = outline_color return endelseendforend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -