Table of Contents

Name

barchart - Bar chart for plotting X-Y coordinate data.

Synopsis

barchart pathName ?option value?...

Description

The barchart command creates a bar chart for plotting two-dimensional data (X-Y coordinates). A bar chart is a graphic means of comparing numbers by displaying bars of lengths proportional to the y-coordinates of the points they represented. The bar chart has many configurable components: coordinate axes, elements, legend, grid lines, cross hairs, etc. They allow you to customize the look and feel of the graph.

Introduction

The barchart command creates a new window for plotting two-dimensional data (X-Y coordinates), using bars of various lengths to represent the data points. The bars are drawn in a rectangular area displayed in the center of the new window. This is the plotting area. The coordinate axes are drawn in the margins surrounding the plotting area. By default, the legend is drawn in the right margin. The title is displayed in top margin.

A barchart widget has several configurable components: coordinate axes, data elements, legend, grid, cross hairs, pens, postscript, and annotation markers. Each component can be queried or modified.

axis

Up to four coordinate axes (two X-coordinate and two Y-coordinate axes) can be displayed, but you can create and use any number of axes. Axes control what region of data is displayed and how the data is scaled. Each axis consists of the axis line, title, major and minor ticks, and tick labels. Tick labels display the value at each major tick.

crosshairs
Cross hairs are used to position the mouse pointer relative to the X and Y coordinate axes. Two perpendicular lines, intersecting at the current location of the mouse, extend across the plotting area to the coordinate axes.
element
An element represents a set of data to be plotted. It contains an x and y vector of values representing the data points. Each data point is displayed as a bar where the length of the bar is proportional to the ordinate (Y-coordinate) of the data point. The appearance of the bar, such as its color, stipple, or relief is configurable.

A special case exists when two or more data points have the same abscissa (X-coordinate). By default, the bars are overlayed, one on top of the other. The bars are drawn in the order of the element display list. But you can also configure the bars to be displayed in two other ways. They may be displayed as a stack, where each bar (with the same abscissa) is stacked on the previous. Or they can be drawn side-by-side as thin bars. The width of each bar is a function of the number of data points with the same abscissa.

grid
Extends the major and minor ticks of the X-axis and/or Y-axis across the plotting area.
legend
The legend displays the name and symbol of each data element. The legend can be drawn in any margin or in the plotting area.
marker
Markers are used annotate or highlight areas of the graph. For example, you could use a text marker to label a particular data point. Markers come in various forms: text strings, bitmaps, connected line segments, images, polygons, or embedded widgets.
pen
Pens define attributes for elements. Data elements use pens to specify how they should be drawn. A data element may use many pens at once. Here the particular pen used for a data point is determined from each element's weight vector (see the element's -weight and -style options).
postscript
The widget can generate encapsulated PostScript output. This component has several options to configure how the PostScript is generated.

Syntax


barchart pathName ?option value?...

The barchart command creates a new window pathName and makes it into a barchart widget. At the time this command is invoked, there must not exist a window named pathName, but pathName's parent must exist. Additional options may be specified on the command line or in the option database to configure aspects of the graph such as its colors and font. See the configure operation below for the exact details about what option and value pairs are valid.

If successful, barchart returns the path name of the widget. It also creates a new Tcl command by the same name. You can use this command to invoke various operations that query or modify the graph. The general form is:

pathName operation ?arg?...

Both operation and its arguments determine the exact behavior of the command. The operations available for the graph are described in the BARCHART OPERATIONS section.

The command can also be used to access components of the graph.

pathName component operation ?arg?...

The operation, now located after the name of the component, is the function to be performed on that component. Each component has its own set of operations that manipulate that component. They will be described below in their own sections.

Example

The barchart command creates a new bar chart.
# Create a new bar chart. Plotting area is black.
barchart .b -plotbackground black

A new Tcl command .b is created. This command can be used to query and modify the bar chart. For example, to change the title of the graph to "My Plot", you use the new command and the configure operation.
# Change the title.
.b configure -title "My Plot"

To add data elements, you use the command and the element component.
# Create a new element named "e1"
.b element create e1 \
   -xdata { 1 2 3 4 5 6 7 8 9 10 } \
   -ydata { 26.18 50.46 72.85 93.31 111.86 128.47 143.14
       155.85 166.60 175.38 }

The element's X-Y coordinates are specified using lists of numbers. Alternately, BLT vectors could be used to hold the X-Y coordinates.
# Create two vectors and add them to the barchart.
vector xVector yVector
xVector set { 1 2 3 4 5 6 7 8 9 10 }
yVector set { 26.18 50.46 72.85 93.31 111.86 128.47 143.14 155.85
   166.60 175.38 }
n.b element create e1 -xdata xVector -ydata yVector

The advantage of using vectors is that when you modify one, the graph is automatically redrawn to reflect the new values.
# Change the y coordinate of the first point.
set yVector(0) 25.18

An element named e1 is now c