blib/lib/SVG/Graph/Kit.pm

filter: Statementstmt 83.3% 80 / 96 Branchbran 53.8% 14 / 26 Conditioncond 36.0% 18 / 50 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 83.3% 10 / 12 Podpod 100% 1 / 1 totaltotal 57.7% 123 / 213 CC 38 SCAR 35.2
CC
38
Cov
65%
_load_axis
66.5
_setup
13.4
new
6.1
CRAP 33.6
↑ Summary
1 package SVG::Graph::Kit;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Simplified SVG data plotting
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.0403';
10  
11 2 ▶use parent qw(SVG::Graph);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use SVG::Graph::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use SVG::Graph::Data::Datum;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 1 ▶sub new {
Subroutine new: called CC=6 SCAR=18.1
Pod documented
19 1 my $class = shift;
20 1 my %args = @_;
21  
22 # Move non-parent arguments to the kit.
23 1 my %kit = ();
24 1 for my $arg (qw(axis data plot polar)) {
25 4 ▶ next unless exists $args{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$arg} 3 1 4
26 1 $kit{$arg} = $args{$arg};
27 1 delete $args{$arg};
28 }
29  
30 # Construct the SVG::Graph object with the remaining arguments.
31 1 ▶ $args{width} ||= 600;
Condition50%1/2
$args{"width"} ||= 600
l!l
01
MC/DC0%0/1
$args{"width"} ||= 600
$args{"width"}
Truth table50%1/2
A
A= $args{"width"}
Aresult
11
00
32 1 ▶ $args{height} ||= 600;
Condition50%1/2
$args{"height"} ||= 600
l!l
01
MC/DC0%0/1
$args{"height"} ||= 600
$args{"height"}
Truth table50%1/2
A
A= $args{"height"}
Aresult
11
00
33 1 ▶ $args{margin} ||= 35;
Condition50%1/2
$args{"margin"} ||= 35
l!l
01
MC/DC0%0/1
$args{"margin"} ||= 35
$args{"margin"}
Truth table50%1/2
A
A= $args{"margin"}
Aresult
11
00
34 1 my $self = $class->SUPER::new(%args);
35  
36 # Re-bless as a Graph::Kit object.
37 1 bless $self, $class;
38 1 $self->_setup(%kit);
39 1 return $self;
40 }
41  
42 1 ▶sub _setup {
Subroutine _setup: called CC=9 SCAR=26.0
43 1 my $self = shift;
44 1 my %args = @_;
45  
46 # Start with an initial frame...
47 1 my $frame = $self->add_frame;
48  
49 # Plot the data.
50 1 ▶ if ($args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"data"}) 1 0 1
51 # Load the graph data and use the SVG::Graph::Data object for label making.
52 1 $self->{graph_data} = _load_data($args{data}, $frame, $args{polar});
53 # Add the data to the graph.
54 my %plot = (
55 stroke => $args{plot}{stroke} || 'red',
56 fill => $args{plot}{fill} || 'red',
57 1 ▶ 'fill-opacity' => $args{plot}{'fill-opacity'} || 0.5,
Condition50%3/6
$args{'plot'}{'stroke'} || 'red'
l!l
01
$args{'plot'}{'fill'} || 'red'
l!l
01
$args{'plot'}{'fill-opacity'} || 0.5
l!l
01
MC/DC0%0/1
$args{'plot'}{'stroke'} || 'red'
$args{'plot'}{'stroke'}
MC/DC0%0/1
$args{'plot'}{'fill'} || 'red'
$args{'plot'}{'fill'}
MC/DC0%0/1
$args{'plot'}{'fill-opacity'} || 0.5
$args{'plot'}{'fill-opacity'}
Truth table50%1/2
A
A= $args{'plot'}{'stroke'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'plot'}{'fill'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'plot'}{'fill-opacity'}
Aresult
11
00
58 );
59 1 ▶ $args{plot}{type} ||= 'scatter';
Condition50%1/2
$args{"plot"}{"type"} ||= "scatter"
l!l
01
MC/DC0%0/1
$args{"plot"}{"type"} ||= "scatter"
$args{"plot"}{"type"}
Truth table50%1/2
A
A= $args{"plot"}{"type"}
Aresult
11
00
60 1 $frame->add_glyph($args{plot}{type}, %plot);
61 }
62  
63 # Handle the axis unless it's set to 0.
64 1 ▶ if (!exists $args{axis} || (exists $args{axis} && $args{axis})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"axis"} or exists $args{"axis"} and $args{"axis"}) 1 0 1
Condition16%1/6
not exists $args{"axis"} or exists $args{"axis"} and $args{"axis"}
l!l&&r!l&&!r
100
exists $args{"axis"} and $args{"axis"}
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $args{"axis"} or exists $args{"axis"} and $args{"axis"}
not exists $args{"axis"} exists $args{"axis"} $args{"axis"}
Truth table25%1/4
not(A) or B and C
A= not exists $args{"axis"}B= exists $args{"axis"}C= $args{"axis"}
ABCresult
1XX1
0111
00X0
0100
65 1 my %axis = $self->_load_axis($args{data}, $args{axis});
66 1 $frame->add_glyph('axis', %axis);
67 }
68 }
69  
70 1 ▶sub _load_axis {
Subroutine _load_axis: called CC=22 SCAR=42.0
71 1 my($self, $data, $axis) = @_;
72  
73 # Initialize an empty axis unless given a hashref.
74 1 ▶ $axis = {} unless ref $axis eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $axis eq "HASH" 1 0 1
75  
76 # Set the default properties and user override.
77 1 my %axis = (
78 x_intercept => 0,
79 y_intercept => 0,
80 stroke => 'gray',
81 'stroke-width' => 2,
82 ticks => 30, # Max data per axis
83 %$axis, # User override
84 );
85  
86 # Set the number of ticks to show on each axis.
87 1 ▶ $axis{xticks} ||= $axis{ticks};
Condition33%1/3
$axis{"xticks"} ||= $axis{"ticks"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$axis{"xticks"} ||= $axis{"ticks"}
$axis{"xticks"} $axis{"ticks"}
Truth table33%1/3
A ||= B
A= $axis{"xticks"}B= $axis{"ticks"}
ABresult
1X1
011
000
88 1 ▶ $axis{yticks} ||= $axis{ticks};
Condition33%1/3
$axis{"yticks"} ||= $axis{"ticks"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$axis{"yticks"} ||= $axis{"ticks"}
$axis{"yticks"} $axis{"ticks"}
Truth table33%1/3
A ||= B
A= $axis{"yticks"}B= $axis{"ticks"}
ABresult
1X1
011
000
89  
90 # Compute scale factors.
91 1 my ($xscale, $yscale) = (1, 1);
92 1 ▶ if ($data && ($self->{graph_data}->xmax - $self->{graph_data}->xmin) > $axis{xticks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and $self->{"graph_data"}->xmax - $self->{"graph_data"}->xmin > $axis{"xticks"}) 0 1 1
Condition33%1/3
$data and $self->{"graph_data"}->xmax - $self->{"graph_data"}->xmin > $axis{"xticks"}
!ll&&!rl&&r
010
MC/DC0%0/2
$data and $self->{"graph_data"}->xmax - $self->{"graph_data"}->xmin > $axis{"xticks"}
$data $self->{"graph_data"}->xmax - $self->{"graph_data"}->xmin > $axis{"xticks"}
Truth table33%1/3
A and B
A= $dataB= $self->{"graph_data"}->xmax - $self->{"graph_data"}->xmin > $axis{"xticks"}
ABresult
0X0
100
111
93 # Round to the integer, i.e. 0 decimal places.
94 0 $xscale = sprintf '%.0f', $self->{graph_data}->xmax / $axis{xticks};
95 }
96 1 ▶ if ($data && ($self->{graph_data}->ymax - $self->{graph_data}->ymin) > $axis{yticks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and $self->{"graph_data"}->ymax - $self->{"graph_data"}->ymin > $axis{"yticks"}) 0 1 1
Condition33%1/3
$data and $self->{"graph_data"}->ymax - $self->{"graph_data"}->ymin > $axis{"yticks"}
!ll&&!rl&&r
010
MC/DC0%0/2
$data and $self->{"graph_data"}->ymax - $self->{"graph_data"}->ymin > $axis{"yticks"}
$data $self->{"graph_data"}->ymax - $self->{"graph_data"}->ymin > $axis{"yticks"}
Truth table33%1/3
A and B
A= $dataB= $self->{"graph_data"}->ymax - $self->{"graph_data"}->ymin > $axis{"yticks"}
ABresult
0X0
100
111
97 # Round to the integer, i.e. 0 decimal places.
98 0 $yscale = sprintf '%.0f', $self->{graph_data}->ymax / $axis{yticks};
99 }
100  
101 # Use absolute_ticks if no tick mark setting is provided.
102 1 ▶ unless (defined $axis{x_absolute_ticks} || defined $axis{x_fractional_ticks}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $axis{"x_absolute_ticks"} or defined $axis{"x_fractional_ticks"}) 1 0 1
Condition33%1/3
defined $axis{"x_absolute_ticks"} or defined $axis{"x_fractional_ticks"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $axis{"x_absolute_ticks"} or defined $axis{"x_fractional_ticks"}
defined $axis{"x_absolute_ticks"} defined $axis{"x_fractional_ticks"}
Truth table33%1/3
A or B
A= defined $axis{"x_absolute_ticks"}B= defined $axis{"x_fractional_ticks"}
ABresult
1X1
011
000
103 1 $axis{x_absolute_ticks} = $xscale;
104 }
105 1 ▶ unless (defined $axis{y_absolute_ticks} || defined $axis{y_fractional_ticks}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $axis{"y_absolute_ticks"} or defined $axis{"y_fractional_ticks"}) 1 0 1
Condition33%1/3
defined $axis{"y_absolute_ticks"} or defined $axis{"y_fractional_ticks"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $axis{"y_absolute_ticks"} or defined $axis{"y_fractional_ticks"}
defined $axis{"y_absolute_ticks"} defined $axis{"y_fractional_ticks"}
Truth table33%1/3
A or B
A= defined $axis{"y_absolute_ticks"}B= defined $axis{"y_fractional_ticks"}
ABresult
1X1
011
000
106 1 $axis{y_absolute_ticks} = $yscale;
107 }
108  
109 # Use increments of 1 to data-max for ticks if none are provided.
110 1 ▶ if ($data && !defined $axis{x_tick_labels} && !defined $axis{x_intertick_labels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and not defined $axis{"x_tick_labels"} and not defined $axis{"x_intertick_labels"}) 1 0 1
Condition33%2/6
$data and not defined $axis{"x_tick_labels"} and not defined $axis{"x_intertick_labels"}
!ll&&!rl&&r
001
$data and not defined $axis{"x_tick_labels"}
!ll&&!rl&&r
001
MC/DC0%0/3
$data and not defined $axis{"x_tick_labels"} and not defined $axis{"x_intertick_labels"}
$data not defined $axis{"x_tick_labels"} not defined $axis{"x_intertick_labels"}
Truth table25%1/4
A and not(B) and not(C)
A= $dataB= not defined $axis{"x_tick_labels"}C= not defined $axis{"x_intertick_labels"}
ABCresult
0XX0
10X0
1100
1111
111 1 ▶ if ($xscale > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xscale > 1) { } 0 1 1
112 0 $axis{x_tick_labels} = [ $self->{graph_data}->xmin ];
113 0 push @{ $axis{x_tick_labels} }, $_ * $xscale for 1 .. $axis{ticks};
114 }
115 else {
116 1 $axis{x_tick_labels} = [ $self->{graph_data}->xmin .. $self->{graph_data}->xmax ];
117 }
118 # XXX This is a lame hack
119 1 $axis{x_intertick_labels} = [ map { '' } $self->{graph_data}->ymin .. $self->{graph_data}->ymax ];
120 }
121 1 ▶ if ($data && !defined $axis{y_tick_labels} && !defined $axis{y_intertick_labels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and not defined $axis{"y_tick_labels"} and not defined $axis{"y_intertick_labels"}) 1 0 1
Condition33%2/6
$data and not defined $axis{"y_tick_labels"} and not defined $axis{"y_intertick_labels"}
!ll&&!rl&&r
001
$data and not defined $axis{"y_tick_labels"}
!ll&&!rl&&r
001
MC/DC0%0/3
$data and not defined $axis{"y_tick_labels"} and not defined $axis{"y_intertick_labels"}
$data not defined $axis{"y_tick_labels"} not defined $axis{"y_intertick_labels"}
Truth table25%1/4
A and not(B) and not(C)
A= $dataB= not defined $axis{"y_tick_labels"}C= not defined $axis{"y_intertick_labels"}
ABCresult
0XX0
10X0
1100
1111
122 1 ▶ if ($yscale > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yscale > 1) { } 0 1 1
123 0 $axis{y_tick_labels} = [ $self->{graph_data}->ymin ];
124 0 push @{ $axis{y_tick_labels} }, $_ * $yscale for 1 .. $axis{ticks};
125 }
126 else {
127 1 $axis{y_tick_labels} = [ $self->{graph_data}->ymin .. $self->{graph_data}->ymax ];
128 }
129 # XXX This is also a lame hack
130 1 $axis{y_intertick_labels} = [ map { '' } $self->{graph_data}->ymin .. $self->{graph_data}->ymax ];
131 }
132  
133 # Remove keys not used by parent module.
134 1 delete $axis{ticks};
135 1 delete $axis{xticks};
136 1 delete $axis{yticks};
137  
138 1 return %axis;
139 }
140  
141 1 ▶sub _load_data {
Subroutine _load_data: called CC=3 SCAR=11.0
142 1 my ($data, $frame, $polar) = @_;
143  
144 # Create individual data points.
145 1 my @data = ();
146 1 for my $datum (@$data) {
147 # Set the coordinate.
148 9 my @coord = @$datum;
149 9 ▶ @coord = _to_polar($datum) if $polar;
Branch: 1 branch
BranchTrueFalseTotal
if $polar 0 9 9
150  
151 # Add our 3D data point.
152 9 push @data, SVG::Graph::Data::Datum->new(
153 x => $coord[0],
154 y => $coord[1],
155 z => $coord[2],
156 );
157 }
158  
159 # Instantiate a new SVG::Graph::Data object;
160 1 my $obj = SVG::Graph::Data->new(data => \@data);
161  
162 # Populate our graph with data.
163 1 $frame->add_data($obj);
164 1 return $obj;
165 }
166  
167 0 ▶sub _theta {
Subroutine _theta: not called CC=1 SCAR=6.9
168 0 my $point = shift;
169 # return int(rand 359);
170 0 return atan2($point->[1], $point->[0]);
171 }
172  
173 0 ▶sub _to_polar {
Subroutine _to_polar: not called CC=2 SCAR=17.9
174 0 my $point = shift;
175 0 my $r = 0;
176 0 $r += $_ ** 2 for @$point;
177 0 $r = sqrt $r;
178 0 my $t = _theta($point);
179 0 return $r, $t;
180 }
181  
182 __END__
↑ Summary