« blib/lib/Term/Fabulous/Widget/PixelCanvas.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/ProgressBar.pm »
| 1 | package Term::Fabulous::Widget::PolarAreaChart; | ||
| 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::PieChart; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::PolarAreaChart | ||
| 15 | :isa(Term::Fabulous::Widget::PieChart) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use List::Util (); | ||
| 19 | use POSIX qw(floor); | ||
| 20 | use Term::Fabulous::Chart::Format qw(check_number_format); | ||
| 21 | use Term::Fabulous::Chart::Marker; | ||
| 22 | use Term::Fabulous::Chart::Palette qw(contrast_rgb); | ||
| 23 | use Term::Fabulous::Chart::Radial qw(CELL_ASPECT TAU point_at); | ||
| 24 | use Term::Fabulous::Chart::Raster; | ||
| 25 | use Term::Fabulous::Chart::Scale::Linear; | ||
| 26 | use Term::Fabulous::Chart::Surface; | ||
| 27 | |||
| 28 | field $max :param = undef; # the value at the outer ring; undef: from the data | ||
| 29 | field $ticks :param = undef; # the number of rings wanted | ||
| 30 | field $_scale; # the scale of the frame being drawn | ||
| 31 | |||
| 32 | ADJUST { | ||
| 33 | $self->_fail( 'max', 'a positive number or undef', $max ) if defined $max && !( !ref $max && $max =~ /\A[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?\z/ && $max > 0 ); | ||
| 34 | $self->_fail( 'ticks', 'a positive integer or undef', $ticks ) if defined $ticks && ( ref $ticks || $ticks !~ /\A[1-9][0-9]*\z/ ); | ||
| 35 | } | ||
| 36 | |||
| 37 | # The rings show the values; the legend repeats them. | ||
| 38 | method default_slice_labels :override () { return 'none' } | ||
| 39 | method default_legend_values :override () { return 'value' } | ||
| 40 | |||
| 41 | method max (@new) { | ||
| 42 | return $max unless @new; | ||
| 43 | $self->_fail( 'max', 'a positive number or undef', $new[0] ) if defined $new[0] && !( !ref $new[0] && $new[0] =~ /\A[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?\z/ && $new[0] > 0 ); | ||
| 44 | $max = $new[0]; | ||
| 45 | $self->mark_changed; | ||
| 46 | return $max; | ||
| 47 | } | ||
| 48 | |||
| 49 | method ticks (@new) { | ||
| 50 | return $ticks unless @new; | ||
| 51 | $self->_fail( 'ticks', 'a positive integer or undef', $new[0] ) if defined $new[0] && ( ref $new[0] || $new[0] !~ /\A[1-9][0-9]*\z/ ); | ||
| 52 | $ticks = $new[0]; | ||
| 53 | $self->mark_changed; | ||
| 54 | return $ticks; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Every slice gets the same angle; its value decides how far out it | ||
| 58 | # reaches. | ||
| 59 | method slice_geometry :override ( $look, @slices ) { | ||
| 60 | my $count = @slices; | ||
| 61 | my $outer = $_scale ? $_scale->max : List::Util::max( map { $_->{value} } @slices ); | ||
| 62 | return map { [ $_ / $count, ( $_ + 1 ) / $count, $outer ? $slices[$_]{value} / $outer : 0 ] } 0 .. $#slices; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Rings at the ticks of the value scale, behind the slices. | ||
| 66 | method draw_background_grid :override ( $surface, $x, $y, $width, $height, $circle, $look, $slices ) { | ||
| 67 | |||
| 68 | # Rings about every three rows (six cell widths) apart. | ||
| 69 | my $rows = List::Util::max( 2, floor( $circle->{radius} / 1.5 ) + 1 ); | ||
| 70 | $_scale = Term::Fabulous::Chart::Scale::Linear->fit( | ||
| 71 | extent => [ 0, List::Util::max( map { $_->{value} } @$slices ) ], | ||
| 72 | cells => $rows, | ||
| 73 | zero => 1, | ||
| 74 | align => 0, | ||
| 75 | format => $self->format, | ||
| 76 | ( defined $max ? ( max => $max ) : () ), | ||
| 77 | ( defined $ticks ? ( ticks => $ticks ) : () ), | ||
| 78 | ); | ||
| 79 | my $raster = Term::Fabulous::Chart::Raster->new( marker => Term::Fabulous::Chart::Marker->named('braille'), columns => $width, rows => $height ); | ||
| 80 | foreach my $tick ( $_scale->ticks ) { | ||
| 81 | next unless $tick->{value} > 0; | ||
| 82 | my $distance = $tick->{position} * $circle->{radius}; | ||
| 83 | my $steps = List::Util::max( 24, floor( TAU * $distance * 2 ) ); | ||
| 84 | my @points = map { [ point_at( $circle, $distance, $_ / $steps ) ] } 0 .. $steps; | ||
| 85 | $raster->line( $points[ $_ - 1 ][0] * 2, $points[ $_ - 1 ][1] * 4, $points[$_][0] * 2, $points[$_][1] * 4, $look->{grid} ) foreach 1 .. $#points; | ||
| 86 | } | ||
| 87 | $surface->composite( $raster, 'stroke', $x, $y ); | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | # The values of the rings along the line to 12 o'clock. | ||
| 92 | method draw_foreground_grid :override ( $surface, $x, $y, $width, $height, $circle, $look, $slices ) { | ||
| 93 | my $last_row; | ||
| 94 | foreach my $tick ( reverse $_scale->ticks ) { | ||
| 95 | next unless $tick->{value} > 0; | ||
| 96 | my ( $at_x, $at_y ) = point_at( $circle, $tick->{position} * $circle->{radius}, 0 ); | ||
| 97 | my ( $column, $row ) = ( floor($at_x) + 1, floor($at_y) ); | ||
| 98 | next if defined $last_row && $row <= $last_row; | ||
| 99 | my $under = $surface->bg_at( $x + $column, $y + $row ); | ||
| 100 | my $color = defined $under && ( !defined $look->{background} || $under != $look->{background} ) ? contrast_rgb($under) : $look->{label}; | ||
| 101 | $surface->text( $x + $column, $y + $row, $tick->{label}, $color, max => $width - $column ); | ||
| 102 | $last_row = $row; | ||
| 103 | } | ||
| 104 | $_scale = undef; | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | method layout_properties :common () { | ||
| 109 | return ( $class->SUPER::layout_properties, max => 'scalar', ticks => 'scalar' ); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | __END__ |