Graph SVG SVG command

Commands in this namespace document graph SVG output.

SVG is used below as a placeholder for:

GRAPHINST svg

The SVG component exports the complete graph, including elements, axes, grid, markers, legend, title and margins. It is available on graph, stripchart, barchart and polar widgets without requiring Cairo. There is one SVG configuration per widget, independent of PostScript and the screen renderer. There are no named SVG objects.

Generating SVG

Return the document as a Tcl string or write it directly to a file:

set document [.g svg output]
.g svg output plot.svg
.g svg output plot.svg -width 1200 -height 800 -decorations no

File output uses UTF-8 with LF line endings. A filename starting with a dash must be prefixed with a directory, for example ./-plot.svg. There is no -- filename separator.

SVG options

Option

Default

Description

-width distance

0

Non-negative output width. Zero uses the graph width.

-height distance

0

Non-negative output height. Zero uses the graph height.

-decorations boolean

1

Include graph/legend backgrounds and relief borders. False uses white backgrounds; axes, labels and symbols remain visible.

Dimensions accept pixels or Tk screen distances, such as 6i or 15c, and are stored as integer pixels. Zero uses the current widget dimension, or its requested dimension before layout is available. The graph is laid out again for the export dimensions; the SVG canvas has a matching viewBox. Export does not resize the on-screen widget.

.g svg configure -width 6i -height 4i
puts [.g svg cget -width]
puts [.g svg configure -decorations]
.g svg configure -width 0 -height 0

Configuration queries use five-element lists:

{option name class default current}

Names/classes are width/Width, height/Height, and decorations/Decorations. These describe the query result only: SVG settings are not read from Tk’s option database. Set them with svg configure or svg output.

Invalid option/value lists leave all SVG settings unchanged. Once an output option list has been validated, its values remain configured even if subsequent rendering or file writing fails.

Supported content and limitations

SVG export supports traces, error bars, grids, axes, solid areas and bars, standard geometric symbols, line/polygon/text markers, legends, borders and editable text. Polar/Smith labels use the same layout and formatting as PostScript. Export remaps full source geometry rather than screen-decimated traces. Solid area fills preserve -areaopacity. Fonts are referenced by family rather than embedded; text appearance depends on installed fonts in the SVG viewer. Text rotation, baseline positions, measured widths and shadows are retained.

Photo image markers embed PNG data directly in the SVG, preserving RGB and alpha without external image files. Mapped scaling and plot clipping are retained. Bitmap markers (including rotation and explicit masks) export as vector pixel shapes. Bitmap element, active-pen and legend symbols reuse vector definitions within each symbol drawing pass; their colors, masks and aspect ratio are retained.

Stipple areas, polygon markers, bars and bar legend samples export as vector patterns. Stipples retain foreground/background colors and transparent gaps. Photo area tiles embed a repeating PNG with alpha and take precedence over stipples. Stipples align with graph pixels; photo tiles retain the screen renderer’s toplevel-relative phase. Bar legend patterns restart at each sample’s corner. Pattern sizes are in export pixels. -areaopacity affects solid fills only, not stipples or tiles.

Non-photo Tk image markers and tiles are drawn off-screen and embedded as PNG. Transparency is recovered from black/white renders, which assumes stable source-over drawing; custom image types whose output depends on destination contents may differ. No external image files are required.

Mapped window markers embed their current native window pixels as opaque PNG. Hidden or unmapped window markers are omitted. Window contents are raster snapshots at their current widget size, not vector drawings; native capture availability and visibility constraints apply. Export does not run the event loop to refresh a window. Refresh pending display work before exporting when needed.

Capture failures and invalid XML text report an error before the output file is opened. PostScript printer options (paper size, landscape, preview, font/color maps) are not SVG options. The SVG canvas uses pixel dimensions and a matching viewBox.

Commands

cget

Queries the current value of one SVG configuration option. See SVG options.

cget option

Parameters

option:

SVG configuration option.

Return value

Current option value, as integer pixels for dimensions or 0/1 for decorations.

configure

Queries or modifies SVG configuration options. With no arguments, returns a list of all option descriptions. With one option, returns its five-element description. Otherwise accepts option/value pairs and updates the settings after validating the entire list. See SVG options.

configure option
configure option value ?option value ...?

Parameters

option:

SVG configuration option.

value:

New value for the option.

Return value

Configuration information when querying, otherwise nothing.

output

Generates the SVG document. Without a filename, returns its XML text. With a filename, writes the document to that file, replacing existing contents. Optional settings remain configured after their option/value list is validated, including when a later rendering or file operation fails. Rendering and XML validation complete before the file is opened; an I/O failure can still leave a partial file. See Supported content and limitations.

output ?fileName? ?option value ...?

Parameters

fileName:

Optional file to create or replace; prefix dash-leading names with ./.

option:

Optional SVG configuration option.

value:

Value for the configuration option.

Return value

SVG XML when no filename is supplied, otherwise nothing.


Copyright (c) George Yashin