Terminal Graphics
Single-file, header-only C++20 functionality for producing graphics on the terminal using the sixel protocol
Loading...
Searching...
No Matches
TG::Plot Class Reference

A class to provide plotting capabilities. More...

Public Member Functions

 Plot (int width, int height, bool show_on_destruct=false)
 
 ~Plot ()
 
Plotreset ()
 clear canvas and reset X & Y limits
 
Plotshow ()
 display the plot to the terminal
 
Plotset_colourmap (const ColourMap &colourmap)
 set the colourmap if the default is not appropriate
 
Plotdisable_transparency ()
 disable transparent background for plot
 
Plotadd_line (float x0, float y0, float x1, float y1, int colour_index=2, int stiple=0, float stiple_frac=0.5)
 add a single line connection point (x0,y0) to (x1,y1).
 
template<class VerticesType>
Plotadd_line (const VerticesType &y, int colour_index=2, int stiple=0, float stiple_frac=0.5)
 plot the data series y along the x-axis
 
template<class VerticesTypeX, class VerticesTypeY>
Plotadd_line (const VerticesTypeX &x, const VerticesTypeY &y, int colour_index=2, int stiple=0, float stiple_frac=0.5)
 plot the data series y against the data series x
 
Plotadd_text (const std::string &text, float x, float y, float anchor_x=0.5, float anchor_y=0.5, int colour_index=1)
 add text at the location specified
 
Plotset_xlim (float min, float max, float expand_by=0.0)
 set the range along the x-axis
 
Plotset_ylim (float min, float max, float expand_by=0.0)
 set the range along the y-axis
 
Plotset_grid (float x_interval, float y_interval)
 set the interval of the gridlines, centered around zero
 

Detailed Description

A class to provide plotting capabilities.

This can be used stand-alone, but it is easier to use it via the TG::plot() function, which essentially simply returns an (anonymous) TG::Plot Object (refer to the documentation for TG::plot() for details).

This class provides a canvas initialised to the desired size (in pixels), with a default font size (6 pixels wide by default). The show_on_destruct argument is false by default, but that be set to true to ensure the class destructor calls show() when the class goes out of scope (this is what the TG::plot() function relies on).

By default, the colourmap is set to:

index red green blue name
0 0 0 0 black
1 100 100 100 white
2 100 100 0 yellow
3 100 0 100 magenta
4 0 100 100 cyan
5 100 0 0 red
6 0 100 0 green
7 0 0 100 blue

Most methods return a reference to the current instance. This allows them to be daisy-chained like this (again, this is what the TG::plot() syntax relies on):

std::vector<float> data (10);
...
Plot p (256, 128);
p.set_xlim (0,10).set_ylim (-1,1).add_line(data).show();

Many methods accept the following arguments:

  • colour_index specifies the colour to use (default: 2 - yellow)
  • stiple specifies the length of dashes (in pixels) if a dashed line is desired. Set to zero (the default) for a full line.
  • stiple_frac specific the fraction of the dash interval to be filled (default: 0.5).

Constructor & Destructor Documentation

◆ Plot()

TG::Plot::Plot ( int width,
int height,
bool show_on_destruct = false )
inline

◆ ~Plot()

TG::Plot::~Plot ( )
inline

Member Function Documentation

◆ reset()

Plot & TG::Plot::reset ( )
inline

clear canvas and reset X & Y limits

◆ show()

Plot & TG::Plot::show ( )
inline

display the plot to the terminal

This is to be called after all rendering instructions have been invoked and the plot is ready to be displayed.

If the plot has been constructed with show_on_destruct set to true, this will automatically be invoked by the destructor (this is the default behaviour when using the TG::plot() function).

◆ set_colourmap()

Plot & TG::Plot::set_colourmap ( const ColourMap & colourmap)
inline

set the colourmap if the default is not appropriate

◆ disable_transparency()

Plot & TG::Plot::disable_transparency ( )
inline

disable transparent background for plot

◆ add_line() [1/3]

Plot & TG::Plot::add_line ( float x0,
float y0,
float x1,
float y1,
int colour_index = 2,
int stiple = 0,
float stiple_frac = 0.5 )
inline

add a single line connection point (x0,y0) to (x1,y1).

If the X and/or Y limits have not yet been set (using set_xlim() or set_ylim(), this will automatically set them to 10% wider than the maximum range of the coordinates.

See main class description for an explanation of the remaining arguments.

◆ add_line() [2/3]

template<class VerticesType>
Plot & TG::Plot::add_line ( const VerticesType & y,
int colour_index = 2,
int stiple = 0,
float stiple_frac = 0.5 )
inline

plot the data series y along the x-axis

The input y can be any class that provides .size() and operator[]() methods (e.g. std::vector). This will plot the data points at locations (0,y[0]), (1,y[1]), ... , (n,y[n]).

If the X and/or Y limits have not yet been set (using set_xlim() or set_ylim(), this will automatically set the X limit to (0,n), and the Y limit to 10% wider than the maximum range of the data in y.

See main class description for an explanation of the remaining arguments.

◆ add_line() [3/3]

template<class VerticesTypeX, class VerticesTypeY>
Plot & TG::Plot::add_line ( const VerticesTypeX & x,
const VerticesTypeY & y,
int colour_index = 2,
int stiple = 0,
float stiple_frac = 0.5 )
inline

plot the data series y against the data series x

The inputs x & y can be any classes that provides .size() and operator[]() methods (e.g. std::vector). This will plot the data points at locations (x[0],[0]), (x[1],y[1]), ... , (x[n],y[n]).

If the X and/or Y limits have not yet been set (using set_xlim() or set_ylim(), these will automatically set them to 10% wider than the maximum range of the data in x & y respectively.

See main class description for an explanation of the remaining arguments.

◆ add_text()

Plot & TG::Plot::add_text ( const std::string & text,
float x,
float y,
float anchor_x = 0.5,
float anchor_y = 0.5,
int colour_index = 1 )

add text at the location specified

This renders the text in text at ithe location (x,y). By default, the text is centred on (x,y), but the location of the 'anchor' can be set using the anchor_x & anchor_y parameters.

See main class description for an explanation of the remaining arguments.

◆ set_xlim()

Plot & TG::Plot::set_xlim ( float min,
float max,
float expand_by = 0.0 )
inline

set the range along the x-axis

Note that this can only be done once, and if required, should be invoked before any rendering commands.

◆ set_ylim()

Plot & TG::Plot::set_ylim ( float min,
float max,
float expand_by = 0.0 )
inline

set the range along the y-axis

Note that this can only be done once, and if required, should be invoked before any rendering commands.

◆ set_grid()

Plot & TG::Plot::set_grid ( float x_interval,
float y_interval )
inline

set the interval of the gridlines, centered around zero