Skip to content

Commit c92b207

Browse files
committed
Add option for formatting tic labels with a callback and as time values
1 parent fbc40a1 commit c92b207

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

demo/axisformat.tcl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set basedir [file dirname [info script]]
2+
lappend auto_path [file dirname $basedir]
3+
4+
package require ukaz 2.0a3
5+
pack [ukaz::graph .g -width 500 -height 400] -expand yes -fill both
6+
set t1 [clock scan "00:04:05"]
7+
set t2 [clock scan "00:08:05"]
8+
set data [list $t1 4.5 $t2 8]
9+
.g plot $data
10+
11+
proc percent {x} {
12+
format %.0f%% [expr {100*$x}]
13+
}
14+
15+
.g set format x %H:%M:%S timedate
16+
.g set format y percent command
17+
18+
# place a tic at every minute
19+
.g set xtics 60
20+

pkgIndex.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
# script is sourced, the variable $dir must contain the
99
# full path name of this file's directory.
1010

11-
package ifneeded ukaz 2.0a2 [list source [file join $dir ukaz.tcl]]
11+
package ifneeded ukaz 2.0a3 [list source [file join $dir ukaz.tcl]]

ukaz.tcl

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package require snit
22
package require Tk 8.6
3-
package provide ukaz 2.0a2
3+
package provide ukaz 2.0a3
44

55
namespace eval ukaz {
66

@@ -415,7 +415,7 @@ namespace eval ukaz {
415415
}
416416

417417
############ Function for automatic axis scaling ##########
418-
proc compute_ticlist {min max tics log widen format} {
418+
proc compute_ticlist {min max tics log widen formatcmd} {
419419
# automatically compute sensible values
420420
# for the tics position, if not requested otherwise
421421
lassign $tics ticrequest spec
@@ -477,7 +477,7 @@ namespace eval ukaz {
477477
foreach mantisse $minor {
478478
set tic [expr {$mantisse*$base}]
479479
if {$tic >= $min && $tic <=$max} {
480-
lappend ticlist [format $format $tic] $tic
480+
lappend ticlist [{*}$formatcmd $tic] $tic
481481
}
482482
}
483483
}
@@ -490,7 +490,7 @@ namespace eval ukaz {
490490
set tic [expr {$mantisse*10.0**$expmax}]
491491
if {$tic >= $max} {
492492
set max $tic
493-
lappend ticlist [format $format $tic] $tic
493+
lappend ticlist [{*}$formatcmd $tic] $tic
494494
break
495495
}
496496
}
@@ -541,7 +541,7 @@ namespace eval ukaz {
541541
for {set i $start} {$i<=$stop} {incr i} {
542542
set v [expr {$i*$ticbase}]
543543
# if {$log && $v<=0} { continue }
544-
lappend ticlist [format $format $v] $v
544+
lappend ticlist [{*}$formatcmd $v] $v
545545
}
546546
return [list $ticlist $min $max]
547547
}
@@ -1121,6 +1121,37 @@ namespace eval ukaz {
11211121
$self RedrawRequest
11221122
}
11231123

1124+
method {set format} {axis args} {
1125+
switch $axis {
1126+
x { upvar 0 options(-xformat) fmtvar }
1127+
y { upvar 0 options(-yformat) fmtvar }
1128+
default { return -code error "Unknown axis $axis" }
1129+
}
1130+
switch [llength $args] {
1131+
0 {
1132+
# restore default
1133+
set fmt %g
1134+
}
1135+
1 {
1136+
# one argument = "format" formatstring
1137+
set fmt [list numeric {*}$args]
1138+
}
1139+
2 {
1140+
# two arguments = swap order for formatcmd
1141+
lassign $args fmtstring type
1142+
if {$type ni {command timedate numeric}} {
1143+
return -code error "Unknown formatting procedure $type"
1144+
}
1145+
set fmt [list $type $fmtstring]
1146+
}
1147+
default {
1148+
return -code error "Wrong # arguments ($args given): $self set format <axis> ?fmt? ?type?"
1149+
}
1150+
}
1151+
set fmtvar $fmt
1152+
$self RedrawRequest
1153+
}
1154+
11241155
method {set key} {args} {
11251156
# no argument - just enable legend
11261157
if {[llength $args]==0} {
@@ -1231,14 +1262,31 @@ namespace eval ukaz {
12311262
# now we have the tight range in xmin,xmax, ymin, ymax
12321263
# compute ticlists and round for data determined values
12331264
lassign [compute_ticlist $xmin $xmax $options(-xtics) \
1234-
$options(-logx) $xwiden $options(-xformat)] xticlist xmin xmax
1265+
$options(-logx) $xwiden [formatcmd $options(-xformat)]] xticlist xmin xmax
12351266

12361267
lassign [compute_ticlist $ymin $ymax $options(-ytics) \
1237-
$options(-logy) $ywiden $options(-yformat)] yticlist ymin ymax
1268+
$options(-logy) $ywiden [formatcmd $options(-yformat)]] yticlist ymin ymax
12381269

12391270
set displayrange [dict create xmin $xmin xmax $xmax ymin $ymin ymax $ymax]
12401271

12411272
}
1273+
1274+
proc formatcmd {fmt} {
1275+
# return a cmd prefix to convert
1276+
# tic positions into strings
1277+
if {[llength $fmt]<=1} {
1278+
# single argument - use format
1279+
return [list format {*}$fmt]
1280+
}
1281+
lassign $fmt type arg
1282+
switch $type {
1283+
command { return $arg }
1284+
timedate { return [list apply {{fmt t} {clock format [expr {entier($t)}] -format $fmt}} $arg] }
1285+
numeric { return [list format $arg] }
1286+
default { return -code error "Wrong tic format option" }
1287+
}
1288+
error "Shit happens"
1289+
}
12421290

12431291
method calcsize {} {
12441292
# compute size of the plot area in pixels

0 commit comments

Comments
 (0)