Version 0 of Data selection in plotchart

Updated 2017-07-23 09:14:25 by hae

hae Having an xyplot it would be great to select data by means of a bars in order to export the data. This is my first start.

lappend auto_path [file join  [pwd] plotchart]

catch {console show}

package require Plotchart

set TMP [list 24.3 25.1 24.6 25.3 24.8 25.01 25.2 25.1 25 \
              27.3 35.1 50   70   85   89    93   95   96 97 97.5 98 99.2 99.5 99.6 99.7 99.8 99.9 100
        ]
        
set Data(Bar,Selected) ""
set Data(Bar,Pixels) ""

set Data(Plot) ""
        
proc InitUI { } {
    canvas .c -background white -width 400 -height 200
    pack   .c -fill both
    
}

proc DrawPlot { YData } {
    variable Data
    #
    # Create the plot with its x- and y-axes
    #
    set p [::Plotchart::createXYPlot .c {0.0 100.0 10.0} {0.0 100.0 20.0}]
    set Data(Plot) $p
    
    set X 0
    foreach Y $YData {
        $p plot series1 $X $Y
        incr X
    }
    
    $p title "Temperatur TMP \[°C\]"
    
    set Data(Bar,List) ""
    lappend Data(Bar,List) [$p object line series1  5   0   5 100 -dash {2 2} -fill green -tags Bar]
    lappend Data(Bar,List) [$p object line series1 20   0  20 100 -dash {2 2} -fill   red -tags Bar]
    
    # set c [$p canvas]
    set c .c
    bind $c <ButtonPress-1>   [list BarAction %W %x %y press]
    bind $c <B1-Motion>       [list BarAction %W %x %y move]
    bind $c <ButtonRelease-1> [list BarAction %W %x %y release]
}

proc BarAction { w x y action args } {
    variable Data
    puts [info level 0]
    
    switch $action {
        press {
            set Closest [$w find closest $x $y]
            puts "Closest: $Closest"
            
            if { $Closest ni $Data(Bar,List) } {
                puts "Not a Bar! Must be a line segment."
                return
            }
        
            set Data(Bar,Selected) $Closest
            set Pixels [Plotchart::coordsToPixel $w 10 101]
            lassign $Pixels XPixel YPixel
            set Data(Bar,Origin) [list $x $YPixel]
            set Data(Bar,Pixels) [list $x $y]
        }
        
        move {
            if { $Data(Bar,Selected) eq "" } {
                return
            }
            
            lassign $Data(Bar,Origin) OriginX OriginY
            $w moveto $Data(Bar,Selected) $x $OriginY
            puts "move $x $OriginY"
        }
        
        release {
            set Data(Bar,Selected) ""
            set Coords [Plotchart::pixelToCoords $w $x $y]
            puts "Coords: $Coords"
        }
        
        default {
        }
    }
}

InitUI
DrawPlot $TMP