FE 'tkGooie' Utilities

'PLOTtools' group

PlotQuik
Pie Chart (2D)

(FE = Freedom Environment)

The FE 'PlotQuik' Pie Chart 'tkGooie'

FE Home Page > FE Downloads Page >

FE 'tkGooies' Description Page >

FE 'tkGooies' 'PLOTtools' Page >

This PlotQuik-Pie -hart tkGooie code Page

INTRODUCTION to 'PlotQuik_pie2D'

In 2012, I made a personal commitment to donate five 'PlotQuik' utilities (Tk scripts) to the Tcl-Tk wiki at wiki.tcl.tk.

Donating them requires 'extracting' the Tk scripts from the 'feHandyTools' subsystem of my free, open-source Freedom Environment software, that is available at www.freedomenv.com.

The Tk code in the 'feHandyTools' system is written to use Tk 'include' files of common code. The 'include' code is merged into various Tk scripts via Tk 'source' statements.

In order to have 'stand-alone' scripts that I can donate to wiki.tcl.tk, I need to merge some statements from those Tk-code 'include' files into the 'PlotQuik' Tk scripts.

I have done that for the 'pie chart' PlotQuik GUI and the code is presented below.

In the process of doing the merge and changing some comments in the code, I made a few improvements in the script (especially in regard to positioning the pie between a top title line and a 'legend' below the pie). So this donation is actually a little better than the 'PlotQuik' pie-chart-making Tk script in the current version of 'feHandyTools'.

I also added braces to some of the 'expr' statements to improve the execution speed by a few milliseconds.

However, it is hard to make a visually significant improvement in the execution speed, because the plot is performed in less than a second (after you enter/change the 3 user input fields --- title line, pie segment percents, pie segment legend-labels).

Note that this utility is 'Quik' in two ways:

  1. Easy for the user to enter the data for the plot.

  2. Once the data is entered, the plot is rendered in a fraction of a second.

Here is a reduced-size image of the pie chart GUI.


Click on this image to see the full-size image
of the GUI --- in a separate window or tab

This GUI is not meant to be a toy. A plot of budget data is used to show that this GUI is meant to be an actual 'productivity' tool --- a free and open-source one.

(When we are all working on 'retina display' monitors --- resolution of more than 2000x1500 for desktop computers, like WQXGA = 2560x1600 --- then the jaggies of the circle may be unnoticeable. The images above were captured on a 1024x768 monitor.)


DESCRIPTION OF THE CODE

Below is the code that produced this GUI.

There are comments at the top of the code, in a section titled 'OUTPUT:', that describe how one could capture and make use of images produced by this GUI.

I follow my usual 'canonical' structure for Tk code, for this Tk script:



  0) Set general window & widget parms (win-name, win-position,
     win-color-scheme, fonts, widget-geometry-parms, win-size-control).

  1a) Define ALL frames and sub-frames.
  1b) Pack   ALL frames and sub-frames.

  2) Define & pack all widgets in the frames.

  3) Define keyboard or mouse/touchpad/touch-sensitive-screen
     action BINDINGS, if needed.

  4) Define PROCS, if needed.

  5) Additional GUI initialization (typically with one or more of
     the procs), if needed.


This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations.

This makes it easy for me to find code sections --- while generating and testing Tk scripts, and when looking for code snippets to include in other Tk scripts (code re-use).


Experimenting with the GUI

As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus Tk scripts, so far), I provide the four main pack parameters --- '-side', '-anchor', '-fill', and '-expand' --- on all the 'pack' commands for the frames and widgets.

I think I have found a good setting of the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various widgets of this GUI. In particular ...

The 'canvas' widget expands/contracts appropriately when the window size is changed --- and button and label widgets stay fixed in size and relative-location as the window size is changed. Entry fields x-expand if the window x-expands.

Note that there are 'DwnCan' and 'UpCan' buttons on the GUI to help re-size the canvas appropriately within the current window size, whatever that may be.

If anyone wants to change the way the GUI configures itself as the main window size is changed, they can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various widgets --- to get the widget behavior that they want.

---

Additional experimentation: You could change the fonts used for the various GUI widgets and plot elements (title and legend). For example, you could change '-weight' from 'bold' to 'normal' --- or '-slant' from 'roman' to 'italic'. Or change font families.

In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing.

Furthermore, there are variables used to set geometry parameters of widgets --- parameters such as border-widths and padding. Feel free to experiment with those parameters as well.

---

Note that the 'BkgdColor' button calls on an RGB color-selector-GUI script to set the canvas background color.

You can make that color-selector script by cutting-and-pasting the code from the page offering 'a non-obfuscated color selector GUI', on this site.


Some features of the code

There are plenty of comments in the code to describe what most of the code-sections are doing.

See the 'PROCS' section of the code for a list of the procs that are used in this Tk script.

The main plotting code is in the proc 'update_plot'.

See the comments in that proc for details on how the plotting is implemented.


Comments in the Code

It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this.

Without the comments (especially in the 'update_plot' proc), the code might look too cryptic --- and potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch America's/Japan's/Germany's/Albania's Funniest Home Videos.


The Tcl-Tk script CODE

Here is a link to CODE for the script 'plot_quik_pie2D.tk'.


Other 'PlotQuik' utilities

This is the first of 5 'PlotQuik' utilities to be converted to stand-alone mode from their 'integrated' form in the 'feHandyTools' system of the Freedom Environment software.

Other Tk 'PlotQuik' scripts to follow are:

  1. bar chart, created from data entered on the GUI

  2. points and/or lines xy-plot, created from data entered on the GUI

  3. math expression xy-plot, created from a math expression entered on the GUI

  4. xy-plot, created by selecting 2 or 3 columns of data from a text file containing columns of data


2013jul23 UPDATE

I have replaced the code above with slightly updated code. The main changes follow.

1) I put most of the text strings for buttons and labels in an array, aRtext, near the top of the code. This should make internationalization of this script much easier.

2) I made some 'esoteric' changes to the 'resize_win', 'downsize_can', and 'upsize_can' procs --- in an attempt to get the 'DwnCan' button to work in the case that the user has maximized the GUI window VIA THE WINDOW MANAGER of their operating system. I was unsuccessful in this. Once the user maximizes the window, it seems there is no Tk command one can use to override the maximize request.

'wm geometry' was not up to the task of doing the override. It may be that most window managers will not honor requests from a program (instead of the user) that would effectively un-maximize a maximized window. The user can simply click on the 'Restore' window-manager button of the window. Then 'DwnCan' should work again. If the user maximizes the GUI window via the 'UpCan' button, 'DwnCan' works as intended.

3) I reduced the font of the Help message, on the left of the plot canvas, so that the text is unlikely to be clipped on netbook screens (600 pixels high).

4) I re-ordered the procs --- mainly to put the 'print_plot', 'print_preview, and 'get_image' procs at the bottom of the script. I put sample (untested but plausible) code for those procs in the script in case some users would like to be able to make a Postscript file from the plot image --- or in case some users would like to add an image to the plot (such as a logo).

5) I changed the names of the 2 procs that do the dragging of the main title, pie-section labels, and legend lines. The procs were 'plotDown' and 'plotMove'. They are now 'itemSelect' and 'itemMove' --- for a clearer indication of their function.

6) I added the suffix 'Px' on the end of some variable names --- so that it is clear (without scanning the code) that these variables are intended to be integer variables (denoting pixels), rather than floating-point variables (denoting 'world coordinates'). I also started all tag names with the capitalized string 'TAG', so that it is clear which names are tag-names.

---

If I have occasion to use this utility some day and find that it would be nice to implement the 'get_image' proc (mentioned above), then I may update this page with the tested 'get_image' code and with a 'GetImage'/'WriteGIF' button added to the GUI.

I may also implement a 'Help' button on the GUI to provide more extensive help --- for example, to explain that the 'DwnCan' button may not work if the user has maximized the GUI window via the user's window manager.

Bottom of this page for a
PlotQuik Pie Chart (2D) tool
--- a utility in the FE 'tkGooies' system,
in the 'PLOTtools' group.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Top of Page, above. >

Page history:

The code was created in 2012 --- and posted 2012 Oct 14 at http://wiki.tcl.tk/37141.

This FE web page was created 2014 May 11 --- as a backup and alternative to the wiki.tcl.tk page.

Page was changed 2015 Oct 05.
(Some minor reformatting of the heading.)

Page was changed 2019 Feb 23.
(Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)

Page was changed 2019 Jun 13.
(Specified image widths in percents to size the images according to width of the browser window.)


NOTE:
The code here MAY BECOME more 'up-to-date' than the code posted on the Tcler's Wiki ---
wiki.tcl-lang.org --- formerly wiki.tcl.tk.