FUNCTION NORM_COORD, range

; This function takes a range vector [min, max] as contained
; in the [XYZ]RANGE property of an object and converts it to
; a scaling vector (suitable for the [XYZ]COORD_CONV property)
; that scales the object to fit in the range [0,1].

scale = [-range[0]/(range[1]-range[0]), 1/(range[1]-range[0])]

RETURN, scale

END


pro sample_colorbar_and_plot

;Introduce array to plot
data=findgen(100)

;For plot,we need a model object and a plot object, also include axes objects
myplot=obj_new('IDLgrPlot',data)
myplotmodel=obj_new('IDLgrModel')
;myxaxis=obj_new('IDLgrAxis',0)		;0 is x
;myyaxis=obj_new('IDLgrAxis',1)		;1 is y

;Get the range with the getproperty method from the myplot object
myplot->Getproperty, XRANGE=xr, YRANGE=yr
;myxaxis->Setproperty, RANGE=xr
;myyaxis->Setproperty, RANGE=yr
myplot->SetProperty, XCOORD_CONV=500*norm_coord(xr), YCOORD_CONV=500*norm_coord(yr)

myxaxis = OBJ_NEW('IDLgrAxis', 0, RANGE=[xr[0], xr[1]])
myxaxis -> SetProperty, XCOORD_CONV=500*norm_coord(xr)
myyaxis = OBJ_NEW('IDLgrAxis', 1, RANGE=[yr[0], yr[1]])
myyaxis -> SetProperty, YCOORD_CONV=500*norm_coord(yr)

xtl = 0.02 * (xr[1] - xr[0])
ytl = 0.02 * (yr[1] - yr[0])
;myxaxis -> SetProperty, TICKLEN=xt1
;myyaxis -> SetProperty, TICKLEN=yt1


xaxis_text=obj_new('IDLgrText','X Axis')
yaxis_text=obj_new('IDLgrText','Y Axis')
myfont=obj_new('IDLgrFont','times',size=8)

xaxis_text->Setproperty,font=myfont
yaxis_text->Setproperty,font=myfont
myxaxis->SetProperty,TITLE=xaxis_text
myyaxis->SetProperty,TITLE=yaxis_text

myyaxis->GetProperty, TICKTEXT=xtick_text
myxaxis->GetProperty, TICKTEXT=ytick_text

xtick_text->SetProperty, font=myfont
ytick_text->SetProperty, font=myfont

myplotmodel->add,myplot
myplotmodel->add,myxaxis
myplotmodel->add,myyaxis
;myplotmodel->scale,2,2,1

;Now deal with colorbar and its properties
mytitle=obj_new('IDLgrText','My Colorbar') ;create an object of IDLgrText class
bardims=[10,200]
redvalues=BINDGEN(256)
greenvalues=redvalues
bluevalues=reverse(redvalues)
mycolorbar=obj_new('IDLgrColorbar',redvalues,greenvalues,bluevalues,$
        TITLE=mytitle, DIMENSIONS=barDims,/Show_Axis,/Show_outline)
mycolorbarmodel=obj_new('IDLgrModel')

mycolorbarmodel->Add,mycolorbar

;Now set up the Window to show the viewing surface and the group
mygroup=obj_new('IDLgrViewgroup')
myview=obj_new('IDLgrView')
myview->add,myplotmodel
myview->add,mycolorbarmodel
mygroup->add,[myview,xaxis_text,yaxis_text,myfont,mytitle]
mywindow=obj_new('IDLgrWindow',RETAIN=2,GRAPHICS_TREE=mygroup,UNITS=2)
mywindow->Getproperty,Screen_Dimensions=these_dim
print,these_dim
;Now that we have the dimensions of the window, we can figure out 
;where to put the colorbar

;myview->add,mycolorbarmodel
;myview->add,myplotmodel
;myview->add,mycolorbarmodel
;barplustextdims=mycolorbar->ComputeDimensions(mywindow)
;mycolorbarmodel->Translate, -bardims[0]+(barplustextdims[0]/.5), $
;  -bardims[1]+(barplustextdims[1]/2.),0
mycolorbarmodel->Translate,-100,150,1 ;the first is x, second is y
;mycolorbarmodel->scale,2,20,1
; Use the SET_VIEW procedure to add an appropriate viewplane rectangle
; to the view object.
SET_VIEW,myview,mywindow

mywindow->Draw,myview

data2=findgen(20)+5
myplot2=obj_new('IdlgrPlot',data2)
myplotmodel->add,myplot2
myplot2->SetProperty, XCOORD_CONV=500*norm_coord(xr), YCOORD_CONV=500*norm_coord(yr)


mywindow->Draw,myview,/draw_instance

stop
myclipboard=obj_new('IDLgrClipboard',DIMENSIONS=windowsize,GRAPHICS_TREE=mygroup)


myclipboard->Draw,FILENAME='./new_sample.ps',/postscript

end
