source: trunk/gui/scripts/histogramresult.tcl @ 2652

Last change on this file since 2652 was 2547, checked in by gah, 13 years ago
File size: 55.3 KB
Line 
1
2# ----------------------------------------------------------------------
3#  COMPONENT: HistogramResult - X/Y plot in a ResultSet
4#
5#  This widget is an X/Y plot, meant to view histograms produced
6#  as output from the run of a Rappture tool.  Use the "add" and
7#  "delete" methods to control the dataobjs showing on the plot.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2005  Purdue Research Foundation
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16package require BLT
17
18option add *HistogramResult*Element.borderWidth 1 widgetDefault
19option add *HistogramResult*Element.relief solid widgetDefault
20option add *HistogramResult*x.loose 0 widgetDefault
21option add *HistogramResult*y.loose 1 widgetDefault
22option add *HistogramResult*Element.relief solid widgetDefault
23option add *HistogramResult*Element.borderWidth 1 widgetDefault
24# Don't let the step size default to 1.0 (for barcharts)
25option add *HistogramResult*x.stepSize 1.0 widgetDefault
26option add *HistogramResult*x.subdivisions 0 widgetDefault
27
28option add *HistogramResult.width 3i widgetDefault
29option add *HistogramResult.height 3i widgetDefault
30option add *HistogramResult.gridColor #d9d9d9 widgetDefault
31option add *HistogramResult.activeColor blue2 widgetDefault
32option add *HistogramResult.dimColor gray widgetDefault
33option add *HistogramResult.controlBackground gray widgetDefault
34option add *HistogramResult.font \
35    -*-helvetica-medium-r-normal-*-12-* widgetDefault
36option add *HistogramResult*Balloon*Entry.background white widgetDefault
37
38option add *HistogramResult*autoColors {
39    #3a5fcd
40    #cdcd00
41    #cd1076
42    #0000cd
43    #cd0000
44    #00cd00
45    #009acd
46    #00c5cd
47    #a2b5cd
48    #7ac5cd
49    #66cdaa
50    #a2cd5a
51    #cd9b9b
52    #cdba96
53    #cd3333
54    #cd6600
55    #cd8c95
56    #cd00cd
57    #9a32cd
58    #6ca6cd
59    #9ac0cd
60    #9bcd9b
61    #00cd66
62    #cdc673
63    #cdad00
64    #cd5555
65    #cd853f
66    #cd7054
67    #cd5b45
68    #cd6889
69    #cd69c9
70    #551a8b
71} widgetDefault
72
73itcl::class Rappture::HistogramResult {
74    inherit itk::Widget
75
76    itk_option define -gridcolor gridColor GridColor ""
77    itk_option define -activecolor activeColor ActiveColor ""
78    itk_option define -dimcolor dimColor DimColor ""
79    itk_option define -autocolors autoColors AutoColors ""
80
81    constructor {args} {
82        # defined below
83    }
84    destructor {
85        # defined below
86    }
87    public method add {dataobj {settings ""}}
88    public method get {}
89    public method delete {args}
90    public method scale {args}
91    public method parameters {title args} {
92        # do nothing
93    }
94    public method download {option args}
95
96    protected method Rebuild {}
97    protected method ResetLimits {}
98    protected method Zoom {option args}
99    protected method Hilite {state x y}
100    protected method Axis {option args}
101    protected method GetAxes {dataobj}
102    protected method GetLineMarkerOptions { style }
103    protected method GetTextMarkerOptions { style }
104    protected method EnterMarker { g name x y text }
105    protected method LeaveMarker { g name }
106    protected method FormatLabels { g value }
107
108    private variable _dispatcher "" ;# dispatcher for !events
109    private variable _dlist ""     ;# list of dataobj objects
110    private variable _dataobj2color  ;# maps dataobj => plotting color
111    private variable _dataobj2width  ;# maps dataobj => line width
112    private variable _dataobj2dashes ;# maps dataobj => BLT -dashes list
113    private variable _dataobj2raise  ;# maps dataobj => raise flag 0/1
114    private variable _dataobj2desc   ;# maps dataobj => description of data
115    private variable _elem2dataobj   ;# maps graph element => dataobj
116    private variable _label2axis   ;# maps axis label => axis ID
117    private variable _limits       ;# axis limits:  x-min, x-max, etc.
118    private variable _autoColorI 0 ;# index for next "-color auto"
119
120    private variable _hilite       ;# info for element currently highlighted
121    private variable _axis         ;# info for axis manipulations
122    private variable _axisPopup    ;# info for axis being edited in popup
123    common _downloadPopup          ;# download options from popup
124    private variable _markers
125    private variable _xlabels
126}
127                                                                               
128itk::usual HistogramResult {
129    keep -background -foreground -cursor -font
130}
131
132# ----------------------------------------------------------------------
133# CONSTRUCTOR
134# ----------------------------------------------------------------------
135itcl::body Rappture::HistogramResult::constructor {args} {
136    Rappture::dispatcher _dispatcher
137    $_dispatcher register !rebuild
138    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
139
140    array set _downloadPopup {
141        format csv
142    }
143
144    option add hull.width hull.height
145    pack propagate $itk_component(hull) no
146
147    itk_component add main {
148        Rappture::SidebarFrame $itk_interior.main
149    }
150    pack $itk_component(main) -expand yes -fill both
151    set f [$itk_component(main) component controls]
152
153    itk_component add reset {
154        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
155            -highlightthickness 0 \
156            -image [Rappture::icon reset-view] \
157            -command [itcl::code $this Zoom reset]
158    } {
159        usual
160        ignore -borderwidth -highlightthickness
161    }
162    pack $itk_component(reset) -padx 4 -pady 2 -anchor e
163    Rappture::Tooltip::for $itk_component(reset) \
164        "Reset the view to the default zoom level"
165
166    set f [$itk_component(main) component frame]
167    itk_component add plot {
168        blt::barchart $f.plot \
169            -highlightthickness 0 -plotpadx 0 -plotpady 4
170    } {
171        keep -foreground -cursor -font
172    }
173    pack $itk_component(plot) -expand yes -fill both
174    $itk_component(plot) pen configure activeBar \
175        -foreground red -borderwidth 0
176
177    #
178    # Add bindings so you can mouse over points to see values:
179    #
180    $itk_component(plot) element bind all <Enter> \
181        [itcl::code $this Hilite at %x %y]
182    $itk_component(plot) element bind all <Motion> \
183        [itcl::code $this Hilite at %x %y]
184    $itk_component(plot) element bind all <Leave> \
185        [itcl::code $this Hilite off %x %y]
186
187    # Add support for editing axes:
188    Rappture::Balloon $itk_component(hull).axes -title "Axis Options"
189    set inner [$itk_component(hull).axes component inner]
190
191    label $inner.labell -text "Label:"
192    entry $inner.label -width 15 -highlightbackground $itk_option(-background)
193    grid $inner.labell -row 1 -column 0 -sticky e
194    grid $inner.label -row 1 -column 1 -sticky ew -pady 4
195
196    label $inner.minl -text "Minimum:"
197    entry $inner.min -width 15 -highlightbackground $itk_option(-background)
198    grid $inner.minl -row 2 -column 0 -sticky e
199    grid $inner.min -row 2 -column 1 -sticky ew -pady 4
200
201    label $inner.maxl -text "Maximum:"
202    entry $inner.max -width 15 -highlightbackground $itk_option(-background)
203    grid $inner.maxl -row 3 -column 0 -sticky e
204    grid $inner.max -row 3 -column 1 -sticky ew -pady 4
205
206    label $inner.formatl -text "Format:"
207    Rappture::Combobox $inner.format -width 15 -editable no
208    $inner.format choices insert end \
209        "%.6g"  "Auto"         \
210        "%.0f"  "X"          \
211        "%.1f"  "X.X"          \
212        "%.2f"  "X.XX"         \
213        "%.3f"  "X.XXX"        \
214        "%.6f"  "X.XXXXXX"     \
215        "%.1e"  "X.Xe+XX"      \
216        "%.2e"  "X.XXe+XX"     \
217        "%.3e"  "X.XXXe+XX"    \
218        "%.6e"  "X.XXXXXXe+XX"
219    grid $inner.formatl -row 4 -column 0 -sticky e
220    grid $inner.format -row 4 -column 1 -sticky ew -pady 4
221
222    label $inner.scalel -text "Scale:"
223    frame $inner.scales
224    radiobutton $inner.scales.linear -text "Linear" \
225        -variable [itcl::scope _axisPopup(scale)] -value "linear"
226    pack $inner.scales.linear -side left
227    radiobutton $inner.scales.log -text "Logarithmic" \
228        -variable [itcl::scope _axisPopup(scale)] -value "log"
229    pack $inner.scales.log -side left
230    grid $inner.scalel -row 5 -column 0 -sticky e
231    grid $inner.scales -row 5 -column 1 -sticky ew -pady 4
232
233    foreach axis {x y} {
234        set _axisPopup(format-$axis) "%.6g"
235    }
236    Axis scale x linear
237    Axis scale y linear
238
239    $itk_component(plot) legend configure -hide yes
240
241    #
242    # Add legend for editing hidden/elements:
243    #
244    set inner [$itk_component(main) insert end \
245        -title "Legend" \
246        -icon [Rappture::icon wrench]]
247    $inner configure -borderwidth 4
248
249    itk_component add legend {
250        Rappture::XyLegend $inner.legend $itk_component(plot)
251    }
252    pack $itk_component(legend) -expand yes -fill both
253    after idle [subst {
254        update idletasks
255        $itk_component(legend) reset
256    }]
257
258    # quick-and-dirty zoom functionality, for now...
259    Blt_ZoomStack $itk_component(plot)
260    eval itk_initialize $args
261
262    set _hilite(elem) ""
263}
264
265# ----------------------------------------------------------------------
266# DESTRUCTOR
267# ----------------------------------------------------------------------
268itcl::body Rappture::HistogramResult::destructor {} {
269}
270
271# ----------------------------------------------------------------------
272# USAGE: add <dataobj> ?<settings>?
273#
274# Clients use this to add a dataobj to the plot.  The optional <settings>
275# are used to configure the plot.  Allowed settings are -color,
276# -brightness, -width, -linestyle and -raise.
277# ----------------------------------------------------------------------
278itcl::body Rappture::HistogramResult::add {dataobj {settings ""}} {
279    array set params {
280        -color auto
281        -brightness 0
282        -width 1
283        -type "histogram"
284        -raise 0
285        -linestyle solid
286        -description ""
287        -param ""
288    }
289    foreach {opt val} $settings {
290        if {![info exists params($opt)]} {
291            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
292        }
293        set params($opt) $val
294    }
295
296    # if the color is "auto", then select a color from -autocolors
297    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
298        if {$params(-color) == "autoreset"} {
299            set _autoColorI 0
300        }
301        set color [lindex $itk_option(-autocolors) $_autoColorI]
302        if {"" == $color} { set color black }
303        set params(-color) $color
304
305        # set up for next auto color
306        if {[incr _autoColorI] >= [llength $itk_option(-autocolors)]} {
307            set _autoColorI 0
308        }
309    }
310
311    # convert -linestyle to BLT -dashes
312    switch -- $params(-linestyle) {
313        dashed { set params(-linestyle) {4 4} }
314        dotted { set params(-linestyle) {2 4} }
315        default { set params(-linestyle) {} }
316    }
317
318    # if -brightness is set, then update the color
319    if {$params(-brightness) != 0} {
320        set params(-color) [Rappture::color::brightness \
321            $params(-color) $params(-brightness)]
322
323        set bg [$itk_component(plot) cget -plotbackground]
324        foreach {h s v} [Rappture::color::RGBtoHSV $bg] break
325        if {$v > 0.5} {
326            set params(-color) [Rappture::color::brightness_max \
327                $params(-color) 0.8]
328        } else {
329            set params(-color) [Rappture::color::brightness_min \
330                $params(-color) 0.2]
331        }
332    }
333
334    set pos [lsearch -exact $dataobj $_dlist]
335    if {$pos < 0} {
336        lappend _dlist $dataobj
337        set _dataobj2color($dataobj) $params(-color)
338        set _dataobj2width($dataobj) $params(-width)
339        set _dataobj2dashes($dataobj) $params(-linestyle)
340        set _dataobj2raise($dataobj) $params(-raise)
341        set _dataobj2desc($dataobj) $params(-description)
342
343        $_dispatcher event -idle !rebuild
344    }
345}
346
347
348# ----------------------------------------------------------------------
349# USAGE: get
350#
351# Clients use this to query the list of objects being plotted, in
352# order from bottom to top of this result.
353# ----------------------------------------------------------------------
354itcl::body Rappture::HistogramResult::get {} {
355    # put the dataobj list in order according to -raise options
356    set bottom {}
357    set top {}
358    foreach obj $_dlist {
359        if {[info exists _dataobj2raise($obj)] && $_dataobj2raise($obj)} {
360            lappend top $obj
361        } else {
362            lappend bottom $obj
363        }
364    }
365    set _dlist [concat $bottom $top]
366    return $_dlist
367}
368
369# ----------------------------------------------------------------------
370# USAGE: delete ?<dataobj1> <dataobj2> ...?
371#
372# Clients use this to delete a dataobj from the plot.  If no dataobjs
373# are specified, then all dataobjs are deleted.
374# ----------------------------------------------------------------------
375itcl::body Rappture::HistogramResult::delete {args} {
376    if {[llength $args] == 0} {
377        set args $_dlist
378    }
379
380    # delete all specified dataobjs
381    set changed 0
382    foreach dataobj $args {
383        set pos [lsearch -exact $_dlist $dataobj]
384        if {$pos >= 0} {
385            set _dlist [lreplace $_dlist $pos $pos]
386            catch {unset _dataobj2color($dataobj)}
387            catch {unset _dataobj2width($dataobj)}
388            catch {unset _dataobj2dashes($dataobj)}
389            catch {unset _dataobj2raise($dataobj)}
390            foreach elem [array names _elem2dataobj] {
391                if {$_elem2dataobj($elem) == $dataobj} {
392                    unset _elem2dataobj($elem)
393                }
394            }
395            set changed 1
396        }
397    }
398
399    # If anything changed, then rebuild the plot
400    if {$changed} {
401        $_dispatcher event -idle !rebuild
402    }
403
404    # Nothing left? then start over with auto colors
405    if {[llength $_dlist] == 0} {
406        set _autoColorI 0
407    }
408}
409
410# ----------------------------------------------------------------------
411# USAGE: scale ?<dataobj1> <dataobj2> ...?
412#
413# Sets the default limits for the overall plot according to the
414# limits of the data for all of the given <dataobj> objects.  This
415# accounts for all dataobjs--even those not showing on the screen.
416# Because of this, the limits are appropriate for all dataobjs as
417# the user scans through data in the ResultSet viewer.
418# ----------------------------------------------------------------------
419itcl::body Rappture::HistogramResult::scale {args} {
420    set allx [$itk_component(plot) x2axis use]
421    lappend allx x  ;# fix main x-axis too
422    foreach axis $allx {
423        Axis scale $axis linear
424    }
425
426    set ally [$itk_component(plot) y2axis use]
427    lappend ally y  ;# fix main y-axis too
428    foreach axis $ally {
429        Axis scale $axis linear
430    }
431
432    catch {unset _limits}
433    foreach dataobj $args {
434        # find the axes for this dataobj (e.g., {x y2})
435        foreach {map(x) map(y)} [GetAxes $dataobj] break
436
437        foreach axis {x y} {
438            # get defaults for both linear and log scales
439            foreach type {lin log} {
440                # store results -- ex: _limits(x2log-min)
441                set id $map($axis)$type
442                foreach {min max} [$dataobj limits $axis$type] break
443                if {"" != $min && "" != $max} {
444                    if {![info exists _limits($id-min)]} {
445                        set _limits($id-min) $min
446                        set _limits($id-max) $max
447                    } else {
448                        if {$min < $_limits($id-min)} {
449                            set _limits($id-min) $min
450                        }
451                        if {$max > $_limits($id-max)} {
452                            set _limits($id-max) $max
453                        }
454                    }
455                }
456            }
457
458            if {[$dataobj hints ${axis}scale] == "log"} {
459                Axis scale $map($axis) log
460            }
461        }
462    }
463    ResetLimits
464}
465
466# ----------------------------------------------------------------------
467# USAGE: download coming
468# USAGE: download controls <downloadCommand>
469# USAGE: download now
470#
471# Clients use this method to create a downloadable representation
472# of the plot.  Returns a list of the form {ext string}, where
473# "ext" is the file extension (indicating the type of data) and
474# "string" is the data itself.
475# ----------------------------------------------------------------------
476itcl::body Rappture::HistogramResult::download {option args} {
477    switch $option {
478        coming {
479            # nothing to do
480        }
481        controls {
482            set popup .histogramresultdownload
483            if {![winfo exists .histogramresultdownload]} {
484                # if we haven't created the popup yet, do it now
485                Rappture::Balloon $popup \
486                    -title "[Rappture::filexfer::label downloadWord] as..."
487                set inner [$popup component inner]
488                label $inner.summary -text "" -anchor w
489                pack $inner.summary -side top
490                radiobutton $inner.csv -text "Data as Comma-Separated Values" \
491                    -variable Rappture::HistogramResult::_downloadPopup(format) \
492                    -value csv
493                pack $inner.csv -anchor w
494                radiobutton $inner.image -text "Image (PS/PDF/PNG/JPEG)" \
495                    -variable Rappture::HistogramResult::_downloadPopup(format) \
496                    -value image
497                pack $inner.image -anchor w
498                button $inner.go -text [Rappture::filexfer::label download] \
499                    -command [lindex $args 0]
500                pack $inner.go -pady 4
501            } else {
502                set inner [$popup component inner]
503            }
504            set num [llength [get]]
505            set num [expr {($num == 1) ? "1 result" : "$num results"}]
506            $inner.summary configure -text "[Rappture::filexfer::label downloadWord] $num in the following format:"
507            update idletasks ;# fix initial sizes
508            return $popup
509        }
510        now {
511            set popup .histogramresultdownload
512            if {[winfo exists .histogramresultdownload]} {
513                $popup deactivate
514            }
515            switch -- $_downloadPopup(format) {
516                csv {
517                    # reverse the objects so the selected data appears on top
518                    set dlist ""
519                    foreach dataobj [get] {
520                        set dlist [linsert $dlist 0 $dataobj]
521                    }
522
523                    # generate the comma-separated value data for these objects
524                    set csvdata ""
525                    foreach dataobj $dlist {
526                        append csvdata "[string repeat - 60]\n"
527                        append csvdata " [$dataobj hints label]\n"
528                        if {[info exists _dataobj2desc($dataobj)]
529                            && [llength [split $_dataobj2desc($dataobj) \n]] > 1} {
530                            set indent "for:"
531                            foreach line [split $_dataobj2desc($dataobj) \n] {
532                                append csvdata " $indent $line\n"
533                                set indent "    "
534                            }
535                        }
536                        append csvdata "[string repeat - 60]\n"
537
538                        append csvdata "[$dataobj hints xlabel], [$dataobj hints ylabel]\n"
539                        set first 1
540                        foreach comp [$dataobj components] {
541                            if {!$first} {
542                                # blank line between components
543                                append csvdata "\n"
544                            }
545                            set xv [$dataobj mesh $comp]
546                            set yv [$dataobj values $comp]
547                            foreach x [$xv range 0 end] y [$yv range 0 end] {
548                                append csvdata \
549                                    [format "%20.15g, %20.15g\n" $x $y]
550                            }
551                            set first 0
552                        }
553                        append csvdata "\n"
554                    }
555                    return [list .txt $csvdata]
556                }
557                image {
558                    set popup .histogramprintdownload
559                    if { ![winfo exists $popup] } {
560                        # Create a popup for the print dialog
561                        Rappture::Balloon $popup -title "Save as image..."
562                        set inner [$popup component inner]
563                        # Create the print dialog widget and add it to the
564                        # the balloon popup.
565                        Rappture::XyPrint $inner.print
566                        $popup configure \
567                            -deactivatecommand [list $inner.print reset]
568                        blt::table $inner 0,0 $inner.print -fill both
569                    }
570                    update
571                    # Activate the popup and call for the output.
572                    foreach { widget toolName plotName } $args break
573                    $popup activate $widget left
574                    set inner [$popup component inner]
575                    set output [$inner.print print $itk_component(plot) \
576                                    $toolName $plotName]
577                    $popup deactivate
578                    return $output
579                }
580            }
581        }
582        default {
583            error "bad option \"$option\": should be coming, controls, now"
584        }
585    }
586}
587
588# ----------------------------------------------------------------------
589# USAGE: Rebuild
590#
591# Called automatically whenever something changes that affects the
592# data in the widget.  Clears any existing data and rebuilds the
593# widget to display new data.
594# ----------------------------------------------------------------------
595itcl::body Rappture::HistogramResult::Rebuild {} {
596    set g $itk_component(plot)
597
598    # First clear out the widget
599    eval $g element delete [$g element names]
600    eval $g marker delete [$g marker names]
601    foreach axis [$g axis names] {
602        $g axis configure $axis -hide yes
603    }
604    # Presumably you want at least an X-axis and Y-axis displayed.
605    $g xaxis configure -hide no
606    $g yaxis configure -hide no
607    array unset _label2axis
608   
609    #
610    # Scan through all objects and create a list of all axes.
611    # The first x-axis gets mapped to "x".  The second, to "x2".
612    # Beyond that, we must create new axes "x3", "x4", etc.
613    # We do the same for y.
614    #
615    set anum(x) 0
616    set anum(y) 0
617    foreach dataobj [get] {
618        foreach ax {x y} {
619            set label [$dataobj hints ${ax}label]
620            if {"" != $label} {
621                if {![info exists _label2axis($ax-$label)]} {
622                    switch [incr anum($ax)] {
623                        1 { set axis $ax }
624                        2 { set axis ${ax}2 }
625                        default {
626                            set axis $ax$anum($ax)
627                            catch {$g axis create $axis}
628                        }
629                    }
630                    $g axis configure $axis -title $label -hide no \
631                        -checklimits no
632                    set _label2axis($ax-$label) $axis
633                   
634                    # if this axis has a description, add it as a tooltip
635                    set desc [string trim [$dataobj hints ${ax}desc]]
636                    Rappture::Tooltip::text $g-$axis $desc
637                }
638            }
639        }
640    }
641   
642    #
643    # All of the extra axes get mapped to the x2/y2 (top/right)
644    # position.
645    #
646    set all ""
647    foreach ax {x y} {
648        lappend all $ax
649       
650        set extra ""
651        for {set i 2} {$i <= $anum($ax)} {incr i} {
652            lappend extra ${ax}$i
653        }
654        eval lappend all $extra
655        $g ${ax}2axis use $extra
656        if {$ax == "y"} {
657            $g configure -rightmargin [expr {($extra == "") ? 10 : 0}]
658        }
659    }
660
661    foreach axis $all {
662        set _axisPopup(format-$axis) "%.6g"
663        $g axis bind $axis <Enter> \
664            [itcl::code $this Axis hilite $axis on]
665        $g axis bind $axis <Leave> \
666            [itcl::code $this Axis hilite $axis off]
667        $g axis bind $axis <ButtonPress-1> \
668            [itcl::code $this Axis click $axis %x %y]
669        $g axis bind $axis <B1-Motion> \
670            [itcl::code $this Axis drag $axis %x %y]
671        $g axis bind $axis <ButtonRelease-1> \
672            [itcl::code $this Axis release $axis %x %y]
673        $g axis bind $axis <KeyPress> \
674            [list ::Rappture::Tooltip::tooltip cancel]
675    }
676    set invert 0
677    array unset _xlabels
678    #
679    # Plot all of the dataobjs.
680    #
681    set count 0
682    foreach dataobj $_dlist {
683        set label [$dataobj hints label]
684        foreach {mapx mapy} [GetAxes $dataobj] break
685        foreach comp [$dataobj components] {
686            set xv [$dataobj mesh $comp]
687            set yv [$dataobj values $comp]
688            set zv [$dataobj widths $comp]
689            if {$xv eq "" || $yv eq "" || $zv eq ""} {
690                continue
691            }
692            if {[info exists _dataobj2color($dataobj)]} {
693                set color $_dataobj2color($dataobj)
694            } else {
695                set color [$dataobj hints color]
696                if {"" == $color} {
697                    set color black
698                }
699            }
700            if {[info exists _dataobj2width($dataobj)]} {
701                set lwidth $_dataobj2width($dataobj)
702            } else {
703                set lwidth 2
704            }
705            if {[info exists _dataobj2dashes($dataobj)]} {
706                set dashes $_dataobj2dashes($dataobj)
707            } else {
708                set dashes ""
709            }
710            if {([$xv length] <= 1) || ($lwidth == 0)} {
711                set sym square
712                set pixels 2
713            } else {
714                set sym ""
715                set pixels 6
716            }
717            # Compute default bar width for histogram elements.
718            if { [$zv length] == [$xv length] } {
719                foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
720                    set elem "elem[incr count]"
721                    set _elem2dataobj($elem) $dataobj
722                    $g element create $elem -x $x -y $y -barwidth $z \
723                        -label $label -foreground $color \
724                        -mapx $mapx -mapy $mapy
725                }
726            } else {
727                set r [blt::vector expr {max($xv) - min($xv)}]
728                set z [expr {$r / ([$xv length]-1) * 0.8}]
729                set elem "elem[incr count]"
730                set _elem2dataobj($elem) $dataobj
731                $g element create $elem -x $xv -y $yv -barwidth $z \
732                    -label $label -foreground $color \
733                    -mapx $mapx -mapy $mapy
734            }
735            set index 0
736            foreach label [$dataobj xlabels $comp] {
737                if  { [string length $label] > 3 } {
738                    set invert 1
739                }
740                set _xlabels($index) $label
741                incr index
742            }
743        }
744    }
745    foreach dataobj $_dlist {
746        set xmin -Inf
747        set ymin -Inf
748        set xmax Inf
749        set ymax Inf
750        #
751        # Create text/line markers for each *axis.marker specified.
752        #
753        foreach m [$dataobj xmarkers] {
754            foreach {at label style} $m break
755            set id [$g marker create line -coords [list $at $ymin $at $ymax]]
756            $g marker bind $id <Enter> \
757                [itcl::code $this EnterMarker $g x-$label $at $ymin $at]
758            $g marker bind $id <Leave> \
759                [itcl::code $this LeaveMarker $g x-$label]
760            set options [GetLineMarkerOptions $style]
761            if { $options != "" } {
762                eval $g marker configure $id $options
763            }
764            if { $label != "" } {
765                set id [$g marker create text -anchor nw \
766                            -text $label -coords [list $at $ymax]]
767                set options [GetTextMarkerOptions $style]
768                if { $options != "" } {
769                    eval $g marker configure $id $options
770                }
771            }
772        }
773        foreach m [$dataobj ymarkers] {
774            foreach {at label style} $m break
775            set id [$g marker create line -coords [list $xmin $at $xmax $at]]
776            $g marker bind $id <Enter> \
777                [itcl::code $this EnterMarker $g y-$label $at $xmin $at]
778            $g marker bind $id <Leave> \
779                [itcl::code $this LeaveMarker $g y-$label]
780            set options [GetLineMarkerOptions $style]
781            if { $options != "" } {
782                eval $g marker configure $id $options
783            }
784            if { $label != "" } {
785                set id [$g marker create text -anchor se \
786                        -text $label -coords [list $xmax $at]]
787                set options [GetTextMarkerOptions $style]
788                if { $options != "" } {
789                    eval $g marker configure $id $options
790                }
791            }
792        }
793    }
794    if { [array size _xlabels] > 0 } {
795        set command [itcl::code $this FormatLabels]
796    } else {
797        set command ""
798    }
799    $g axis configure x -command $command
800    $g configure -invertxy $invert
801    $itk_component(legend) reset
802}
803
804# ----------------------------------------------------------------------
805# USAGE: ResetLimits
806#
807# Used internally to apply automatic limits to the axes for the
808# current plot.
809# ----------------------------------------------------------------------
810itcl::body Rappture::HistogramResult::ResetLimits {} {
811    set g $itk_component(plot)
812    foreach axis [$g axis names] {
813        $g axis configure $axis -min "" -max ""
814    }
815}
816
817# ----------------------------------------------------------------------
818# USAGE: Zoom reset
819#
820# Called automatically when the user clicks on one of the zoom
821# controls for this widget.  Changes the zoom for the current view.
822# ----------------------------------------------------------------------
823itcl::body Rappture::HistogramResult::Zoom {option args} {
824    switch -- $option {
825        reset {
826            ResetLimits
827        }
828    }
829}
830
831# ----------------------------------------------------------------------
832# USAGE: Hilite <state> <x> <y>
833#
834# Called automatically when the user brushes one of the elements
835# on the plot.  Causes the element to highlight and a tooltip to
836# pop up with element info.
837# ----------------------------------------------------------------------
838itcl::body Rappture::HistogramResult::Hilite {state x y} {
839    set g $itk_component(plot)
840    set elem ""
841 
842    # Peek inside of Blt_ZoomStack package to see if we're currently in the
843    # middle of a zoom selection.
844    if {[info exists ::zoomInfo($g,corner)] && $::zoomInfo($g,corner) == "B" } {
845        return;
846    }
847    set tip ""
848    set index ""
849    if {$state == "at"} {
850        set bool [$g element closest $x $y info -along y -halo 1]
851        # Must be in the element.
852        if { $bool } {
853            # for dealing with xy line plots
854            set elem $info(name)
855
856            # Some elements are generated dynamically and therefore will
857            # not have a dataobj object associated with them.
858            set mapx [$g element cget $elem -mapx]
859            set mapy [$g element cget $elem -mapy]
860            if {[info exists _elem2dataobj($elem)]} {
861                foreach {mapx mapy} [GetAxes $_elem2dataobj($elem)] break
862            }
863
864            # search again for an exact point -- this time don't interpolate
865            set tip ""
866            if { $info(name) == $elem} {
867                set x [$g axis transform $mapx $info(x)]
868                set y [$g axis transform $mapy $info(y)]
869                if { [$g cget -invertxy] } {
870                    set tmp $x
871                    set x $y
872                    set y $tmp
873                }
874                 if {[info exists _elem2dataobj($elem)]} {
875                    set dataobj $_elem2dataobj($elem)
876                    set yunits [$dataobj hints yunits]
877                    set xunits [$dataobj hints xunits]
878                } else {
879                    set xunits ""
880                    set yunits ""
881                }
882                set tip [$g element cget $elem -label]
883                set yval [Axis format y dummy $info(y)]
884                append tip "\n$yval$yunits"
885                set xval [Axis format x dummy $info(x)]
886                append tip " @ $xval"
887                set tip [string trim $tip]
888                set index $info(index)
889            }
890            set state 1
891        } else {
892            set bool [$g element closest $x $y info -interpolate no -along y]
893            if { $bool } {
894                # for dealing with xy scatter plot
895                set elem $info(name)
896                # Some elements are generated dynamically and therefore will
897                # not have a dataobj object associated with them.
898                set mapx [$g element cget $elem -mapx]
899                set mapy [$g element cget $elem -mapy]
900                if {[info exists _elem2dataobj($elem)]} {
901                    foreach {mapx mapy} [GetAxes $_elem2dataobj($elem)] break
902                }
903                set tip ""
904                set x [$g axis transform $mapx $info(x)]
905                set y [$g axis transform $mapy $info(y)]
906                if { [$g cget -invertxy] } {
907                    set tmp $x
908                    set x $y
909                    set y $tmp
910                }
911               if {[info exists _elem2dataobj($elem)]} {
912                    set dataobj $_elem2dataobj($elem)
913                    set yunits [$dataobj hints yunits]
914                    set xunits [$dataobj hints xunits]
915                } else {
916                    set xunits ""
917                    set yunits ""
918                }
919                set tip [$g element cget $elem -label]
920                set yval [Axis format y dummy $info(y)]
921                append tip "\n$yval$yunits"
922                set xval [Axis format x dummy $info(x)]
923                append tip " @ $xval"
924                set tip [string trim $tip]
925                set index $info(index)
926                set state 1
927            } else {
928                set state 0
929            }
930        }
931    }
932
933    if {$state} {
934        #
935        # Highlight ON:
936        # - activate trace
937        # - multiple axes? dim other axes
938        # - pop up tooltip about data
939        #
940        if { [$g element exists $_hilite(elem)] && $_hilite(elem) != $elem } {
941            $g element deactivate $_hilite(elem)
942            $g crosshairs configure -hide yes
943            Rappture::Tooltip::tooltip cancel
944        }
945        if { $index != "" } {
946            $g element activate $elem $index
947            set _hilite(index) $index
948        }
949        set _hilite(elem) $elem
950
951        set mapx [$g element cget $elem -mapx]
952        set mapy [$g element cget $elem -mapy]
953        if {[info exists _elem2dataobj($elem)]} {
954            foreach {mapx mapy} [GetAxes $_elem2dataobj($elem)] break
955        }
956        set allx [$g x2axis use]
957        if {[llength $allx] > 0} {
958            lappend allx x  ;# fix main x-axis too
959            foreach axis $allx {
960                if {$axis == $mapx} {
961                    $g axis configure $axis -color $itk_option(-foreground) \
962                        -titlecolor $itk_option(-foreground)
963                } else {
964                    $g axis configure $axis -color $itk_option(-dimcolor) \
965                        -titlecolor $itk_option(-dimcolor)
966                }
967            }
968        }
969        set ally [$g y2axis use]
970        if {[llength $ally] > 0} {
971            lappend ally y  ;# fix main y-axis too
972            foreach axis $ally {
973                if {$axis == $mapy} {
974                    $g axis configure $axis -color $itk_option(-foreground) \
975                        -titlecolor $itk_option(-foreground)
976                } else {
977                    $g axis configure $axis -color $itk_option(-dimcolor) \
978                        -titlecolor $itk_option(-dimcolor)
979                }
980            }
981        }
982
983        if {"" != $tip} {
984            $g crosshairs configure -hide no -position @$x,$y
985
986            if {$x > 0.5*[winfo width $g]} {
987                if {$x < 4} {
988                    set tipx "-0"
989                } else {
990                    set tipx "-[expr {$x-20}]"  ;# move tooltip to the left
991                }
992            } else {
993                if {$x < -4} {
994                    set tipx "+0"
995                } else {
996                    set tipx "+[expr {$x+20}]"  ;# move tooltip to the right
997                }
998            }
999            if {$y > 0.5*[winfo height $g]} {
1000                if {$y < 4} {
1001                    set tipy "-0"
1002                } else {
1003                    set tipy "-[expr {$y-20}]"  ;# move tooltip to the top
1004                }
1005            } else {
1006                if {$y < -4} {
1007                    set tipy "+0"
1008                } else {
1009                    set tipy "+[expr {$y+20}]"  ;# move tooltip to the bottom
1010                }
1011            }
1012            Rappture::Tooltip::text $g $tip
1013            Rappture::Tooltip::tooltip show $g $tipx,$tipy
1014        }
1015    } else {
1016        #
1017        # Highlight OFF:
1018        # - deactivate (color back to normal)
1019        # - put all axes back to normal color
1020        # - take down tooltip
1021        #
1022        if { [$g element exists $_hilite(elem)] } {
1023            $g element deactivate $_hilite(elem)
1024        }
1025        set allx [$g x2axis use]
1026        if {[llength $allx] > 0} {
1027            lappend allx x  ;# fix main x-axis too
1028            foreach axis $allx {
1029                $g axis configure $axis -color $itk_option(-foreground) \
1030                    -titlecolor $itk_option(-foreground)
1031            }
1032        }
1033       
1034        set ally [$g y2axis use]
1035        if {[llength $ally] > 0} {
1036            lappend ally y  ;# fix main y-axis too
1037            foreach axis $ally {
1038                $g axis configure $axis -color $itk_option(-foreground) \
1039                    -titlecolor $itk_option(-foreground)
1040            }
1041        }
1042
1043        $g crosshairs configure -hide yes
1044
1045        # only cancel in plotting area or we'll mess up axes
1046        if {[$g inside $x $y]} {
1047            Rappture::Tooltip::tooltip cancel
1048        }
1049
1050        # There is no currently highlighted element
1051        set _hilite(elem) ""
1052    }
1053}
1054
1055# ----------------------------------------------------------------------
1056# USAGE: Axis hilite <axis> <state>
1057#
1058# USAGE: Axis click <axis> <x> <y>
1059# USAGE: Axis drag <axis> <x> <y>
1060# USAGE: Axis release <axis> <x> <y>
1061#
1062# USAGE: Axis edit <axis>
1063# USAGE: Axis changed <axis> <what>
1064# USAGE: Axis format <axis> <widget> <value>
1065# USAGE: Axis scale <axis> linear|log
1066#
1067# Used internally to handle editing of the x/y axes.  The hilite
1068# operation causes the axis to light up.  The edit operation pops
1069# up a panel with editing options.  The changed operation applies
1070# changes from the panel.
1071# ----------------------------------------------------------------------
1072itcl::body Rappture::HistogramResult::Axis {option args} {
1073    set inner [$itk_component(hull).axes component inner]
1074
1075    switch -- $option {
1076        hilite {
1077            if {[llength $args] != 2} {
1078                error "wrong # args: should be \"Axis hilite axis state\""
1079            }
1080            set g $itk_component(plot)
1081            set axis [lindex $args 0]
1082            set state [lindex $args 1]
1083
1084            if {$state} {
1085                $g axis configure $axis \
1086                    -color $itk_option(-activecolor) \
1087                    -titlecolor $itk_option(-activecolor)
1088                set x [expr {[winfo pointerx $g]+4}]
1089                set y [expr {[winfo pointery $g]+4}]
1090                Rappture::Tooltip::tooltip pending $g-$axis @$x,$y
1091            } else {
1092                $g axis configure $axis \
1093                    -color $itk_option(-foreground) \
1094                    -titlecolor $itk_option(-foreground)
1095                Rappture::Tooltip::tooltip cancel
1096            }
1097        }
1098        click {
1099            if {[llength $args] != 3} {
1100                error "wrong # args: should be \"Axis click axis x y\""
1101            }
1102            foreach { axis x y } $args break
1103            set g $itk_component(plot)
1104            if { [$g cget -invertxy] } {
1105                set tmp $x
1106                set x $y
1107                set y $tmp
1108            }
1109
1110            set _axis(moved) 0
1111            set _axis(click-x) $x
1112            set _axis(click-y) $y
1113            foreach {min max} [$g axis limits $axis] break
1114            set _axis(min0) $min
1115            set _axis(max0) $max
1116            Rappture::Tooltip::tooltip cancel
1117        }
1118        drag {
1119            if {[llength $args] != 3} {
1120                error "wrong # args: should be \"Axis drag axis x y\""
1121            }
1122            if {![info exists _axis(moved)]} {
1123                return  ;# must have skipped click event -- ignore
1124            }
1125            foreach { axis x y } $args break
1126            set g $itk_component(plot)
1127            if { [$g cget -invertxy] } {
1128                set tmp $x
1129                set x $y
1130                set y $tmp
1131            }
1132
1133            if {[info exists _axis(click-x)] && [info exists _axis(click-y)]} {
1134                foreach {x0 y0 pw ph} [$g extents plotarea] break
1135                switch -glob $axis {
1136                  x* {
1137                    set pix $x
1138                    set pix0 $_axis(click-x)
1139                    set pixmin $x0
1140                    set pixmax [expr {$x0+$pw}]
1141                  }
1142                  y* {
1143                    set pix $y
1144                    set pix0 $_axis(click-y)
1145                    set pixmin [expr {$y0+$ph}]
1146                    set pixmax $y0
1147                  }
1148                }
1149                set log [$g axis cget $axis -logscale]
1150                set min $_axis(min0)
1151                set max $_axis(max0)
1152                set dpix [expr {abs($pix-$pix0)}]
1153                set v0 [$g axis invtransform $axis $pixmin]
1154                set v1 [$g axis invtransform $axis [expr {$pixmin+$dpix}]]
1155                if {$log} {
1156                    set v0 [expr {log10($v0)}]
1157                    set v1 [expr {log10($v1)}]
1158                    set min [expr {log10($min)}]
1159                    set max [expr {log10($max)}]
1160                }
1161
1162                if {$pix > $pix0} {
1163                    set delta [expr {$v1-$v0}]
1164                } else {
1165                    set delta [expr {$v0-$v1}]
1166                }
1167                set min [expr {$min-$delta}]
1168                set max [expr {$max-$delta}]
1169                if {$log} {
1170                    set min [expr {pow(10.0,$min)}]
1171                    set max [expr {pow(10.0,$max)}]
1172                }
1173                $g axis configure $axis -min $min -max $max
1174
1175                # move axis, don't edit on release
1176                set _axis(move) 1
1177            }
1178        }
1179        release {
1180            if {[llength $args] != 3} {
1181                error "wrong # args: should be \"Axis release axis x y\""
1182            }
1183            if {![info exists _axis(moved)]} {
1184                return  ;# must have skipped click event -- ignore
1185            }
1186            foreach { axis x y } $args break
1187            set g $itk_component(plot)
1188            if { [$g cget -invertxy] } {
1189                set tmp $x
1190                set x $y
1191                set y $tmp
1192            }
1193
1194            if {!$_axis(moved)} {
1195                # small movement? then treat as click -- pop up axis editor
1196                set dx [expr {abs($x-$_axis(click-x))}]
1197                set dy [expr {abs($y-$_axis(click-y))}]
1198                if {$dx < 2 && $dy < 2} {
1199                    Axis edit $axis
1200                }
1201            } else {
1202                # one last movement
1203                Axis drag $axis $x $y
1204            }
1205            catch {unset _axis}
1206        }
1207        edit {
1208            if {[llength $args] != 1} {
1209                error "wrong # args: should be \"Axis edit axis\""
1210            }
1211            set axis [lindex $args 0]
1212            set _axisPopup(current) $axis
1213
1214            # apply last value when deactivating
1215            $itk_component(hull).axes configure -deactivatecommand \
1216                [itcl::code $this Axis changed $axis focus]
1217
1218            # fix axis label controls...
1219            set label [$itk_component(plot) axis cget $axis -title]
1220            $inner.label delete 0 end
1221            $inner.label insert end $label
1222            bind $inner.label <KeyPress-Return> \
1223                [itcl::code $this Axis changed $axis label]
1224            bind $inner.label <FocusOut> \
1225                [itcl::code $this Axis changed $axis label]
1226
1227            # fix min/max controls...
1228            foreach {min max} [$itk_component(plot) axis limits $axis] break
1229            $inner.min delete 0 end
1230            $inner.min insert end $min
1231            bind $inner.min <KeyPress-Return> \
1232                [itcl::code $this Axis changed $axis min]
1233            bind $inner.min <FocusOut> \
1234                [itcl::code $this Axis changed $axis min]
1235
1236            $inner.max delete 0 end
1237            $inner.max insert end $max
1238            bind $inner.max <KeyPress-Return> \
1239                [itcl::code $this Axis changed $axis max]
1240            bind $inner.max <FocusOut> \
1241                [itcl::code $this Axis changed $axis max]
1242
1243            # fix format control...
1244            set fmts [$inner.format choices get -value]
1245            set i [lsearch -exact $fmts $_axisPopup(format-$axis)]
1246            if {$i < 0} { set i 0 }  ;# use Auto choice
1247            $inner.format value [$inner.format choices get -label $i]
1248
1249            bind $inner.format <<Value>> \
1250                [itcl::code $this Axis changed $axis format]
1251
1252            # fix scale control...
1253            if {[$itk_component(plot) axis cget $axis -logscale]} {
1254                set _axisPopup(scale) "log"
1255                $inner.format configure -state disabled
1256            } else {
1257                set _axisPopup(scale) "linear"
1258                $inner.format configure -state normal
1259            }
1260            $inner.scales.linear configure \
1261                -command [itcl::code $this Axis changed $axis scale]
1262            $inner.scales.log configure \
1263                -command [itcl::code $this Axis changed $axis scale]
1264
1265            #
1266            # Figure out where the window should pop up.
1267            #
1268            set x [winfo rootx $itk_component(plot)]
1269            set y [winfo rooty $itk_component(plot)]
1270            set w [winfo width $itk_component(plot)]
1271            set h [winfo height $itk_component(plot)]
1272            foreach {x0 y0 pw ph} [$itk_component(plot) extents plotarea] break
1273            switch -glob -- $axis {
1274                x {
1275                    set x [expr {round($x + $x0+0.5*$pw)}]
1276                    set y [expr {round($y + $y0+$ph + 0.5*($h-$y0-$ph))}]
1277                    set dir "above"
1278                }
1279                x* {
1280                    set x [expr {round($x + $x0+0.5*$pw)}]
1281                    set dir "below"
1282                    set allx [$itk_component(plot) x2axis use]
1283                    set max [llength $allx]
1284                    set i [lsearch -exact $allx $axis]
1285                    set y [expr {round($y + ($i+0.5)*$y0/double($max))}]
1286                }
1287                y {
1288                    set x [expr {round($x + 0.5*$x0)}]
1289                    set y [expr {round($y + $y0+0.5*$ph)}]
1290                    set dir "right"
1291                }
1292                y* {
1293                    set y [expr {round($y + $y0+0.5*$ph)}]
1294                    set dir "left"
1295                    set ally [$itk_component(plot) y2axis use]
1296                    set max [llength $ally]
1297                    set i [lsearch -exact $ally $axis]
1298                    set y [expr {round($y + ($i+0.5)*$y0/double($max))}]
1299                    set x [expr {round($x+$x0+$pw + ($i+0.5)*($w-$x0-$pw)/double($max))}]
1300                }
1301            }
1302            $itk_component(hull).axes activate @$x,$y $dir
1303        }
1304        changed {
1305            if {[llength $args] != 2} {
1306                error "wrong # args: should be \"Axis changed axis what\""
1307            }
1308            set axis [lindex $args 0]
1309            set what [lindex $args 1]
1310            if {$what == "focus"} {
1311                set what [focus]
1312                if {[winfo exists $what]} {
1313                    set what [winfo name $what]
1314                }
1315            }
1316
1317            switch -- $what {
1318                label {
1319                    set val [$inner.label get]
1320                    $itk_component(plot) axis configure $axis -title $val
1321                }
1322                min {
1323                    set val [$inner.min get]
1324                    if {![string is double -strict $val]} {
1325                        Rappture::Tooltip::cue $inner.min "Must be a number"
1326                        bell
1327                        return
1328                    }
1329
1330                    set max [lindex [$itk_component(plot) axis limits $axis] 1]
1331                    if {$val >= $max} {
1332                        Rappture::Tooltip::cue $inner.min "Must be <= max ($max)"
1333                        bell
1334                        return
1335                    }
1336                    catch {
1337                        # can fail in log mode
1338                        $itk_component(plot) axis configure $axis -min $val
1339                    }
1340                    foreach {min max} [$itk_component(plot) axis limits $axis] break
1341                    $inner.min delete 0 end
1342                    $inner.min insert end $min
1343                }
1344                max {
1345                    set val [$inner.max get]
1346                    if {![string is double -strict $val]} {
1347                        Rappture::Tooltip::cue $inner.max "Should be a number"
1348                        bell
1349                        return
1350                    }
1351
1352                    set min [lindex [$itk_component(plot) axis limits $axis] 0]
1353                    if {$val <= $min} {
1354                        Rappture::Tooltip::cue $inner.max "Must be >= min ($min)"
1355                        bell
1356                        return
1357                    }
1358                    catch {
1359                        # can fail in log mode
1360                        $itk_component(plot) axis configure $axis -max $val
1361                    }
1362                    foreach {min max} [$itk_component(plot) axis limits $axis] break
1363                    $inner.max delete 0 end
1364                    $inner.max insert end $max
1365                }
1366                format {
1367                    set fmt [$inner.format translate [$inner.format value]]
1368                    set _axisPopup(format-$axis) $fmt
1369
1370                    # force a refresh
1371                    $itk_component(plot) axis configure $axis -min \
1372                        [$itk_component(plot) axis cget $axis -min]
1373                }
1374                scale {
1375                    Axis scale $axis $_axisPopup(scale)
1376
1377                    if {$_axisPopup(scale) == "log"} {
1378                        $inner.format configure -state disabled
1379                    } else {
1380                        $inner.format configure -state normal
1381                    }
1382
1383                    foreach {min max} [$itk_component(plot) axis limits $axis] break
1384                    $inner.min delete 0 end
1385                    $inner.min insert end $min
1386                    $inner.max delete 0 end
1387                    $inner.max insert end $max
1388                }
1389                default {
1390                    # be lenient so we can handle the "focus" case
1391                }
1392            }
1393        }
1394        format {
1395            if {[llength $args] != 3} {
1396                error "wrong # args: should be \"Axis format axis widget value\""
1397            }
1398            set axis [lindex $args 0]
1399            set value [lindex $args 2]
1400            if { $axis == "x" } {
1401                return [FormatLabels $itk_component(plot) $value]
1402            }
1403            if {[$itk_component(plot) axis cget $axis -logscale]} {
1404                set fmt "%.6g"
1405            } else {
1406                set fmt $_axisPopup(format-$axis)
1407            }
1408            return [format $fmt $value]
1409        }
1410        scale {
1411            if {[llength $args] != 2} {
1412                error "wrong # args: should be \"Axis scale axis type\""
1413            }
1414            set axis [lindex $args 0]
1415            set type [lindex $args 1]
1416
1417            if {$type == "log"} {
1418                catch {$itk_component(plot) axis configure $axis -logscale 1}
1419                # leave format alone in log mode
1420                $itk_component(plot) axis configure $axis -command ""
1421            } else {
1422                catch {$itk_component(plot) axis configure $axis -logscale 0}
1423                # use special formatting for linear mode
1424                $itk_component(plot) axis configure $axis -command \
1425                    [itcl::code $this Axis format $axis]
1426            }
1427        }
1428        default {
1429            error "bad option \"$option\": should be changed, edit, hilite, or format"
1430        }
1431    }
1432}
1433
1434# ----------------------------------------------------------------------
1435# USAGE: GetLineMarkerOptions <style>
1436#
1437# Used internally to create a list of configuration options specific to the
1438# axis line marker.  The input is a list of name value pairs.  Options that
1439# are not recognized are ignored.
1440# ----------------------------------------------------------------------
1441itcl::body Rappture::HistogramResult::GetLineMarkerOptions {style} {
1442    array set lineOptions {
1443        "-color"  "-outline"
1444        "-dashes" "-dashes"
1445        "-linecolor" "-outline"
1446        "-linewidth" "-linewidth"
1447    }
1448    set options {}
1449    foreach {name value} $style {
1450        if { [info exists lineOptions($name)] } {
1451            lappend options $lineOptions($name) $value
1452        }
1453    }
1454    return $options
1455}
1456
1457# ----------------------------------------------------------------------
1458# USAGE: GetTextMarkerOptions <style>
1459#
1460# Used internally to create a list of configuration options specific to the
1461# axis text marker.  The input is a list of name value pairs.  Options that
1462# are not recognized are ignored.
1463# ----------------------------------------------------------------------
1464itcl::body Rappture::HistogramResult::GetTextMarkerOptions {style} {
1465    array set textOptions {
1466        "-color"  "-outline"
1467        "-textcolor"  "-outline"
1468        "-font"   "-font"
1469        "-xoffset" "-xoffset"
1470        "-yoffset" "-yoffset"
1471        "-anchor" "-anchor"
1472        "-rotate" "-rotate"
1473    }
1474    set options {}
1475    foreach {name value} $style {
1476        if { [info exists textOptions($name)] } {
1477            lappend options $textOptions($name) $value
1478        }
1479    }
1480    return $options
1481}
1482
1483# ----------------------------------------------------------------------
1484# USAGE: GetAxes <dataobj>
1485#
1486# Used internally to figure out the axes used to plot the given
1487# <dataobj>.  Returns a list of the form {x y}, where x is the
1488# x-axis name (x, x2, x3, etc.), and y is the y-axis name.
1489# ----------------------------------------------------------------------
1490itcl::body Rappture::HistogramResult::GetAxes {dataobj} {
1491    # rebuild if needed, so we know about the axes
1492    if {[$_dispatcher ispending !rebuild]} {
1493        $_dispatcher cancel !rebuild
1494        $_dispatcher event -now !rebuild
1495    }
1496
1497    # what is the x axis?  x? x2? x3? ...
1498    set xlabel [$dataobj hints xlabel]
1499    if {[info exists _label2axis(x-$xlabel)]} {
1500        set mapx $_label2axis(x-$xlabel)
1501    } else {
1502        set mapx "x"
1503    }
1504
1505    # what is the y axis?  y? y2? y3? ...
1506    set ylabel [$dataobj hints ylabel]
1507    if {[info exists _label2axis(y-$ylabel)]} {
1508        set mapy $_label2axis(y-$ylabel)
1509    } else {
1510        set mapy "y"
1511    }
1512
1513    return [list $mapx $mapy]
1514}
1515
1516# ----------------------------------------------------------------------
1517# CONFIGURATION OPTION: -gridcolor
1518# ----------------------------------------------------------------------
1519itcl::configbody Rappture::HistogramResult::gridcolor {
1520    if {"" == $itk_option(-gridcolor)} {
1521        $itk_component(plot) grid off
1522    } else {
1523        $itk_component(plot) grid configure -color $itk_option(-gridcolor)
1524        $itk_component(plot) grid on
1525    }
1526}
1527
1528# ----------------------------------------------------------------------
1529# CONFIGURATION OPTION: -autocolors
1530# ----------------------------------------------------------------------
1531itcl::configbody Rappture::HistogramResult::autocolors {
1532    foreach c $itk_option(-autocolors) {
1533        if {[catch {winfo rgb $itk_component(hull) $c}]} {
1534            error "bad color \"$c\""
1535        }
1536    }
1537    if {$_autoColorI >= [llength $itk_option(-autocolors)]} {
1538        set _autoColorI 0
1539    }
1540}
1541
1542itcl::body Rappture::HistogramResult::EnterMarker { g name x y text } {
1543    LeaveMarker $g $name
1544    set id [$g marker create text \
1545                -coords [list $x $y] \
1546                -anchor n \
1547                -text $text]
1548    set _markers($name) $id
1549}
1550
1551itcl::body Rappture::HistogramResult::LeaveMarker { g name } {
1552    if { [info exists _markers($name)] } {
1553        set id $_markers($name)
1554        $g marker delete $id
1555        unset _markers($name)
1556    }
1557}
1558
1559
1560itcl::body Rappture::HistogramResult::FormatLabels { w value } {
1561    # Determine the element name from the value
1562    set index [expr round($value)]
1563    if { [info exists _xlabels($index)] } {
1564        return $_xlabels($index)
1565    }
1566    return " "
1567}
Note: See TracBrowser for help on using the repository browser.