#!/usr/bin/wish
#
#


set DATA {1 3 5 6 7 9 11 12 12 10}
set DATA2 {-3 3 7 8 9 9 11 12 12 10}
set DATA3 {-1 4 8 10 2 9 11 12 2 10}
set UDATA {0 1000 2000 3000 4000 5000 6000 7000 8000 9000}

set DISPLAY_W 600
set DISPLAY_H 180
set DISPLAY_PAD_X 50
set DISPLAY_PAD_X2 50
set DISPLAY_PAD_Y 50

set move(lastX) 0
set move(lastY) 0

proc plotMove {w i x y} {
	global move
	global PLOT
	if {$x - $PLOT($i,x) > [expr $PLOT($i,w) / 2]} {
		incr PLOT($i,w) [expr {$x-$move(lastX)}]
		incr PLOT($i,h) [expr {$y-$move(lastY)}]
	} else {
		incr PLOT($i,x) [expr {$x-$move(lastX)}]
		incr PLOT($i,y) [expr {$y-$move(lastY)}]

		.curve move "curve_frame_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
		.curve move "curve_scale_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
		.curve move "curve_bg_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
		.curve move "curve_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
	}
	set move(lastX) $x
	set move(lastY) $y
	if {$x - $PLOT($i,x) > [expr $PLOT($i,w) / 2]} {
		plot_bg $i
		plot_draw_data $i
	}
}

proc plotSet {x y} {
	global move
	set move(lastX) $x
	set move(lastY) $y
}

proc plotMoveEnd {i} {
	plot_redraw $i
}

proc set_point {i L POINT UPDOWN} {
	global PLOT
	set VAL [expr [lindex $PLOT($i,ydata,$L) $POINT] + $UPDOWN]
	set PLOT($i,ydata,$L) [lreplace $PLOT($i,ydata,$L) $POINT $POINT $VAL]
	eval .frame$L.point_$POINT.label configure -text "[lindex $PLOT($i,ydata,$L) $POINT]"
	plot_draw_data 1
	puts "## $L $POINT $UPDOWN ##"
}

proc plot_draw_data {i} {
	global PLOT
	global tk_library

	.curve delete "curve_$i"
	.curve delete "curve_scale_$i"

	set X_OFF $PLOT($i,x)
	set Y_OFF $PLOT($i,y)
	set W $PLOT($i,w)
	set H $PLOT($i,h)
	set TITLE_X $PLOT($i,title_x)
	set TITLE $PLOT($i,title)
	set PAD_X $PLOT($i,pad_x1)
	set PAD_X2 $PLOT($i,pad_x2)
	set PAD_Y $PLOT($i,pad_y2)
	set PAD_Y2 $PLOT($i,pad_y1)

	set L [expr ($W - $PAD_X - $PAD_X2) / 200.0]
	set VMIN 1000000
	set VMAX -1000000
	foreach POINT_VAL $PLOT($i,udata) {
		if {$VMIN > $POINT_VAL} {
			set VMIN $POINT_VAL
		} elseif {$VMAX < $POINT_VAL} {
			set VMAX $POINT_VAL
		}
	}

	set VMIN [expr $VMIN - 0.0]
	set VMAX [expr $VMAX + 0.0]
	set MAX [expr $VMAX - $VMIN]

	set PM 0
	while {[info exist PLOT($i,ydata,$PM)]} {
		incr PM
	}
	if {$PAD_X2 == -1} {
		set PAD_X2 [expr ($PM - 1) * $PAD_X]
	}
	if {$PAD_Y == -1} {
		set PAD_Y 40
	}

	set L_X $L
	set VMIN_X $VMIN
	set VMAX_X $VMAX
	set MAX_X $MAX

	set PN 0
	while {[info exist PLOT($i,ydata,$PN)]} {

		if {! [info exist PLOT($i,title_y,$PN)]} {
			set PLOT($i,title_y,$PN) "title-y $PN"
		}
		if {! [info exist PLOT($i,data_color,$PN)]} {
			set PLOT($i,data_color,$PN) "#ABABAB"
		}

		set VMIN 1000000
		set VMAX -1000000

		if {$PLOT($i,ysync) == 1} {
			set PN2 0
			while {[info exist PLOT($i,ydata,$PN2)]} {
				foreach POINT_VAL $PLOT($i,ydata,$PN2) {
					if {$VMIN > $POINT_VAL} {
						set VMIN $POINT_VAL
					} elseif {$VMAX < $POINT_VAL} {
						set VMAX $POINT_VAL
					}
				}
				incr PN2
			}
		} else {
			foreach POINT_VAL $PLOT($i,ydata,$PN) {
				if {$VMIN > $POINT_VAL} {
					set VMIN $POINT_VAL
				} elseif {$VMAX < $POINT_VAL} {
					set VMAX $POINT_VAL
				}
			}
		}
		if {$PLOT($i,min,$PN) != ""} {
			set VMIN $PLOT($i,min,$PN)
		}
		if {$PLOT($i,max,$PN) != ""} {
			set VMAX $PLOT($i,max,$PN)
		}

		set VMIN [expr $VMIN - 1.0]
		set VMAX [expr $VMAX + 1.0]
		set MAX [expr $VMAX - $VMIN]
		set DMAX [llength $PLOT($i,udata)]

		set X $PAD_X
		set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ([lindex $PLOT($i,ydata,$PN) 0] + ($VMIN * -1)) / $MAX)]
		set LAST_Y $Y
		set LAST_X $X
		set NUM 0
		foreach POINT_VAL $PLOT($i,ydata,$PN) {
			set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ($POINT_VAL + ($VMIN * -1)) / $MAX)]
			set X [expr $PAD_X + ([expr $W - $PAD_X - $PAD_X2] * ([lindex $PLOT($i,udata) $NUM] + ($VMIN_X * -1)) / $MAX_X)]
			if {$PN == 0 && $PLOT($i,fill,$PN) == 1} {
				.curve create polygon \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $LAST_Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $H - $PAD_Y] \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $H - $PAD_Y] \
				-fill "#ABABAB" \
				-tag "curve_$i"
			} elseif {$PLOT($i,fill,$PN) == 1} {
				.curve create polygon \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $LAST_Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $H - $PAD_Y] \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $H - $PAD_Y] \
				-fill "#DEDEDE" \
				-stipple @[file join $tk_library demos images gray25.bmp] \
				-tag "curve_$i"
			}

			if {$PLOT($i,bar,$PN) == 1} {
				.curve create rectangle [expr $X_OFF + $X + ($PN * 5) - 12] [expr $Y_OFF + $PAD_Y2 + $Y] [expr $X_OFF + $X + ($PN * 5) + 12] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "$PLOT($i,data_color_fill,$PN)" -outline "$PLOT($i,data_color_fill,$PN)" -tag "curve_$i"
			} else {
				.curve create line [expr $X_OFF + $LAST_X] [expr $Y_OFF + $LAST_Y + $PAD_Y2] [expr $X_OFF + $X] [expr $Y_OFF + $Y + $PAD_Y2] -width 2 -fill "$PLOT($i,data_color,$PN)" -tag "curve_$i"
			}


			set LAST_X $X
			set LAST_Y $Y
			incr NUM
		}

		set X $PAD_X
		set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ([lindex $PLOT($i,ydata,$PN) 0] + ($VMIN * -1)) / $MAX)]
		set LAST_Y $Y
		set LAST_X $X
		set NUM 0
		foreach POINT_VAL $PLOT($i,ydata,$PN) {
			set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ($POINT_VAL + ($VMIN * -1)) / $MAX)]
			set X [expr $PAD_X + ([expr $W - $PAD_X - $PAD_X2] * ([lindex $PLOT($i,udata) $NUM] + ($VMIN_X * -1)) / $MAX_X)]
			if {$NUM > 0} {
				if {$PLOT($i,point,$PN) == 1} {
					.curve create text [expr $X_OFF + $X - 1] [expr $Y_OFF + $Y + $PAD_Y2 - 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.curve create text [expr $X_OFF + $X - 1] [expr $Y_OFF + $Y + $PAD_Y2 - 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.curve create text [expr $X_OFF + $X] [expr $Y_OFF + $Y + $PAD_Y2] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,data_color,$PN)" -anchor s -tag "curve_scale_$i"
				}

				#.curve delete "curve_scale-a_$i-$PN-$NUM"
				#.curve delete "curve_scale-b_$i-$PN-$NUM"
				#.curve create rectangle [expr $X_OFF + $X - 5] [expr $Y_OFF + $Y + $PAD_Y2 - 10] [expr $X_OFF + $X + 5] [expr $Y_OFF + $Y + $PAD_Y2] -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale-a_$i-$PN-$NUM"
				#.curve create rectangle [expr $X_OFF + $X - 5] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $X + 5] [expr $Y_OFF + $Y + $PAD_Y2 + 10] -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale-b_$i-$PN-$NUM"
				#eval .curve bind "curve_scale-a_$i-$PN-$NUM" <ButtonPress-1> \{set_point $i $PN $NUM 1\}
				#eval .curve bind "curve_scale-b_$i-$PN-$NUM" <ButtonPress-1> \{set_point $i $PN $NUM -1\}
			}
			set LAST_X $X
			set LAST_Y $Y
			incr NUM
		}

		set L [expr ($H - $PAD_Y - $PAD_Y2) / 25.0]
		for {set POINT_VAL $VMIN} {$POINT_VAL <= $VMAX} {set POINT_VAL [expr $POINT_VAL + ($MAX / $L)]} {
			set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ($POINT_VAL + ($VMIN * -1)) / $MAX)]
			if {$PN == 0} {

				if {$PLOT($i,ysync) == 1} {
					.curve create line [expr $X_OFF + $PAD_X - 5] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "#CDCDCD" -tag "curve_scale_$i"
				} else {
					.curve create line [expr $X_OFF + $PAD_X - 5] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $PAD_X] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale_$i"
				}

				.curve create text [expr $X_OFF + $PAD_X / 3 * 2 + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor c -tag "curve_scale_$i"
				.curve create text [expr $X_OFF + $PAD_X / 3 * 2] [expr $Y_OFF + $Y + $PAD_Y2] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,data_color,$PN)" -anchor c -tag "curve_scale_$i"
			} else {
				.curve create line [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0)] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0) + 5] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale_$i"
				.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor c -tag "curve_scale_$i"
				.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1)] [expr $Y_OFF + $Y + $PAD_Y2] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,data_color,$PN)" -anchor c -tag "curve_scale_$i"
			}
		}


		destroy .curve.sync2-$i-$PN
		eval checkbutton .curve.sync2-$i-$PN -text \"\" -variable wipers -relief flat -variable PLOT($i,point,$PN) -command \{ \
			plot_redraw $i \
		\}

		destroy .curve.sync3-$i-$PN
		eval checkbutton .curve.sync3-$i-$PN -text \"\" -variable wipers -relief flat -variable PLOT($i,fill,$PN) -command \{ \
			plot_redraw $i \
		\}

		destroy .curve.sync4-$i-$PN
		eval checkbutton .curve.sync4-$i-$PN -text \"\" -variable wipers -relief flat -variable PLOT($i,bar,$PN) -command \{ \
			plot_redraw $i \
		\}

		if {$PN == 0} {
			.curve create text [expr $X_OFF + $PAD_X / 3 + 1] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2 + 1] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,shadow_color)" -anchor c -justify center -tag "curve_scale_$i"
			.curve create text [expr $X_OFF + $PAD_X / 3] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,data_color,$PN)" -anchor c -justify center -tag "curve_scale_$i"

			.curve create window [expr $X_OFF + $PAD_X / 3] [expr $Y_OFF + $H - 5] -anchor s -window .curve.sync2-$i-$PN -tag "curve_bg_$i-$PN"
			.curve create window [expr $X_OFF + $PAD_X / 3 + 15] [expr $Y_OFF + $H - 5] -anchor s -window .curve.sync3-$i-$PN -tag "curve_bg_$i-$PN"
			.curve create window [expr $X_OFF + $PAD_X / 3 + 30] [expr $Y_OFF + $H - 5] -anchor s -window .curve.sync4-$i-$PN -tag "curve_bg_$i-$PN"
		} else {
			.curve create line [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0) + 1] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0) + 1] [expr $Y_OFF + $H - $PAD_Y] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale_$i"
			.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 2) + 1] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2 + 1] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,shadow_color)" -anchor c -justify center -tag "curve_scale_$i"
			.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 2)] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,data_color,$PN)" -anchor c -justify center -tag "curve_scale_$i"

			.curve create window [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 0] [expr $Y_OFF + $H - 5] -anchor s -window .curve.sync2-$i-$PN -tag "curve_bg_$i-$PN"
			.curve create window [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 15] [expr $Y_OFF + $H - 5] -anchor s -window .curve.sync3-$i-$PN -tag "curve_bg_$i-$PN"
			.curve create window [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 30] [expr $Y_OFF + $H - 5] -anchor s -window .curve.sync4-$i-$PN -tag "curve_bg_$i-$PN"
		}
		incr PN
	}

	# Scale X
	set NUM 0
	foreach POINT_VAL $PLOT($i,udata) {
		set X [expr $PAD_X + ([expr $W - $PAD_X - $PAD_X2] * ([lindex $PLOT($i,udata) $NUM] + ($VMIN_X * -1)) / $MAX_X)]
		.curve create line [expr $X_OFF + $X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $X] [expr $Y_OFF + $H - $PAD_Y + 2] -width 1 -fill "$PLOT($i,hl_color_x)" -tag "curve_$i"
		.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $H - $PAD_Y / 3 * 2 + 1] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor c -tag "curve_$i"
		.curve create text [expr $X_OFF + $X] [expr $Y_OFF + $H - $PAD_Y / 3 * 2] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,title_color)" -anchor c -tag "curve_$i"
		incr NUM
	}

	# Border
	.curve create line [expr $X_OFF + $PAD_X] [expr $Y_OFF + $H - $PAD_Y] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "#EFEFEF" -tag "curve_scale_$i"
	.curve create line [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "#EFEFEF" -tag "curve_scale_$i"
	.curve create line [expr $X_OFF + $PAD_X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $PAD_Y2] -width 2 -fill "#ABABAB" -tag "curve_scale_$i"
	.curve create line [expr $X_OFF + $PAD_X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $PAD_X] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "#ABABAB" -tag "curve_scale_$i"

}

proc plot_bg {i} {
	global PLOT
	set X_OFF $PLOT($i,x)
	set Y_OFF $PLOT($i,y)
	set W $PLOT($i,w)
	set H $PLOT($i,h)
	set PAD_X $PLOT($i,pad_x1)
	set PAD_X2 $PLOT($i,pad_x2)
	set PAD_Y $PLOT($i,pad_y2)
	set TITLE_X $PLOT($i,title_x)
	set TITLE $PLOT($i,title)
	set PAD_X $PLOT($i,pad_x1)
	set PAD_X2 $PLOT($i,pad_x2)
	set PAD_Y $PLOT($i,pad_y2)
	set PAD_Y2 $PLOT($i,pad_y1)

	.curve delete "curve_bg_$i"

	set PM 0
	while {[info exist PLOT($i,ydata,$PM)]} {
		incr PM
	}
	if {$PAD_X2 == -1} {
		set PAD_X2 [expr ($PM - 1) * $PAD_X]
	}
	if {$PAD_Y == -1} {
		set PAD_Y 40
	}

	.curve create rectangle $X_OFF $Y_OFF [expr $X_OFF + $W] [expr $Y_OFF + $H] -width 1 -fill "$PLOT($i,frame_color)" -tag "curve_frame_$i"
	.curve create rectangle [expr $X_OFF + $PAD_X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $H - $PAD_Y] -width 0 -fill "$PLOT($i,bg_color)" -tag "curve_bg_$i"

	.curve create text [expr $X_OFF + $W / 2 + 1] [expr $Y_OFF + $PAD_Y2 / 2 + 1] -text "$TITLE" -fill "$PLOT($i,title_bg_color)" -anchor c -justify center -tag "curve_bg_$i" -font "12x24"
	.curve create text [expr $X_OFF + $W / 2] [expr $Y_OFF + $PAD_Y2 / 2] -text "$TITLE" -fill "$PLOT($i,title_color)" -anchor c -justify center -tag "curve_bg_$i" -font "12x24"

	.curve create line $X_OFF [expr $Y_OFF + $H] [expr $X_OFF + $W] [expr $Y_OFF + $H] -width 2 -fill "#ABABAB" -tag "curve_bg_$i"
	.curve create line [expr $X_OFF + $W] $Y_OFF [expr $X_OFF + $W] [expr $Y_OFF + $H] -width 2 -fill "#ABABAB" -tag "curve_bg_$i"
	.curve create line $X_OFF $Y_OFF [expr $X_OFF + $W] $Y_OFF -width 2 -fill "#EFEFEF" -tag "curve_bg_$i"
	.curve create line $X_OFF $Y_OFF $X_OFF [expr $Y_OFF + $H] -width 2 -fill "#EFEFEF" -tag "curve_bg_$i"


	destroy .curve.sync-$i
	eval checkbutton .curve.sync-$i -text \"Sync-Y\" -variable wipers -relief flat -variable PLOT($i,ysync) -command \{ \
		plot_redraw $i\
	\}
	.curve create window [expr $X_OFF + 10] [expr $Y_OFF + 10] -anchor nw -window .curve.sync-$i -tag "curve_bg_$i"

	destroy .curve.padx-$i
	spinbox .curve.padx-$i -from 0 -to 300 -increment 5 -format %05.2f -width 10 -textvariable PLOT($i,pad_x1) -command {
		plot_redraw $i\
	}
	.curve create window [expr $X_OFF + 110] [expr $Y_OFF + 10] -anchor nw -window .curve.padx-$i -tag "curve_bg_$i"

	eval bind .curve.padx-$i <ButtonPress-4> \{set PLOT($i,pad_x1) \[expr \$PLOT($i,pad_x1) + 1\]\nplot_bg $i\nplot_draw_data $i\}
	eval bind .curve.padx-$i <ButtonPress-5> \{set PLOT($i,pad_x1) \[expr \$PLOT($i,pad_x1) - 1\]\nplot_bg $i\nplot_draw_data $i\}



.curve bind curve_frame_$i <B1-Motion> "plotMove .curve $i %x %y"
.curve bind curve_frame_$i <ButtonPress-1> "plotSet %x %y"
.curve bind curve_frame_$i <ButtonRelease-1> "plotMoveEnd $i"


}

proc plot_redraw {i} {
	.curve delete curve_frame_$i
	plot_bg $i
	plot_draw_data $i
}

proc plot_add {NUM DATA} {
	global PLOT
	set PN 0
	while {[info exist PLOT($NUM,ydata,$PN)]} {
		incr PN
	}
	set PLOT($NUM,ydata,$PN) $DATA
	set PLOT($NUM,title_y,$PN) "title_y0"
	if {$PN == 1} {
		set PLOT($NUM,data_color,$PN) "#00FF00"
		set PLOT($NUM,data_color_fill,$PN) "#00FFF0"
	} elseif {$PN == 2} {
		set PLOT($NUM,data_color,$PN) "#0000FF"
		set PLOT($NUM,data_color_fill,$PN) "#000FFF"
	} else {
		set PLOT($NUM,data_color,$PN) "#FFFF00"
		set PLOT($NUM,data_color_fill,$PN) "#FFF000"
	}

	set PLOT($NUM,point,$PN) "0"
	set PLOT($NUM,fill,$PN) "0"
	set PLOT($NUM,bar,$PN) "0"
	set PLOT($NUM,min,$PN) ""
	set PLOT($NUM,max,$PN) ""
}

proc plot_new {X Y W H UDATA DATA} {
	global PLOT
	set NUM 1
	while {[info exist PLOT($NUM,x)]} {
		incr NUM
	}
	set PLOT($NUM,udata) $UDATA
	set PLOT($NUM,x) $X
	set PLOT($NUM,y) $Y
	set PLOT($NUM,w) $W
	set PLOT($NUM,h) $H
	set PLOT($NUM,pad_x1) 70
	set PLOT($NUM,pad_x2) -1
	set PLOT($NUM,pad_y1) 70
	set PLOT($NUM,pad_y2) 70
	set PLOT($NUM,title_x) "title_x"
	set PLOT($NUM,title) "title"

	set PLOT($NUM,shadow_color) "#676767"

	set PLOT($NUM,ydata,0) $DATA
	set PLOT($NUM,title_y,0) "title_y0"
	set PLOT($NUM,data_color,0) "#FF0000"
	set PLOT($NUM,data_color_fill,0) "#FFF000"
	set PLOT($NUM,point,0) "0"
	set PLOT($NUM,fill,0) "0"
	set PLOT($NUM,bar,0) "0"
	set PLOT($NUM,min,0) ""
	set PLOT($NUM,max,0) ""

	set PLOT($NUM,frame_color) "#BCBCBC"
	set PLOT($NUM,bg_color) "#343434"
	set PLOT($NUM,title_color) "#FFFFFF"
	set PLOT($NUM,title_bg_color) "#000000"
	set PLOT($NUM,hl_color_x) "#343434"
	set PLOT($NUM,hl_color_y) "#343434"

	set PLOT($NUM,ysync) "1"

	return $NUM
}

proc plot_configure {NUM NAME VALUE} {
	global PLOT
	set PLOT($NUM,$NAME) $VALUE
}





##############################################################################################################################################################



#wm geometry . 320x240
wm title . "RCDI-Tool"

canvas .curve -relief raised -width 1400 -height 700
pack .curve -side top -fill none -expand no
.curve create rectangle 0 0 1700 2400 -width 1 -fill "#121212"

frame .frame0
#pack .frame0 -side top -fill both -expand yes

frame .frame1
#pack .frame1 -side top -fill both -expand yes

for {set POINT 0} {$POINT < [llength $DATA]} {incr POINT} {

	frame .frame0.point_$POINT
	pack .frame0.point_$POINT -side left -fill x -expand yes

		eval button .frame0.point_\$POINT.up -text \"+\" -command \{set_point 1 \"0\" \"$POINT\" \"0.5\"\}
		pack .frame0.point_$POINT.up -side top -fill x -expand yes

		eval label .frame0.point_\$POINT.label -text \"[lindex $DATA $POINT]\"
		pack .frame0.point_$POINT.label -side top -fill x -expand yes

		eval button .frame0.point_\$POINT.down -text \"-\" -command \{set_point 1 \"0\" \"$POINT\" \"-0.5\"\}
		pack .frame0.point_$POINT.down -side top -fill x -expand yes

	frame .frame1.point_$POINT
	pack .frame1.point_$POINT -side left -fill x -expand yes

		eval button .frame1.point_\$POINT.up -text \"+\" -command \{set_point 1 \"1\" \"$POINT\" \"0.5\"\}
		pack .frame1.point_$POINT.up -side top -fill x -expand yes

		eval label .frame1.point_\$POINT.label -text \"[lindex $DATA2 $POINT]\"
		pack .frame1.point_$POINT.label -side top -fill x -expand yes

		eval button .frame1.point_\$POINT.down -text \"-\" -command \{set_point 1 \"1\" \"$POINT\" \"-0.5\"\}
		pack .frame1.point_$POINT.down -side top -fill x -expand yes

}



set PLOT_ID [plot_new 50 20 1300 300 {} {}]
plot_configure $PLOT_ID "title" "Frequenzband"
plot_configure $PLOT_ID "title_x" "Freq"
plot_configure $PLOT_ID "title_y,0" "Level"
plot_configure $PLOT_ID "max,0" "27"
plot_configure $PLOT_ID "min,0" "0"
plot_add $PLOT_ID {}
plot_configure $PLOT_ID "title_y,1" "Max-Level"
plot_configure $PLOT_ID "max,1" "27"
plot_configure $PLOT_ID "min,1" "0"
#plot_add $PLOT_ID $DATA3
plot_redraw $PLOT_ID



set PLOT_ID [plot_new 50 340 1300 300 $UDATA $DATA]
plot_configure $PLOT_ID "title" "Zuendzeitpunkt"
plot_configure $PLOT_ID "title_x" "RPM"
plot_configure $PLOT_ID "title_y,0" "Grad"
plot_add $PLOT_ID $DATA2
plot_redraw $PLOT_ID






proc Serial_Init {ComPort ComRate} {
	set iChannel [open $ComPort w+]
	set rate $ComRate
	fconfigure $iChannel -mode $ComRate,n,8,1
	fconfigure $iChannel -blocking 0
	fconfigure $iChannel -buffering none
	fileevent $iChannel readable ""
	return $iChannel
}


proc append2_point {i L COUNT VALUE} {
	global PLOT

	lappend PLOT($i,udata) "$COUNT"
	lappend PLOT($i,ydata,$L) "$VALUE"

	plot_draw_data $i
}

proc set2_point {i L COUNTS VALUES} {
	global PLOT

	set PLOT($i,udata) "$COUNTS"
	set PLOT($i,ydata,$L) "$VALUES"

	plot_draw_data $i
}


set line ""
set COUNT 0

set max_line ""

proc rd_chid {chid} {
	global line
	global max_line
	global COUNT
	set msg [read $chid 1]
	if {$msg == "\n"} {
		if {[string match "SCN;*" $line]} {
			if {[expr $COUNT % 2] == 0} {


#				puts "[lrange [split $line ";"] 1 end]"

set max_line2 {}

for {set NV 0} {$NV < [llength [lrange [split $line ";"] 1 end]]} {set NV [expr $NV + 1]} {

	set MVAL [lindex [split $line ";"] [expr $NV + 1]]
	set MVAL2 [lindex $max_line $NV]

puts "## [lindex $max_line $NV] $NV"

	if {$MVAL < $MVAL2} {
		lappend max_line2 $MVAL2
	} else {
		lappend max_line2 $MVAL
	}
}

puts "$max_line -- $max_line2"

set max_line $max_line2


set xline {}

for {set NV 2405} {$NV < 2485} {set NV [expr $NV + 5]} {
	lappend xline $NV
}

set2_point 1 0 $xline [lrange [split $line ";"] 1 end]
set2_point 1 1 $xline $max_line



			}
			incr COUNT

		}
		set line ""
	} else {
		set line "$line$msg"
	}
}




if {[lindex $argv 0] != ""} {
	puts "Open Serial-Port: [lindex $argv 0]"
	if {[lindex $argv 1] != ""} {
		set Serial [Serial_Init "[lindex $argv 0]" [lindex $argv 1]]
	} else {
		set Serial [Serial_Init "[lindex $argv 0]" 38400]
	}
} else {
	puts "USAGE: rx-tool COMPORT \[BAUD\]"
	exit 1
}

fileevent $Serial readable [list rd_chid $Serial]
puts -nonewline $Serial "c"







