chart.tcl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. ##########################################################################
  2. # chart.tcl - vector chart layer options file for GRASS GIS Manager
  3. # March 2006 Michael Barton, Arizona State University
  4. # COPYRIGHT: (C) 1999 - 2006 by the GRASS Development Team
  5. #
  6. # This program is free software under the GNU General Public
  7. # License (>=v2). Read the file COPYING that comes with GRASS
  8. # for details.
  9. #
  10. ##########################################################################
  11. namespace eval GmChart {
  12. variable array opt # chart options
  13. variable count 1
  14. variable array lfile # vector
  15. variable array lfilemask # vector
  16. variable optlist
  17. variable array dup # vector
  18. }
  19. ###############################################################################
  20. proc GmChart::create { tree parent } {
  21. variable opt
  22. variable count
  23. variable dup
  24. variable lfile
  25. variable lfilemask
  26. variable optlist
  27. global iconpath
  28. set node "chart:$count"
  29. set frm [ frame .charticon$count]
  30. set check [checkbutton $frm.check \
  31. -variable GmChart::opt($count,1,_check) \
  32. -height 1 -padx 0 -width 0]
  33. image create photo chartico -file "$iconpath/module-d.vect.chart.gif"
  34. set ico [label $frm.ico -image chartico -bd 1 -relief raised]
  35. bind $ico <ButtonPress-1> "GmTree::selectn $tree $node"
  36. pack $check $ico -side left
  37. #insert new layer
  38. if {[$tree selection get] != "" } {
  39. set sellayer [$tree index [$tree selection get]]
  40. } else {
  41. set sellayer "end"
  42. }
  43. $tree insert $sellayer $parent $node \
  44. -text "chart $count"\
  45. -window $frm \
  46. -drawcross auto
  47. set opt($count,1,_check) 1
  48. set dup($count) 0
  49. set opt($count,1,map) ""
  50. set opt($count,1,opacity) 1.0
  51. set opt($count,1,type_point) 1
  52. set opt($count,1,type_line) 1
  53. set opt($count,1,type_boundary) 1
  54. set opt($count,1,type_centroid) 1
  55. set opt($count,1,type_area) 0
  56. set opt($count,1,layer) 1
  57. set opt($count,1,ctype) "pie"
  58. set opt($count,1,columns) ""
  59. set opt($count,1,sizecol) ""
  60. set opt($count,1,csize) 40
  61. set opt($count,1,cscale) 1
  62. set opt($count,1,ocolor) "black"
  63. set opt($count,1,fcolors) ""
  64. set opt($count,1,mod) 1
  65. set optlist { _check map opacity layer ctype columns sizecol csize cscale ocolor fcolors \
  66. type_point type_line type_boundary type_centroid type_area}
  67. foreach key $optlist {
  68. set opt($count,0,$key) $opt($count,1,$key)
  69. }
  70. # create files in tmp diretory for layer output
  71. set mappid [pid]
  72. if {[catch {set lfile($count) [exec g.tempfile pid=$mappid]} error]} {
  73. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  74. }
  75. set lfilemask($count) $lfile($count)
  76. append lfile($count) ".ppm"
  77. append lfilemask($count) ".pgm"
  78. incr count
  79. return $node
  80. }
  81. ###############################################################################
  82. proc GmChart::set_option { node key value } {
  83. variable opt
  84. set id [GmTree::node_id $node]
  85. set opt($id,1,$key) $value
  86. }
  87. proc GmChart::select_map { id } {
  88. variable tree
  89. variable node
  90. set m [GSelect vector title [G_msg "Vector map for chart"] parent "."]
  91. if { $m != "" } {
  92. set GmChart::opt($id,1,map) $m
  93. GmTree::autonamel [format [G_msg "chart for %s"] $m ]
  94. }
  95. }
  96. ###############################################################################
  97. proc GmChart::show_columns { id } {
  98. variable opt
  99. global bgcolor
  100. set mapname $opt($id,1,map)
  101. set layernum $opt($id,1,layer)
  102. set cmd "v.info -c map=$mapname layer=$layernum"
  103. run_panel $cmd
  104. }
  105. proc GmChart::show_data { id } {
  106. variable opt
  107. global bgcolor
  108. set mapname $opt($id,1,map)
  109. set layer $opt($id,1,layer)
  110. if ![catch {open "|v.db.connect map=$mapname layer=$layer -g" r} vdb] {
  111. set vectdb [read $vdb]
  112. if {[catch {close $vdb} error]} {
  113. GmLib::errmsg $error
  114. }
  115. set vdblist [split $vectdb " "]
  116. set tbl [lindex $vdblist 1]
  117. set db [lindex $vdblist 3]
  118. set drv [lindex $vdblist 4]
  119. set cmd "db.select table=$tbl database=$db driver=$drv"
  120. run_panel $cmd
  121. }
  122. }
  123. ###############################################################################
  124. # chart options
  125. proc GmChart::options { id frm } {
  126. variable opt
  127. global bgcolor
  128. global iconpath
  129. # Panel heading
  130. set row [ frame $frm.heading1 ]
  131. Label $row.a -text [G_msg "Display pie and bar charts of attribute values at vector object locations"] \
  132. -fg MediumBlue
  133. pack $row.a -side left
  134. pack $row -side top -fill both -expand yes
  135. #opacity
  136. set row [ frame $frm.opc]
  137. Label $row.a -text [G_msg "Opaque "]
  138. scale $row.b -from 1.0 -to 0.0 -showvalue 1 \
  139. -orient horizontal -length 300 -resolution 0.01 -fg "#656565"\
  140. -variable GmChart::opt($id,1,opacity)
  141. Label $row.c -text [G_msg " Transparent"]
  142. pack $row.a $row.b $row.c -side left
  143. pack $row -side top -fill both -expand yes
  144. # vector name
  145. set row [ frame $frm.map ]
  146. Label $row.a -text [G_msg "Vector map to chart:"]
  147. Button $row.b -image [image create photo -file "$iconpath/element-vector.gif"] \
  148. -highlightthickness 0 -takefocus 0 -relief raised -borderwidth 1 \
  149. -helptext [G_msg "vector map to chart"] \
  150. -command "GmChart::select_map $id"
  151. Entry $row.c -width 30 -text " $opt($id,1,map)" \
  152. -textvariable GmChart::opt($id,1,map)
  153. Label $row.d -text " "
  154. Button $row.e -text [G_msg "Help"] \
  155. -image [image create photo -file "$iconpath/gui-help.gif"] \
  156. -command "spawn g.manual --q d.vect.chart" \
  157. -background $bgcolor \
  158. -helptext [G_msg "Help"]
  159. pack $row.a $row.b $row.c $row.d $row.e -side left
  160. pack $row -side top -fill both -expand yes
  161. # vector type
  162. set row [ frame $frm.type ]
  163. Label $row.a -text [G_msg "Vector type:"]
  164. checkbutton $row.b -text [G_msg "points"] -variable GmChart::opt($id,1,type_point)
  165. checkbutton $row.c -text [G_msg "lines"] -variable GmChart::opt($id,1,type_line)
  166. checkbutton $row.d -text [G_msg "boundaries"] -variable GmChart::opt($id,1,type_boundary)
  167. checkbutton $row.e -text [G_msg "centroids"] -variable GmChart::opt($id,1,type_centroid)
  168. checkbutton $row.f -text [G_msg "areas"] -variable GmChart::opt($id,1,type_area)
  169. pack $row.a $row.b $row.c $row.d $row.e $row.f -side left
  170. pack $row -side top -fill both -expand yes
  171. # attributes1 and data
  172. set row [ frame $frm.attr1 ]
  173. Label $row.a -text [G_msg "Attributes to chart: attribute layer"]
  174. LabelEntry $row.b -textvariable GmChart::opt($id,1,layer) -width 5
  175. pack $row.a $row.b -side left
  176. pack $row -side top -fill both -expand yes
  177. # show data and columns
  178. set row [ frame $frm.showcolumns ]
  179. Label $row.a -text [G_msg " show attribute columns"]
  180. Button $row.b -text [G_msg "columns"] \
  181. -image [image create photo -file "$iconpath/db-columns.gif"] \
  182. -command "GmChart::show_columns $id" \
  183. -background $bgcolor \
  184. -helptext [G_msg "Show columns"]
  185. Label $row.c -text [G_msg " show attribute data"]
  186. Button $row.d -text [G_msg "data"] \
  187. -image [image create photo -file "$iconpath/db-values.gif"] \
  188. -command "GmChart::show_data $id" \
  189. -background $bgcolor \
  190. -helptext [G_msg "Show data"]
  191. pack $row.a $row.b $row.c $row.d -side left
  192. pack $row -side top -fill both -expand yes
  193. # attributes2
  194. set row [ frame $frm.attr2 ]
  195. Label $row.a -text [G_msg "\tcolumns to chart (col1,col2,...) "]
  196. LabelEntry $row.b -textvariable GmChart::opt($id,1,columns) -width 30
  197. pack $row.a $row.b -side left
  198. pack $row -side top -fill both -expand yes
  199. # attributes3
  200. set row [ frame $frm.attr3 ]
  201. Label $row.a -text [G_msg "\tcolors for columns (clr1,clr2,...)"]
  202. LabelEntry $row.b -textvariable GmChart::opt($id,1,fcolors) -width 30 -padx 2
  203. pack $row.a $row.b -side left
  204. pack $row -side top -fill both -expand yes
  205. # attributes4
  206. set row [ frame $frm.attr4 ]
  207. Label $row.a -text [G_msg "\tcolumn for variable chart size"]
  208. LabelEntry $row.b -textvariable GmChart::opt($id,1,sizecol) -width 12 -padx 8
  209. Label $row.c -text [G_msg " scale factor"]
  210. LabelEntry $row.d -textvariable GmChart::opt($id,1,cscale) -width 4
  211. pack $row.a $row.b $row.c $row.d -side left
  212. pack $row -side top -fill both -expand yes
  213. # chart options1
  214. set row [ frame $frm.chopt1 ]
  215. Label $row.a -text [G_msg "Chart type:"]
  216. ComboBox $row.b -padx 2 -width 4 -textvariable GmChart::opt($id,1,ctype) \
  217. -values {"pie" "bar"}
  218. Label $row.c -text [G_msg "\tfixed chart size (if size column not used)"]
  219. LabelEntry $row.d -textvariable GmChart::opt($id,1,csize) -width 4
  220. pack $row.a $row.b $row.c $row.d -side left
  221. pack $row -side top -fill both -expand yes
  222. # chart options2
  223. set row [ frame $frm.chopt2 ]
  224. Label $row.a -text [G_msg "\tchart outline color:"]
  225. ComboBox $row.b -padx 0 -width 10 -textvariable GmChart::opt($id,1,ocolor) \
  226. -values {"none" "white" "grey" "gray" "black" "brown" "red" "orange" \
  227. "yellow" "green" "aqua" "cyan" "indigo" "blue" "purple" "violet" "magenta"}
  228. pack $row.a $row.b -side left
  229. pack $row -side top -fill both -expand yes
  230. }
  231. ###############################################################################
  232. proc GmChart::save { tree depth node } {
  233. variable opt
  234. variable optlist
  235. set id [GmTree::node_id $node]
  236. foreach key $optlist {
  237. GmTree::rc_write $depth "$key $opt($id,1,$key)"
  238. }
  239. }
  240. ###############################################################################
  241. # get selected vector map
  242. proc GmChart::mapname { node } {
  243. variable opt
  244. variable tree
  245. global mon
  246. set tree($mon) $GmTree::tree($mon)
  247. set id [GmTree::node_id $node]
  248. if { $opt($id,1,map) == "" } { return ""}
  249. set mapname $opt($id,1,map)
  250. return $mapname
  251. }
  252. ###############################################################################
  253. proc GmChart::display { node mod } {
  254. global mon
  255. variable optlist
  256. variable lfile
  257. variable lfilemask
  258. variable opt
  259. variable tree
  260. variable dup
  261. variable count
  262. set line ""
  263. set input ""
  264. set cmd ""
  265. set tree($mon) $GmTree::tree($mon)
  266. set id [GmTree::node_id $node]
  267. # If we are told dirty (for zoom) force dirty
  268. # Don't remove a dirty from a previous unrendered zoom
  269. if {$mod} {set opt($id,1,mod) 1}
  270. if { $opt($id,1,map) == "" } { return }
  271. if { $opt($id,1,columns) == "" } { return }
  272. if { !$opt($id,1,type_point) && !$opt($id,1,type_line) &&
  273. !$opt($id,1,type_boundary) && !$opt($id,1,type_centroid) &&
  274. !$opt($id,1,type_area) } { return }
  275. # combine vector types
  276. set type ""
  277. if { $opt($id,1,type_point) } { append type "point" }
  278. if { $opt($id,1,type_line) && "$type" != "" } {
  279. append type ",line"
  280. } elseif { $opt($id,1,type_line) && "$type" == "" } {
  281. append type "line"}
  282. if { $opt($id,1,type_boundary) && "$type" != "" } {
  283. append type ",boundary"
  284. } elseif { $opt($id,1,type_boundary) && "$type" == "" } {
  285. append type "boundary"}
  286. if { $opt($id,1,type_centroid) && "$type" != "" } {
  287. append type ",centroid"
  288. } elseif { $opt($id,1,type_centroid) && "$type" == "" } {
  289. append type "centroid"}
  290. if { $opt($id,1,type_area) && "$type" != "" } {
  291. append type ",area"
  292. } elseif { $opt($id,1,type_area) && "$type" == "" } {
  293. append type "area"}
  294. #create d.vect.chart command
  295. set cmd "d.vect.chart map=$opt($id,1,map) type=$type \
  296. layer=$opt($id,1,layer) columns=$opt($id,1,columns) \
  297. ctype=$opt($id,1,ctype) ocolor=$opt($id,1,ocolor) "
  298. # sizecol
  299. if { $opt($id,1,sizecol) != "" } {
  300. append cmd " sizecol=$opt($id,1,sizecol)"
  301. }
  302. # csize
  303. if { $opt($id,1,csize) != "" } {
  304. append cmd " size=$opt($id,1,csize)"
  305. }
  306. # cscale
  307. if { $opt($id,1,cscale) != "" } {
  308. append cmd " scale=$opt($id,1,cscale)"
  309. }
  310. # fcolors
  311. if { $opt($id,1,fcolors) != "" } {
  312. append cmd " colors=$opt($id,1,fcolors)"
  313. }
  314. # Decide whether to run, run command, and copy files to temp
  315. GmCommonLayer::display_command [namespace current] $id $cmd
  316. }
  317. ###############################################################################
  318. proc GmChart::duplicate { tree parent node id } {
  319. variable optlist
  320. variable lfile
  321. variable lfilemask
  322. variable opt
  323. variable count
  324. variable dup
  325. global iconpath
  326. set node "chart:$count"
  327. set dup($count) 1
  328. set frm [ frame .charticon$count]
  329. set check [checkbutton $frm.check \
  330. -variable GmChart::opt($count,1,_check) \
  331. -height 1 -padx 0 -width 0]
  332. image create photo chartico -file "$iconpath/module-d.vect.chart.gif"
  333. set ico [label $frm.ico -image chartico -bd 1 -relief raised]
  334. bind $ico <ButtonPress-1> "GmTree::selectn $tree $node"
  335. pack $check $ico -side left
  336. #insert new layer
  337. if {[$tree selection get] != "" } {
  338. set sellayer [$tree index [$tree selection get]]
  339. } else {
  340. set sellayer "end"
  341. }
  342. if { $opt($id,1,map) == ""} {
  343. $tree insert $sellayer $parent $node \
  344. -text "chart $count" \
  345. -window $frm \
  346. -drawcross auto
  347. } else {
  348. $tree insert $sellayer $parent $node \
  349. -text "chart for $opt($id,1,map)" \
  350. -window $frm \
  351. -drawcross auto
  352. }
  353. set opt($count,1,opacity) $opt($id,1,opacity)
  354. set optlist { _check map layer ctype columns sizecol csize cscale ocolor fcolors \
  355. type_point type_line type_boundary type_centroid type_area}
  356. foreach key $optlist {
  357. set opt($count,1,$key) $opt($id,1,$key)
  358. set opt($count,0,$key) $opt($count,1,$key)
  359. }
  360. set id $count
  361. # create files in tmp directory for layer output
  362. set mappid [pid]
  363. if {[catch {set lfile($count) [exec g.tempfile pid=$mappid]} error]} {
  364. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  365. }
  366. set lfilemask($count) $lfile($count)
  367. append lfile($count) ".ppm"
  368. append lfilemask($count) ".pgm"
  369. incr count
  370. return $node
  371. }