blib/lib/Term/Fabulous/Chart/Series.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Text/Style.pm ↑ Summary blib/lib/Term/Fabulous/Editor.pm »
1 package Term::Fabulous::Chart::Series;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 class Term::Fabulous::Chart::Series :strict(params) {
13 1 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶ use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶ use Term::Fabulous::Check qw(describe glyph one_of);
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Term::Fabulous::Chart::Curve qw(check_curve);
17 use Term::Fabulous::Chart::Palette qw(chart_color);
18 use Term::Fabulous::Chart::Transform qw(parse_transforms);
19  
20 my @TYPES = qw(line area bar scatter);
21  
22 # The markers each type may draw with.
23 my %MARKERS = (
24 line => [qw(braille half quadrant sextant box)],
25 area => [qw(block braille half quadrant sextant)],
26 bar => [qw(block braille half quadrant sextant)],
27 scatter => [qw(braille half quadrant sextant)],
28 );
29  
30 # Dash patterns in subpixel steps.
31 my %LINE_STYLE = ( solid => undef, dashed => [ 5, 3 ], dotted => [ 1, 2 ] );
32  
33 # The options a series takes besides name, type, data and color; undef
34 # means "the chart's setting".
35 my @OPTIONS = qw(marker curve tension line line_style points point fill_opacity stack from to transform span_gaps visible max_points trend value_labels);
36  
37 field $owner :param; # the chart class, for messages
38 field $described_as :param = undef; # how messages name the series; undef: series 'NAME', '': not at all
39 field $check_points :param = undef; # called with the series and its new points before they are stored
40 field $name :param :reader;
41 field $type :param :reader;
42 field $slot :param :reader; # the palette slot, kept for life
43  
44 field $color;
45 field $color_opacity = 1;
46 field %option;
47 field @points; # [ x as given (undef: the point's index), y or undef ]
48 field $revision = 0;
49  
50 ADJUST :params (%options) {
51 croak "$owner: a series name must be a non-empty string, got " . describe($name)
52 unless defined $name && !ref $name && length $name;
53 one_of( $owner, "type of series '$name'", $type, @TYPES );
54 my ( $data, $given_color ) = ( delete $options{data} // [], delete $options{color} );
55 $self->set_color($given_color);
56 $self->set_option( $_ => delete $options{$_} ) foreach grep { exists $options{$_} } @OPTIONS;
57 croak "$owner: series '$name' does not take " . join( ', ', sort keys %options ) . " (known: name, type, data, color, " . join( ', ', @OPTIONS ) . ")" if %options;
58 $self->set_data($data);
59 }
60  
61 sub _is_number ($value) {
62 return defined $value && !ref $value && looks_like_number($value) && $value == $value && $value - $value == 0;
63 }
64  
65 method revision () {
66 return $revision;
67 }
68  
69 # ---------------------------------------------------------------------
70 # Options
71 # ---------------------------------------------------------------------
72  
73 # A new type; a marker the new type cannot draw with is dropped.
74 method set_type ($new) {
75 one_of( $owner, "type of series '$name'", $new, @TYPES );
76 $type = $new;
77 delete $option{marker} if defined $option{marker} && !grep { $_ eq $option{marker} } $MARKERS{$type}->@*;
78 $revision++;
79 return $self;
80 }
81  
82 method color () {
83 return $color;
84 }
85  
86 method color_opacity () {
87 return $color_opacity;
88 }
89  
90 method set_color ($new) {
91 ( $color, $color_opacity ) = defined $new ? chart_color( $owner, "color of series '$name'", $new ) : ( undef, 1 );
92 $revision++;
93 return $self;
94 }
95  
96 method option ($key) {
97 croak "$owner: unknown series option '$key'" unless grep { $_ eq $key } @OPTIONS;
98 return $option{$key};
99 }
100  
101 # What messages call an option of the series.
102 method _label ($key) {
103 my $what = $described_as // "series '$name'";
104 return length $what ? "$key of $what" : $key;
105 }
106  
107 method set_option ( $key, $value ) {
108 my $label = $self->_label($key);
109 if ( !defined $value ) {
110 delete $option{$key};
111 }
112 elsif ( $key eq 'marker' ) {
113 my @allowed = $MARKERS{$type}->@*;
114 croak "$owner: $label must be one of " . join( ', ', @allowed ) . " for a $type series, got " . describe($value) unless !ref $value && grep { $_ eq $value } @allowed;
115 $option{$key} = $value;
116 }
117 elsif ( $key eq 'curve' ) {
118 $option{$key} = check_curve( $owner, $label, $value );
119 }
120 elsif ( $key eq 'line_style' ) {
121 one_of( $owner, $label, $value, keys %LINE_STYLE );
122 $option{$key} = $value;
123 }
124 elsif ( $key eq 'tension' || $key eq 'fill_opacity' ) {
125 croak "$owner: $label must be a number from 0 to 1, got " . describe($value) unless _is_number($value) && $value >= 0 && $value <= 1;
126 $option{$key} = $value + 0;
127 }
128 elsif ( $key eq 'point' ) {
129 $option{$key} = !ref $value && ( $value eq 'dot' || $value eq 'square' ) ? $value : glyph( $owner, $label, $value );
130 }
131 elsif ( $key eq 'stack' ) {
132 croak "$owner: $label must be a group name, got " . describe($value) if ref $value;
133 $option{$key} = "$value";
134 }
135 elsif ( $key eq 'from' || $key eq 'to' ) {
136 croak "$owner: $label must be an x value, got " . describe($value) if ref $value && !( blessed $value && $value->can('epoch') );
137 $option{$key} = $value;
138 }
139 elsif ( $key eq 'transform' ) {
140 $option{$key} = parse_transforms( $owner, $label, $value );
141 }
142 elsif ( $key eq 'max_points' ) {
143 croak "$owner: $label must be a positive integer, got " . describe($value) unless !ref $value && $value =~ /\A[1-9][0-9]*\z/;
144 $option{$key} = $value + 0;
145 $self->_trim;
146 }
147 else { # line points span_gaps visible trend value_labels
148 croak "$owner: $label must be a plain true or false value, got " . describe($value) if ref $value;
149 $option{$key} = $value ? 1 : 0;
150 }
151 $revision++;
152 return $self;
153 }
154  
155 # The dash pattern of the line style, in subpixel steps.
156 method dash_pattern ($style) {
157 return $LINE_STYLE{$style};
158 }
159  
160 method is_visible () {
161 return $option{visible} // 1;
162 }
163  
164 method marker_names :common ($series_type) {
165 return ( $MARKERS{$series_type} // [] )->@*;
166 }
167  
168 method option_names :common () {
169 return @OPTIONS;
170 }
171  
172 # ---------------------------------------------------------------------
173 # Data
174 # ---------------------------------------------------------------------
175  
176 # A data point as [ x or undef, y or undef ]: a number (y; the x is its
177 # index), undef (a gap), [ x, y ] or { x => ..., y => ... }.
178 method _point ( $item, $index ) {
179 return [ undef, undef ] unless defined $item;
180 my ( $x, $y );
181 if ( !ref $item ) {
182 $y = $item;
183 }
184 elsif ( ref $item eq 'ARRAY' ) {
185 croak "$owner: data point $index of series '$name' must be [ x, y ], got an array of " . scalar(@$item) . " values" unless @$item == 2;
186 ( $x, $y ) = @$item;
187 }
188 elsif ( ref $item eq 'HASH' ) {
189 my @unknown = grep { $_ ne 'x' && $_ ne 'y' } sort keys %$item;
190 croak "$owner: data point $index of series '$name' takes only the keys x and y, got @unknown" if @unknown;
191 ( $x, $y ) = @$item{qw(x y)};
192 }
193 else {
194 croak "$owner: data point $index of series '$name' must be a number, [ x, y ] or { x, y }, got " . describe($item);
195 }
196 croak "$owner: the y value of data point $index of series '$name' must be a finite number or undef, got " . describe($y) if defined $y && !_is_number($y);
197 croak "$owner: the x value of data point $index of series '$name' must be a number, a label or a date, got " . describe($x)
198 if ref $x && !( blessed $x && $x->can('epoch') );
199 return [ $x, defined $y ? $y + 0 : undef ];
200 }
201  
202 # The points of new data, parsed and checked; nothing is stored until
203 # every point passed.
204 method _parsed_points ( $items, $first_index ) {
205 my $index = $first_index;
206 my @new = map { $self->_point( $_, $index++ ) } @$items;
207 $check_points->( $self, \@new ) if $check_points;
208 return @new;
209 }
210  
211 method set_data ($data) {
212 croak "$owner: the data of series '$name' must be an array reference, got " . describe($data) unless ref $data eq 'ARRAY';
213 @points = $self->_parsed_points( $data, 0 );
214 $self->_trim;
215 $revision++;
216 return $self;
217 }
218  
219 method add_points (@items) {
220 return $self->push_parsed( $self->parsed_points(@items) );
221 }
222  
223 # New points as add_points would store them: parsed and checked, and
224 # numbered on from the points the series has. Stores nothing.
225 method parsed_points (@items) {
226 return $self->_parsed_points( \@items, scalar @points );
227 }
228  
229 # Stores points parsed_points made.
230 method push_parsed (@parsed) {
231 push @points, @parsed;
232 $self->_trim;
233 $revision++;
234 return $self;
235 }
236  
237 method clear () {
238 @points = ();
239 $revision++;
240 return $self;
241 }
242  
243 method _trim () {
244 my $limit = $option{max_points} // return;
245 $self->keep_last($limit);
246 return;
247 }
248  
249 # Drops all but the newest $limit points.
250 method keep_last ($limit) {
251 return $self if @points <= $limit;
252 splice @points, 0, @points - $limit;
253 $revision++;
254 return $self;
255 }
256  
257 # The points as given: [ x or undef, y or undef ] each. Read only.
258 method points () {
259 return \@points;
260 }
261  
262 method count () {
263 return scalar @points;
264 }
265 }
266  
267 1;
268  
269 __END__
« blib/lib/Term/Fabulous/Text/Style.pm ↑ Summary blib/lib/Term/Fabulous/Editor.pm »