Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | add ukaz to assets folder |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
35c4f510aa3ffe582a49b9ceb6e41e0e |
User & Date: | chw 2016-11-16 07:55:50.157 |
Context
2016-11-16
| ||
07:56 | add VecTcLab to assets folder check-in: be67606d89 user: chw tags: trunk | |
07:55 | add ukaz to assets folder check-in: 35c4f510aa user: chw tags: trunk | |
05:56 | add nsf and xotcl to [undroidwish] build check-in: 6718184d53 user: chw tags: trunk | |
Changes
Added assets/ukaz2.0/LICENSE.
> > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | The MIT License (MIT) Copyright (c) 2013 Christian Gollwitzer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Added assets/ukaz2.0/README.md.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | Ukaž ==== A graph widget written in pure Tcl/Tk. This is a package which provides a widget to plot data in x-y format for scientific applications. It sets reasonable defaults to display most datasets without the need for individual settings. Ukaž was designed to be easy to use and borrows most of the syntax from the popular gnuplot package [http://www.gnuplot.info], slightly adapted to be more Tcl friendly. The simplest usage looks like this: package require ukaz pack [ukaz::graph .g] -expand yes -fill both set data {1 4.5 2 7.3 3 1.2 4 6.5} .g plot $data This displays a resizable plot of the data, which is expected as a list of alternating x-y coordinates. The range of the axis, the number and placement of the the tic marks and the plot style is automatically chosen. The data can be zoomed in by dragging the left mouse button over the canvas, and zoomed out by clicking the right mouse button. The style of the plot can be adjusted by passing more options to the plot command, and by setting global options using either gnuplot-style commands or Tcl-style configure commands: package require ukaz pack [ukaz::graph .g] -expand yes -fill both set data {1 4.5 2 7.3 3 1.2 4 6.5} .g plot $data with points pointtype filled-squares color "#A0FFA0" .g set log y This displays the same data with light-green squares on a logarithmic y-axis. Datafiles can be displayed by the /using/ modifier as in gnuplot: .g plot "somedata.dat" using 1:2 with lines lc blue .g plot "somedata.dat" using {1:($2/$4)} This reads the file "somedata.dat" and plots the second column versus the first with a blue line. In addition, it plots the ratio of the 2nd to the 4th column with points. Interactive features -------------------- The graph widget sends virtual events when the user zooms the data or clicks into the plot. This enables simple interactive data dialogs. In addition, special widget-like objects, controls, can be added to the graph. The first implemented one is the /dragline/. This is a horizontal or vertical line, which displays a variable in the graph, e.g. a threshold, and can be dragged by the user. dragline d -orient horizontal -variable v set v 0 .g addcontrol d How does ukaž compare to ------------------------ * Plotchart Data management, automatic scaling * Gnuplot much simpler, not easy to embed, many terminals ... TODO ==== * More gnuplot-like set methods (set xrange, xtics, xlabel ...) * More examples * Data transformation for lists * Legend |
Added assets/ukaz2.0/demo/axisformat.tcl.
> > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | set basedir [file dirname [info script]] lappend auto_path [file dirname $basedir] package require ukaz 2.0a3 pack [ukaz::graph .g -width 500 -height 400] -expand yes -fill both set t1 [clock scan "00:04:05"] set t2 [clock scan "00:08:05"] set data [list $t1 4.5 $t2 8] .g plot $data proc percent {x} { format %.0f%% [expr {100*$x}] } .g set format x %H:%M:%S timedate .g set format y percent command # place a tic at every minute .g set xtics 60 |
Added assets/ukaz2.0/demo/linuxload.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # display load avg on Linux package require ukaz package require fileutil pack [ukaz::graph .l] -expand yes -fill both set lastload {0 0} set time 0 proc newload {} { variable lastload variable time variable graphid # read current loadavg last 1, 5 and 15 minutes lassign [fileutil::cat /proc/loadavg] l1 l5 l15 incr time lappend lastload $time $l1 if {[llength $lastload]>1000} { # for more than 1000 entries, cut it down set lastload [lrange $lastload end-999 end] } .l update $graphid data $lastload after 500 newload } set graphid [.l plot $lastload with lines color red title "CPU load"] .l set xlabel "Time" .l set ylabel "Load" newload |
Added assets/ukaz2.0/demo/simpletest.tcl.
> > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | set basedir [file dirname [info script]] lappend auto_path [file join $basedir .. lib] if {0} { package require ukaz pack [ukaz::graph .g -width 500 -height 400] -expand yes -fill both set data {1 4.5 2 7.3 3 1.2 4 6.5} .g plot $data } package require ukaz pack [ukaz::graph .g] -expand yes -fill both set data {1 4.5 2 7.3 3 1.2 4 6.5} .g plot $data with points pointtype filled-squares color "#A0FFA0" .g set log y |
Added assets/ukaz2.0/demo/sine.dat.
> > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # Curve 0 of 1, 15 points # Curve title: "sin(x)" # x y type 0 0 i 0.897598 0.781831 i 1.7952 0.974928 i 2.69279 0.433884 i 3.59039 -0.433884 i 4.48799 -0.974928 i 5.38559 -0.781831 i 6.28319 -2.44929e-16 i 7.18078 0.781831 i 8.07838 0.974928 i 8.97598 0.433884 i 9.87358 -0.433884 i 10.7712 -0.974928 i 11.6688 -0.781831 i 12.5664 -4.89859e-16 i |
Added assets/ukaz2.0/demo/uktest.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | set basedir [file dirname [info script]] set datafile [file join $basedir sine.dat] lappend auto_path [file join $basedir .. lib] package require Tk package require ukaz ukaz::graph .g ttk::label .l -textvariable status grid .g -sticky nsew grid .l -sticky nsew grid columnconfigure . 0 -weight 1 grid rowconfigure . 0 -weight 1 bind .g <<MotionEvent>> { pointerinfo {*}%d} bind .g <<Click>> { click %x %y {*}%d } proc click {x y xtr ytr} { # output the position of the click and transformed coords puts "Click at $x, $y (graph [format %.5g $xtr], [format %.5g $ytr])" # look for data point nearby lassign [.g pickpoint $x $y] id dpnr xd yd if {$id != {}} { puts "Data point $dpnr, set $id, ([format %.5g $xd], [format %.5g $yd])" } else { puts "No data point nearby" } } proc pointerinfo {x y} { set ::status "[format %.5g $x], [format %.5g $y]" } .g plot $datafile using 1:2 with points pt filled-triangles color blue ps 1.0 title "shortsine" .g plot $datafile using {1:(2*$2)} with lines color red lw 3 title "doubled sine" for {set i 0} {$i<1000} {incr i} { set x [expr {double($i)/100.0}] lappend data $x [expr {sin($x*2*3.1415926535/3.2)*exp(-$x/5.0)}] } .g plot $data w lp pt filled-squares ukaz::dragline d -variable v -orient horizontal .g addcontrol d set v 0 |
Added assets/ukaz2.0/pkgIndex.tcl.
> > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 | # Tcl package index file, version 1.1 # This file is generated by the "pkg_mkIndex" command # and sourced either when an application starts up or # by a "package unknown" script. It invokes the # "package ifneeded" command to set up package-related # information so that packages will be loaded automatically # in response to "package require" commands. When this # script is sourced, the variable $dir must contain the # full path name of this file's directory. package ifneeded ukaz 2.0a3 [list source [file join $dir ukaz.tcl]] |
Added assets/ukaz2.0/ukaz.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 | package require snit package require Tk 8.6 package provide ukaz 2.0a3 namespace eval ukaz { variable ns [namespace current] ##### General functions ############### proc lremove {list element} { lsearch -all -inline -not -exact $list $element } ##### Functions for geometric operations (clipping) ############ namespace eval geometry { proc polylineclip {cdata range} { variable xmin [dict get $range xmin] variable xmax [dict get $range xmax] variable ymin [dict get $range ymin] variable ymax [dict get $range ymax] if {$xmin > $xmax} { lassign [list $xmin $xmax] xmax xmin } if {$ymin > $ymax} { lassign [list $ymin $ymax] ymax ymin } set result {} set piece {} # clip infinity of first point set x1 Inf set y1 Inf while {[indefinite $x1 $y1]} { set cdata [lassign $cdata x1 y1] if {[llength $cdata]<2} { return {} } } foreach {x2 y2} $cdata { # clip total indefinite points if {[indefinite $x2 $y2]} { # end last line if {$piece != {}} { lappend result $piece } set piece {} continue } lassign [cohensutherland $x1 $y1 $x2 $y2] clipline type switch $type { rightclip { # second point was clipped if {$piece == {}} { # it is the first line segment # make single segment lappend result $clipline } else { lappend piece {*}[lrange $clipline 2 3] lappend result $piece set piece {} } } leftclip { # first point was clipped, begin new line set piece $clipline } noclip { # append as given # if we are the first, include 1st point if {[llength $piece]==0} { set piece [list $x1 $y1] } lappend piece $x2 $y2 } empty { # end last line if {$piece != {}} { lappend result $piece } set piece {} } bothclip { # create line on it's own # end last line if {$piece != {}} { lappend result $piece } set piece {} lappend result $clipline } } # advance set x1 $x2 set y1 $y2 } # end last line if {$piece != {}} { lappend result $piece } return $result } proc cohensutherland {x1 y1 x2 y2} { variable xmin variable xmax variable ymin variable ymax set codeleft [pointcode $x1 $y1] set coderight [pointcode $x2 $y2] if {($codeleft | $coderight) == 0} { return [list [list $x1 $y1 $x2 $y2] noclip] } if {($codeleft & $coderight) != 0} { return {{} empty} } # if we are here, one of the points must be clipped set left false set right false for {set iter 0} {$iter<20} {incr iter} { if {$codeleft != 0} { # left point is outside set left true lassign [intersect $x1 $y1 $x2 $y2] x1 y1 set codeleft [pointcode $x1 $y1] } else { # right point outside set right true lassign [intersect $x2 $y2 $x1 $y1] x2 y2 set coderight [pointcode $x2 $y2] } if {($codeleft & $coderight) != 0} { return {{} empty} } if {($codeleft | $coderight) == 0} { if {$left && $right} { return [list [list $x1 $y1 $x2 $y2] bothclip] } if {$left} { return [list [list $x1 $y1 $x2 $y2] leftclip] } if {$right} { return [list [list $x1 $y1 $x2 $y2] rightclip] } return "Can't happen $x1 $y1 $x2 $y2" } } return "Infinite loop $x1 $y1 $x2 $y2 " } proc pointcode {x y} { variable xmin variable xmax variable ymin variable ymax expr {(($x<$xmin)?1:0) | (($x>$xmax)?2:0) | (($y<$ymin)?4:0) | (($y>$ymax)?8:0) } } proc intersect {x1 y1 x2 y2} { variable xmin variable xmax variable ymin variable ymax # check for infinity if {$y1 == Inf} { return [list $x2 $ymax] } if {$y1 == -Inf} { return [list $x2 $ymin] } if {$x1 == Inf} { return [list $xmax $y2] } if {$x1 == -Inf} { return [list $xmin $y2] } if {$y1>$ymax} { return [list [expr {$x1+($x2-$x1)*($ymax-$y1)/($y2-$y1)}] $ymax] } if {$y1<$ymin} { return [list [expr {$x1+($x2-$x1)*($ymin-$y1)/($y2-$y1)}] $ymin] } if {$x1>$xmax} { return [list $xmax [expr {$y1+($y2-$y1)*($xmax-$x1)/($x2-$x1)}]] } return [list $xmin [expr {$y1+($y2-$y1)*($xmin-$x1)/($x2-$x1)}]] } proc indefinite {x y} { expr { ($x!=$x) || ($y != $y) || (abs($x) == Inf && abs($y) == Inf)} } proc pointclip {cdata range} { # remove all points which are NaN or outside # the clip region set xmin [dict get $range xmin] set xmax [dict get $range xmax] set ymin [dict get $range ymin] set ymax [dict get $range ymax] set result {} set clipinfo {} set clipid 0 foreach {x y} $cdata { if {$x!=$x || $y!=$y || $x<$xmin || $x>$xmax || $y<$ymin || $y>$ymax} { dict incr clipinfo $clipid continue } lappend result $x $y incr clipid } list $result $clipinfo } } ############## Functions for deferred execution ############################ variable Requests {} proc defer {cmd} { # defer cmd to idle time. Multiple requests are merged variable ns variable Requests if {[dict size $Requests] == 0} { after idle ${ns}::doRequests } dict set Requests $cmd 1 } proc doRequests {} { variable Requests # first clear Requests, so that new requests are only recorded # during execution and do not interfere with the execution set ReqCopy $Requests set Requests {} dict for {cmd val} $ReqCopy { uplevel #0 $cmd } } ########## Functions for math on data ############################## proc parsedata_using {fdata args} { # read column data # analogous to "using" in gnuplot # the elements of formatlist are interpreted as expr-String with embedded $0, $1, ... # return as flat a list set ncomments 0 set nblanks 0 set ndata 0 set skip 0 variable parseerrors {} set 0 0 set lno 0 set result {} # $0 contains the linenumber, initially it's 0 foreach line [split $fdata \n] { # make list incr lno set cols [regexp -all -inline {[^[:space:]]+} $line] # puts "$0: $cols" if {[regexp {^[[:space:]]*#} $line]} { # it is a comment starting with "#" #puts "Comment $line" incr ncomments continue } if {[llength $cols]==0} { # blank line #puts "Blank line" incr nblanks continue } # puts $line # extract the columns and put them as double into $ind # if possible namespace eval formula [list set 0 $0] namespace eval formula [list set lno $lno] for {set ind 1} {$ind<=[llength $line]} {incr ind} { set indtext [lindex $line [expr {$ind - 1}]] if {[string is double -strict $indtext]} { namespace eval formula [list set $ind $indtext] } } set thisline {} set err {} foreach fmt $args { if {[catch {namespace eval formula [list expr $fmt]} datum]} { set err $datum break } lappend thisline $datum } namespace delete formula if {$err != {}} { lappend parseerrors "Line $lno: $err" incr skip } else { lappend result $thisline incr 0 incr ndata } } variable parseinfo [list $ndata $ncomments $nblanks $skip] # return as a list of lists return $result } proc transformdata_using {data using} { # read file the same way as gnuplot does lassign [split $using :] xformat yformat if {[string is integer -strict $xformat] && $xformat >=0} { set xformat "\$$xformat" } if {[string is integer -strict $yformat] && $yformat >=0} { set yformat "\$$yformat" } set fd [open $data r] set fdata [read $fd] close $fd concat {*}[parsedata_using $fdata $xformat $yformat] } ############## Functions for intervals ################## proc calcdatarange {data} { # compute min/max and corresponding log min/max # for dataset # unfortunately, four cases for log on/off must be considered # indexes into list are logx & logy set xmin {{+Inf +Inf} {+Inf +Inf}} set xmax {{-Inf -Inf} {-Inf -Inf}} set ymin {{+Inf +Inf} {+Inf +Inf}} set ymax {{-Inf -Inf} {-Inf -Inf}} foreach {x y} $data { set xfin [list [expr {isfinite($x)}] [expr {islogfinite($x)}]] set yfin [list [expr {isfinite($y)}] [expr {islogfinite($y)}]] foreach logx {0 1} { foreach logy {0 1} { if {[lindex $xfin $logx] && [lindex $yfin $logy]} { if {$x<[lindex $xmin $logx $logy]} { lset xmin $logx $logy $x} if {$x>[lindex $xmax $logx $logy]} { lset xmax $logx $logy $x} if {$y<[lindex $ymin $logx $logy]} { lset ymin $logx $logy $y} if {$y>[lindex $ymax $logx $logy]} { lset ymax $logx $logy $y} } } } } dict create xmin $xmin ymin $ymin xmax $xmax ymax $ymax } proc combine_range {range1 range2} { if {$range1 == {}} { return $range2 } if {$range2 == {}} { return $range1 } set result {} foreach key {xmin ymin} { set l1 [dict get $range1 $key] set l2 [dict get $range2 $key] foreach logx {0 1} lx1 $l1 lx2 $l2 { foreach logy {0 1} v1 $lx1 v2 $lx2 { lset l1 $logx $logy [expr {min($v1, $v2)}] } } dict set result $key $l1 } foreach key {xmax ymax} { set l1 [dict get $range1 $key] set l2 [dict get $range2 $key] foreach logx {0 1} lx1 $l1 lx2 $l2 { foreach logy {0 1} v1 $lx1 v2 $lx2 { lset l1 $logx $logy [expr {max($v1, $v2)}] } } dict set result $key $l1 } return $result } proc compute_rangetransform {r1min r1max r2min r2max} { set mul [expr {($r2max - $r2min)/($r1max -$r1min)}] set add [expr {$r2min-$r1min*$mul}] list $mul $add } ############ Function for automatic axis scaling ########## proc compute_ticlist {min max tics log widen formatcmd} { # automatically compute sensible values # for the tics position, if not requested otherwise lassign $tics ticrequest spec switch $ticrequest { off { return [list {} $min $max] } list { # take the tics as they are # list must be text, number,... # don't widen return [list $spec $min $max] } every { # put a tic mark at integer multiples of spec set ticbase $spec } auto { # automatic placement. In log case, # put a mark at every power of ten # and subdivide for small span if {$log} { set decades [expr {log10($max)-log10($min)}] if {$decades<=2} { set minor {1 2 3 4 5} } elseif {$decades<=3} { set minor {1 2 5} } elseif {$decades<=5} { set minor {1 5} } else { set minor {1} } set expmin [expr {int(floor(log10($min)))}] set expmax [expr {int(floor(log10($max)))}] # the range is between 10^expmin and 10^(expmax+1) # if widening downwards, look for the largest # tic that is smaller or equal to the required minimum if {[dict get $widen min]} { foreach mantisse $minor { set tic [expr {$mantisse*10.0**$expmin}] if {$tic <= $min} { set wmin $tic } } set min $wmin } set ticlist {} for {set exp $expmin} {$exp <= $expmax} {incr exp} { set base [expr {10.0**$exp}] foreach mantisse $minor { set tic [expr {$mantisse*$base}] if {$tic >= $min && $tic <=$max} { lappend ticlist [{*}$formatcmd $tic] $tic } } } # if widening upwards, look for a tic >= the requested max # unles it has been reached before if {[dict get $widen max] && [lindex $ticlist end]<$max} { lappend minor 10 foreach mantisse $minor { set tic [expr {$mantisse*10.0**$expmax}] if {$tic >= $max} { set max $tic lappend ticlist [{*}$formatcmd $tic] $tic break } } } return [list $ticlist $min $max] } else { # automatic placement. In linear case, # compute value as a multiple # of 1, 2 or 5 times a power of ten set exp [expr {log10(abs($max - $min))}] set base [expr {pow(10, floor($exp)-1)}] set xfrac [expr {fmod($exp, 1.0)}] if {$xfrac < 0 } {set xfrac [expr {$xfrac+1.0}]} # Exponent und Bruchteil des Zehnerlogarithmus set xb 10 if {$xfrac <= 0.70} { set xb 5} if {$xfrac <= 0.31} { set xb 2} set ticbase [expr {$xb*$base}] } } default { error "Unknown tic mode $ticrequest" } } # if we are here, place marks at regular intervals # at integer multiples of ticbase # if we should widen, update min & max if {[dict get $widen min] && !$log} { set start [expr {int(floor(double($min)/double($ticbase)))}] set min [expr {$ticbase*$start}] } else { set start [expr {int(ceil(double($min)/double($ticbase)))}] } if {[dict get $widen max]} { set stop [expr {int(ceil(double($max)/double($ticbase)))}] set max [expr {$ticbase*$stop}] } else { set stop [expr {int(floor(double($max)/double($ticbase)))}] } set ticlist {} for {set i $start} {$i<=$stop} {incr i} { set v [expr {$i*$ticbase}] # if {$log && $v<=0} { continue } lappend ticlist [{*}$formatcmd $v] $v } return [list $ticlist $min $max] } ######### Functions for parsing gnuplot style commands ########### proc initparsearg {} { # only checks whether args is a valid dictionary upvar 1 args procargs if {[catch {dict size $procargs}]} { return -code error -level 2 "Malformed argument list: $procargs" } } proc parsearg {option default} { # read argument from args, set to default # if unset in args. option can have alternative # names. Return true if the option was set # from the arguments, false if the default was substituted upvar 1 args procargs set optname [lindex $option 0] upvar 1 $optname resvar set success false foreach name $option { if {[dict exists $procargs $name]} { set resvar [dict get $procargs $name] dict unset procargs $name set success true } } if {!$success} { set resvar $default } return $success } ########### Functions for drawing marks on a canvas ############## proc shape-circles {can coord color size tag} { set r [expr {5.0*$size}] set ids {} foreach {x y} $coord { lappend ids [$can create oval \ [expr {$x-$r}] [expr {$y-$r}] \ [expr {$x+$r}] [expr {$y+$r}] \ -outline $color -fill "" -tag $tag] } return $ids } proc shape-filled-circles {can coord color size tag} { set r [expr {5.0*$size}] set ids {} foreach {x y} $coord { lappend ids [$can create oval \ [expr {$x-$r}] [expr {$y-$r}] \ [expr {$x+$r}] [expr {$y+$r}] \ -outline "" -fill $color -tag $tag] } return $ids } proc shape-squares {can coord color size tag} { set s [expr {5.0*$size}] set ids {} foreach {x y} $coord { lappend ids [$can create rectangle \ [expr {$x-$s}] [expr {$y-$s}] [expr {$x+$s}] [expr {$y+$s}] \ -outline $color -fill "" -tag $tag] } return $ids } proc shape-filled-squares {can coord color size tag} { set s [expr {5.0*$size}] set ids {} foreach {x y} $coord { lappend ids [$can create rectangle \ [expr {$x-$s}] [expr {$y-$s}] [expr {$x+$s}] [expr {$y+$s}] \ -outline "" -fill $color -tag $tag] } return $ids } proc shape-hexagons {can coord color size tag} { set s [expr {5.0*$size}] set clist {1 -0.5 0 -1.12 -1 -0.5 -1 0.5 0 1.12 1 0.5} set ids {} foreach {x y} $coord { set hc {} foreach {xc yc} $clist { lappend hc [expr {$xc*$s+$x}] lappend hc [expr {$yc*$s+$y}] } lappend ids [$can create polygon $hc \ -outline $color -fill "" -tag $tag] } return $ids } proc shape-filled-hexagons {can coord color size tag} { set s [expr {5.0*$size}] set clist {1 -0.5 0 -1.12 -1 -0.5 -1 0.5 0 1.12 1 0.5} set ids {} foreach {x y} $coord { set hc {} foreach {xc yc} $clist { lappend hc [expr {$xc*$s+$x}] lappend hc [expr {$yc*$s+$y}] } lappend ids [$can create polygon $hc \ -outline "" -fill $color -tag $tag] } return $ids } proc shape-triangles {can coord color size tag} { set s [expr {8.0*$size}] set clist {0.0 +1.0 0.5 -0.5 -0.5 -0.5} set ids {} foreach {x y} $coord { set hc {} foreach {xc yc} $clist { lappend hc [expr {$xc*$s+$x}] lappend hc [expr {$yc*$s+$y}] } lappend ids [$can create polygon $hc \ -outline $color -fill "" -tag $tag] } return $ids } proc shape-filled-triangles {can coord color size tag} { set s [expr {8.0*$size}] set clist {0.0 +1.0 0.5 -0.5 -0.5 -0.5} set ids {} foreach {x y} $coord { set hc {} foreach {xc yc} $clist { lappend hc [expr {$xc*$s+$x}] lappend hc [expr {$yc*$s+$y}] } lappend ids [$can create polygon $hc \ -outline "" -fill $color -tag $tag] } return $ids } proc shape-uptriangles {can coord color size tag} { set s [expr {8.0*$size}] set clist {0.0 -1.0 0.5 0.5 -0.5 0.5} set ids {} foreach {x y} $coord { set hc {} foreach {xc yc} $clist { lappend hc [expr {$xc*$s+$x}] lappend hc [expr {$yc*$s+$y}] } lappend ids [$can create polygon $hc \ -outline $color -fill "" -tag $tag] } return $ids } proc shape-filled-uptriangles {can coord color size tag} { set s [expr {8.0*$size}] set clist {0.0 -1.0 0.5 0.5 -0.5 0.5} set ids {} foreach {x y} $coord { set hc {} foreach {xc yc} $clist { lappend hc [expr {$xc*$s+$x}] lappend hc [expr {$yc*$s+$y}] } lappend ids [$can create polygon $hc \ -outline "" -fill $color -tag $tag] } return $ids } snit::widgetadaptor graph { delegate option -width to hull delegate option -height to hull delegate option -background to hull option -xrange -default {* *} -configuremethod rangeset option -yrange -default {* *} -configuremethod rangeset option -logx -default 0 -configuremethod opset option -logy -default 0 -configuremethod opset option -grid -default false -configuremethod opset option -xtics -default auto -configuremethod opset option -ytics -default auto -configuremethod opset option -xlabel -default {} -configuremethod opset option -ylabel -default {} -configuremethod opset option -xformat -default %g -configuremethod opset option -yformat -default %g -configuremethod opset option -font -default {} -configuremethod fontset option -ticlength -default 5 option -samplelength -default 20 option -samplesize -default 1.0 option -key -default {vertical top horizontal right disabled false} option -keyspacing -default 1.0 option -enhanced -default false -configuremethod unimplemented option -redraw -default 0 -readonly yes # backing store for plot data variable plotdata {} variable datasetnr 0 variable zstack {} # computed list of tics and displayrange variable xticlist variable yticlist variable displayrange variable displaysize # store the history of ranges # by zooming with the mouse variable zoomstack {} # state during mouse action (dragging or clicking) variable dragdata {dragging false clicking false} # identity transform variable transform {1.0 0.0 1.0 0.0} variable axisfont default # store for the interactive elements (=controls) variable controls {} constructor {args} { installhull using canvas $self configurelist $args bind $win <Configure> [mymethod RedrawRequest] # bindings for dragging & clicking bind $win <ButtonPress-1> [mymethod drag start %x %y] bind $win <Button1-Motion> [mymethod drag move %x %y] bind $win <ButtonRelease-1> [mymethod drag end %x %y] bind $win <ButtonRelease-2> [mymethod zoomout] bind $win <ButtonRelease-3> [mymethod zoomout] bind $win <Motion> [mymethod motionevent %x %y] } destructor { foreach c $controls { # release all embedded controls catch {$c Parent {} {}} } } method unimplemented {op value} { return -code error "Option $op not implemented" } method opset {op value} { set options($op) $value $self RedrawRequest } method RedrawRequest {} { defer [mymethod Redraw] return {} } #################### gnuplot style interface functions########### method plot {data args} { # main plot command # simulate gnuplot # syntax plot <data> # ?using usingspec? # ?with lines/points/linespoints? # ?color colorspec? # ?pointtype ...? # ?pointsize ...? # ?linewidth ...? # ?dash ...? # ?title ...? initparsearg parsearg {using u} {} parsearg {with w} points parsearg {color lc} auto parsearg {pointtype pt} circles parsearg {pointsize ps} 1.0 parsearg {linewidth lw} 1.0 parsearg {dash} "" parsearg {title t} "" #puts "Plot config: $using $with $color $pointtype $pointsize $linewidth" if {$using != {}} { set data [transformdata_using $data $using] } if {$color == "auto"} { set colors {red green blue black} set ncolors [llength $color] set color [lindex $colors [expr {$datasetnr%$ncolors}]] } set datarange [calcdatarange $data] set plotwith {} set id $datasetnr switch $with { p - points { dict set plotwith points 1 } l - lines { dict set plotwith lines 1 } lp - linespoints { dict set plotwith points 1 dict set plotwith lines 1 } default { return -code error "with must be: points, lines or linespoints" } } if {[dict exists $plotwith points]} { # check that pointtype exists lassign [info commands shape-$pointtype] ptproc if {$ptproc ne "shape-$pointtype"} { return -code error "Unknown pointtype $pointtype" } } dict set plotdata $id type $plotwith dict set plotdata $id data $data dict set plotdata $id datarange $datarange dict set plotdata $id color $color dict set plotdata $id pointtype $pointtype dict set plotdata $id pointsize $pointsize dict set plotdata $id title $title # dict set plotdata $id linewidth $linewidth dict set plotdata $id dash $dash lappend zstack $id $self RedrawRequest incr datasetnr return $id } method update {id args} { # same as plot, but change existing dataset if {![dict exists $plotdata $id]} { return -code error "No such dataset: $id" } initparsearg if {[parsearg data NONE]} { # new data was supplied. Only then check for transformation if {[parsearg {using u} {}]} { set data [transformdata_using $data $using] } set datarange [calcdatarange $data] dict set plotdata $id data $data dict set plotdata $id datarange $datarange } if {[parsearg {with w} {}]} { switch $with { p - points { dict set plotdata $id type points 1 dict unset plotdata $id type lines } l - lines { dict set plotdata $id type lines 1 dict unset plotdata $id type points } lp - linespoints { dict set plotdata $id type points 1 dict set plotdata $id type lines 1 } default { return -code error "with must be: points, lines or linespoints" } } } parsearg {color lc} auto if {$color != "auto"} { dict set plotdata $id color $color } if {[parsearg {pointtype pt} {}]} { dict set plotdata $id pointtype $pointtype } if {[parsearg {pointsize ps} {}]} { dict set plotdata $id pointsize $pointsize } if {[parsearg {linewidth lw} {}]} { dict set plotdata $id linewidth $linewidth } if {[parsearg {dash} {}]} { dict set plotdata $id dash $dash } if {[parsearg {title t} {}]} { dict set plotdata $id title $title } $self RedrawRequest } method remove {id} { set oldzstacklen [llength $zstack] dict unset plotdata $id set zstack [lremove $zstack $id] if {$oldzstacklen != [llength $zstack]} { # redraw only if we actually deleted something $self RedrawRequest } } method {set log} {{what xy} {how on}} { # cast boolean how into canonical form 0,1 if {![string is boolean -strict $how]} { return -code error "Expected boolean value instead of $how" } if {$how} { set how 1 } else { set how 0 } switch $what { x { $self configure -logx $how } y { $self configure -logy $how } xy { $self configure -logx $how $self configure -logy $how } default { return -code error "Unknown axis for log setting $what" } } } method {unset log} {{what {}}} { $self set log $what off } # helper function to parse gnuplot-style ranges proc rangeparse {arglist} { if {[llength $arglist]==1} { # single string in gnuplot form - decompose at : # after removal of [] (potentially) set rangestring [lindex $arglist 0] ;# unpack if {[string trim $rangestring]=="auto"} { return [list * *] } set arglist [split [string trim $rangestring {[]} ] :] } if {[llength $arglist]==2} { # argument is a Tcl list min max lassign $arglist min max set min [string trim $min] set max [string trim $max] if {$min == ""} { set min * } if {$max == ""} { set max * } if {(!isfinite($min) && $min!="*") || (!isfinite($max) && $max !="*")} { return -code error -level 2 "Range limits must be floats or *; got $min:$max" } } else { return -code error -level 2 "Range must consist of two limits min:max" } list $min $max } method {set xrange} {args} { set options(-xrange) [rangeparse $args] $self RedrawRequest } method {set yrange} {args} { set options(-yrange) [rangeparse $args] $self RedrawRequest } method {set auto x} {} { $self set xrange *:* } method {set auto y} {} { $self set yrange *:* } method {set grid} {{how on}} { if {$how} { set options(-grid) on } else { set options(-grid) off } $self RedrawRequest } method {unset grid} {} { $self set grid off } proc parsetics {arglist} { if {[llength $arglist]==1} { # unpack set val [lindex $arglist 0] set sval [string trim $val] # either auto or double value if {$sval=="auto" || $sval == "off"} { return $sval } else { # try to parse as float if {isfinite($val) && $val > 0} { return [list every $val] } else { return -code error -level 2 "Tics must be float or \"auto\" or \"off\"" } } } if {[llength $arglist]%2==1} { return -code error -level 2 "Tic list must be label pos ?label pos ...?" } # check for float value at every odd pos foreach {text pos} $arglist { if {!isfinite($pos)} { return -code error -level 2 "All tics must be at finite position: \"$text\", $pos" } } list list $arglist } method {set xtics} {args} { set options(-xtics) [parsetics $args] $self RedrawRequest } method {set ytics} {args} { set options(-ytics) [parsetics $args] $self RedrawRequest } method {set xlabel} {text} { set options(-xlabel) $text $self RedrawRequest } method {set ylabel} {text} { set options(-ylabel) $text $self RedrawRequest } method {set format} {axis args} { switch $axis { x { upvar 0 options(-xformat) fmtvar } y { upvar 0 options(-yformat) fmtvar } default { return -code error "Unknown axis $axis" } } switch [llength $args] { 0 { # restore default set fmt %g } 1 { # one argument = "format" formatstring set fmt [list numeric {*}$args] } 2 { # two arguments = swap order for formatcmd lassign $args fmtstring type if {$type ni {command timedate numeric}} { return -code error "Unknown formatting procedure $type" } set fmt [list $type $fmtstring] } default { return -code error "Wrong # arguments ($args given): $self set format <axis> ?fmt? ?type?" } } set fmtvar $fmt $self RedrawRequest } method {set key} {args} { # no argument - just enable legend if {[llength $args]==0} { dict set options(-key) disabled false $self RedrawRequest return } # otherwise process args foreach arg $args { switch $arg { top - bottom { dict set options(-key) vertical $arg } right - left { dict set options(-key) horizontal $arg } on { dict set options(-key) disabled false } off { dict set options(-key) disabled true } default { return -code error "Unknown option for set key: $arg" } } } $self RedrawRequest } method getdata {id args} { if {[dict exists $plotdata $id]} { return [dict get $plotdata $id {*}$args] } } method getdatasetids {} { dict keys $plotdata } method calcranges {} { # compute ranges spanned by data set datarange {} dict for {id data} $plotdata { set datarange [combine_range $datarange [dict get $data datarange]] } set dxmin [lindex [dict get $datarange xmin] $options(-logx) $options(-logy)] set dxmax [lindex [dict get $datarange xmax] $options(-logx) $options(-logy)] set dymin [lindex [dict get $datarange ymin] $options(-logx) $options(-logy)] set dymax [lindex [dict get $datarange ymax] $options(-logx) $options(-logy)] # now compute range from request & data set xwiden {min false max false} set ywiden {min false max false} lassign $options(-xrange) xmin xmax lassign $options(-yrange) ymin ymax if {$xmin =="*" || ($options(-logx) && !islogfinite($xmin))} { set xmin $dxmin dict set xwiden min true } if {$ymin =="*" || ($options(-logy) && !islogfinite($ymin))} { set ymin $dymin dict set ywiden min true } if {$xmax =="*" || ($options(-logx) && !islogfinite($xmax))} { set xmax $dxmax dict set xwiden max true } if {$ymax =="*" || ($options(-logy) && !islogfinite($ymax))} { set ymax $dymax dict set ywiden max true } # now, we could still have an unusable range in case the data # doesn't provide us with a sensible range; then fake it if {$xmin > $xmax} { # not a single valid point lassign {1.0 2.0} xmin xmax } if {$xmin == $xmax} { # only one value set xm $xmin if {$xm != 0} { set xmin [expr {$xm*0.999}] set xmax [expr {$xm*1.001}] if {$xm < 0} { lassign [list $xmin $xmax] xmax xmin } } else { lassign {-0.001 0.001} xmin xmax } } if {$ymin > $ymax} { # not a single valid point lassign {1.0 2.0} ymin ymax } if {$ymin == $ymax} { # only one value set ym $ymin if {$ym != 0} { set ymin [expr {$ym*0.999}] set ymax [expr {$ym*1.001}] if {$ym < 0} { lassign [list $ymin $ymax] ymax ymin } } else { lassign {-0.001 0.001} ymin ymax } } # now we have the tight range in xmin,xmax, ymin, ymax # compute ticlists and round for data determined values lassign [compute_ticlist $xmin $xmax $options(-xtics) \ $options(-logx) $xwiden [formatcmd $options(-xformat)]] xticlist xmin xmax lassign [compute_ticlist $ymin $ymax $options(-ytics) \ $options(-logy) $ywiden [formatcmd $options(-yformat)]] yticlist ymin ymax set displayrange [dict create xmin $xmin xmax $xmax ymin $ymin ymax $ymax] } proc formatcmd {fmt} { # return a cmd prefix to convert # tic positions into strings if {[llength $fmt]<=1} { # single argument - use format return [list format {*}$fmt] } lassign $fmt type arg switch $type { command { return $arg } timedate { return [list apply {{fmt t} {clock format [expr {entier($t)}] -format $fmt}} $arg] } numeric { return [list format $arg] } default { return -code error "Wrong tic format option" } } error "Shit happens" } method calcsize {} { # compute size of the plot area in pixels # such that it fits with all labels etc. into the canvas set w [winfo width $win] set h [winfo height $win] # width of xtic labels to the left and right set xmaxwidth [font measure $axisfont [lindex $xticlist end-1]] set xminwidth [font measure $axisfont [lindex $xticlist 0]] # maximum width of the ytic labels set lwidth 0 foreach {text tic} $yticlist { set nw [font measure $axisfont $text] set lwidth [expr {max($lwidth, $nw)}] } set lascent [font metrics $axisfont -ascent] set ldescent [font metrics $axisfont -descent] set lineheight [font metrics $axisfont -linespace] set margin [expr {0.03*$w}] # set left margin to have room for ytic labels + ylabel set deskxmin [expr {($lwidth+$options(-ticlength))+$margin}] if { $options(-ylabel) != "" } { set ylabelx [expr {$margin/2}] set deskxmin [expr {$deskxmin + 1.2 * $lineheight}] } else { set ylabelx 0 } # if necessary, make space for first xtic set deskxmin [expr {max($deskxmin, 0.5*$xminwidth)}] set deskxmax [expr {$w-0.5*$xmaxwidth-$margin}] set deskymax [expr {max($options(-ticlength),$lascent)+$margin}] set deskymin [expr {($h-$options(-ticlength)-$lineheight-$ldescent)-$margin}] if { $options(-xlabel) != "" } { set xlabely [expr {$deskymin+$margin/2}] set deskymin [expr {$deskymin - 1.2 * $lineheight}] } else { set xlabely $deskymin } set displaysize [dict create xmin $deskxmin xmax $deskxmax ymin $deskymin ymax $deskymax \ margin $margin xlabely $xlabely ylabelx $ylabelx] } # compute the transform from graph coordinates # to pixels method calctransform {} { set xmin [dict get $displayrange xmin] set xmax [dict get $displayrange xmax] set ymin [dict get $displayrange ymin] set ymax [dict get $displayrange ymax] set dxmin [dict get $displaysize xmin] set dxmax [dict get $displaysize xmax] set dymin [dict get $displaysize ymin] set dymax [dict get $displaysize ymax] if {$options(-logx)} { set xmin [expr {log($xmin)}] set xmax [expr {log($xmax)}] } lassign [compute_rangetransform \ $xmin $xmax $dxmin $dxmax] xmul xadd if {$options(-logy)} { set ymin [expr {log($ymin)}] set ymax [expr {log($ymax)}] } lassign [compute_rangetransform \ $ymin $ymax $dymin $dymax] ymul yadd set transform [list $xmul $xadd $ymul $yadd] } method graph2pix {coords} { # transform a list of coordinates to pixels lassign $transform xmul xadd ymul yadd set result {} set logcode {} if {$options(-logx)} { append logcode x } if {$options(-logy)} { append logcode y } switch $logcode { {} { foreach {x y} $coords { lappend result [expr {$x*$xmul+$xadd}] [expr {$y*$ymul+$yadd}] } } x { foreach {x y} $coords { lappend result [expr {($x<=0)? -Inf*$xmul : log($x)*$xmul+$xadd}] \ [expr {$y*$ymul+$yadd}] } } y { foreach {x y} $coords { lappend result [expr {$x*$xmul+$xadd}] \ [expr {($y<=0)? -Inf*$ymul : log($y)*$ymul+$yadd}] } } xy { foreach {x y} $coords { lappend result [expr {($x<=0)? -Inf*$xmul : log($x)*$xmul+$xadd}] \ [expr {($y<=0)? -Inf*$ymul : log($y)*$ymul+$yadd}] } } } return $result } # convert a single value to/from graph coordinates method xToPix {x} { lassign $transform xmul xadd ymul yadd if {$options(-logx)} { expr {($x<=0)? -Inf*$xmul : log($x)*$xmul+$xadd} } else { expr {$x*$xmul+$xadd} } } method yToPix {y} { lassign $transform xmul xadd ymul yadd if {$options(-logy)} { expr {($y<=0) ? -Inf*$ymul : log($y)*$ymul+$yadd} } else { expr {$y*$ymul+$yadd} } } method pixToX {x} { lassign $transform xmul xadd ymul yadd if {$options(-logx)} { expr {exp(($x-$xadd)/$xmul)} } else { expr {($x-$xadd)/$xmul} } } method pixToY {y} { lassign $transform xmul xadd ymul yadd if {$options(-logy)} { expr {exp(($y-$yadd)/$ymul)} } else { expr {($y-$yadd)/$ymul} } } method drawdata {} { foreach id $zstack { # draw in correct order, dispatch between # lines and points if {[dict exists $plotdata $id type points]} { $self drawpoints $id } if {[dict exists $plotdata $id type lines]} { $self drawlines $id } } } method drawpoints {id} { set data [dict get $plotdata $id data] lassign [geometry::pointclip $data $displayrange] clipdata clipinfo # store away the clipped & transformed data # together with the info of the clipping # needed for picking points dict set plotdata $id clipinfo $clipinfo set transdata [$self graph2pix $clipdata] dict set plotdata $id transdata $transdata set shapeproc shape-[dict get $plotdata $id pointtype] $shapeproc $hull $transdata \ [dict get $plotdata $id color] \ [dict get $plotdata $id pointsize] \ $selfns } method drawlines {id} { set data [dict get $plotdata $id data] set color [dict get $plotdata $id color] set width [dict get $plotdata $id linewidth] set dash [dict get $plotdata $id dash] set piece {} set pieces {} foreach {x y} $data { if {isnan($x) || isnan($y)} { # NaN value, start a new piece if {[llength $piece]>0} { lappend pieces [$self graph2pix $piece] } set piece {} continue } lappend piece $x $y } lappend pieces [$self graph2pix $piece] set ids {} foreach piece $pieces { if {[llength $piece]>=4} { set clipped [geometry::polylineclip $piece $displaysize] foreach coord $clipped { if {[llength $coord]<4} { # error puts "Input coords: " puts "set piece [list $piece]" puts "ukaz::geometry::polylineclip {$piece} $displaysize" error "polyline did wrong, look in console" } lappend ids [$hull create line $coord \ -fill $color -width $width -tag $selfns -dash $dash] } } } return $ids } method drawlegend {} { # check if legend is enabled if {[dict get $options(-key) disabled]} { return } # draw the titles and a sample set lineheight [expr {[font metrics $axisfont -linespace]*$options(-keyspacing)}] # create list of all ids that have titles # in correct zstack order set titleids {} foreach id $zstack { if {[dict exists $plotdata $id title]} { set title [dict get $plotdata $id title] if {$title != ""} { lappend titleids $id } } } # compute size needed for legend set dxmin [dict get $displaysize xmin] set dymin [dict get $displaysize ymin] set dxmax [dict get $displaysize xmax] set dymax [dict get $displaysize ymax] set totalheight [expr {[llength $titleids]*$lineheight}] set yoffset $lineheight ;# one line distance from border set xoffset [expr {$options(-samplelength)/4}] ;# 1/4 length distance from border # y position of top sample if {[dict get $options(-key) vertical]=="top"} { set y0 [expr {$dymax+$yoffset}] } else { # bottom set y0 [expr {$dymin-$totalheight}] } # x coordinates of line, sample and text anchor if {[dict get $options(-key) horizontal]=="left"} { set x0 [expr {$dxmin+$xoffset}] set x1 [expr {$dxmin+$xoffset+$options(-samplelength)}] set sx [expr {($x0+$x1)/2}] set tx [expr {$x1+$xoffset}] set anchor w } else { # right set x0 [expr {$dxmax-$xoffset-$options(-samplelength)}] set x1 [expr {$dxmax-$xoffset}] set sx [expr {($x0+$x1)/2}] set tx [expr {$x0-$xoffset}] set anchor e } # draw ! set ycur $y0 foreach id $titleids { set title [dict get $plotdata $id title] if {[dict exists $plotdata $id type points]} { set shapeproc shape-[dict get $plotdata $id pointtype] $shapeproc $hull [list $sx $ycur] \ [dict get $plotdata $id color] \ [dict get $plotdata $id pointsize] \ $selfns } if {[dict exists $plotdata $id type lines]} { $hull create line [list $x0 $ycur $x1 $ycur] \ -fill [dict get $plotdata $id color] \ -width [dict get $plotdata $id linewidth] \ -dash [dict get $plotdata $id dash] -tag $selfns } $hull create text $tx $ycur \ -anchor $anchor \ -text $title -font $axisfont -tag $selfns # advance set ycur [expr {$ycur+$lineheight}] } } method drawcoordsys {} { set dxmin [dict get $displaysize xmin] set dymin [dict get $displaysize ymin] set dxmax [dict get $displaysize xmax] set dymax [dict get $displaysize ymax] # draw border $hull create rectangle $dxmin $dymin $dxmax $dymax -tag $selfns # draw xtics foreach {text xval} $xticlist { set deskx [$self xToPix $xval] if { $options(-grid) } { $hull create line $deskx $dymin $deskx $dymax -fill gray -tag $selfns } $hull create line $deskx $dymin $deskx [expr {$dymin+$options(-ticlength)}] -tag $selfns $hull create text $deskx [expr {$dymin+$options(-ticlength)}] \ -anchor n -justify center \ -text $text -font $axisfont -tag $selfns } # draw ytics foreach {text yval} $yticlist { set desky [$self yToPix $yval] if { $options(-grid) } { $hull create line $dxmin $desky $dxmax $desky -fill gray -tag $selfns } $hull create line $dxmin $desky [expr {$dxmin-$options(-ticlength)}] $desky -tag $selfns $hull create text [expr {$dxmin-$options(-ticlength)}] $desky \ -anchor e -justify right \ -text $text -font $axisfont -tag $selfns } # draw xlabel and ylabel if {$options(-xlabel) != {}} { set xcenter [expr {($dxmin + $dxmax) / 2}] set ypos [dict get $displaysize xlabely] $hull create text $xcenter $ypos -anchor n \ -text $options(-xlabel) -font $axisfont -tag $selfns } if {$options(-ylabel) != {}} { set ycenter [expr {($dymin + $dymax) / 2}] set xpos [dict get $displaysize ylabelx] $hull create text $xpos $ycenter -anchor n \ -angle 90 -text $options(-ylabel) -font $axisfont -tag $selfns } } method canv {args} { $hull {*}$args } method Redraw {} { $hull delete $selfns # puts "Now drawing [$self configure]" if {[dict size $plotdata] == 0} { return } $self calcranges $self calcsize $self calctransform $self drawcoordsys $self drawdata $self drawlegend incr options(-redraw) # notify embedded controls set errors {} foreach c $controls { if {[catch {$c Configure $displaysize} err]} { lappend errors $err } } if {[llength $errors] > 0} { # rethrow errors return -code error [join $errors \n] } } method clear {} { $hull delete $selfns set plotdata {} set zstack {} $self RedrawRequest } method fontset {option value} { # when -font is set, create the font # possibly delete the old one # let error propagate from here, before accepting the setting set newfont [font create {*}$value] if {$options(-font) != {}} { font delete $axisfont } set axisfont $newfont set options(-font) $value $self RedrawRequest } method rangeset {option value} { # configuremethod for -xrange, -yrange # first check validity lassign $value from to if {(isfinite($from) || $from == "*") && (isfinite($to) || $to == "*")} { set options($option) [list $from $to] set zoomstack {} $self RedrawRequest } else { return -code error "Range limits must be either a float or *" } } method zoomin {range} { # store current range in zoomstack lappend zoomstack [list $options(-xrange) $options(-yrange)] # apply zoom range lassign $range xmin xmax ymin ymax set options(-xrange) [list $xmin $xmax] set options(-yrange) [list $ymin $ymax] $self RedrawRequest event generate $win <<Zoom>> -data $range } method zoomout {} { if {[llength $zoomstack] > 0} { set range [lindex $zoomstack end] set zoomstack [lrange $zoomstack 0 end-1] lassign $range options(-xrange) options(-yrange) $self RedrawRequest event generate $win <<Zoom>> -data [concat $range] } } #### Methods for dragging rectangles (for zooming) ##### method {drag start} {x y} { # try to see if this is our object set cid [$hull find withtag current] # only for empty result or an object tagged with # $selfns, start action if {$cid == {} || $selfns in [$hull gettags $cid]} { dict set dragdata clicking true dict set dragdata dragging false dict set dragdata x0 $x dict set dragdata y0 $y } } method {drag move} {x y} { if {[dict get $dragdata dragging]} { $hull coords dragrect \ [dict get $dragdata x0] [dict get $dragdata y0] $x $y } if {[dict get $dragdata clicking]} { # user has moved mouse - it's dragging now # not clicking $hull create rectangle $x $y $x $y -fill "" -outline red -tag dragrect dict set dragdata clicking false dict set dragdata dragging true } # if nothing is set in dragdata, it's not our business } method {drag end} {x y} { # user has released mouse button # check if it was a click or a drag (zoom) if {[dict get $dragdata clicking]} { # inverse transform this point set xgraph [$self pixToX $x] set ygraph [$self pixToY $y] event generate $win <<Click>> -x $x -y $y -data [list $xgraph $ygraph] } if {[dict get $dragdata dragging]} { # inverse transform both coordinates set x1 [$self pixToX $x] set y1 [$self pixToY $y] set x0 [$self pixToX [dict get $dragdata x0]] set y0 [$self pixToY [dict get $dragdata y0]] # remove drag rectangle $hull delete dragrect # check for correct ordering if {$x1 < $x0} { lassign [list $x0 $x1] x1 x0 } if {$y1 < $y0} { lassign [list $y0 $y1] y1 y0 } if {$x0 != $x1 && $y0 != $y1} { # zoom in! $self zoomin [list $x0 $x1 $y0 $y1] } } dict set dragdata dragging false dict set dragdata clicking false } method motionevent {x y} { # inverse transform this point set xgraph [$self pixToX $x] set ygraph [$self pixToY $y] event generate $win <<MotionEvent>> -x $x -y $y -data [list $xgraph $ygraph] } method pickpoint {x y {maxdist 5}} { # identify point in dataset given by *screen coordinates* x,y # maximum distance from center maxdist # return the topmost datapoint nearer than maxdist set maxdistsq [expr {$maxdist**2}] foreach id [lreverse $zstack] { if {[dict exists $plotdata $id transdata]} { # the transformed data is only available after drawing set transdata [dict get $plotdata $id transdata] set clipinfo [dict get $plotdata $id clipinfo] set mindistsq Inf set nr 0 foreach {xp yp} $transdata { set distsq [expr {($xp-$x)**2+($yp-$y)**2}] if {$distsq<$mindistsq} { set mindistsq $distsq set minnr $nr } incr nr } if {$mindistsq < $maxdistsq} { # now compute the real datapointnr after clipping set dpnr $minnr foreach {clipnr cliplength} $clipinfo { if {$clipnr <= $minnr} { incr dpnr $cliplength } } # get the original data set data [dict get $plotdata $id data] set xd [lindex $data [expr {$dpnr*2}]] set yd [lindex $data [expr {$dpnr*2+1}]] # short circuiting ensures the 1st, topmost point is returned return [list $id $dpnr $xd $yd] } } } return {} } method saveAsPDF {fn} { package require pdf4tcl set size [list [winfo width $win] [winfo height $win]] set pdf [pdf4tcl::new %AUTO% -paper $size -compress false] $pdf canvas $hull $pdf write -file $fn $pdf destroy } method addcontrol {c} { # add the control object c to the list of managed # objects. First send a Parent command to notify it $c Parent $self $hull lappend controls $c # get notification if this thing is deleted trace add command $c delete [mymethod controldeleted $c] # during Redraw it'll get a Configure request $self RedrawRequest } method removecontrol {c} { if {[lsearch -exact $controls $c]>=0} { set controls [lremove $controls $c] # remove the delete trace trace remove command $c delete [mymethod controldeleted $c] $c Parent {} {} } } method controldeleted {c args} { # the control was deleted from outside. Remove # from our list without executing anything if {[lsearch -exact $controls $c]>=0} { set controls [lremove $controls $c] } } } snit::type dragline { variable dragging option -command -default {} option -orient -default horizontal -configuremethod SetOrientation option -variable -default {} -configuremethod SetVariable option -color -default {gray} variable pos variable canv {} variable graph {} variable xmin variable xmax variable ymin variable ymax variable loopescape false variable commandescape false constructor {args} { $self configurelist $args } destructor { $self untrace if { [info commands $canv] != {} } { $canv delete $selfns } } method Parent {parent canvas} { if {$parent != {}} { # this control is now managed if {$graph != {}} { return -code error "$self: Already managed by $graph" } if {[info commands $canvas] == {}} { return -code error "$self: No drawing canvas: $canv" } set graph $parent set canv $canvas $canv create line {-1 -1 -1 -1} -fill $options(-color) -dash . -tag $selfns # Bindings for dragging $canv bind $selfns <ButtonPress-1> [mymethod dragstart %x %y] $canv bind $selfns <Motion> [mymethod dragmove %x %y] $canv bind $selfns <ButtonRelease-1> [mymethod dragend %x %y] # Bindings for hovering - change cursor $canv bind $selfns <Enter> [mymethod dragenter] $canv bind $selfns <Leave> [mymethod dragleave] set dragging 0 } else { # this control was unmanaged. Remove our line if {$canv != {} && [info commands $canv] != {}} { $canv delete $selfns } set graph {} set canv {} } } method Configure {range} { # the plot range has changed set loopescape false set xmin [dict get $range xmin] set xmax [dict get $range xmax] set ymin [dict get $range ymin] set ymax [dict get $range ymax] set loopescape false set commandescape true $self SetValue } method SetVariable {option varname} { $self untrace if {$varname != {}} { upvar #0 $varname v trace add variable v write [mymethod SetValue] set options(-variable) $varname $self SetValue } } method untrace {} { if {$options(-variable)!={} } { upvar #0 $options(-variable) v trace remove variable v write [mymethod SetValue] set options(-variable) {} } } method SetValue {args} { if {$loopescape} { set loopescape false return } set loopescape true upvar #0 $options(-variable) v if {[info exists v]} { catch {$self gotoCoords $v $v} # ignore any errors if the graph is incomplete } } method DoTraces {} { if {$loopescape} { set loopescape false } else { if {$options(-variable)!={}} { set loopescape true upvar #0 $options(-variable) v set v $pos } } if {$options(-command)!={}} { if {$commandescape} { set commandescape false } else { uplevel #0 [list {*}$options(-command) $pos] } } } method SetOrientation {option value} { switch $value { vertical - horizontal { set options($option) $value } default { return -code error "Unknown orientation $value: must be vertical or horizontal" } } } method dragenter {} { if {!$dragging} { $canv configure -cursor hand2 } } method dragleave {} { if {!$dragging} { $canv configure -cursor {} } } method dragstart {x y} { set dragging 1 } method dragmove {x y} { if {$dragging} { $self GotoPixel $x $y } } method dragend {x y} { set dragging 0 } method GotoPixel {x y} { if {$graph=={}} { return } set rx [$graph pixToX $x] set ry [$graph pixToY $y] if {$options(-orient)=="horizontal"} { set pos $ry $canv coords $selfns [list $xmin $y $xmax $y] } else { set pos $rx $canv coords $selfns [list $x $ymin $x $ymax] } $self DoTraces } method gotoCoords {x y} { if {$graph=={}} { return } lassign [$graph graph2pix [list $x $y]] nx ny if {$options(-orient)=="horizontal"} { set pos $y $canv coords $selfns [list $xmin $ny $xmax $ny] } else { set pos $x $canv coords $selfns [list $nx $ymin $nx $ymax] } $canv raise $selfns $self DoTraces } } proc ::tcl::mathfunc::isfinite {x} { # determine, whether x,y is a valid point expr {[string is double -strict $x] && $x < Inf && $x > -Inf} } proc ::tcl::mathfunc::islogfinite {x} { # determine, whether x,y is a valid point on the logscale expr {[string is double -strict $x] && $x < Inf && $x > 0} } proc ::tcl::mathfunc::isnan {x} { expr {$x != $x} } } |