; $Id: testplot.pro,v 1.1 2006/05/18 20:49:36 kneller Exp $
;###############################################################################
;
;FILENAME:
;        testplot.pro
;
;PURPOSE:
;       PROGRAM TO DEMONSTRATE THE USAGE OF cwo_drawplot and cwo_drawplot_data.
;CATEGORY:
;
;METHODS IN FILE:
;
;METHODS:
;    testplot_event
;    testplot_cleanup
;    testplot
;
;
; AUTHOR:
; Larry Kneller
; NIST Center for Neutron Research
; 100 Bureau Drive, Gaithersburg, MD 20899
; United States
; kneller@nist.gov  301-975-8839
; Thu May 18 20:45:01 2006
;
; LICENSE:
; The software in this file is written by an employee of
; National Institute of Standards and Technology
; as part of the DAVE software project.
;
; The DAVE software package is not subject to copyright protection
; and is in the public domain. It should be considered as an
; experimental neutron scattering data reduction, visualization, and
; analysis system. As such, the authors assume no responsibility
; whatsoever for its use, and make no guarantees, expressed or
; implied, about its quality, reliability, or any other
; characteristic. The use of certain trade names or commercial
; products does not imply any endorsement of a particular product,
; nor does it imply that the named product is necessarily the best
; product for the stated purpose. We would appreciate acknowledgment
; if the DAVE software is used or if the code in this file is
; included in another product.
;
;###############################################################################
;
;NAME:
;        testplot_event
;
;PURPOSE:
;
;PARAMETERS:
;
;KEYWORDS:
;
;###############################################################################
pro testplot_event,event

    widget_control,event.handler,get_uvalue=pstate

    case event.id of
    (*pstate).quit:begin
        widget_control,(*pstate).tlb,/destroy
    end;quit
    (*pstate).newdata:begin
        ;CHANGING THE DATA IN THE PLOT:

        ;CLEAR OLD DATA (IF YOU DON'T WANT TO KEEP IT)
        (*pstate).plotobj->cleardata

        npts = 21

        ;GET NEW DATA
        seed = (*pstate).seed
        ;SAMPLE DATA
        x1 = 4*!PI*findgen(2*npts)/(2*npts-1)
        x = 4*!PI*findgen(npts)/(npts-1)
        y1 = 100.0*sin(x1) + 200.0   & sy1 = sqrt(y1)
        y2 = 100.0*cos(x) + 200.0   & sy2 = sqrt(y2)
        y3 = y2 + 50.0*(randomu(seed,npts)-0.5) & sy3 = sqrt(y3)

        ;CREATE DATA OBJECTS FROM NEW DATA
        dat1 = obj_new('cwo_drawplot_data',x,y1,sy1,psym=2,color=255L*256L*256L,legend='sin')
        dat2 = obj_new('cwo_drawplot_data',x,y2,sy2,psym=-3,color=0L,legend='cos',$
                                            ebshow=0,thick=6)
        dat3 = obj_new('cwo_drawplot_data',x,y3,sy3,psym=4,color=255L,legend='cos data')

        ;ADD THE NEW DATA OBJECTS TO THE PLOT
        (*pstate).plotobj->add,dat1
        (*pstate).plotobj->add,dat2
        (*pstate).plotobj->add,dat3


        ;TRY EACH draw OPTION SEPARATELY TO SEE WHAT THEY DO.
        ;IT'S MORE CLEAR IF YOU ZOOM THE DATA BEFORE CHANGING IT.
        ;(*pstate).plotobj->defaultDraw  ;defaultDraw UPDATES THE SCALING AND DRAWS
        (*pstate).plotobj->draw         ;draw DOES NOT UPDATE THE SCALING

        (*pstate).seed = seed

    end;newdata
    (*pstate).plot:begin
    end;do nothing
    else:begin
    end;else
    endcase


end;testplot

;###############################################################################
;
;NAME:
;        testplot_cleanup
;
;PURPOSE:
;
;PARAMETERS:
;
;KEYWORDS:
;
;###############################################################################
pro testplot_cleanup,id
    widget_control,id,get_uvalue=pstate
    ptr_free,pstate
end;testplot_cleanup

;###############################################################################
;
;NAME:
;        testplot
;
;PURPOSE:
;
;PARAMETERS:
;
;KEYWORDS:
;
;###############################################################################
pro testplot

    npts = 21

    ;SAMPLE DATA
    x = 4*!PI*findgen(npts)/(npts-1)
    y1 = 100.0*sin(x) + 200.0   & sy1 = sqrt(y1)
    y2 = 100.0*cos(x) + 200.0   & sy2 = sqrt(y2)
    y3 = y2 + 50.0*(randomu(seed,npts)-0.5) & sy3 = sqrt(y3)


    ;CREATE DATA OBJECTS
    dat1 = obj_new('cwo_drawplot_data',x,y1,sy1,psym=2,color=255L,legend='sin')
    dat2 = obj_new('cwo_drawplot_data',x,y2,sy2,psym=-3,color=0L,legend='cos',$
                                        ebshow=0,$ ;OMIT ERROR BARS
                                        thick=4)
    dat3 = obj_new('cwo_drawplot_data',x,y3,sy3,psym=4,color=255L,legend='cos data')


    tlb = widget_base(title='testplot',mbar=bar,xoffset=100,yoffset=100)

    file    = widget_button(bar, value='File',/menu)
    newdata = widget_button(file,value='new data')
    quit    = widget_button(file,value='quit')

    ;CREATE PLOT AND GET PLOT OBJECT
    plot = cwo_drawplot(tlb,xsize=400,ysize=400,obj=plotobj,title ='DATA',$
                                                            xtitle='X',$
                                                            ytitle='Counts')

    ;ADD DATA OBJECTS TO PLOT OBJECT
    plotobj->add,dat1
    plotobj->add,dat2
    plotobj->add,dat3

    ;SAVE INFO IN STATE
    state = {tlb:tlb,quit:quit,plot:plot,plotobj:plotobj,seed:seed,newdata:newdata}
    pstate = ptr_new(state)

    widget_control,tlb,/realize,set_uvalue=pstate
    xmanager,'testplot',tlb,/no_block
end;testplot

