source: trunk/gui/scripts/xyresult.tcl @ 1794

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