
BLT-newbie - barchart: How to display value under mouse cursor
Quote:
> I'm displaying a histogram (vectors X,Y) with BLT's barchart.
> # X,Y are some data lists
> barchart .bar -plotbackground white
> .bar element create e1 -xdata $X -ydata $Y
> Does somebody has an idea how to display the corresponding
> data (x,y) values when the mouse cursor is moved over the barchart ?
> Something like
> bind $bar <Motion> {show %x %y}
> proc show {x y} {
> ... ??? ...
> puts stderr "show($x,$y) = "
> }
I'm guessing that what you want is the X-Y coordinates of a bar
segment as the mouse passes over it.
.bar element bind all <Enter> {
.bar element closest %x %y info
puts stderr "$info(x) $info(y)"
}
Note that this is using the "element bind" operation, not Tk's bind
command. [The "element bind" operation works a lot like the canvas'
"bind" operation of canvas items.]
Whenever the pointer enters a bar segment, you can check for the
closest element. There must be one, since you've entered an element.
The X,Y coordinates are returned through the array variable than you
provided as an argument (in this example, it's called "info").
If you want to X-Y graph coordinates of the mouse pointer, where ever
the mouse moves, track Motion events on the barchart.
bind .bar <Motion> {
if { [.bar inside %x %y] } {
set x [.bar axis invtransform x %x]
set y [.bar axis invtransform y %y]
puts stderr "$x $y"
}
}
Verify that the pointer is inside the plotting area with the "inside"
operation and then transform the mouse screen coordinates to graph
coordinates.
If you want the X-Y coordinates of the closest bar segment from the
mouse pointer, where ever the mouse is
bind .bar <Motion> {
if { [.bar element closest %x %y info] } {
puts stderr "$info(x) $info(y)"
}
}
use the "element closest" operation. It returns 1 if there is an
element within the threshold distance (see -halo option) and 0
otherwise.
--gah