【IDL】 绘制颜色棒 (colorbar)
在IDL绘图过程中,经常使用颜色棒,对iTools和IDL8.0下的快速可视化非常容易,在直接图形法和对象图形法下实现颜色棒的绘制,特别是特殊颜色棒,如两边带箭头标识的。下面colorbar对象类实现绘制颜色棒,调用参考类文件中COLORBAR_EXAMPLE,示例效果见下图。
【参考源代码】
;+
; :Description:
; 析构函数
;-
PRO COLORBAR::CLEANUP
COMPILE_OPT idl2
OBJ_DESTROY, self.OFONT
PTR_FREE,self.STEPCOLOR
self->IDLGRMODEL::CLEANUP
END
;+
;创建功能
;-
PRO COLORBAR::CreateColorBar
COMPILE_OPT idl2
objPolys = OBJARR(2,self.SCALENUM)
startLoc = self.LOCATION
colors = *(self.STEPCOLOR)
IF self.DIRECTION EQ 1 THEN eachDims = self.DIMENSION/[1,self.SCALENUM] $
ELSE eachDims = self.DIMENSION/[self.SCALENUM,1]
IF self.FLOATLEN EQ 0 THEN format='(I0)' ELSE format='(f0.'+STRTRIM(self.FLOATLEN,2)+')'
self.OFONT = OBJ_NEW('IDLgrFont','Times New Roman')
;带尖的
IF self.AXISSTYLE EQ 1 THEN BEGIN
oTexts = OBJARR(self.SCALENUM-1)
;水平方向,10个颜色,10个多边形,9个刻度
eachValue = (self.MAXVALUE - self.MINVALUE)/self.SCALENUM
oModel= OBJ_NEW('IDLgrModel')
FOR i=0, self.SCALENUM-1 DO BEGIN
;竖直的话
IF self.DIRECTION EQ 1 THEN BEGIN
xLen = eachDims[0]/2
tmpx = startLoc[0,0]
tmpy = startLoc[1,0]+eachDims[1]*i
IF i EQ 0 THEN BEGIN
data = [[tmpx+xLen/2.,tmpy],[tmpx+xLen,tmpy+eachDims[1]],[tmpx,tmpy+eachDims[1]]]
ENDIF ELSE IF i EQ self.SCALENUM-1 THEN BEGIN
data = [[tmpx+xLen,tmpy],[tmpx+xLen/2.,tmpy+eachDims[1]],[tmpx,tmpy]]
;绘制刻度
curValue = self.MINVALUE+eachValue*i
oTexts[i-1] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,tmpY]-[-1.4,.4]*xLen, $
CHAR_DIMENSIONS =[0.5,0.75]*xLen, $
font = self.OFONT)
self->ADD,oTexts[i-1]
ENDIF ELSE BEGIN
data = [[tmpx+xLen,tmpy],[tmpx+xLen,tmpy+eachDims[1]],[tmpx,tmpy+eachDims[1]],[tmpx,tmpy]]
;绘制刻度
curValue = self.MINVALUE+eachValue*i
oTexts[i-1] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,tmpY]-[-1.4,.4]*xLen, $
CHAR_DIMENSIONS =[0.5,0.75]*xLen, $
font = self.OFONT)
self->ADD,oTexts[i-1]
ENDELSE
objPolys[0,i] = OBJ_NEW('IDLgrPolygon',data,color =colors[*,i] )
objPolys[1,i] = OBJ_NEW('IDLgrPolygon',data,color =[0,0,0],style =1,$
thick =2 )
;水平方向,10个颜色,10个多边形,9个刻度
ENDIF ELSE BEGIN
tmpx = startLoc[0,0]+eachDims[0]*i
yLen = eachDims[1]/2
tmpy = startLoc[1,0]+yLen
;;最后的[1,2]表示可用X、Y,则颜色棒大小仅为X,Y/2,剩下为文字
IF i EQ 0 THEN BEGIN
data = [[tmpx,tmpy+yLen/2.],[tmpx+eachDims[0],tmpy],[tmpx+eachDims[0],tmpy+yLen]]
ENDIF ELSE IF i EQ self.SCALENUM-1 THEN BEGIN
data = [[tmpx,tmpy],[tmpx+eachDims[0],tmpy+yLen/2],[tmpx,tmpy+yLen]]
;绘制刻度
curValue = self.MINVALUE+eachValue*i
oTexts[i-1] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,0]-[0.4,0]*yLen, $
CHAR_DIMENSIONS =[0.5,0.75]*yLen, $
font = self.OFONT)
self->ADD,oTexts[i-1]
ENDIF ELSE BEGIN
;绘制刻度
curValue = self.MINVALUE+eachValue*i
oTexts[i-1] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,0]-[0.4,0]*yLen, $
CHAR_DIMENSIONS =[0.5,0.75]*yLen, $
font = self.OFONT)
self->ADD,oTexts[i-1]
data = [[tmpx,tmpy],[tmpx+eachDims[0],tmpy],[tmpx+eachDims[0],tmpy+yLen],[tmpx,tmpy+yLen]]
ENDELSE
objPolys[0,i] = OBJ_NEW('IDLgrPolygon',data,color =colors[*,i])
objPolys[1,i] = OBJ_NEW('IDLgrPolygon',data,color =[0,0,0],style =1,$
thick =2 )
ENDELSE
self->ADD,[objPolys[0,i],objPolys[1,i]]
ENDFOR
ENDIF ELSE BEGIN
oTexts = OBJARR(self.SCALENUM+1)
;10个颜色,10个多边形,11个刻度
eachValue = (self.MAXVALUE - self.MINVALUE)/self.SCALENUM
;竖直的
IF self.DIRECTION EQ 1 THEN BEGIN
xLen = eachDims[0]/2
FOR i=0, self.SCALENUM-1 DO BEGIN
tmpx = startLoc[0,0]
tmpy = startLoc[1,0]+eachDims[1]*i
data = [[tmpx,tmpy],[tmpx+xLen,tmpy],[tmpx+xLen,tmpy+eachDims[1]],[tmpx,tmpy+eachDims[1]]]
objPolys[0,i] = OBJ_NEW('IDLgrPolygon',data,color =colors[*,i] )
objPolys[1,i] = OBJ_NEW('IDLgrPolygon',data,color =[0,0,0],style =1,$
thick =2 )
self->ADD,[objPolys[0,i],objPolys[1,i]]
;绘制刻度
curValue = self.MINVALUE+eachValue*i
oTexts[i] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,tmpY]-[-1.4,.4]*xLen, $
CHAR_DIMENSIONS =[0.5,0.75]*xLen, $
font = oFont)
self->ADD,oTexts[i]
IF i EQ self.SCALENUM-1 THEN BEGIN
i++
tmpy+=eachDims[1]
curValue = self.MINVALUE+eachValue*i
oTexts[i] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,tmpY]-[-1.4,.4]*xLen, $
CHAR_DIMENSIONS =[0.5,0.75]*xLen, $
font = oFont)
self->ADD,oTexts[i]
ENDIF
ENDFOR
ENDIF ELSE BEGIN
yLen = eachDims[1]/2
;水平的
FOR i=0, self.SCALENUM-1 DO BEGIN
tmpx = startLoc[0,0]+eachDims[0]*i
tmpy = startLoc[1,0]+yLen
data = [[tmpx,tmpy],[tmpx+eachDims[0],tmpy],[tmpx+eachDims[0],tmpy+yLen],[tmpx,tmpy+yLen]]
objPolys[0,i] = OBJ_NEW('IDLgrPolygon',data,color =colors[*,i] )
objPolys[1,i] = OBJ_NEW('IDLgrPolygon',data,color =[0,0,0],style =1,$
thick =2 )
self->ADD,[objPolys[0,i],objPolys[1,i]]
;绘制刻度
curValue = self.MINVALUE+eachValue*i
oTexts[i] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,0]-[0.2,0]*yLen, $
CHAR_DIMENSIONS =[0.5,0.75]*yLen, $
font = oFont)
self->ADD,oTexts[i]
IF i EQ self.SCALENUM-1 THEN BEGIN
i++
tmpx+=eachDims[0]
curValue = self.MINVALUE+eachValue*i
oTexts[i] = OBJ_NEW('IDLgrText',STRING(curValue,format=format),$
location = [tmpx,0]-[0.2,0]*yLen, $
CHAR_DIMENSIONS =[0.5,0.75]*yLen, $
font = oFont)
self->ADD,oTexts[i]
ENDIF
ENDFOR
ENDELSE
ENDELSE
END
;
;颜色棒类初始化函数
FUNCTION COLORBAR::INIT,$
maxValue = maxValue, $ ;刻度最大值
minValue = minValue, $ ;刻度最小值
floatLen = floatLen, $ ;浮点数小数长度
direction = direction, $ ;方向,默认为水平,1为竖直
scaleNum = scaleNum, $ ;刻度个数,默认是10
stepColor = stepColor, $ ;刻度颜色,为[3,num]数组
Dimension = dimension, $ ;颜色表尺度,默认为
location = location , $ ;颜色表位置,左下角起点坐标
axisstyle = axisstyle , $ ;style=0默认普通,style=1带尖的颜色表
extra = extra
IF (self->IDLGRMODEL::INIT(_Extra=extra) NE 1) THEN RETURN, 0
;如不设置则调用默认参数
IF N_ELEMENTS(maxValue) GT 0 THEN self.MAXVALUE = maxValue ELSE self.MAXVALUE =1
IF N_ELEMENTS(minValue) GT 0 THEN self.MINVALUE = minValue ELSE self.MINVALUE =0
IF N_ELEMENTS(floatLen) GT 0 THEN self.FLOATLEN = floatLen ELSE self.FLOATLEN =2
IF N_ELEMENTS(scaleNum) GT 0 THEN self.SCALENUM = scaleNum ELSE self.SCALENUM = 10
IF N_ELEMENTS(axisstyle) GT 0 THEN self.AXISSTYLE = axisstyle
IF N_ELEMENTS(direction) GT 0 THEN self.DIRECTION = direction
IF N_ELEMENTS(Dimension) GT 0 THEN self.DIMENSION = Dimension ELSE BEGIN
IF self.DIRECTION THEN self.DIMENSION = [60,800] ELSE self.DIMENSION = [800,60]
ENDELSE
IF N_ELEMENTS(location) GT 0 THEN self.LOCATION = location
;默认颜色表
IF N_ELEMENTS(stepColor) GT 0 THEN self.STEPCOLOR = PTR_NEW(stepColor,/No_Copy) ELSE BEGIN
LOADCT,3
TVLCT,r,g,b,/get
stepColor = BYTARR(3,self.SCALENUM)
eachStep = FIX(256/self.SCALENUM)
FOR i=0,self.SCALENUM-1 DO BEGIN
stepColor[*,i] = [r[255-i*eachStep],g[255-i*eachStep],b[255-i*eachStep]]
ENDFOR
self.STEPCOLOR =PTR_NEW(stepColor,/No_Copy)
ENDELSE
;创建
self->CREATECOLORBAR
;直接返回1了,不太严谨。
RETURN,1
END
;类初始化函数
PRO COLORBAR__DEFINE
;结构体
void = {colorBar, $
INHERITS IDLgrModel, $
polygon : PTR_NEW(), $
oFont : OBJ_NEW(), $
oModel: OBJ_NEW(), $
direction:0, $
maxValue:1., $
minValue:0., $
floatLen:2, $
scaleNum:10, $
axisstyle : 0b, $
location:[0,0],$
dimension:[0,0],$
stepcolor:PTR_NEW() }
END
;+++
;Write_By DYQ
;http://hi.baidu.com/dyqwrp
;http://blog.sciencenet.cn/?344887
;QQ: /ty笑因快乐
;调用示例程序,继承IDLgrModel,可以在程序中Add到oModel中或oView中。
;
PRO COLORBAR_EXAMPLE
;通常的颜色棒
XOBJVIEW, OBJ_NEW('colorBar')
;带尖的颜色棒
XOBJVIEW, OBJ_NEW('colorBar',/axisstyle)
;创建竖直带尖头的颜色棒
XOBJVIEW,OBJ_NEW('colorBar',axisstyle=1,/direction)
;给定最值的颜色棒,floatlen为浮点位数,0为整数显示
XOBJVIEW,OBJ_NEW('colorBar',axisstyle=1,/direction,maxValue= 10,minValue=0,floatLen=0)
;给定最值的颜色棒,floatlen为浮点位数
XOBJVIEW,OBJ_NEW('colorBar',axisstyle=0,/direction,maxValue= 10,minValue=0,floatLen=2)
;指定显示色段和颜色
XOBJVIEW,OBJ_NEW('colorBar',axisstyle=1,maxValue= 10,minValue=0,floatLen=2,$
scaleNum=3, $;三个颜色段
stepcolor = [[0,0,0],[1,0,0],[0,1,0]]*255,$;三个颜色的RGB
dimension = [200,50]);颜色棒大小
END