Terminal Graphics
Single-file, header-only C++20 functionality for producing graphics on the terminal using the sixel protocol
|
A class to provide plotting capabilities. More...
Public Member Functions | |
Plot (int width, int height, bool show_on_destruct=false) | |
~Plot () | |
Plot & | reset () |
clear canvas and reset X & Y limits | |
Plot & | show () |
display the plot to the terminal | |
Plot & | set_colourmap (const ColourMap &colourmap) |
set the colourmap if the default is not appropriate | |
Plot & | disable_transparency () |
disable transparent background for plot | |
Plot & | add_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> | |
Plot & | add_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> | |
Plot & | add_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 | |
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 | |
Plot & | set_xlim (float min, float max, float expand_by=0.0) |
set the range along the x-axis | |
Plot & | set_ylim (float min, float max, float expand_by=0.0) |
set the range along the y-axis | |
Plot & | set_grid (float x_interval, float y_interval) |
set the interval of the gridlines, centered around zero | |
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).
|
inline |
|
inline |
|
inline |
clear canvas and reset X & Y limits
|
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 the colourmap if the default is not appropriate
|
inline |
disable transparent background for plot
|
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.
|
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.
|
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.
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.
|
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.
|
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.
|
inline |
set the interval of the gridlines, centered around zero