NAME

Tickit::Widget::SparkLine - minimal graph implementation for Tickit

VERSION

version 0.106

SYNOPSIS

my $vbox = Tickit::Widget::VBox->new;
my $widget = Tickit::Widget::SparkLine->new(
   data   => [ 0, 3, 2, 5, 1, 6, 0, 7 ]
);
$vbox->add($widget, expand => 1);

DESCRIPTION

Generates a mini ("sparkline") graph.

Sparkline widget in action

STYLE

Set the base style background/foreground to determine the graph colours. Note that reverse video and bold don't work very well on some terminals, since the background+foreground colours won't match.

METHODS

new

Instantiate the widget. Takes the following named parameters:

  • data - graph data

data

Accessor for stored data.

With no parameters, returns the stored data as a list.

Pass either an array or an arrayref to set the data values and request display refresh.

data_chars

Returns the set of characters corresponding to the current data values. Each value is assigned a single character, so the string length is equal to the number of data items and represents the minimal string capable of representing all current data items.

push

Helper method to add one or more items to the end of the list.

$widget->push(3,4,2);

pop

Helper method to remove one item from the end of the list, returns the item.

my $item = $widget->pop;

shift

Helper method to remove one item from the start of the list, returns the item.

my $item = $widget->shift;

unshift

Helper method to add items to the start of the list. Takes a list.

$widget->unshift(0, 1, 3);

splice

Equivalent to the standard Perl splice function.

# Insert 3,4,5 at position 2
$widget->splice(2, 0, 3, 4, 5);

graph_steps

Returns an arrayref of characters in order of magnitude.

For example:

[ ' ', qw(_ x X) ]

would yield a granularity of 4 steps.

Override this in subclasses to provide different visualisations - there's no limit to the number of characters you provide in this arrayref.

resample

Given a width $w, resamples the data (remaining list of parameters) to fit, using the current "resample_method".

Used internally.

render_to_rb

Rendering implementation. Uses "graph_steps" as the base character set.

char_for_value

Returns the character code corresponding to the given data value.

max_value

Returns the maximum value seen so far, used for autoscaling.

resample_mode

Change method for resampling when we have more data than will fit on the graph.

Current values include:

  • average - takes the average of combined values for this bucket

  • min - lowest value for this bucket

  • median - highest value for this bucket

  • max - largest value for this bucket

  • peak - alias for 'max'

The default is 'average'.

Returns $self if setting a value, or the current value.

INHERITED METHODS

Tickit::Widget

get_style_pen, get_style_text, get_style_values, key_focus_next_after, key_focus_next_before, parent, pen, redraw, requested_cols, requested_lines, requested_size, reshape, resized, set_parent, set_requested_size, set_style, set_style_tag, set_window, style_classes, take_focus, window, window_gained, window_lost

AUTHOR

Tom Molesworth <TEAM@cpan.org>

LICENSE

Copyright Tom Molesworth 2011-2015. Licensed under the same terms as Perl itself.