| 1 | package Term::Fabulous::Widget::PieChart; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | use Term::Fabulous::Widget::Chart; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::PieChart | ||
| 15 | :isa(Term::Fabulous::Widget::Chart) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Carp qw(croak); | ||
| 19 | use List::Util qw(max min sum0 first); | ||
| 20 | use POSIX qw(floor ceil); | ||
| 21 | use Scalar::Util qw(looks_like_number); | ||
| 22 | use Term::Fabulous::Check qw(boolean describe one_of); | ||
| 23 | use Term::Fabulous::Chart::Format qw(format_value check_number_format); | ||
| 24 | use Term::Fabulous::Chart::Marker; | ||
| 25 | use Term::Fabulous::Chart::Palette qw(chart_color contrast_rgb mix_rgb); | ||
| 26 | use Term::Fabulous::Chart::Radial qw(CELL_ASPECT TAU circle_frame polar_of point_at); | ||
| 27 | use Term::Fabulous::Chart::Raster; | ||
| 28 | use Term::Fabulous::Chart::Surface; | ||
| 29 | use Term::Fabulous::Termbox qw(TB_BOLD); | ||
| 30 | |||
| 31 | use constant { | ||
| 32 | GAP_WIDTH => 0.3, # cell widths between two slices, at least a subpixel | ||
| 33 | OTHER => "\0other", | ||
| 34 | LABEL_ROOM => 1, # columns free around a slice label | ||
| 35 | LEGEND_PERCENT_DECIMALS => 0, | ||
| 36 | }; | ||
| 37 | |||
| 38 | my @SORT = qw(none desc asc); | ||
| 39 | my @SLICE_LABELS = qw(percent value label none); | ||
| 40 | my @LEGEND_VALUES = qw(percent value both none); | ||
| 41 | my @PIE_MARKERS = qw(quadrant half sextant braille); | ||
| 42 | |||
| 43 | field $initial_data :param(data) = []; | ||
| 44 | field $initial_labels :param(labels) = undef; | ||
| 45 | field $initial_colors :param(colors) = undef; | ||
| 46 | field $hole :param = undef; | ||
| 47 | field $start_angle :param = 0; | ||
| 48 | field $sort :param = 'none'; | ||
| 49 | field $slice_labels :param = undef; | ||
| 50 | field $legend_values :param = undef; | ||
| 51 | field $center_text :param = undef; | ||
| 52 | field $marker :param = 'quadrant'; | ||
| 53 | field $other :param = 0; | ||
| 54 | field $other_label :param = 'Other'; | ||
| 55 | field $gap :param = 0; | ||
| 56 | field $format :param = undef; | ||
| 57 | |||
| 58 | field @_slices; # { label, value, color, opacity, slot } | ||
| 59 | field %_slice_by_label; | ||
| 60 | field $_next_slot = 0; | ||
| 61 | |||
| 62 | ADJUST { | ||
| 63 | $hole //= $self->default_hole; | ||
| 64 | $slice_labels //= $self->default_slice_labels; | ||
| 65 | $legend_values //= $self->default_legend_values; | ||
| 66 | $hole = $self->_checked_hole($hole); | ||
| 67 | $start_angle = $self->_checked_angle($start_angle); | ||
| 68 | $sort = one_of( $self, sort => $sort, @SORT ); | ||
| 69 | $slice_labels = one_of( $self, slice_labels => $slice_labels, @SLICE_LABELS ); | ||
| 70 | $legend_values = one_of( $self, legend_values => $legend_values, @LEGEND_VALUES ); | ||
| 71 | $marker = $self->_checked_marker($marker); | ||
| 72 | $other = $self->_checked_fraction( other => $other ); | ||
| 73 | $gap = boolean( $self, gap => $gap ); | ||
| 74 | $format = check_number_format( ref $self, 'format', $format ); | ||
| 75 | $self->_check_title( center_text => $center_text ); | ||
| 76 | $other_label = $self->_check_title( other_label => $other_label ) // 'Other'; | ||
| 77 | $self->set_data( $initial_data, $initial_labels, $initial_colors ); | ||
| 78 | ( $initial_data, $initial_labels, $initial_colors ) = (); | ||
| 79 | } | ||
| 80 | |||
| 81 | # A pie has no hole and shows percentages on its slices and in the | ||
| 82 | # legend. | ||
| 83 | method default_hole () { return 0 } | ||
| 84 | method default_slice_labels () { return 'percent' } | ||
| 85 | method default_legend_values () { return 'percent' } | ||
| 86 | |||
| 87 | method _checked_hole ($value) { | ||
| 88 | $self->_fail( 'hole', 'a number from 0 to 0.9', $value ) unless defined $value && !ref $value && looks_like_number($value) && $value >= 0 && $value <= 0.9; | ||
| 89 | return $value + 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | method _checked_angle ($value) { | ||
| 93 | $self->_fail( 'start_angle', 'a number of degrees', $value ) unless defined $value && !ref $value && looks_like_number($value) && $value == $value && $value - $value == 0; | ||
| 94 | return $value + 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | method _checked_marker ($value) { | ||
| 98 | $self->_fail( 'marker', join( ', ', @PIE_MARKERS ), $value ) unless defined $value && !ref $value && grep { $_ eq $value } @PIE_MARKERS; | ||
| 99 | return $value; | ||
| 100 | } | ||
| 101 | |||
| 102 | method _checked_value ( $label, $value ) { | ||
| 103 | croak ref($self) . ": the value of slice '$label' must be a number of at least 0, got " . describe($value) | ||
| 104 | unless defined $value && !ref $value && looks_like_number($value) && $value == $value && $value - $value == 0 && $value >= 0; | ||
| 105 | return $value + 0; | ||
| 106 | } | ||
| 107 | |||
| 108 | # --------------------------------------------------------------------- | ||
| 109 | # Slices | ||
| 110 | # --------------------------------------------------------------------- | ||
| 111 | |||
| 112 | # A new slice hash; a label the chart knows keeps its palette slot | ||
| 113 | # (its color), a new label gets one from _place. | ||
| 114 | method _slice ( $label, $value, $color ) { | ||
| 115 | croak ref($self) . ": a slice label must be a non-empty string, got " . describe($label) unless defined $label && !ref $label && length $label; | ||
| 116 | my $known = $_slice_by_label{$label}; | ||
| 117 | my %slice = ( label => "$label", slot => $known ? $known->{slot} : undef, value => $self->_checked_value( $label, $value ) ); | ||
| 118 | ( $slice{color}, $slice{opacity} ) = defined $color ? chart_color( $self, "color of slice '$label'", $color ) : ( undef, 1 ); | ||
| 119 | return \%slice; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Makes checked slices the chart's; new labels take the next slots. | ||
| 123 | method _place (@slices) { | ||
| 124 | $_->{slot} //= $_next_slot++ foreach @slices; | ||
| 125 | @_slices = @slices; | ||
| 126 | %_slice_by_label = map { $_->{label} => $_ } @_slices; | ||
| 127 | $self->_end_gone_hover; | ||
| 128 | $self->mark_changed; | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | |||
| 132 | # A hover on a slice the chart no longer has ends; the folded slice | ||
| 133 | # stays while small slices can fold. | ||
| 134 | method _end_gone_hover () { | ||
| 135 | $self->end_hover_unless_shown( ( map { $_->{label} } @_slices ), ( $other > 0 ? $other_label : () ) ); | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | # data: numbers (named by labels), [ label, value ] pairs, or | ||
| 140 | # { label, value, color } hashes. | ||
| 141 | method set_data ( $data, $labels = undef, $colors = undef ) { | ||
| 142 | croak ref($self) . ": data must be an array reference, got " . describe($data) unless ref $data eq 'ARRAY'; | ||
| 143 | croak ref($self) . ": labels must be an array reference, got " . describe($labels) if defined $labels && ref $labels ne 'ARRAY'; | ||
| 144 | croak ref($self) . ": colors must be an array reference, got " . describe($colors) if defined $colors && ref $colors ne 'ARRAY'; | ||
| 145 | my @slices; | ||
| 146 | foreach my $index ( 0 .. $#$data ) { | ||
| 147 | my $item = $data->[$index]; | ||
| 148 | my ( $label, $value, $color ); | ||
| 149 | if ( ref $item eq 'ARRAY' ) { | ||
| 150 | croak ref($self) . ": slice $index must be [ label, value ], got an array of " . scalar(@$item) . " values" unless @$item == 2; | ||
| 151 | ( $label, $value ) = @$item; | ||
| 152 | } | ||
| 153 | elsif ( ref $item eq 'HASH' ) { | ||
| 154 | my @unknown = grep { !/\A(?:label|value|color)\z/ } sort keys %$item; | ||
| 155 | croak ref($self) . ": slice $index takes only label, value and color, got @unknown" if @unknown; | ||
| 156 | ( $label, $value, $color ) = @$item{qw(label value color)}; | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | ( $label, $value ) = ( $labels && defined $labels->[$index] ? $labels->[$index] : 'Slice ' . ( $index + 1 ), $item ); | ||
| 160 | } | ||
| 161 | $color //= $colors->[$index] if $colors; | ||
| 162 | croak ref($self) . ": two slices are labeled '$label'" if grep { $_->{label} eq ( $label // '' ) } @slices; | ||
| 163 | push @slices, $self->_slice( $label, $value, $color ); | ||
| 164 | } | ||
| 165 | return $self->_place(@slices); | ||
| 166 | } | ||
| 167 | |||
| 168 | method set_value ( $label, $value ) { | ||
| 169 | my $known = $_slice_by_label{ $label // '' }; | ||
| 170 | return $self->add_slice( $label, $value ) unless $known; | ||
| 171 | $known->{value} = $self->_checked_value( $label, $value ); | ||
| 172 | $self->mark_changed; | ||
| 173 | return $self; | ||
| 174 | } | ||
| 175 | |||
| 176 | method add_slice ( $label, $value, $color = undef ) { | ||
| 177 | croak ref($self) . ": a slice labeled '$label' exists already" if defined $label && $_slice_by_label{$label}; | ||
| 178 | return $self->_place( @_slices, $self->_slice( $label, $value, $color ) ); | ||
| 179 | } | ||
| 180 | |||
| 181 | method remove_slice (@labels) { | ||
| 182 | foreach my $label (@labels) { | ||
| 183 | croak ref($self) . ": no slice labeled " . describe($label) unless defined $label && $_slice_by_label{$label}; | ||
| 184 | } | ||
| 185 | my %gone = map { $_ => 1 } @labels; | ||
| 186 | @_slices = grep { !$gone{ $_->{label} } } @_slices; | ||
| 187 | delete @_slice_by_label{@labels}; | ||
| 188 | $self->_end_gone_hover; | ||
| 189 | $self->mark_changed; | ||
| 190 | return $self; | ||
| 191 | } | ||
| 192 | |||
| 193 | method clear_slices () { | ||
| 194 | @_slices = (); | ||
| 195 | %_slice_by_label = (); | ||
| 196 | $self->_end_gone_hover; | ||
| 197 | $self->mark_changed; | ||
| 198 | return $self; | ||
| 199 | } | ||
| 200 | |||
| 201 | method set_slice_color ( $label, $color ) { | ||
| 202 | my $slice = $_slice_by_label{ $label // '' } // croak ref($self) . ": no slice labeled " . describe($label); | ||
| 203 | ( $slice->{color}, $slice->{opacity} ) = defined $color ? chart_color( $self, "color of slice '$label'", $color ) : ( undef, 1 ); | ||
| 204 | $self->mark_changed; | ||
| 205 | return $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | method slices () { | ||
| 209 | return map { { label => $_->{label}, value => $_->{value}, ( defined $_->{color} ? ( color => sprintf '#%06x', $_->{color} ) : () ) } } @_slices; | ||
| 210 | } | ||
| 211 | |||
| 212 | method value ($label) { | ||
| 213 | my $slice = $_slice_by_label{ $label // '' } // return undef; | ||
| 214 | return $slice->{value}; | ||
| 215 | } | ||
| 216 | |||
| 217 | method total () { | ||
| 218 | return sum0 map { $_->{value} } @_slices; | ||
| 219 | } | ||
| 220 | |||
| 221 | # The slices as drawn: sorted, small ones folded into "Other", each | ||
| 222 | # with its share of the total. | ||
| 223 | method shown_slices ($look) { | ||
| 224 | my @slices = grep { $_->{value} > 0 } @_slices; | ||
| 225 | @slices = sort { $b->{value} <=> $a->{value} } @slices if $sort eq 'desc'; | ||
| 226 | @slices = sort { $a->{value} <=> $b->{value} } @slices if $sort eq 'asc'; | ||
| 227 | my $total = sum0 map { $_->{value} } @slices; | ||
| 228 | return () unless $total > 0; | ||
| 229 | my @shown = map { | ||
| 230 | { %$_, share => $_->{value} / $total } | ||
| 231 | } @slices; | ||
| 232 | if ( $other > 0 ) { | ||
| 233 | my @small = grep { $_->{share} < $other } @shown; | ||
| 234 | if ( @small >= 2 ) { | ||
| 235 | my %small = map { $_->{label} => 1 } @small; | ||
| 236 | @shown = grep { !$small{ $_->{label} } } @shown; | ||
| 237 | my $value = sum0 map { $_->{value} } @small; | ||
| 238 | push @shown, { label => $other_label, value => $value, share => $value / $total, color => mix_rgb( $look->{label}, $look->{base}, 0.35 ), opacity => 1, is_other => 1 }; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | foreach my $slice (@shown) { | ||
| 242 | my $color = $slice->{color} // $self->slot_color( $look, $slice->{slot} ); | ||
| 243 | $color = mix_rgb( $look->{base}, $color, $slice->{opacity} ) if $slice->{opacity} < 1; | ||
| 244 | $slice->{base_color} = $color; | ||
| 245 | $slice->{shown} = $self->shown_color( $look, $slice->{label}, $color ); | ||
| 246 | } | ||
| 247 | return @shown; | ||
| 248 | } | ||
| 249 | |||
| 250 | # --------------------------------------------------------------------- | ||
| 251 | # Drawing | ||
| 252 | # --------------------------------------------------------------------- | ||
| 253 | |||
| 254 | method default_legend_position () { | ||
| 255 | return 'right'; | ||
| 256 | } | ||
| 257 | |||
| 258 | method _value_text ($value) { | ||
| 259 | return format_value( $value, $format ); | ||
| 260 | } | ||
| 261 | |||
| 262 | method _percent_text ($share) { | ||
| 263 | my $percent = $share * 100; | ||
| 264 | return ( $percent > 0 && $percent < 1 ? '<1' : sprintf( '%.0f', $percent ) ) . '%'; | ||
| 265 | } | ||
| 266 | |||
| 267 | method legend_entries ($look) { | ||
| 268 | return map { | ||
| 269 | my $slice = $_; | ||
| 270 | my $value | ||
| 271 | = $legend_values eq 'percent' ? $self->_percent_text( $slice->{share} ) | ||
| 272 | : $legend_values eq 'value' ? $self->_value_text( $slice->{value} ) | ||
| 273 | : $legend_values eq 'both' ? [ $self->_value_text( $slice->{value} ), $self->_percent_text( $slice->{share} ) ] | ||
| 274 | : undef; | ||
| 275 | { series => $slice->{label}, label => $slice->{label}, color => $slice->{base_color}, symbol => 'fill', value => $value, raw_value => $slice->{value} } | ||
| 276 | } $self->shown_slices($look); | ||
| 277 | } | ||
| 278 | |||
| 279 | # The angles [from, to) of each slice in turns, and how far out it | ||
| 280 | # reaches (1 is the full radius). | ||
| 281 | method slice_geometry ( $look, @slices ) { | ||
| 282 | my $at = 0; | ||
| 283 | return map { | ||
| 284 | my $from = $at; | ||
| 285 | $at += $_->{share}; | ||
| 286 | [ $from, $at, 1 ]; | ||
| 287 | } @slices; | ||
| 288 | } | ||
| 289 | |||
| 290 | method draw_plot ( $surface, $x, $y, $width, $height, $look ) { | ||
| 291 | my @slices = $self->shown_slices($look); | ||
| 292 | my $circle = circle_frame( 0, 0, $width, $height, 0.5 ); | ||
| 293 | return unless @slices && $circle->{radius} >= 1; | ||
| 294 | $self->draw_background_grid( $surface, $x, $y, $width, $height, $circle, $look, \@slices ); | ||
| 295 | |||
| 296 | my @geometry = $self->slice_geometry( $look, @slices ); | ||
| 297 | my $start = $start_angle / 360; | ||
| 298 | my $radius = $circle->{radius}; | ||
| 299 | my $inner = $hole * $radius; | ||
| 300 | my @targets = map { $self->register_target( series => $slices[$_]{label}, index => $_, label => $slices[$_]{label}, value => $slices[$_]{value} ) } 0 .. $#slices; | ||
| 301 | |||
| 302 | my $raster = Term::Fabulous::Chart::Raster->new( marker => Term::Fabulous::Chart::Marker->named($marker), columns => $width, rows => $height ); | ||
| 303 | my ( $sx, $sy ) = ( $raster->marker->columns, $raster->marker->rows ); | ||
| 304 | my $gaps = $gap && @slices > 1; | ||
| 305 | |||
| 306 | # At least a subpixel wide in both directions, so it shows at any | ||
| 307 | # resolution: a subpixel whose center lies on the gap's edge is out. | ||
| 308 | my $gap_width = max( GAP_WIDTH, 1 / $sx, CELL_ASPECT / $sy ); | ||
| 309 | $raster->paint_area( | ||
| 310 | 0, 0, | ||
| 311 | $raster->width, | ||
| 312 | $raster->height, | ||
| 313 | sub ( $px, $py ) { | ||
| 314 | my ( $distance, $angle ) = polar_of( $circle, ( $px + 0.5 ) / $sx, ( $py + 0.5 ) / $sy, $start ); | ||
| 315 | return () if $distance < $inner || $distance > $radius; | ||
| 316 | my $index = first { $angle >= $geometry[$_][0] && $angle < $geometry[$_][1] } 0 .. $#geometry; | ||
| 317 | return () unless defined $index; | ||
| 318 | my ( $from, $to, $reach ) = $geometry[$index]->@*; | ||
| 319 | return () if $distance > $reach * $radius; | ||
| 320 | if ($gaps) { | ||
| 321 | my $turns = min( $angle - $from, $to - $angle, 0.25 ); | ||
| 322 | my $away = sin( $turns * TAU ) * $distance; # from the nearer edge of the slice | ||
| 323 | return () if $away <= $gap_width / 2 + 1e-9; | ||
| 324 | } | ||
| 325 | return ( $slices[$index]{shown}, $targets[$index] ); | ||
| 326 | } | ||
| 327 | ); | ||
| 328 | $surface->composite( $raster, 'fill', $x, $y ); | ||
| 329 | |||
| 330 | $self->_draw_slice_labels( $surface, $x, $y, $circle, \@slices, \@geometry, $look, $start, $inner ) if $slice_labels ne 'none'; | ||
| 331 | $self->_draw_center( $surface, $x, $y, $circle, \@slices, $look, $inner ) if $inner >= 3; | ||
| 332 | $self->draw_foreground_grid( $surface, $x, $y, $width, $height, $circle, $look, \@slices ); | ||
| 333 | return; | ||
| 334 | } | ||
| 335 | |||
| 336 | # Hooks for charts that draw rings or labels around the slices. | ||
| 337 | method draw_background_grid ( $surface, $x, $y, $width, $height, $circle, $look, $slices ) { return } | ||
| 338 | method draw_foreground_grid ( $surface, $x, $y, $width, $height, $circle, $look, $slices ) { return } | ||
| 339 | |||
| 340 | # The percentage (value, label) on each slice that has room for it. | ||
| 341 | method _draw_slice_labels ( $surface, $x, $y, $circle, $slices, $geometry, $look, $start, $inner ) { | ||
| 342 | foreach my $index ( 0 .. $#$slices ) { | ||
| 343 | my $slice = $slices->[$index]; | ||
| 344 | my ( $from, $to, $reach ) = $geometry->[$index]->@*; | ||
| 345 | my $outer = $reach * $circle->{radius}; | ||
| 346 | next if $outer - $inner < 2; | ||
| 347 | my $text | ||
| 348 | = $slice_labels eq 'percent' ? $self->_percent_text( $slice->{share} ) | ||
| 349 | : $slice_labels eq 'value' ? $self->_value_text( $slice->{value} ) | ||
| 350 | : $slice->{label}; | ||
| 351 | my $width = Term::Fabulous::Chart::Surface->text_columns($text); | ||
| 352 | my $distance = $inner + ( $outer - $inner ) * ( $inner ? 0.5 : 0.6 ); | ||
| 353 | my ( $center_x, $center_y ) = point_at( $circle, $distance, ( $from + $to ) / 2, $start ); | ||
| 354 | my ( $column, $row ) = ( floor( $center_x - $width / 2 + 0.5 ), floor($center_y) ); | ||
| 355 | |||
| 356 | # Only where the text and a cell around it lie inside the slice. | ||
| 357 | my $fits = 1; | ||
| 358 | foreach my $cell ( $column - LABEL_ROOM .. $column + $width - 1 + LABEL_ROOM ) { | ||
| 359 | my ( $distance_of_cell, $angle ) = polar_of( $circle, $cell + 0.5, $row + 0.5, $start ); | ||
| 360 | $fits = 0 unless $distance_of_cell >= $inner && $distance_of_cell <= $outer - 0.5 && $angle >= $from && $angle < $to; | ||
| 361 | } | ||
| 362 | next unless $fits; | ||
| 363 | $surface->text( $x + $column, $y + $row, $text, contrast_rgb( $slice->{shown} ), bg => $slice->{shown} ); | ||
| 364 | } | ||
| 365 | return; | ||
| 366 | } | ||
| 367 | |||
| 368 | # In the hole of a donut: the center_text, or the slice the pointer | ||
| 369 | # is on, or the total. | ||
| 370 | method _draw_center ( $surface, $x, $y, $circle, $slices, $look, $inner ) { | ||
| 371 | return if defined $center_text && !length $center_text; | ||
| 372 | my $hovered = defined $look->{emphasis} ? first { $_->{label} eq $look->{emphasis} } @$slices : undef; | ||
| 373 | my @lines | ||
| 374 | = defined $center_text ? ( split /\n/, $center_text ) | ||
| 375 | : $hovered ? ( $self->_percent_text( $hovered->{share} ), $hovered->{label} ) | ||
| 376 | : ( $self->_value_text( sum0 map { $_->{value} } @$slices ), 'Total' ); | ||
| 377 | my $room = floor( 2 * $inner / sqrt 2 ) - 1; | ||
| 378 | my $top = floor( $circle->{center_y} - @lines / 2 + 0.5 ); | ||
| 379 | foreach my $index ( 0 .. $#lines ) { | ||
| 380 | my $line = $lines[$index]; | ||
| 381 | my $width = min( Term::Fabulous::Chart::Surface->text_columns($line), $room ); | ||
| 382 | next if $width < 1; | ||
| 383 | my $color = $index == 0 ? $look->{title} : $look->{label}; | ||
| 384 | $surface->text( $x + floor( $circle->{center_x} - $width / 2 + 0.5 ), $y + $top + $index, $line, $color, max => $room, flags => $index == 0 ? TB_BOLD : 0 ); | ||
| 385 | } | ||
| 386 | return; | ||
| 387 | } | ||
| 388 | |||
| 389 | # --------------------------------------------------------------------- | ||
| 390 | # Accessors | ||
| 391 | # --------------------------------------------------------------------- | ||
| 392 | |||
| 393 | method _set ( $field_ref, $value ) { | ||
| 394 | $$field_ref = $value; | ||
| 395 | $self->mark_changed; | ||
| 396 | return $value; | ||
| 397 | } | ||
| 398 | |||
| 399 | method hole (@new) { return @new ? $self->_set( \$hole, $self->_checked_hole( $new[0] ) ) : $hole } | ||
| 400 | method start_angle (@new) { return @new ? $self->_set( \$start_angle, $self->_checked_angle( $new[0] ) ) : $start_angle } | ||
| 401 | method sort (@new) { return @new ? $self->_set( \$sort, one_of( $self, sort => $new[0], @SORT ) ) : $sort } | ||
| 402 | method slice_labels (@new) { return @new ? $self->_set( \$slice_labels, one_of( $self, slice_labels => $new[0], @SLICE_LABELS ) ) : $slice_labels } | ||
| 403 | method legend_values (@new) { return @new ? $self->_set( \$legend_values, one_of( $self, legend_values => $new[0], @LEGEND_VALUES ) ) : $legend_values } | ||
| 404 | method center_text (@new) { return @new ? $self->_set( \$center_text, $self->_check_title( center_text => $new[0] ) ) : $center_text } | ||
| 405 | method marker (@new) { return @new ? $self->_set( \$marker, $self->_checked_marker( $new[0] ) ) : $marker } | ||
| 406 | method other (@new) { return @new ? $self->_set( \$other, $self->_checked_fraction( other => $new[0] ) ) : $other } | ||
| 407 | method other_label (@new) { return @new ? $self->_set( \$other_label, $self->_check_title( other_label => $new[0] ) // 'Other' ) : $other_label } | ||
| 408 | method gap (@new) { return @new ? $self->_set( \$gap, boolean( $self, gap => $new[0] ) ) : $gap } | ||
| 409 | method format (@new) { return @new ? $self->_set( \$format, check_number_format( ref $self, 'format', $new[0] ) ) : $format } | ||
| 410 | |||
| 411 | # --------------------------------------------------------------------- | ||
| 412 | # KDL | ||
| 413 | # --------------------------------------------------------------------- | ||
| 414 | |||
| 415 | method layout_properties :common () { | ||
| 416 | return ( | ||
| 417 | $class->SUPER::layout_properties, | ||
| 418 | hole => 'scalar', | ||
| 419 | start_angle => 'scalar', | ||
| 420 | sort => 'scalar', | ||
| 421 | slice_labels => 'scalar', | ||
| 422 | legend_values => 'scalar', | ||
| 423 | center_text => 'scalar', | ||
| 424 | marker => 'scalar', | ||
| 425 | other => 'scalar', | ||
| 426 | other_label => 'scalar', | ||
| 427 | gap => 'boolean', | ||
| 428 | format => 'scalar', | ||
| 429 | slice => \&_parse_slice, | ||
| 430 | ); | ||
| 431 | } | ||
| 432 | |||
| 433 | # slice "Rent" 1200 color="#3987e5" | ||
| 434 | method _parse_slice ($kid) { | ||
| 435 | my @args = map { $_->as_perl } $kid->args->@*; | ||
| 436 | my $props = { map { $_->[0] => $_->[1]->as_perl } $kid->props->@* }; | ||
| 437 | croak ref($self) . ": layout property 'slice' takes a label and a value" unless @args == 2 && !$kid->children->@*; | ||
| 438 | my @unknown = grep { $_ ne 'color' } sort keys %$props; | ||
| 439 | croak ref($self) . ": layout property 'slice' takes only the property color, got @unknown" if @unknown; | ||
| 440 | $self->add_slice( "$args[0]", $args[1], $props->{color} ); | ||
| 441 | return; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | 1; | ||
| 446 | |||
| 447 | __END__ |