| 1 | package HTML::D3; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 32 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 32 | ▶ | use Carp qw(carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 32 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 32 | my $_JSON = JSON::MaybeXS->new(utf8 => 0); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 32 | ▶ | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 32 | ▶ | use Params::Get; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 32 | ▶ | use Params::Validate::Strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 32 | ▶ | use Scalar::Util qw(blessed looks_like_number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | HTML::D3 - A simple Perl module for generating charts using D3.js. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Version 0.29 | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 32 | our $VERSION = '0.29'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | use HTML::D3; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | my $chart = HTML::D3->new( | ||||||||||||||||||||||||||||||||||||||||||
| 33 | width => 1024, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | height => 768, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | title => 'Sample Bar Chart' | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | my $data = [ | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ['Category 1', 10], | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ['Category 2', 20], | ||||||||||||||||||||||||||||||||||||||||||
| 41 | ['Category 3', 30] | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | my $html = $chart->render_bar_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | print $html; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | $chart = HTML::D3->new(title => 'Sales Data'); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | $data = [ | ||||||||||||||||||||||||||||||||||||||||||
| 50 | ['Product A', 100], | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ['Product B', 150], | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ['Product C', 200] | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | $html = $chart->render_line_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | print $html; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | HTML::D3 is a Perl module that provides functionality to create simple charts using D3.js. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | The module generates HTML and JavaScript code to render the chart in a web browser. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | Text in a chart's data and options is written into a C<E<lt>scriptE<gt>> so that it | ||||||||||||||||||||||||||||||||||||||||||
| 64 | cannot end the script: C<E<lt>> is written as C<\u003c>, which JavaScript reads back | ||||||||||||||||||||||||||||||||||||||||||
| 65 | as the same character. An C<id> is written into an attribute, a selector and a | ||||||||||||||||||||||||||||||||||||||||||
| 66 | script, so it must be letters, digits, C<_> and C<->, not starting with a digit or | ||||||||||||||||||||||||||||||||||||||||||
| 67 | C<->. Any other C<id> is ignored and the method's default used, so read the id a | ||||||||||||||||||||||||||||||||||||||||||
| 68 | method actually used from the C<svg_id> or C<table_id> it returns. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | The C<=head3 API SPECIFICATION> subsections use L<Params::Validate::Strict> | ||||||||||||||||||||||||||||||||||||||||||
| 73 | schema syntax (C<< type => 'arrayref' >> etc.) as a documentation convention. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | The module is also used at runtime in C<new()> to validate constructor | ||||||||||||||||||||||||||||||||||||||||||
| 75 | arguments; it is therefore a required runtime dependency. The schemas describe | ||||||||||||||||||||||||||||||||||||||||||
| 76 | the parameter contract in machine-readable notation and can be plumbed into a | ||||||||||||||||||||||||||||||||||||||||||
| 77 | WAF or test generator if desired. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | my $chart = HTML::D3->new(%args); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Creates a new HTML::D3 object. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | Accepts the following optional arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =item * C<width> - The width of the chart (default: 800). | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =item * C<height> - The height of the chart (default: 600). | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =item * C<title> - The title of the chart (default: 'Chart'). | ||||||||||||||||||||||||||||||||||||||||||
| 93 | It is plain text, escaped when it is written to the page: C<E<lt>> and C<&> appear | ||||||||||||||||||||||||||||||||||||||||||
| 94 | as themselves, and HTML in a title is shown rather than obeyed. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =item * C<responsive> - If true, SVG elements are rendered with | ||||||||||||||||||||||||||||||||||||||||||
| 97 | C<viewBox> and C<width="100%"> instead of fixed pixel dimensions, so the | ||||||||||||||||||||||||||||||||||||||||||
| 98 | chart scales fluidly with its container; with no C<height> attribute, the | ||||||||||||||||||||||||||||||||||||||||||
| 99 | height follows the C<viewBox>'s proportions. (C<height="auto"> is not a | ||||||||||||||||||||||||||||||||||||||||||
| 100 | valid value for the attribute: Chromium logs an error for it and ignores it.) | ||||||||||||||||||||||||||||||||||||||||||
| 101 | For snippet methods the per-call C<< opts => { responsive => 1 } >> | ||||||||||||||||||||||||||||||||||||||||||
| 102 | takes precedence over this object-level setting (default: 0). | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 549 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 549 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 549 | ▶ | my $params = Params::Validate::Strict::validate_strict({ | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
Params::Get::get_params(undef, \@_) || {}
MC/DC100%1/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | args => Params::Get::get_params(undef, \@_) || {}, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | height => { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||
| 117 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | minimum => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | }, width => { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||
| 121 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | minimum => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 123 | }, title => { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||
| 125 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | }, responsive => { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | type => 'boolean', | ||||||||||||||||||||||||||||||||||||||||||
| 128 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 549 | ▶ | if(!defined($class)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | if((scalar keys %{$params}) > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | carp(__PACKAGE__, ' use ->new() not ::new() to instantiate'); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # When called as HTML::D3::new(undef) with no args, default to the package. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # Passing args via ::new() is unsupported and carps above. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $class = __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } elsif(blessed($class)) { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | return bless { %{$class}, %{$params} }, ref($class); | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 544 | $params = Object::Configure::configure($class, $params); | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | return bless { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | width => $params->{width} || 800, | ||||||||||||||||||||||||||||||||||||||||||
| 149 | height => $params->{height} || 600, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | title => $params->{title} || 'Chart', | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 544 | ▶ | responsive => $params->{responsive} || 0, | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%8/8
$params->{'width'} || 800
$params->{'height'} || 600
$params->{'title'} || 'Chart'
$params->{'responsive'} || 0
MC/DC100%1/1
$params->{'width'} || 800
$params->{'width'}
MC/DC100%1/1
$params->{'height'} || 600
$params->{'height'}
MC/DC100%1/1
$params->{'title'} || 'Chart'
$params->{'title'}
MC/DC100%1/1
$params->{'responsive'} || 0
$params->{'responsive'}
Truth table100%2/2
A
A= $params->{'width'}
Truth table100%2/2
A
A= $params->{'height'}
Truth table100%2/2
A
A= $params->{'title'}
Truth table100%2/2
A
A= $params->{'responsive'}
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # ───────────────────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # The categorical colour schemes, written down once. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # The scheme names and the colours themselves live here and nowhere else. The | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # snippets inline these colours as a JavaScript array - not as a d3.scheme* | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # expression - so a page draws the exact numbers palette() hands a map, and a | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # change to d3's own palette cannot move a chart's colours under it. Perl | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # callers - a map, a legend, a static renderer - read the same colours through | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # categorical_schemes(), palette() and colour_at(). A chart and a map of the | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # same data therefore cannot disagree about what scheme 'set2' means, which two | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # separate lists of names and colours eventually would. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # | ||||||||||||||||||||||||||||||||||||||||||
| 167 | # The colours are D3's own, in D3's order: the scheme arrays in | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # d3-scale-chromatic 3.x, which is the version d3 v7 - what _head() loads - | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # bundles. Each entry names the d3 scheme it transcribes, so the palette can be | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # checked against the library (t/d3-schemes.t does that when Node and d3 are | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # installed), but the chart does not depend on d3 for the numbers it draws. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 32 | my %CATEGORICAL_SCHEMES = ( | |||||||||||||||||||||||||||||||||||||||||
| 173 | tableau10 => { | ||||||||||||||||||||||||||||||||||||||||||
| 174 | colours => [ '#4e79a7', '#f28e2c', '#e15759', '#76b7b2', '#59a14f', | ||||||||||||||||||||||||||||||||||||||||||
| 175 | '#edc949', '#af7aa1', '#ff9da7', '#9c755f', '#bab0ab' ], | ||||||||||||||||||||||||||||||||||||||||||
| 176 | }, category10 => { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | colours => [ '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', | ||||||||||||||||||||||||||||||||||||||||||
| 178 | '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf' ], | ||||||||||||||||||||||||||||||||||||||||||
| 179 | }, set2 => { | ||||||||||||||||||||||||||||||||||||||||||
| 180 | colours => [ '#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', | ||||||||||||||||||||||||||||||||||||||||||
| 181 | '#a6d854', '#ffd92f', '#e5c494', '#b3b3b3' ], | ||||||||||||||||||||||||||||||||||||||||||
| 182 | }, set3 => { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | colours => [ '#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', | ||||||||||||||||||||||||||||||||||||||||||
| 184 | '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', | ||||||||||||||||||||||||||||||||||||||||||
| 185 | '#ccebc5', '#ffed6f' ], | ||||||||||||||||||||||||||||||||||||||||||
| 186 | }, paired => { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | colours => [ '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', | ||||||||||||||||||||||||||||||||||||||||||
| 188 | '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', | ||||||||||||||||||||||||||||||||||||||||||
| 189 | '#ffff99', '#b15928' ], | ||||||||||||||||||||||||||||||||||||||||||
| 190 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # The scheme a chart draws with when it is not told which one to use. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 32 | my $DEFAULT_CATEGORICAL_SCHEME = 'tableau10'; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # The palette the charts that offer no scheme option draw with: the pie family | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # and the multi-series line charts that predate the option have always drawn | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # category10. The colours are read from the definition above rather than | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # written out again here, so that a chart with no choice and a chart told which | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # scheme to use are the same palette and not two copies of it. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 32 | my $CATEGORY10_JS = _scheme_colours_js('category10'); | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | # The sequential schemes a chart shades a number with -- the heatmap's cells and | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # a treemap coloured by a measure -- and the d3 interpolator each one names. | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # Only these names reach a script. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 32 | my %SEQUENTIAL_SCHEMES = ( | |||||||||||||||||||||||||||||||||||||||||
| 207 | YlOrRd => 'd3.interpolateYlOrRd', | ||||||||||||||||||||||||||||||||||||||||||
| 208 | Blues => 'd3.interpolateBlues', | ||||||||||||||||||||||||||||||||||||||||||
| 209 | Greens => 'd3.interpolateGreens', | ||||||||||||||||||||||||||||||||||||||||||
| 210 | Purples => 'd3.interpolatePurples', | ||||||||||||||||||||||||||||||||||||||||||
| 211 | RdPu => 'd3.interpolateRdPu', | ||||||||||||||||||||||||||||||||||||||||||
| 212 | YlGnBu => 'd3.interpolateYlGnBu', | ||||||||||||||||||||||||||||||||||||||||||
| 213 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | # The function a chart's script escapes text with before handing it to | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # .html(): a tooltip or legend entry is markup, for its bold and its line | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # breaks, and the text in it is the caller's -- labels, series names and data | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # from a file -- so "<img onerror=...>" in a cell would otherwise run when the | ||||||||||||||||||||||||||||||||||||||||||
| 219 | # entry is drawn or hovered. .text() needs none of this. Written once here | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # for the scripts that had no esc() of their own. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 32 | my $ESC_JS = q{function esc(s) { return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); }}; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | =head2 categorical_schemes | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | my $names = HTML::D3->categorical_schemes(); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | Returns an arrayref of the names of the categorical colour schemes this module | ||||||||||||||||||||||||||||||||||||||||||
| 228 | can draw with, sorted alphabetically: C<category10>, C<paired>, C<set2>, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | C<set3> and C<tableau10>. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | These are the names the C<scheme> and C<color_scheme> options accept. | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | sub categorical_schemes | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
categorical_schemes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 3 | return [ sort keys %CATEGORICAL_SCHEMES ]; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | =head2 palette | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | my $colours = HTML::D3->palette('set2'); | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | Returns an arrayref of the colours of one scheme - the same colours, in the same | ||||||||||||||||||||||||||||||||||||||||||
| 244 | order, that a chart drawn with C<< scheme => 'set2' >> draws with. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | A map, a legend or a static renderer can therefore colour the same data the way | ||||||||||||||||||||||||||||||||||||||||||
| 246 | the chart does, from Perl, without a browser: | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | my $scheme = 'set2'; | ||||||||||||||||||||||||||||||||||||||||||
| 249 | my $colours = HTML::D3->palette($scheme); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | my $index = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 251 | for my $category (@categories) { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | printf "%s is %s\n", $category, HTML::D3->colour_at($scheme, $index++); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | Dies with C<Unknown scheme: E<lt>nameE<gt>> for a name that is not in | ||||||||||||||||||||||||||||||||||||||||||
| 256 | C<categorical_schemes()>, rather than quietly returning the default scheme's | ||||||||||||||||||||||||||||||||||||||||||
| 257 | colours, so that a misspelt scheme is found where it is asked for. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | The arrayref returned is a copy: pushing onto it does not change the palette. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | =head2 colour_at | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | my $fill = HTML::D3->colour_at('tableau10', $index); | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | Returns the colour one scheme gives the C<$index>th category, counting from | ||||||||||||||||||||||||||||||||||||||||||
| 265 | zero, cyclically: an index past the end of the palette wraps around to its | ||||||||||||||||||||||||||||||||||||||||||
| 266 | start, which is how C<d3.scaleOrdinal> - what the snippets draw with - assigns | ||||||||||||||||||||||||||||||||||||||||||
| 267 | colours. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | Colour 0 is the first colour of the scheme, the one C<render_histogram_snippet> | ||||||||||||||||||||||||||||||||||||||||||
| 269 | uses as the bar fill when it is given a C<scheme>. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | The British spelling matches the C<colour_by_series> option. | ||||||||||||||||||||||||||||||||||||||||||
| 272 | Dies with C<Unknown scheme: E<lt>nameE<gt>> for an unknown scheme, and with | ||||||||||||||||||||||||||||||||||||||||||
| 273 | C<Colour index must be a non-negative integer> for an index that is not one. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | sub palette | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
palette: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 31 | ▶ | my $params = Params::Validate::Strict::validate_strict({ | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("scheme", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("scheme", \@_) // {}
Params::Get::get_params("scheme", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("scheme", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | args => Params::Get::get_params('scheme', \@_) // {}, | ||||||||||||||||||||||||||||||||||||||||||
| 282 | schema => { scheme => { type => 'string' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 283 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 31 | return [ @{_categorical_scheme($params->{scheme})->{colours}} ]; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | sub colour_at | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 29 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
colour_at: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 29 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 29 | my ($scheme, $index); | |||||||||||||||||||||||||||||||||||||||||
| 291 | 29 | ▶ | if (@_ == 1 && ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $h = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | $scheme = $h->{scheme}; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $index = $h->{index}; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 29 | ($scheme, $index) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 29 | ▶ | die 'Colour index must be a non-negative integer' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $index and $index =~ /\A[0-9]+\z/
MC/DC100%2/2
defined $index and $index =~ /\A[0-9]+\z/
defined $index
$index =~ /\A[0-9]+\z/
Truth table100%3/3
A and B
A= defined $indexB= $index =~ /\A[0-9]+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | unless defined($index) && $index =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 25 | my $colours = _categorical_scheme($scheme)->{colours}; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 23 | return $colours->[$index % scalar(@$colours)]; | |||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | =head2 render_bar_chart | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | my $html = $chart->render_bar_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | Generates HTML and JavaScript code to render a bar chart. Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | =item * C<$data> - An array reference containing data points. Each data point should | ||||||||||||||||||||||||||||||||||||||||||
| 313 | be an array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | Returns a string containing the HTML and JavaScript code for the chart. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =item * Throws C<Data is not optional> when C<$data> is C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | data => { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||
| 336 | element_type => [ 'string', 'number' ] | ||||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 341 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | Str -- complete HTML5 document starting with C<< <!DOCTYPE html> >>; | ||||||||||||||||||||||||||||||||||||||||||
| 346 | D3.js loaded from CDN; bar chart rendered with C<d3.scaleBand>. | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | # Method to render a bar chart with given data | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 14 | ▶ | sub render_bar_chart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_bar_chart: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 14 | ▶ | my $first = @_ ? $_[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 14 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 14 | ▶ | if (!defined($raw->{data})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 4 | ▶ | die(defined($first) ? 'Data must be an array of arrays' : 'Data is not optional'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | Params::Validate::Strict::validate_strict({ | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 10 | args => $raw, | |||||||||||||||||||||||||||||||||||||||||
| 360 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 361 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 362 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 363 | 8 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | # Generate JSON representation of data | ||||||||||||||||||||||||||||||||||||||||||
| 366 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 8 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 368 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | # Generate HTML and D3.js JavaScript for rendering the bar chart | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 8 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 372 | 8 | $html .= $self->_head(); | |||||||||||||||||||||||||||||||||||||||||
| 373 | 8 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 374 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 8 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 376 | 8 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 377 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 378 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 381 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 382 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 383 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | // Set up scales for x and y axes | ||||||||||||||||||||||||||||||||||||||||||
| 386 | const x = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 387 | .domain(data.map(d => d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 388 | .range([0, width]) | ||||||||||||||||||||||||||||||||||||||||||
| 389 | .padding(0.1); | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 392 | .domain([0, d3.max(data, d => d.value)]) | ||||||||||||||||||||||||||||||||||||||||||
| 393 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 394 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 397 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | // Add bars to the chart | ||||||||||||||||||||||||||||||||||||||||||
| 400 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 401 | .selectAll("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 402 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 403 | .join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 404 | .attr("x", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 405 | .attr("y", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 406 | .attr("height", d => height - y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 407 | .attr("width", x.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 408 | .attr("fill", "steelblue"); | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | // Add the y-axis | ||||||||||||||||||||||||||||||||||||||||||
| 411 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 412 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | // Add the x-axis with labels rotated for better readability | ||||||||||||||||||||||||||||||||||||||||||
| 415 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 416 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 417 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 418 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 419 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 420 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 421 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 422 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 423 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 424 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 8 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | =head2 render_animated_bar_chart | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | my $html = $chart->render_animated_bar_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | Generates HTML and JavaScript code to render a bar chart where each bar grows | ||||||||||||||||||||||||||||||||||||||||||
| 434 | upward from the baseline on page load. Bars are staggered so they rise | ||||||||||||||||||||||||||||||||||||||||||
| 435 | one-after-another from left to right. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =item * C<$data> - An array reference of data points. Each data point is an | ||||||||||||||||||||||||||||||||||||||||||
| 441 | array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | Returns a string containing the complete HTML5 document. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | =item * Throws C<Data is not optional> when C<$data> is C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | { | ||||||||||||||||||||||||||||||||||||||||||
| 462 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 466 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | Str -- complete HTML5 document; each bar animates from height=0 upward | ||||||||||||||||||||||||||||||||||||||||||
| 471 | using C<d3.transition()> with a staggered per-bar delay. | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 11 | ▶ | sub render_animated_bar_chart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_animated_bar_chart: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 11 | ▶ | my $first = @_ ? $_[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | 11 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 11 | ▶ | if (!defined($raw->{data})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 4 | ▶ | die(defined($first) ? 'Data must be an array of arrays' : 'Data is not optional'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||
| 482 | Params::Validate::Strict::validate_strict({ | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 7 | args => $raw, | |||||||||||||||||||||||||||||||||||||||||
| 484 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 485 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 486 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 5 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 491 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 494 | 5 | $html .= $self->_head(); | |||||||||||||||||||||||||||||||||||||||||
| 495 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 496 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 497 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 498 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 499 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 500 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 503 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 504 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 505 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | const x = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 508 | .domain(data.map(d => d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 509 | .range([0, width]) | ||||||||||||||||||||||||||||||||||||||||||
| 510 | .padding(0.1); | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 513 | .domain([0, d3.max(data, d => d.value)]) | ||||||||||||||||||||||||||||||||||||||||||
| 514 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 515 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 518 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | // Each bar starts at the baseline (y=height, height=0) and grows upward. | ||||||||||||||||||||||||||||||||||||||||||
| 521 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 522 | .selectAll("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 523 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 524 | .join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 525 | .attr("x", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 526 | .attr("y", height) | ||||||||||||||||||||||||||||||||||||||||||
| 527 | .attr("height", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 528 | .attr("width", x.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 529 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 530 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 531 | .duration(800) | ||||||||||||||||||||||||||||||||||||||||||
| 532 | .delay((d, i) => i * 100) | ||||||||||||||||||||||||||||||||||||||||||
| 533 | .attr("y", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 534 | .attr("height", d => height - y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 537 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 540 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 541 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 542 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 543 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 544 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 545 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 546 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 547 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 548 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | =head2 render_line_chart | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | my $html = $chart->render_line_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | Generates HTML and JavaScript code to render a line chart. Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | =item * C<$data> - An array reference containing data points. Each data point should | ||||||||||||||||||||||||||||||||||||||||||
| 562 | be an array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | Returns a string containing the HTML and JavaScript code for the chart. | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | { | ||||||||||||||||||||||||||||||||||||||||||
| 581 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 585 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | Str -- complete HTML5 document; line chart with C<d3.scalePoint> and C<d3.line()>. | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | 7 | ▶ | sub render_line_chart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_line_chart: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 595 | 7 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 596 | 7 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 598 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 599 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 600 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 601 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | # Generate JSON for data | ||||||||||||||||||||||||||||||||||||||||||
| 605 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 606 | 5 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 607 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | # Generate HTML and D3.js code | ||||||||||||||||||||||||||||||||||||||||||
| 610 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 611 | 5 | $html .= $self->_head(); | |||||||||||||||||||||||||||||||||||||||||
| 612 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 613 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 615 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 616 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 617 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||
| 619 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 620 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 621 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 622 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 625 | .domain(data.map(d => d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 626 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 629 | .domain([0, d3.max(data, d => d.value)]) | ||||||||||||||||||||||||||||||||||||||||||
| 630 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 631 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 634 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | // Draw line | ||||||||||||||||||||||||||||||||||||||||||
| 637 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 638 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 639 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 642 | .datum(data) | ||||||||||||||||||||||||||||||||||||||||||
| 643 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 644 | .attr("stroke", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 645 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 646 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||
| 648 | // Add points to the line | ||||||||||||||||||||||||||||||||||||||||||
| 649 | chart.selectAll("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 650 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 651 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 652 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 653 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 654 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 655 | .attr("fill", "steelblue"); | ||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||
| 657 | // Add axes | ||||||||||||||||||||||||||||||||||||||||||
| 658 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 659 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 662 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 663 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 664 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 665 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 666 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 667 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 668 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 669 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 670 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||
| 672 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||
| 675 | =head2 render_animated_line_chart | ||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||
| 677 | my $html = $chart->render_animated_line_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | Generates HTML and JavaScript code to render a line chart where the line | ||||||||||||||||||||||||||||||||||||||||||
| 680 | draws itself from left to right on page load, followed by each data-point | ||||||||||||||||||||||||||||||||||||||||||
| 681 | circle fading in once the line is complete. | ||||||||||||||||||||||||||||||||||||||||||
| 682 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||
| 684 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | =item * C<$data> - An array reference of data points. Each data point is an | ||||||||||||||||||||||||||||||||||||||||||
| 687 | array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||
| 689 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | Returns a string containing the complete HTML5 document. | ||||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||
| 693 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||||||
| 695 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||||||||||||||||
| 697 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||
| 701 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||
| 703 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||
| 705 | { | ||||||||||||||||||||||||||||||||||||||||||
| 706 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||
| 709 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 710 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||
| 712 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||
| 714 | Str -- complete HTML5 document; the line path animates via | ||||||||||||||||||||||||||||||||||||||||||
| 715 | C<stroke-dashoffset> with C<d3.easeLinear>; data-point circles | ||||||||||||||||||||||||||||||||||||||||||
| 716 | fade in with C<opacity> after the line transition completes. | ||||||||||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||||||||||
| 718 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||
| 720 | 8 | ▶ | sub render_animated_line_chart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_animated_line_chart: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 721 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 722 | 8 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 723 | 8 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 724 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 725 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 726 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 727 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 728 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 729 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||
| 731 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 732 | 5 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 733 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 736 | 5 | $html .= $self->_head(); | |||||||||||||||||||||||||||||||||||||||||
| 737 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 738 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 739 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 740 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 741 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 742 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||
| 744 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 745 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 746 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 747 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||
| 749 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 750 | .domain(data.map(d => d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 751 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 754 | .domain([0, d3.max(data, d => d.value)]) | ||||||||||||||||||||||||||||||||||||||||||
| 755 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 756 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||
| 758 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 759 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||
| 761 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 762 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 763 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||||||
| 765 | // Animate the line drawing left-to-right using stroke-dashoffset. | ||||||||||||||||||||||||||||||||||||||||||
| 766 | const path = chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 767 | .datum(data) | ||||||||||||||||||||||||||||||||||||||||||
| 768 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 769 | .attr("stroke", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 770 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 771 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||
| 773 | const totalLength = path.node().getTotalLength(); | ||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||
| 775 | path | ||||||||||||||||||||||||||||||||||||||||||
| 776 | .attr("stroke-dasharray", totalLength) | ||||||||||||||||||||||||||||||||||||||||||
| 777 | .attr("stroke-dashoffset", totalLength) | ||||||||||||||||||||||||||||||||||||||||||
| 778 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 779 | .duration(1500) | ||||||||||||||||||||||||||||||||||||||||||
| 780 | .ease(d3.easeLinear) | ||||||||||||||||||||||||||||||||||||||||||
| 781 | .attr("stroke-dashoffset", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | // Data-point circles fade in after the line finishes. | ||||||||||||||||||||||||||||||||||||||||||
| 784 | chart.selectAll("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 785 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 786 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 787 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 788 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 789 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 790 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 791 | .attr("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 792 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 793 | .delay(1500) | ||||||||||||||||||||||||||||||||||||||||||
| 794 | .duration(300) | ||||||||||||||||||||||||||||||||||||||||||
| 795 | .attr("opacity", 1); | ||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 798 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||
| 800 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 801 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 802 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 803 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 804 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 805 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 806 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 807 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 808 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 809 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 812 | } | ||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||
| 814 | =head2 render_pie_chart | ||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||
| 816 | my $html = $chart->render_pie_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 817 | my $html = $chart->render_pie_chart($data, { separator => ':' }); | ||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||
| 819 | Generates HTML and JavaScript code to render a pie chart. | ||||||||||||||||||||||||||||||||||||||||||
| 820 | Each slice is coloured with the ten hex values of C<d3.schemeCategory10>, | ||||||||||||||||||||||||||||||||||||||||||
| 821 | inlined into the page rather than read from D3's own palette (see L</palette>); | ||||||||||||||||||||||||||||||||||||||||||
| 822 | percentage labels appear inside each slice and a colour legend is shown to the | ||||||||||||||||||||||||||||||||||||||||||
| 823 | right of the pie. | ||||||||||||||||||||||||||||||||||||||||||
| 824 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||
| 826 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||
| 828 | =item * C<$data> - An array reference of data points. Each data point is an | ||||||||||||||||||||||||||||||||||||||||||
| 829 | array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||||||||||
| 831 | =item * C<%opts> - Optional hashref of options. | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | =over 8 | ||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||
| 835 | =item * C<separator> (string, default C<'/'>) - Character shown between the | ||||||||||||||||||||||||||||||||||||||||||
| 836 | label and value in the SVG legend. | ||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||
| 838 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||
| 840 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||||||
| 842 | Returns a string containing the complete HTML5 document. | ||||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||
| 844 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||
| 846 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||
| 848 | =item * Throws C<Data is not optional> when C<$data> is C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||
| 850 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||||||||
| 852 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||||||||||||
| 854 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||||||||||
| 856 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||
| 858 | { | ||||||||||||||||||||||||||||||||||||||||||
| 859 | data => { | ||||||||||||||||||||||||||||||||||||||||||
| 860 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||
| 861 | element_type => [ 'string', 'number' ] | ||||||||||||||||||||||||||||||||||||||||||
| 862 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 863 | opts => { type => 'hashref', optional => 1, default => {} }, | ||||||||||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||
| 866 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 867 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 868 | Recognised C<opts> key: C<separator> (string, default C<'/'>) | ||||||||||||||||||||||||||||||||||||||||||
| 869 | - character shown between label and value in the SVG legend. | ||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||
| 871 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||
| 873 | Str -- complete HTML5 document; pie rendered with C<d3.pie()> and | ||||||||||||||||||||||||||||||||||||||||||
| 874 | C<d3.arc()>; slices coloured with category10's hex values, | ||||||||||||||||||||||||||||||||||||||||||
| 875 | inlined; percentage label inside each slice; legend to the right. | ||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||
| 877 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||
| 879 | 18 | ▶ | sub render_pie_chart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_pie_chart: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 880 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 881 | 18 | ▶ | my $first = @_ ? $_[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 882 | 18 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 883 | 18 | ▶ | if (!defined($raw->{data})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 884 | 4 | ▶ | die(defined($first) ? 'Data must be an array of arrays' : 'Data is not optional'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||||||||||||||||
| 886 | Params::Validate::Strict::validate_strict({ | ||||||||||||||||||||||||||||||||||||||||||
| 887 | 14 | args => $raw, | |||||||||||||||||||||||||||||||||||||||||
| 888 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 889 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 890 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 891 | 12 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 892 | 12 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||
| 894 | 12 | ▶ | my $separator = _js_any_string($opts->{separator} // '/'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"separator"} // "/"
MC/DC100%1/1
$opts->{"separator"} // "/"
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
| |||||||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||||||
| 896 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 897 | 12 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 898 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||||||||||
| 900 | 12 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 901 | 12 | $html .= $self->_head(); | |||||||||||||||||||||||||||||||||||||||||
| 902 | 12 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 903 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 904 | 12 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 905 | 12 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 906 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 907 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||
| 909 | const width = $self->{width}; | ||||||||||||||||||||||||||||||||||||||||||
| 910 | const height = $self->{height}; | ||||||||||||||||||||||||||||||||||||||||||
| 911 | const radius = Math.min(width, height) / 2 - 40; | ||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||
| 913 | const color = d3.scaleOrdinal($CATEGORY10_JS); | ||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||
| 915 | const pie = d3.pie() | ||||||||||||||||||||||||||||||||||||||||||
| 916 | .sort(null) | ||||||||||||||||||||||||||||||||||||||||||
| 917 | .value(d => d.value); | ||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||
| 919 | const arc = d3.arc() | ||||||||||||||||||||||||||||||||||||||||||
| 920 | .innerRadius(0) | ||||||||||||||||||||||||||||||||||||||||||
| 921 | .outerRadius(radius); | ||||||||||||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||||||||||||
| 923 | const labelArc = d3.arc() | ||||||||||||||||||||||||||||||||||||||||||
| 924 | .innerRadius(radius * 0.65) | ||||||||||||||||||||||||||||||||||||||||||
| 925 | .outerRadius(radius * 0.65); | ||||||||||||||||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||||||||||||||||
| 927 | const total = d3.sum(data, d => d.value); | ||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||
| 929 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||
| 931 | const pieGroup = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 932 | .attr("transform", `translate(\${width * 0.45},\${height / 2})`); | ||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||
| 934 | const arcs = pieGroup.selectAll(".arc") | ||||||||||||||||||||||||||||||||||||||||||
| 935 | .data(pie(data)) | ||||||||||||||||||||||||||||||||||||||||||
| 936 | .join("g") | ||||||||||||||||||||||||||||||||||||||||||
| 937 | .attr("class", "arc"); | ||||||||||||||||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||||||||||||||||
| 939 | arcs.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 940 | .attr("d", arc) | ||||||||||||||||||||||||||||||||||||||||||
| 941 | .attr("fill", d => color(d.data.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 942 | .attr("stroke", "white") | ||||||||||||||||||||||||||||||||||||||||||
| 943 | .style("stroke-width", "2px"); | ||||||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||||||
| 945 | arcs.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 946 | .attr("transform", d => `translate(\${labelArc.centroid(d)})`) | ||||||||||||||||||||||||||||||||||||||||||
| 947 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 948 | .attr("font-size", "11px") | ||||||||||||||||||||||||||||||||||||||||||
| 949 | .attr("fill", "white") | ||||||||||||||||||||||||||||||||||||||||||
| 950 | .attr("pointer-events", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 951 | .text(d => Math.round(d.data.value / total * 100) + "%"); | ||||||||||||||||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||||||||||||||||
| 953 | // Legend | ||||||||||||||||||||||||||||||||||||||||||
| 954 | const legend = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 955 | .attr("transform", `translate(\${width * 0.72},\${(height - data.length * 22) / 2})`); | ||||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||
| 957 | legend.selectAll("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 958 | .data(pie(data)) | ||||||||||||||||||||||||||||||||||||||||||
| 959 | .join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 960 | .attr("x", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 961 | .attr("y", (d, i) => i * 22) | ||||||||||||||||||||||||||||||||||||||||||
| 962 | .attr("width", 14) | ||||||||||||||||||||||||||||||||||||||||||
| 963 | .attr("height", 14) | ||||||||||||||||||||||||||||||||||||||||||
| 964 | .attr("fill", d => color(d.data.label)); | ||||||||||||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||||||||||||
| 966 | legend.selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 967 | .data(pie(data)) | ||||||||||||||||||||||||||||||||||||||||||
| 968 | .join("text") | ||||||||||||||||||||||||||||||||||||||||||
| 969 | .attr("x", 20) | ||||||||||||||||||||||||||||||||||||||||||
| 970 | .attr("y", (d, i) => i * 22 + 11) | ||||||||||||||||||||||||||||||||||||||||||
| 971 | .attr("font-size", "12px") | ||||||||||||||||||||||||||||||||||||||||||
| 972 | .text(d => `\${d.data.label} $separator \${d.data.value}`); | ||||||||||||||||||||||||||||||||||||||||||
| 973 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 974 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 975 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 976 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||
| 978 | 12 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 979 | } | ||||||||||||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||||||||||||
| 981 | =head2 render_animated_pie_chart | ||||||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||||||
| 983 | my $html = $chart->render_animated_pie_chart($data); | ||||||||||||||||||||||||||||||||||||||||||
| 984 | my $html = $chart->render_animated_pie_chart($data, { separator => ':' }); | ||||||||||||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||||||||||||
| 986 | Generates HTML and JavaScript code to render an animated pie chart where each | ||||||||||||||||||||||||||||||||||||||||||
| 987 | slice fans out from zero angle on page load using C<attrTween> and | ||||||||||||||||||||||||||||||||||||||||||
| 988 | C<d3.interpolate>. Percentage labels fade in once all slices are drawn. | ||||||||||||||||||||||||||||||||||||||||||
| 989 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||||||||||||
| 991 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||||||||||
| 993 | =item * C<$data> - An array reference of data points. Each data point is an | ||||||||||||||||||||||||||||||||||||||||||
| 994 | array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||||||||||||||||
| 996 | =item * C<%opts> - Optional hashref of options. | ||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||
| 998 | =over 8 | ||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||
| 1000 | =item * C<separator> (string, default C<'/'>) - Character shown between the | ||||||||||||||||||||||||||||||||||||||||||
| 1001 | label and value in the SVG legend. | ||||||||||||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||||||||||||
| 1003 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||||||
| 1005 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||||||
| 1007 | Returns a string containing the complete HTML5 document. | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||
| 1009 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||||||||||||||||
| 1011 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1012 | |||||||||||||||||||||||||||||||||||||||||||
| 1013 | =item * Throws C<Data is not optional> when C<$data> is C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||||||||||||
| 1015 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||||||||||||||||
| 1017 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||||||||||||||||
| 1019 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||||||||||||||||
| 1021 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||
| 1023 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | data => { | ||||||||||||||||||||||||||||||||||||||||||
| 1025 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||
| 1026 | element_type => [ 'string', 'number' ] | ||||||||||||||||||||||||||||||||||||||||||
| 1027 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 1028 | opts => { type => 'hashref', optional => 1, default => {} }, | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||||||
| 1031 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 1033 | Recognised C<opts> key: C<separator> (string, default C<'/'>) | ||||||||||||||||||||||||||||||||||||||||||
| 1034 | - character shown between label and value in the SVG legend. | ||||||||||||||||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||||||||||||||||
| 1036 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||||||||||||||||
| 1038 | Str -- complete HTML5 document; slices animate via C<attrTween> with | ||||||||||||||||||||||||||||||||||||||||||
| 1039 | C<d3.interpolate> (1000 ms); percentage labels fade in afterwards. | ||||||||||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||||||||
| 1041 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||||||||||||
| 1043 | 15 | ▶ | sub render_animated_pie_chart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_animated_pie_chart: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1044 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1045 | 15 | ▶ | my $first = @_ ? $_[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1046 | 15 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 1047 | 15 | ▶ | if (!defined($raw->{data})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | 2 | ▶ | die(defined($first) ? 'Data must be an array of arrays' : 'Data is not optional'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1049 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1050 | Params::Validate::Strict::validate_strict({ | ||||||||||||||||||||||||||||||||||||||||||
| 1051 | 13 | args => $raw, | |||||||||||||||||||||||||||||||||||||||||
| 1052 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 1053 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | 11 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1056 | 11 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||||||||||||||||
| 1058 | 11 | ▶ | my $separator = _js_any_string($opts->{separator} // '/'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"separator"} // "/"
MC/DC100%1/1
$opts->{"separator"} // "/"
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||||||||||||||||
| 1060 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 1061 | 11 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 1062 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||||||||||||||||
| 1064 | 11 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 1065 | 11 | $html .= $self->_head(); | |||||||||||||||||||||||||||||||||||||||||
| 1066 | 11 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 1067 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 1068 | 11 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 1069 | 11 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 1070 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 1071 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 1072 | |||||||||||||||||||||||||||||||||||||||||||
| 1073 | const width = $self->{width}; | ||||||||||||||||||||||||||||||||||||||||||
| 1074 | const height = $self->{height}; | ||||||||||||||||||||||||||||||||||||||||||
| 1075 | const radius = Math.min(width, height) / 2 - 40; | ||||||||||||||||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||||||||||||||||
| 1077 | const color = d3.scaleOrdinal($CATEGORY10_JS); | ||||||||||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||||||||||
| 1079 | const pie = d3.pie() | ||||||||||||||||||||||||||||||||||||||||||
| 1080 | .sort(null) | ||||||||||||||||||||||||||||||||||||||||||
| 1081 | .value(d => d.value); | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||||||||||||||||
| 1083 | const arc = d3.arc() | ||||||||||||||||||||||||||||||||||||||||||
| 1084 | .innerRadius(0) | ||||||||||||||||||||||||||||||||||||||||||
| 1085 | .outerRadius(radius); | ||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||
| 1087 | const labelArc = d3.arc() | ||||||||||||||||||||||||||||||||||||||||||
| 1088 | .innerRadius(radius * 0.65) | ||||||||||||||||||||||||||||||||||||||||||
| 1089 | .outerRadius(radius * 0.65); | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||||||||||||||||
| 1091 | const total = d3.sum(data, d => d.value); | ||||||||||||||||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||||||||||||||||
| 1093 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||||||||||||||||
| 1095 | const pieGroup = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1096 | .attr("transform", `translate(\${width * 0.45},\${height / 2})`); | ||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||
| 1098 | const arcs = pieGroup.selectAll(".arc") | ||||||||||||||||||||||||||||||||||||||||||
| 1099 | .data(pie(data)) | ||||||||||||||||||||||||||||||||||||||||||
| 1100 | .join("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | .attr("class", "arc"); | ||||||||||||||||||||||||||||||||||||||||||
| 1102 | |||||||||||||||||||||||||||||||||||||||||||
| 1103 | // Each slice fans out from zero angle using attrTween. | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | arcs.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 1105 | .attr("fill", d => color(d.data.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 1106 | .attr("stroke", "white") | ||||||||||||||||||||||||||||||||||||||||||
| 1107 | .style("stroke-width", "2px") | ||||||||||||||||||||||||||||||||||||||||||
| 1108 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 1109 | .duration(1000) | ||||||||||||||||||||||||||||||||||||||||||
| 1110 | .attrTween("d", function(d) { | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | const i = d3.interpolate({ startAngle: 0, endAngle: 0 }, d); | ||||||||||||||||||||||||||||||||||||||||||
| 1112 | return t => arc(i(t)); | ||||||||||||||||||||||||||||||||||||||||||
| 1113 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||||||||||||||||
| 1115 | // Percentage labels fade in after slices finish drawing. | ||||||||||||||||||||||||||||||||||||||||||
| 1116 | arcs.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1117 | .attr("transform", d => `translate(\${labelArc.centroid(d)})`) | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 1119 | .attr("font-size", "11px") | ||||||||||||||||||||||||||||||||||||||||||
| 1120 | .attr("fill", "white") | ||||||||||||||||||||||||||||||||||||||||||
| 1121 | .attr("pointer-events", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | .attr("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 1123 | .text(d => Math.round(d.data.value / total * 100) + "%") | ||||||||||||||||||||||||||||||||||||||||||
| 1124 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | .delay(1000) | ||||||||||||||||||||||||||||||||||||||||||
| 1126 | .duration(300) | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | .attr("opacity", 1); | ||||||||||||||||||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||||||||||||||||
| 1129 | // Legend | ||||||||||||||||||||||||||||||||||||||||||
| 1130 | const legend = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1131 | .attr("transform", `translate(\${width * 0.72},\${(height - data.length * 22) / 2})`); | ||||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||
| 1133 | legend.selectAll("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 1134 | .data(pie(data)) | ||||||||||||||||||||||||||||||||||||||||||
| 1135 | .join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 1136 | .attr("x", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 1137 | .attr("y", (d, i) => i * 22) | ||||||||||||||||||||||||||||||||||||||||||
| 1138 | .attr("width", 14) | ||||||||||||||||||||||||||||||||||||||||||
| 1139 | .attr("height", 14) | ||||||||||||||||||||||||||||||||||||||||||
| 1140 | .attr("fill", d => color(d.data.label)); | ||||||||||||||||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||||||||||||||||
| 1142 | legend.selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1143 | .data(pie(data)) | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | .join("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | .attr("x", 20) | ||||||||||||||||||||||||||||||||||||||||||
| 1146 | .attr("y", (d, i) => i * 22 + 11) | ||||||||||||||||||||||||||||||||||||||||||
| 1147 | .attr("font-size", "12px") | ||||||||||||||||||||||||||||||||||||||||||
| 1148 | .text(d => `\${d.data.label} $separator \${d.data.value}`); | ||||||||||||||||||||||||||||||||||||||||||
| 1149 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 1150 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 1151 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 1152 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||||||||||
| 1154 | 11 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 1155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||||||||||||||||
| 1157 | =head2 render_pie_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||||||||||||||||
| 1159 | my $fragment = $chart->render_pie_chart_snippet(\@slices); | ||||||||||||||||||||||||||||||||||||||||||
| 1160 | my $fragment = $chart->render_pie_chart_snippet(\@slices, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 1161 | # $fragment->{svg_id} - always 'pie_chart' | ||||||||||||||||||||||||||||||||||||||||||
| 1162 | # $fragment->{html} - embeddable fragment; caller must load D3 v7 | ||||||||||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||||||||||
| 1164 | Generates an embeddable pie or donut chart fragment for use in existing HTML | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | layouts. Returns C<{ svg_id =E<gt> 'pie_chart', html =E<gt> Str }>. The | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | caller is responsible for loading D3 v7 before embedding the fragment. | ||||||||||||||||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||||||||||||||||
| 1168 | =head3 Data format | ||||||||||||||||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||||||||||||||||
| 1170 | Each element of C<\@slices> is C<[$label, $value]> or C<[$label, $value, \%extra]>. | ||||||||||||||||||||||||||||||||||||||||||
| 1171 | Negative values are silently converted to their absolute value. Zero-value | ||||||||||||||||||||||||||||||||||||||||||
| 1172 | slices are silently omitted. C<\%extra> key/value pairs are shown as | ||||||||||||||||||||||||||||||||||||||||||
| 1173 | additional rows in the hover tooltip. | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||||||||||||||||
| 1175 | =head3 Options (C<\%opts>) | ||||||||||||||||||||||||||||||||||||||||||
| 1176 | |||||||||||||||||||||||||||||||||||||||||||
| 1177 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1178 | |||||||||||||||||||||||||||||||||||||||||||
| 1179 | =item * C<animated> (bool, default 0) - fan slices in from arc-length 0 on | ||||||||||||||||||||||||||||||||||||||||||
| 1180 | first render using C<attrTween> / C<d3.easeBackOut> (800 ms, staggered). | ||||||||||||||||||||||||||||||||||||||||||
| 1181 | Respects C<prefers-reduced-motion>. | ||||||||||||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||||||||||||
| 1183 | =item * C<donut> (bool, default 0) - render as a donut chart (inner radius | ||||||||||||||||||||||||||||||||||||||||||
| 1184 | 38% of outer radius); the total sum appears in the centre hole. | ||||||||||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||||||||||
| 1186 | =item * C<sort_slices> (string, default C<'none'>) - C<'value'> for | ||||||||||||||||||||||||||||||||||||||||||
| 1187 | largest-first, C<'label'> for alphabetical, C<'none'> for input order. | ||||||||||||||||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||||||||||||||||
| 1189 | =item * C<max_slices> (int, default 0) - when E<gt> 0, only the top N-1 | ||||||||||||||||||||||||||||||||||||||||||
| 1190 | slices are shown individually; the rest are collapsed into an C<"Other"> slice. | ||||||||||||||||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||||||||||||||||
| 1192 | =item * C<legend> (bool, default 1) - render an HTML legend panel beside the chart. | ||||||||||||||||||||||||||||||||||||||||||
| 1193 | |||||||||||||||||||||||||||||||||||||||||||
| 1194 | =item * C<color_scheme> (string, default C<'tableau10'>) - D3 categorical | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | colour scheme. Supported: C<tableau10>, C<category10>, C<set2>, C<set3>, | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | C<paired> - the names C<categorical_schemes()> returns, whose colours C<palette()> | ||||||||||||||||||||||||||||||||||||||||||
| 1197 | makes available to Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||||||||||||||||
| 1199 | =item * C<separator> (string, default C<'/'>) - Character shown between the | ||||||||||||||||||||||||||||||||||||||||||
| 1200 | label and value in each legend entry (e.g. C<'/'> produces | ||||||||||||||||||||||||||||||||||||||||||
| 1201 | C<Label / 12.34 (42.0%)>, C<':'> produces C<Label : 12.34 (42.0%)>). | ||||||||||||||||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||||||||||||||||
| 1203 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||||||||||||||||
| 1205 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||
| 1207 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||||||||||||
| 1209 | =item * Throws C<Data must be an array of arrays> when C<\@slices> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 1210 | |||||||||||||||||||||||||||||||||||||||||||
| 1211 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||||||||||||||||
| 1213 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 1214 | |||||||||||||||||||||||||||||||||||||||||||
| 1215 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 1216 | |||||||||||||||||||||||||||||||||||||||||||
| 1217 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1218 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 1219 | opts => { type => 'hashref', optional => 1, default => {} }, | ||||||||||||||||||||||||||||||||||||||||||
| 1220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||||||||||||||||
| 1222 | Each element of C<$data> is C<[ Str, Num ]> or C<[ Str, Num, HashRef ]>; | ||||||||||||||||||||||||||||||||||||||||||
| 1223 | passing C<undef> or a non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | Recognised C<opts> keys: C<animated> (boolean, default C<0>), | ||||||||||||||||||||||||||||||||||||||||||
| 1225 | C<donut> (boolean, default C<0>), C<sort_slices> (string: C<'value'>, | ||||||||||||||||||||||||||||||||||||||||||
| 1226 | C<'label'>, or C<'none'>; default C<'none'>), C<max_slices> (integer, | ||||||||||||||||||||||||||||||||||||||||||
| 1227 | default C<0>), C<legend> (boolean, default C<1>), | ||||||||||||||||||||||||||||||||||||||||||
| 1228 | C<color_scheme> (string, default C<'tableau10'>), | ||||||||||||||||||||||||||||||||||||||||||
| 1229 | C<separator> (string, default C<'/'> - shown between label and value in | ||||||||||||||||||||||||||||||||||||||||||
| 1230 | legend entries). | ||||||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||||||
| 1232 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||||||
| 1234 | HashRef -- C<{ svg_id =E<gt> 'pie_chart', html =E<gt> Str }>; | ||||||||||||||||||||||||||||||||||||||||||
| 1235 | embeddable fragment; no DOCTYPE, no page shell, no D3 CDN tag. | ||||||||||||||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||||||||||||||
| 1237 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||||||||||||||||
| 1239 | 66 | ▶ | sub render_pie_chart_snippet { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_pie_chart_snippet: called CC=30 SCAR=34.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1240 | 66 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1241 | 66 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 1242 | 66 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1243 | 63 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 1244 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 1245 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 1246 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 1247 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1248 | 63 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1249 | 63 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1250 | |||||||||||||||||||||||||||||||||||||||||||
| 1251 | 63 | ▶ | my $animated = $opts->{animated} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1252 | 63 | ▶ | my $donut = $opts->{donut} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1253 | 63 | ▶ | my $sort_slices = $opts->{sort_slices} // 'none'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"sort_slices"} // "none"
MC/DC100%1/1
$opts->{"sort_slices"} // "none"
$opts->{"sort_slices"}
Truth table100%2/2
A
A= $opts->{"sort_slices"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1254 | 63 | ▶ | my $max_slices = int($opts->{max_slices} // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"max_slices"} // 0
MC/DC100%1/1
$opts->{"max_slices"} // 0
$opts->{"max_slices"}
Truth table100%2/2
A
A= $opts->{"max_slices"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1255 | 63 | ▶ | my $show_legend = exists $opts->{legend} ? ($opts->{legend} ? 1 : 0) : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1256 | 63 | my $color_scheme = _scheme_name($opts->{color_scheme}); | |||||||||||||||||||||||||||||||||||||||||
| 1257 | 63 | ▶ | my $separator = _js_any_string($opts->{separator} // '/'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"separator"} // "/"
MC/DC100%1/1
$opts->{"separator"} // "/"
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1258 | 63 | my $schemes_js = $self->_schemes_js('const'); | |||||||||||||||||||||||||||||||||||||||||
| 1259 | 63 | my $default_js = _default_scheme_js(); | |||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||
| 1261 | # Normalise: absolute values, drop zeros, pull optional extra hashref | ||||||||||||||||||||||||||||||||||||||||||
| 1262 | 63 | my @slices; | |||||||||||||||||||||||||||||||||||||||||
| 1263 | 63 | for my $pt (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 1264 | 168 | ▶ | my $val = abs($pt->[1] // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pt->[1] // 0
MC/DC0%0/1
$pt->[1] // 0
$pt->[1]
Truth table50%1/2
A
A= $pt->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 1265 | 168 | ▶ | next if $val == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1266 | 165 | my %s = (label => $pt->[0], value => $val); | |||||||||||||||||||||||||||||||||||||||||
| 1267 | 165 | ▶ | $s{extra} = $pt->[2] if ref($pt->[2]) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1268 | 165 | push @slices, \%s; | |||||||||||||||||||||||||||||||||||||||||
| 1269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1270 | |||||||||||||||||||||||||||||||||||||||||||
| 1271 | # Sort before max_slices so the "top N" is stable | ||||||||||||||||||||||||||||||||||||||||||
| 1272 | 63 | ▶ | if ($sort_slices eq 'value') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1273 | 1 | @slices = sort { $b->{value} <=> $a->{value} } @slices; | |||||||||||||||||||||||||||||||||||||||||
| 1274 | } elsif ($sort_slices eq 'label') { | ||||||||||||||||||||||||||||||||||||||||||
| 1275 | 0 | @slices = sort { $a->{label} cmp $b->{label} } @slices; | |||||||||||||||||||||||||||||||||||||||||
| 1276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||||||||||||||||
| 1278 | # Collapse tail into "Other" | ||||||||||||||||||||||||||||||||||||||||||
| 1279 | 63 | ▶ | if ($max_slices >= 2 && scalar(@slices) > $max_slices) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$max_slices >= 2 and scalar @slices > $max_slices
MC/DC50%1/2
$max_slices >= 2 and scalar @slices > $max_slices
$max_slices >= 2
scalar @slices > $max_slices
Truth table66%2/3
A and B
A= $max_slices >= 2B= scalar @slices > $max_slices
| |||||||||||||||||||||||||||||||||||||||||||
| 1280 | 2 | my @by_val = sort { $b->{value} <=> $a->{value} } @slices; | |||||||||||||||||||||||||||||||||||||||||
| 1281 | 2 | my @top = @by_val[0 .. $max_slices - 2]; | |||||||||||||||||||||||||||||||||||||||||
| 1282 | 2 | my $other = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1283 | 2 | $other += $_->{value} for @by_val[$max_slices - 1 .. $#by_val]; | |||||||||||||||||||||||||||||||||||||||||
| 1284 | 2 | push @top, { label => 'Other', value => $other }; | |||||||||||||||||||||||||||||||||||||||||
| 1285 | 2 | @slices = @top; | |||||||||||||||||||||||||||||||||||||||||
| 1286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1287 | |||||||||||||||||||||||||||||||||||||||||||
| 1288 | 63 | my $json_data = _script_json(\@slices); | |||||||||||||||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||||||||||||||||
| 1290 | 63 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'pie_chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "pie_chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "pie_chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 1291 | 63 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 1292 | 63 | my $leg_id = $svg_id . '_legend'; | |||||||||||||||||||||||||||||||||||||||||
| 1293 | 63 | my $wrap_id = $svg_id . '_wrap'; | |||||||||||||||||||||||||||||||||||||||||
| 1294 | 63 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 1295 | 63 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 1296 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 1297 | 63 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1298 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||
| 1300 | 63 | ▶ | my $inner_radius_js = $donut ? 'radius * 0.38' : '0'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||
| 1302 | # Animation initialisation (empty when not animated) | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | 63 | ▶ | my $anim_init_js = $animated | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1304 | ? 'var noAnim = window.matchMedia("(prefers-reduced-motion: reduce)").matches;' . "\n" . | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | " let initialDrawDone = false;\n" | ||||||||||||||||||||||||||||||||||||||||||
| 1306 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||
| 1308 | # Legend fade-in as part of the animation block | ||||||||||||||||||||||||||||||||||||||||||
| 1309 | 63 | ▶ | my $legend_anim_js = ($animated && $show_legend) ? <<"LANIM" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$animated and $show_legend
MC/DC50%1/2
$animated and $show_legend
$animated
$show_legend
Truth table66%2/3
A and B
A= $animatedB= $show_legend
| |||||||||||||||||||||||||||||||||||||||||||
| 1310 | d3.select("#$leg_id") | ||||||||||||||||||||||||||||||||||||||||||
| 1311 | .style("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | .delay(data.length * 60 + 100) | ||||||||||||||||||||||||||||||||||||||||||
| 1314 | .duration(300) | ||||||||||||||||||||||||||||||||||||||||||
| 1315 | .style("opacity", 1); | ||||||||||||||||||||||||||||||||||||||||||
| 1316 | LANIM | ||||||||||||||||||||||||||||||||||||||||||
| 1317 | |||||||||||||||||||||||||||||||||||||||||||
| 1318 | # Path drawing code — animated vs plain | ||||||||||||||||||||||||||||||||||||||||||
| 1319 | 63 | my $anim_draw_js; | |||||||||||||||||||||||||||||||||||||||||
| 1320 | 63 | ▶ | if ($animated) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1321 | 4 | $anim_draw_js = <<"ANIM_DRAW"; | |||||||||||||||||||||||||||||||||||||||||
| 1322 | if (!initialDrawDone && !noAnim) { | ||||||||||||||||||||||||||||||||||||||||||
| 1323 | paths.attr("d", arc({ startAngle: 0, endAngle: 0 })) | ||||||||||||||||||||||||||||||||||||||||||
| 1324 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | .duration(800) | ||||||||||||||||||||||||||||||||||||||||||
| 1326 | .ease(d3.easeBackOut.overshoot(1.2)) | ||||||||||||||||||||||||||||||||||||||||||
| 1327 | .delay((d, i) => Math.min(i * 60, 300)) | ||||||||||||||||||||||||||||||||||||||||||
| 1328 | .attrTween("d", function(d) { | ||||||||||||||||||||||||||||||||||||||||||
| 1329 | var interp = d3.interpolate({ startAngle: 0, endAngle: 0 }, d); | ||||||||||||||||||||||||||||||||||||||||||
| 1330 | return function(t) { return arc(interp(t)); }; | ||||||||||||||||||||||||||||||||||||||||||
| 1331 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1332 | $legend_anim_js initialDrawDone = true; | ||||||||||||||||||||||||||||||||||||||||||
| 1333 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1334 | paths.attr("d", arc); | ||||||||||||||||||||||||||||||||||||||||||
| 1335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1336 | ANIM_DRAW | ||||||||||||||||||||||||||||||||||||||||||
| 1337 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1338 | 59 | $anim_draw_js = " paths.attr(\"d\", arc);\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||||||||||||||||
| 1341 | # Centre label in donut hole | ||||||||||||||||||||||||||||||||||||||||||
| 1342 | 63 | ▶ | my $donut_center_js = $donut ? <<'DONUT' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1343 | pieGroup.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1344 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | .attr("dominant-baseline", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 1346 | .attr("font-size", "16px") | ||||||||||||||||||||||||||||||||||||||||||
| 1347 | .attr("font-weight", "bold") | ||||||||||||||||||||||||||||||||||||||||||
| 1348 | .text(fmt(total)); | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | DONUT | ||||||||||||||||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||||||||||||||||
| 1351 | # HTML legend panel — built by D3 against the legend div | ||||||||||||||||||||||||||||||||||||||||||
| 1352 | # Single-quote heredoc so JS template-literal ${...} is preserved verbatim | ||||||||||||||||||||||||||||||||||||||||||
| 1353 | 63 | ▶ | my $legend_section_js = $show_legend ? <<"LEGEND_JS" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1354 | d3.select("#$leg_id") | ||||||||||||||||||||||||||||||||||||||||||
| 1355 | .selectAll(".bi-pie-legend-entry") | ||||||||||||||||||||||||||||||||||||||||||
| 1356 | .data(pieSlices) | ||||||||||||||||||||||||||||||||||||||||||
| 1357 | .join("div") | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | .attr("class", "bi-pie-legend-entry") | ||||||||||||||||||||||||||||||||||||||||||
| 1359 | .attr("data-slice-index", (d, i) => i) | ||||||||||||||||||||||||||||||||||||||||||
| 1360 | .html((d, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | const pct = (d.data.value / total * 100).toFixed(1); | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | const sw = '<span class="bi-pie-swatch" style="background:' + color(d.data.label) + '"></span>'; | ||||||||||||||||||||||||||||||||||||||||||
| 1363 | // The entry is markup, for the swatch, so the label and the | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | // separator are escaped: both are the caller's text. | ||||||||||||||||||||||||||||||||||||||||||
| 1365 | return sw + ' ' + esc(d.data.label) + ' ' + esc('$separator') + ' ' + fmt(d.data.value) + ' (' + pct + '%)'; | ||||||||||||||||||||||||||||||||||||||||||
| 1366 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1367 | LEGEND_JS | ||||||||||||||||||||||||||||||||||||||||||
| 1368 | |||||||||||||||||||||||||||||||||||||||||||
| 1369 | 63 | ▶ | my $legend_div_html = $show_legend | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1370 | ? qq( <div id="$leg_id" class="bi-pie-legend"></div>\n) | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 1372 | |||||||||||||||||||||||||||||||||||||||||||
| 1373 | 63 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 1374 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 1375 | #$wrap_id { | ||||||||||||||||||||||||||||||||||||||||||
| 1376 | display: flex; | ||||||||||||||||||||||||||||||||||||||||||
| 1377 | flex-wrap: wrap; | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | align-items: center; | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1380 | .bi-pie-legend { | ||||||||||||||||||||||||||||||||||||||||||
| 1381 | padding-left: 16px; | ||||||||||||||||||||||||||||||||||||||||||
| 1382 | font-size: 13px; | ||||||||||||||||||||||||||||||||||||||||||
| 1383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | .bi-pie-legend-entry { | ||||||||||||||||||||||||||||||||||||||||||
| 1385 | margin: 4px 0; | ||||||||||||||||||||||||||||||||||||||||||
| 1386 | white-space: nowrap; | ||||||||||||||||||||||||||||||||||||||||||
| 1387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1388 | .bi-pie-swatch { | ||||||||||||||||||||||||||||||||||||||||||
| 1389 | display: inline-block; | ||||||||||||||||||||||||||||||||||||||||||
| 1390 | width: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 1391 | height: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 1392 | border-radius: 2px; | ||||||||||||||||||||||||||||||||||||||||||
| 1393 | vertical-align: middle; | ||||||||||||||||||||||||||||||||||||||||||
| 1394 | margin-right: 4px; | ||||||||||||||||||||||||||||||||||||||||||
| 1395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1396 | .bi-pie-tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 1397 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 1398 | background: rgba(255,255,255,0.95); | ||||||||||||||||||||||||||||||||||||||||||
| 1399 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 1400 | border-radius: 4px; | ||||||||||||||||||||||||||||||||||||||||||
| 1401 | padding: 6px 10px; | ||||||||||||||||||||||||||||||||||||||||||
| 1402 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 1403 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 1404 | display: none; | ||||||||||||||||||||||||||||||||||||||||||
| 1405 | line-height: 1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1407 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 1408 | <div id="$wrap_id"> | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | $legend_div_html</div> | ||||||||||||||||||||||||||||||||||||||||||
| 1411 | <div class="bi-pie-tooltip" id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 1412 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 1413 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||||||||||||||||
| 1415 | function esc(s) { | ||||||||||||||||||||||||||||||||||||||||||
| 1416 | return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||||||||||||||||||||||||||||||||||||||||||
| 1417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||||||||||||||||
| 1419 | $schemes_js | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | const color = d3.scaleOrdinal(SCHEMES['$color_scheme'] || $default_js) | ||||||||||||||||||||||||||||||||||||||||||
| 1421 | .domain(data.map(d => d.label)); | ||||||||||||||||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||||||||||||||||
| 1423 | const width = $width; | ||||||||||||||||||||||||||||||||||||||||||
| 1424 | const height = $height; | ||||||||||||||||||||||||||||||||||||||||||
| 1425 | const radius = Math.min(width, height) / 2 - 40; | ||||||||||||||||||||||||||||||||||||||||||
| 1426 | const innerRadius = $inner_radius_js; | ||||||||||||||||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||||||||||||||||
| 1428 | const arc = d3.arc().innerRadius(innerRadius).outerRadius(radius); | ||||||||||||||||||||||||||||||||||||||||||
| 1429 | const labelArc = d3.arc().innerRadius(radius * 0.7).outerRadius(radius * 0.7); | ||||||||||||||||||||||||||||||||||||||||||
| 1430 | |||||||||||||||||||||||||||||||||||||||||||
| 1431 | const pie = d3.pie().sort(null).value(d => d.value); | ||||||||||||||||||||||||||||||||||||||||||
| 1432 | const pieSlices = pie(data); | ||||||||||||||||||||||||||||||||||||||||||
| 1433 | |||||||||||||||||||||||||||||||||||||||||||
| 1434 | const total = d3.sum(data, d => d.value); | ||||||||||||||||||||||||||||||||||||||||||
| 1435 | const fmt = d3.format(",.2f"); | ||||||||||||||||||||||||||||||||||||||||||
| 1436 | |||||||||||||||||||||||||||||||||||||||||||
| 1437 | $anim_init_js | ||||||||||||||||||||||||||||||||||||||||||
| 1438 | const svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 1439 | const tooltip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||||||||||||||||
| 1441 | const pieGroup = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1442 | .attr("transform", \`translate(\${$width * 0.5},\${$height / 2})\`); | ||||||||||||||||||||||||||||||||||||||||||
| 1443 | |||||||||||||||||||||||||||||||||||||||||||
| 1444 | const arcs = pieGroup.selectAll(".arc") | ||||||||||||||||||||||||||||||||||||||||||
| 1445 | .data(pieSlices) | ||||||||||||||||||||||||||||||||||||||||||
| 1446 | .join("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1447 | .attr("class", "arc"); | ||||||||||||||||||||||||||||||||||||||||||
| 1448 | |||||||||||||||||||||||||||||||||||||||||||
| 1449 | const paths = arcs.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 1450 | .attr("fill", d => color(d.data.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 1451 | .attr("stroke", "white") | ||||||||||||||||||||||||||||||||||||||||||
| 1452 | .style("stroke-width", "2px") | ||||||||||||||||||||||||||||||||||||||||||
| 1453 | .attr("data-label", d => d.data.label) | ||||||||||||||||||||||||||||||||||||||||||
| 1454 | .attr("data-value", d => d.data.value) | ||||||||||||||||||||||||||||||||||||||||||
| 1455 | .on("mouseover", function(event, d) { | ||||||||||||||||||||||||||||||||||||||||||
| 1456 | const pct = (d.data.value / total * 100).toFixed(1); | ||||||||||||||||||||||||||||||||||||||||||
| 1457 | let tip = "<b>" + esc(d.data.label) + "<\\/b><br>" + | ||||||||||||||||||||||||||||||||||||||||||
| 1458 | fmt(d.data.value) + " (" + pct + "%)"; | ||||||||||||||||||||||||||||||||||||||||||
| 1459 | if (d.data.extra) { | ||||||||||||||||||||||||||||||||||||||||||
| 1460 | Object.entries(d.data.extra).forEach(([k, v]) => { | ||||||||||||||||||||||||||||||||||||||||||
| 1461 | tip += "<br>" + esc(k) + ": " + esc(v); | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1463 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1464 | tooltip.html(tip) | ||||||||||||||||||||||||||||||||||||||||||
| 1465 | .style("display", "block") | ||||||||||||||||||||||||||||||||||||||||||
| 1466 | .style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 1467 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 1468 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 1469 | .on("mousemove", function(event) { | ||||||||||||||||||||||||||||||||||||||||||
| 1470 | tooltip.style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 1471 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 1472 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 1473 | .on("mouseout", function() { | ||||||||||||||||||||||||||||||||||||||||||
| 1474 | tooltip.style("display", "none"); | ||||||||||||||||||||||||||||||||||||||||||
| 1475 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||||||||||||||||
| 1477 | $anim_draw_js | ||||||||||||||||||||||||||||||||||||||||||
| 1478 | $donut_center_js | ||||||||||||||||||||||||||||||||||||||||||
| 1479 | $legend_section_js</script> | ||||||||||||||||||||||||||||||||||||||||||
| 1480 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 1481 | |||||||||||||||||||||||||||||||||||||||||||
| 1482 | 63 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 1483 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1484 | |||||||||||||||||||||||||||||||||||||||||||
| 1485 | =head2 render_heatmap_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 1486 | |||||||||||||||||||||||||||||||||||||||||||
| 1487 | my $fragment = $chart->render_heatmap_snippet(\@triples); | ||||||||||||||||||||||||||||||||||||||||||
| 1488 | my $fragment = $chart->render_heatmap_snippet(\@triples, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 1489 | # $fragment->{svg_id} - always 'heatmap' | ||||||||||||||||||||||||||||||||||||||||||
| 1490 | # $fragment->{html} - embeddable fragment; caller must load D3 v7 | ||||||||||||||||||||||||||||||||||||||||||
| 1491 | |||||||||||||||||||||||||||||||||||||||||||
| 1492 | Generates an embeddable grid heatmap for use in existing HTML layouts. | ||||||||||||||||||||||||||||||||||||||||||
| 1493 | Each cell sits at the intersection of an X-axis label and a Y-axis label; | ||||||||||||||||||||||||||||||||||||||||||
| 1494 | its colour encodes the cell's numeric value using a sequential D3 colour | ||||||||||||||||||||||||||||||||||||||||||
| 1495 | scale. Returns C<{ svg_id =E<gt> 'heatmap', html =E<gt> Str }>. The | ||||||||||||||||||||||||||||||||||||||||||
| 1496 | caller is responsible for loading D3 v7 before embedding the fragment. | ||||||||||||||||||||||||||||||||||||||||||
| 1497 | |||||||||||||||||||||||||||||||||||||||||||
| 1498 | =head3 Data format | ||||||||||||||||||||||||||||||||||||||||||
| 1499 | |||||||||||||||||||||||||||||||||||||||||||
| 1500 | Each element of C<\@triples> is C<[$x_label, $y_label, $value]>. | ||||||||||||||||||||||||||||||||||||||||||
| 1501 | C<$value> must be numeric or C<undef> (C<undef> rows are silently | ||||||||||||||||||||||||||||||||||||||||||
| 1502 | skipped). Zero is a valid value and maps to the lightest cell colour. | ||||||||||||||||||||||||||||||||||||||||||
| 1503 | The caller is responsible for any aggregation: if multiple triples share | ||||||||||||||||||||||||||||||||||||||||||
| 1504 | the same (x_label, y_label) pair, the last one wins. | ||||||||||||||||||||||||||||||||||||||||||
| 1505 | |||||||||||||||||||||||||||||||||||||||||||
| 1506 | =head3 Options (C<\%opts>) | ||||||||||||||||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||||||||||||||||
| 1508 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1509 | |||||||||||||||||||||||||||||||||||||||||||
| 1510 | =item * C<color_scheme> (string, default C<'YlOrRd'>) - D3 sequential | ||||||||||||||||||||||||||||||||||||||||||
| 1511 | colour scheme. Supported: C<YlOrRd>, C<Blues>, C<Greens>, C<Purples>, | ||||||||||||||||||||||||||||||||||||||||||
| 1512 | C<RdPu>, C<YlGnBu>. | ||||||||||||||||||||||||||||||||||||||||||
| 1513 | |||||||||||||||||||||||||||||||||||||||||||
| 1514 | =item * C<x_label> (string, default C<''>) - Axis title below the X axis. | ||||||||||||||||||||||||||||||||||||||||||
| 1515 | The value is JavaScript-escaped (backslash, double-quote, newline, carriage | ||||||||||||||||||||||||||||||||||||||||||
| 1516 | return, C<E<lt>>) before being embedded in the page; other characters are taken | ||||||||||||||||||||||||||||||||||||||||||
| 1517 | literally. | ||||||||||||||||||||||||||||||||||||||||||
| 1518 | |||||||||||||||||||||||||||||||||||||||||||
| 1519 | =item * C<y_label> (string, default C<''>) - Axis title left of the Y axis. | ||||||||||||||||||||||||||||||||||||||||||
| 1520 | Same JS-escaping as C<x_label> applies. | ||||||||||||||||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||||||||||||||||
| 1522 | =item * C<val_label> (string, default C<'Value'>) - Tooltip value label. | ||||||||||||||||||||||||||||||||||||||||||
| 1523 | Same JS-escaping as C<x_label> applies. | ||||||||||||||||||||||||||||||||||||||||||
| 1524 | |||||||||||||||||||||||||||||||||||||||||||
| 1525 | =item * C<show_values> (bool, default 0) - Print value inside each cell. | ||||||||||||||||||||||||||||||||||||||||||
| 1526 | Auto-suppressed when any cell is narrower than 28 px. | ||||||||||||||||||||||||||||||||||||||||||
| 1527 | |||||||||||||||||||||||||||||||||||||||||||
| 1528 | =item * C<cell_padding> (int 0-8, default 2) - Gap in pixels between cells. | ||||||||||||||||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||||||||||||||||
| 1530 | =item * C<legend> (bool, default 1) - Render a colour-scale legend bar. | ||||||||||||||||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||||||||||||||||
| 1532 | =item * C<animated> (bool, default 0) - Fade cells in on first load. | ||||||||||||||||||||||||||||||||||||||||||
| 1533 | Respects C<prefers-reduced-motion>. | ||||||||||||||||||||||||||||||||||||||||||
| 1534 | |||||||||||||||||||||||||||||||||||||||||||
| 1535 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1536 | |||||||||||||||||||||||||||||||||||||||||||
| 1537 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 1538 | |||||||||||||||||||||||||||||||||||||||||||
| 1539 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1540 | |||||||||||||||||||||||||||||||||||||||||||
| 1541 | =item * Dies with C<Data must be an array of arrays> when C<\@triples> | ||||||||||||||||||||||||||||||||||||||||||
| 1542 | is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 1543 | |||||||||||||||||||||||||||||||||||||||||||
| 1544 | =item * Dies with C<Each data point must be an array reference> when a | ||||||||||||||||||||||||||||||||||||||||||
| 1545 | triple element is not an arrayref. | ||||||||||||||||||||||||||||||||||||||||||
| 1546 | |||||||||||||||||||||||||||||||||||||||||||
| 1547 | =item * Dies with C<Each data point must have at least 3 elements> when | ||||||||||||||||||||||||||||||||||||||||||
| 1548 | a triple has fewer than 3 elements. | ||||||||||||||||||||||||||||||||||||||||||
| 1549 | |||||||||||||||||||||||||||||||||||||||||||
| 1550 | =item * Dies with C<Value must be numeric> when C<$value> is defined but | ||||||||||||||||||||||||||||||||||||||||||
| 1551 | not numeric. | ||||||||||||||||||||||||||||||||||||||||||
| 1552 | |||||||||||||||||||||||||||||||||||||||||||
| 1553 | =item * Dies with C<Unknown color_scheme: E<lt>nameE<gt>> for an | ||||||||||||||||||||||||||||||||||||||||||
| 1554 | unsupported C<color_scheme> value. | ||||||||||||||||||||||||||||||||||||||||||
| 1555 | |||||||||||||||||||||||||||||||||||||||||||
| 1556 | =item * Dies with C<cell_padding must be between 0 and 8> when | ||||||||||||||||||||||||||||||||||||||||||
| 1557 | C<cell_padding> is outside the valid range. | ||||||||||||||||||||||||||||||||||||||||||
| 1558 | |||||||||||||||||||||||||||||||||||||||||||
| 1559 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1560 | |||||||||||||||||||||||||||||||||||||||||||
| 1561 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||
| 1562 | |||||||||||||||||||||||||||||||||||||||||||
| 1563 | Appends a tooltip C<div> to the page when the fragment is rendered in | ||||||||||||||||||||||||||||||||||||||||||
| 1564 | the browser. | ||||||||||||||||||||||||||||||||||||||||||
| 1565 | |||||||||||||||||||||||||||||||||||||||||||
| 1566 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 1567 | |||||||||||||||||||||||||||||||||||||||||||
| 1568 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 1569 | |||||||||||||||||||||||||||||||||||||||||||
| 1570 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1571 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 1572 | opts => { type => 'hashref', optional => 1, default => {} }, | ||||||||||||||||||||||||||||||||||||||||||
| 1573 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1574 | |||||||||||||||||||||||||||||||||||||||||||
| 1575 | Each element of C<$data> is C<[ Str, Str, Num|undef ]>. | ||||||||||||||||||||||||||||||||||||||||||
| 1576 | Recognised C<opts> keys: C<color_scheme> (string, default C<'YlOrRd'>), | ||||||||||||||||||||||||||||||||||||||||||
| 1577 | C<x_label> (string, default C<''>), C<y_label> (string, default C<''>), | ||||||||||||||||||||||||||||||||||||||||||
| 1578 | C<val_label> (string, default C<'Value'>), C<show_values> (boolean, | ||||||||||||||||||||||||||||||||||||||||||
| 1579 | default C<0>), C<cell_padding> (integer 0-8, default C<2>), | ||||||||||||||||||||||||||||||||||||||||||
| 1580 | C<legend> (boolean, default C<1>), C<animated> (boolean, default C<0>). | ||||||||||||||||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||||||||||||||||
| 1582 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 1583 | |||||||||||||||||||||||||||||||||||||||||||
| 1584 | HashRef -- C<{ svg_id =E<gt> 'heatmap', html =E<gt> Str }>; | ||||||||||||||||||||||||||||||||||||||||||
| 1585 | embeddable fragment; no DOCTYPE, no page shell, no D3 CDN tag. | ||||||||||||||||||||||||||||||||||||||||||
| 1586 | |||||||||||||||||||||||||||||||||||||||||||
| 1587 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1588 | |||||||||||||||||||||||||||||||||||||||||||
| 1589 | 84 | ▶ | sub render_heatmap_snippet { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_heatmap_snippet: called CC=28 SCAR=33.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1590 | 84 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1591 | 84 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 1592 | 84 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1593 | 81 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 1594 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 1595 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 1596 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 1597 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1598 | 81 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1599 | 81 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1600 | |||||||||||||||||||||||||||||||||||||||||||
| 1601 | 81 | ▶ | my $color_scheme = $opts->{color_scheme} // 'YlOrRd'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"color_scheme"} // "YlOrRd"
MC/DC100%1/1
$opts->{"color_scheme"} // "YlOrRd"
$opts->{"color_scheme"}
Truth table100%2/2
A
A= $opts->{"color_scheme"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1602 | die "Unknown color_scheme: $color_scheme" | ||||||||||||||||||||||||||||||||||||||||||
| 1603 | 81 | ▶ | unless exists $SEQUENTIAL_SCHEMES{$color_scheme}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1604 | 78 | my $d3_interpolator = $SEQUENTIAL_SCHEMES{$color_scheme}; | |||||||||||||||||||||||||||||||||||||||||
| 1605 | |||||||||||||||||||||||||||||||||||||||||||
| 1606 | 78 | ▶ | my $cell_padding = defined($opts->{cell_padding}) ? int($opts->{cell_padding}) : 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1607 | 78 | ▶ | die 'cell_padding must be between 0 and 8' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cell_padding >= 0 and $cell_padding <= 8
MC/DC100%2/2
$cell_padding >= 0 and $cell_padding <= 8
$cell_padding >= 0
$cell_padding <= 8
Truth table100%3/3
A and B
A= $cell_padding >= 0B= $cell_padding <= 8
| |||||||||||||||||||||||||||||||||||||||||||
| 1608 | unless $cell_padding >= 0 && $cell_padding <= 8; | ||||||||||||||||||||||||||||||||||||||||||
| 1609 | |||||||||||||||||||||||||||||||||||||||||||
| 1610 | 73 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1611 | 73 | ▶ | my $y_label = $opts->{y_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"y_label"} // ""
MC/DC100%1/1
$opts->{"y_label"} // ""
$opts->{"y_label"}
Truth table100%2/2
A
A= $opts->{"y_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1612 | 73 | ▶ | my $val_label = $opts->{val_label} // 'Value'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"val_label"} // "Value"
MC/DC100%1/1
$opts->{"val_label"} // "Value"
$opts->{"val_label"}
Truth table100%2/2
A
A= $opts->{"val_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1613 | 73 | ▶ | my $show_values = $opts->{show_values} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1614 | 73 | ▶ | my $legend = exists $opts->{legend} ? ($opts->{legend} ? 1 : 0) : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1615 | 73 | ▶ | my $animated = $opts->{animated} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1616 | |||||||||||||||||||||||||||||||||||||||||||
| 1617 | 73 | my @triples; | |||||||||||||||||||||||||||||||||||||||||
| 1618 | 73 | for my $pt (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 1619 | 191 | ▶ | die 'Each data point must be an array reference' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1620 | unless ref($pt) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 1621 | 188 | ▶ | die 'Each data point must have at least 3 elements' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1622 | unless scalar(@$pt) >= 3; | ||||||||||||||||||||||||||||||||||||||||||
| 1623 | 183 | ▶ | next unless defined $pt->[2]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1624 | 180 | ▶ | die 'Value must be numeric' unless looks_like_number($pt->[2]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1625 | 177 | push @triples, { x => $pt->[0], y => $pt->[1], v => $pt->[2] + 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 1626 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1627 | |||||||||||||||||||||||||||||||||||||||||||
| 1628 | 62 | my $json_data = _script_json(\@triples); | |||||||||||||||||||||||||||||||||||||||||
| 1629 | |||||||||||||||||||||||||||||||||||||||||||
| 1630 | # encode_json only accepts refs; escape label strings manually for JS | ||||||||||||||||||||||||||||||||||||||||||
| 1631 | 62 | my ($x_label_esc, $y_label_esc, $val_label_esc) = map { _js_string($_) } ($x_label, $y_label, $val_label); | |||||||||||||||||||||||||||||||||||||||||
| 1632 | |||||||||||||||||||||||||||||||||||||||||||
| 1633 | 62 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'heatmap'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "heatmap"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "heatmap"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 1634 | 62 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 1635 | |||||||||||||||||||||||||||||||||||||||||||
| 1636 | 62 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 1637 | 62 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 1638 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 1639 | 62 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1640 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 1641 | |||||||||||||||||||||||||||||||||||||||||||
| 1642 | 62 | my $margin_top = 30; | |||||||||||||||||||||||||||||||||||||||||
| 1643 | 62 | ▶ | my $margin_right = $legend ? 65 : 20; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1644 | 62 | ▶ | my $margin_bottom = $x_label ? 60 : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1645 | 62 | ▶ | my $margin_left = $y_label ? 80 : 60; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1646 | 62 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 1647 | 62 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 1648 | |||||||||||||||||||||||||||||||||||||||||||
| 1649 | 62 | ▶ | my $anim_block = $animated ? <<"ANIM" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1650 | var noAnim = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; | ||||||||||||||||||||||||||||||||||||||||||
| 1651 | if (!noAnim) { | ||||||||||||||||||||||||||||||||||||||||||
| 1652 | cellGs.attr("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 1653 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 1654 | .duration(300) | ||||||||||||||||||||||||||||||||||||||||||
| 1655 | .delay(function(d) { return yMap.get(d.y) * 50; }) | ||||||||||||||||||||||||||||||||||||||||||
| 1656 | .attr("opacity", 1); | ||||||||||||||||||||||||||||||||||||||||||
| 1657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1658 | ANIM | ||||||||||||||||||||||||||||||||||||||||||
| 1659 | |||||||||||||||||||||||||||||||||||||||||||
| 1660 | 62 | ▶ | my $legend_block = $legend ? <<"LEGBLOCK" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1661 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1662 | var defs = svg.append("defs"); | ||||||||||||||||||||||||||||||||||||||||||
| 1663 | var lgId = "heatmap-lg"; | ||||||||||||||||||||||||||||||||||||||||||
| 1664 | var lg = defs.append("linearGradient") | ||||||||||||||||||||||||||||||||||||||||||
| 1665 | .attr("id", lgId) | ||||||||||||||||||||||||||||||||||||||||||
| 1666 | .attr("x1", "0").attr("y1", "1") | ||||||||||||||||||||||||||||||||||||||||||
| 1667 | .attr("x2", "0").attr("y2", "0"); | ||||||||||||||||||||||||||||||||||||||||||
| 1668 | for (var i = 0; i <= 6; i++) { | ||||||||||||||||||||||||||||||||||||||||||
| 1669 | lg.append("stop") | ||||||||||||||||||||||||||||||||||||||||||
| 1670 | .attr("offset", (i / 6 * 100) + "%") | ||||||||||||||||||||||||||||||||||||||||||
| 1671 | .attr("stop-color", colorScale(maxV * i / 6)); | ||||||||||||||||||||||||||||||||||||||||||
| 1672 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1673 | var lgX = margin.left + innerW + 15; | ||||||||||||||||||||||||||||||||||||||||||
| 1674 | var lgBarH = Math.min(innerH, 150); | ||||||||||||||||||||||||||||||||||||||||||
| 1675 | var lgBarY = margin.top + (innerH - lgBarH) / 2; | ||||||||||||||||||||||||||||||||||||||||||
| 1676 | svg.append("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 1677 | .attr("x", lgX) | ||||||||||||||||||||||||||||||||||||||||||
| 1678 | .attr("y", lgBarY) | ||||||||||||||||||||||||||||||||||||||||||
| 1679 | .attr("width", 12) | ||||||||||||||||||||||||||||||||||||||||||
| 1680 | .attr("height", lgBarH) | ||||||||||||||||||||||||||||||||||||||||||
| 1681 | .attr("fill", "url(#" + lgId + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 1682 | var lgScale = d3.scaleLinear().domain([0, maxV]).range([lgBarH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 1683 | svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1684 | .attr("transform", "translate(" + (lgX + 12) + "," + lgBarY + ")") | ||||||||||||||||||||||||||||||||||||||||||
| 1685 | .call(d3.axisRight(lgScale).ticks(4)); | ||||||||||||||||||||||||||||||||||||||||||
| 1686 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1687 | LEGBLOCK | ||||||||||||||||||||||||||||||||||||||||||
| 1688 | |||||||||||||||||||||||||||||||||||||||||||
| 1689 | 62 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 1690 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 1691 | #$svg_id { display: block; } | ||||||||||||||||||||||||||||||||||||||||||
| 1692 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 1693 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 1694 | background: rgba(255,255,255,0.95); | ||||||||||||||||||||||||||||||||||||||||||
| 1695 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 1696 | border-radius: 4px; | ||||||||||||||||||||||||||||||||||||||||||
| 1697 | padding: 6px 10px; | ||||||||||||||||||||||||||||||||||||||||||
| 1698 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 1699 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 1700 | display: none; | ||||||||||||||||||||||||||||||||||||||||||
| 1701 | line-height: 1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 1702 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1703 | .hm-cell-text { | ||||||||||||||||||||||||||||||||||||||||||
| 1704 | font-size: 10px; | ||||||||||||||||||||||||||||||||||||||||||
| 1705 | fill: #222; | ||||||||||||||||||||||||||||||||||||||||||
| 1706 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 1707 | text-anchor: middle; | ||||||||||||||||||||||||||||||||||||||||||
| 1708 | dominant-baseline: middle; | ||||||||||||||||||||||||||||||||||||||||||
| 1709 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1710 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 1711 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 1712 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 1713 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 1714 | (function() { | ||||||||||||||||||||||||||||||||||||||||||
| 1715 | var data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 1716 | var cellPad = $cell_padding; | ||||||||||||||||||||||||||||||||||||||||||
| 1717 | var showVals0 = $show_values; | ||||||||||||||||||||||||||||||||||||||||||
| 1718 | var xLabelStr = "$x_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 1719 | var yLabelStr = "$y_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 1720 | var valLabelStr = "$val_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 1721 | var margin = { top: $margin_top, right: $margin_right, bottom: $margin_bottom, left: $margin_left }; | ||||||||||||||||||||||||||||||||||||||||||
| 1722 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 1723 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 1724 | |||||||||||||||||||||||||||||||||||||||||||
| 1725 | function esc(s) { | ||||||||||||||||||||||||||||||||||||||||||
| 1726 | return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||||||||||||||||||||||||||||||||||||||||||
| 1727 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1728 | |||||||||||||||||||||||||||||||||||||||||||
| 1729 | var xMap = new Map(), yMap = new Map(); | ||||||||||||||||||||||||||||||||||||||||||
| 1730 | data.forEach(function(d) { | ||||||||||||||||||||||||||||||||||||||||||
| 1731 | if (!xMap.has(d.x)) xMap.set(d.x, xMap.size); | ||||||||||||||||||||||||||||||||||||||||||
| 1732 | if (!yMap.has(d.y)) yMap.set(d.y, yMap.size); | ||||||||||||||||||||||||||||||||||||||||||
| 1733 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1734 | var xLabels = Array.from(xMap.keys()); | ||||||||||||||||||||||||||||||||||||||||||
| 1735 | var yLabels = Array.from(yMap.keys()); | ||||||||||||||||||||||||||||||||||||||||||
| 1736 | |||||||||||||||||||||||||||||||||||||||||||
| 1737 | var maxV = d3.max(data, function(d) { return d.v; }) || 0; | ||||||||||||||||||||||||||||||||||||||||||
| 1738 | var colorScale = d3.scaleSequential($d3_interpolator) | ||||||||||||||||||||||||||||||||||||||||||
| 1739 | .domain(maxV === 0 ? [0, 1] : [0, maxV]); | ||||||||||||||||||||||||||||||||||||||||||
| 1740 | |||||||||||||||||||||||||||||||||||||||||||
| 1741 | var xPad = Math.min(cellPad / Math.max(1, innerW / Math.max(1, xLabels.length)), 0.4); | ||||||||||||||||||||||||||||||||||||||||||
| 1742 | var yPad = Math.min(cellPad / Math.max(1, innerH / Math.max(1, yLabels.length)), 0.4); | ||||||||||||||||||||||||||||||||||||||||||
| 1743 | |||||||||||||||||||||||||||||||||||||||||||
| 1744 | var xScale = d3.scaleBand().domain(xLabels).range([0, innerW]).paddingInner(xPad); | ||||||||||||||||||||||||||||||||||||||||||
| 1745 | var yScale = d3.scaleBand().domain(yLabels).range([0, innerH]).paddingInner(yPad); | ||||||||||||||||||||||||||||||||||||||||||
| 1746 | |||||||||||||||||||||||||||||||||||||||||||
| 1747 | var showVals = showVals0 && xScale.bandwidth() >= 28 && yScale.bandwidth() >= 28; | ||||||||||||||||||||||||||||||||||||||||||
| 1748 | |||||||||||||||||||||||||||||||||||||||||||
| 1749 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 1750 | var g = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1751 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 1752 | |||||||||||||||||||||||||||||||||||||||||||
| 1753 | g.append("g").attr("class", "hm-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 1754 | .call(d3.axisTop(xScale).tickSize(0)) | ||||||||||||||||||||||||||||||||||||||||||
| 1755 | .call(function(a) { a.select(".domain").remove(); }); | ||||||||||||||||||||||||||||||||||||||||||
| 1756 | g.append("g").attr("class", "hm-y-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 1757 | .call(d3.axisLeft(yScale).tickSize(0)) | ||||||||||||||||||||||||||||||||||||||||||
| 1758 | .call(function(a) { a.select(".domain").remove(); }); | ||||||||||||||||||||||||||||||||||||||||||
| 1759 | |||||||||||||||||||||||||||||||||||||||||||
| 1760 | if (xLabelStr) { | ||||||||||||||||||||||||||||||||||||||||||
| 1761 | svg.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1762 | .attr("x", margin.left + innerW / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 1763 | .attr("y", $height - 4) | ||||||||||||||||||||||||||||||||||||||||||
| 1764 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 1765 | .attr("font-size", "12px") | ||||||||||||||||||||||||||||||||||||||||||
| 1766 | .text(xLabelStr); | ||||||||||||||||||||||||||||||||||||||||||
| 1767 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1768 | if (yLabelStr) { | ||||||||||||||||||||||||||||||||||||||||||
| 1769 | svg.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1770 | .attr("transform", "rotate(-90)") | ||||||||||||||||||||||||||||||||||||||||||
| 1771 | .attr("x", -(margin.top + innerH / 2)) | ||||||||||||||||||||||||||||||||||||||||||
| 1772 | .attr("y", 14) | ||||||||||||||||||||||||||||||||||||||||||
| 1773 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 1774 | .attr("font-size", "12px") | ||||||||||||||||||||||||||||||||||||||||||
| 1775 | .text(yLabelStr); | ||||||||||||||||||||||||||||||||||||||||||
| 1776 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1777 | |||||||||||||||||||||||||||||||||||||||||||
| 1778 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 1779 | |||||||||||||||||||||||||||||||||||||||||||
| 1780 | var gridMap = new Map(); | ||||||||||||||||||||||||||||||||||||||||||
| 1781 | data.forEach(function(d) { gridMap.set(d.x + "\\t" + d.y, d); }); | ||||||||||||||||||||||||||||||||||||||||||
| 1782 | var cells = Array.from(gridMap.values()); | ||||||||||||||||||||||||||||||||||||||||||
| 1783 | |||||||||||||||||||||||||||||||||||||||||||
| 1784 | var cellGs = g.selectAll(".hm-cell") | ||||||||||||||||||||||||||||||||||||||||||
| 1785 | .data(cells) | ||||||||||||||||||||||||||||||||||||||||||
| 1786 | .join("g") | ||||||||||||||||||||||||||||||||||||||||||
| 1787 | .attr("class", "hm-cell") | ||||||||||||||||||||||||||||||||||||||||||
| 1788 | .attr("transform", function(d) { | ||||||||||||||||||||||||||||||||||||||||||
| 1789 | return "translate(" + xScale(d.x) + "," + yScale(d.y) + ")"; | ||||||||||||||||||||||||||||||||||||||||||
| 1790 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1791 | |||||||||||||||||||||||||||||||||||||||||||
| 1792 | cellGs.append("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 1793 | .attr("width", xScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 1794 | .attr("height", yScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 1795 | .attr("fill", function(d) { return colorScale(d.v); }) | ||||||||||||||||||||||||||||||||||||||||||
| 1796 | .on("mouseover", function(event, d) { | ||||||||||||||||||||||||||||||||||||||||||
| 1797 | tip.html("X: " + esc(d.x) + "<br>Y: " + esc(d.y) + "<br>" + esc(valLabelStr) + ": " + d.v.toLocaleString()) | ||||||||||||||||||||||||||||||||||||||||||
| 1798 | .style("display", "block") | ||||||||||||||||||||||||||||||||||||||||||
| 1799 | .style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 1800 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 1801 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 1802 | .on("mousemove", function(event) { | ||||||||||||||||||||||||||||||||||||||||||
| 1803 | tip.style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 1804 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 1805 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 1806 | .on("mouseout", function() { tip.style("display", "none"); }); | ||||||||||||||||||||||||||||||||||||||||||
| 1807 | |||||||||||||||||||||||||||||||||||||||||||
| 1808 | if (showVals) { | ||||||||||||||||||||||||||||||||||||||||||
| 1809 | cellGs.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 1810 | .attr("class", "hm-cell-text") | ||||||||||||||||||||||||||||||||||||||||||
| 1811 | .attr("x", xScale.bandwidth() / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 1812 | .attr("y", yScale.bandwidth() / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 1813 | .text(function(d) { return d.v.toLocaleString(); }); | ||||||||||||||||||||||||||||||||||||||||||
| 1814 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1815 | |||||||||||||||||||||||||||||||||||||||||||
| 1816 | $legend_block | ||||||||||||||||||||||||||||||||||||||||||
| 1817 | $anim_block})(); | ||||||||||||||||||||||||||||||||||||||||||
| 1818 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 1819 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||||||||||||||||
| 1821 | 62 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 1822 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1823 | |||||||||||||||||||||||||||||||||||||||||||
| 1824 | =head2 render_bar_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 1825 | |||||||||||||||||||||||||||||||||||||||||||
| 1826 | my $result = $chart->render_bar_chart_snippet(\@bars); | ||||||||||||||||||||||||||||||||||||||||||
| 1827 | my $result = $chart->render_bar_chart_snippet(\@bars, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 1828 | |||||||||||||||||||||||||||||||||||||||||||
| 1829 | Generates an embeddable D3.js v7 bar chart fragment. Returns a hashref | ||||||||||||||||||||||||||||||||||||||||||
| 1830 | C<{ svg_id =E<gt> 'bar_chart', html =E<gt> $str }> where C<$str> is a | ||||||||||||||||||||||||||||||||||||||||||
| 1831 | self-contained HTML fragment (no page shell, no D3 CDN tag) that the caller | ||||||||||||||||||||||||||||||||||||||||||
| 1832 | embeds directly after loading D3.js. C<$str> is a Perl character string | ||||||||||||||||||||||||||||||||||||||||||
| 1833 | with the UTF-8 flag set (or pure ASCII when all labels are ASCII). | ||||||||||||||||||||||||||||||||||||||||||
| 1834 | |||||||||||||||||||||||||||||||||||||||||||
| 1835 | Each element of C<\@bars> is an array reference: | ||||||||||||||||||||||||||||||||||||||||||
| 1836 | |||||||||||||||||||||||||||||||||||||||||||
| 1837 | [ $label, $value ] | ||||||||||||||||||||||||||||||||||||||||||
| 1838 | [ $label, $value, \%extra ] | ||||||||||||||||||||||||||||||||||||||||||
| 1839 | |||||||||||||||||||||||||||||||||||||||||||
| 1840 | C<$label> is the category name (string); C<$value> is a non-negative number | ||||||||||||||||||||||||||||||||||||||||||
| 1841 | (negative values are silently converted to their absolute value); the optional | ||||||||||||||||||||||||||||||||||||||||||
| 1842 | C<\%extra> hashref supplies additional key/value pairs shown in the hover | ||||||||||||||||||||||||||||||||||||||||||
| 1843 | tooltip. Data points with an undefined C<$value> are silently skipped. | ||||||||||||||||||||||||||||||||||||||||||
| 1844 | |||||||||||||||||||||||||||||||||||||||||||
| 1845 | =head3 Options (C<\%opts>) | ||||||||||||||||||||||||||||||||||||||||||
| 1846 | |||||||||||||||||||||||||||||||||||||||||||
| 1847 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1848 | |||||||||||||||||||||||||||||||||||||||||||
| 1849 | =item C<orientation> (string, default C<'vertical'>) | ||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||
| 1851 | C<'vertical'> draws bars rising from a horizontal category axis. | ||||||||||||||||||||||||||||||||||||||||||
| 1852 | C<'horizontal'> draws bars extending from a vertical category axis. | ||||||||||||||||||||||||||||||||||||||||||
| 1853 | |||||||||||||||||||||||||||||||||||||||||||
| 1854 | =item C<sort_bars> (string, default C<'none'>) | ||||||||||||||||||||||||||||||||||||||||||
| 1855 | |||||||||||||||||||||||||||||||||||||||||||
| 1856 | Sort order applied before rendering: C<'value'> sorts descending by bar | ||||||||||||||||||||||||||||||||||||||||||
| 1857 | height, C<'label'> sorts ascending alphabetically, C<'none'> preserves the | ||||||||||||||||||||||||||||||||||||||||||
| 1858 | input order. | ||||||||||||||||||||||||||||||||||||||||||
| 1859 | |||||||||||||||||||||||||||||||||||||||||||
| 1860 | =item C<max_bars> (integer, default C<0>) | ||||||||||||||||||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||||||||||||||||
| 1862 | When non-zero, only the first C<max_bars> entries (after any sort) are | ||||||||||||||||||||||||||||||||||||||||||
| 1863 | rendered; the remaining entries are collapsed into a single C<Other> bar | ||||||||||||||||||||||||||||||||||||||||||
| 1864 | whose value equals their sum. Zero means no limit. | ||||||||||||||||||||||||||||||||||||||||||
| 1865 | |||||||||||||||||||||||||||||||||||||||||||
| 1866 | =item C<color> (string, default C<'steelblue'>) | ||||||||||||||||||||||||||||||||||||||||||
| 1867 | |||||||||||||||||||||||||||||||||||||||||||
| 1868 | Either a CSS colour value (e.g. C<'steelblue'>, C<'#4e79a7'>) applied to all | ||||||||||||||||||||||||||||||||||||||||||
| 1869 | bars, or the special token C<'categorical'>, which colours each bar | ||||||||||||||||||||||||||||||||||||||||||
| 1870 | distinctively using D3's Tableau-10 palette. | ||||||||||||||||||||||||||||||||||||||||||
| 1871 | |||||||||||||||||||||||||||||||||||||||||||
| 1872 | A value holding anything a CSS colour cannot, such as a quote or C<E<lt>>, is | ||||||||||||||||||||||||||||||||||||||||||
| 1873 | ignored and the default used. | ||||||||||||||||||||||||||||||||||||||||||
| 1874 | |||||||||||||||||||||||||||||||||||||||||||
| 1875 | =item C<show_values> (bool, default C<0>) | ||||||||||||||||||||||||||||||||||||||||||
| 1876 | |||||||||||||||||||||||||||||||||||||||||||
| 1877 | When true, the numeric value of each bar is printed above vertical bars or | ||||||||||||||||||||||||||||||||||||||||||
| 1878 | to the right of horizontal bars. | ||||||||||||||||||||||||||||||||||||||||||
| 1879 | |||||||||||||||||||||||||||||||||||||||||||
| 1880 | =item C<animated> (bool, default C<0>) | ||||||||||||||||||||||||||||||||||||||||||
| 1881 | |||||||||||||||||||||||||||||||||||||||||||
| 1882 | When true, bars grow from the baseline on page load: vertical bars rise from | ||||||||||||||||||||||||||||||||||||||||||
| 1883 | the bottom; horizontal bars extend from the left. An 800 ms D3 transition | ||||||||||||||||||||||||||||||||||||||||||
| 1884 | with a per-bar stagger up to 100 ms is used. The animation is suppressed | ||||||||||||||||||||||||||||||||||||||||||
| 1885 | when C<prefers-reduced-motion> is set in the viewer's OS. | ||||||||||||||||||||||||||||||||||||||||||
| 1886 | |||||||||||||||||||||||||||||||||||||||||||
| 1887 | =item C<value_label> (string, default C<'Value'>) | ||||||||||||||||||||||||||||||||||||||||||
| 1888 | |||||||||||||||||||||||||||||||||||||||||||
| 1889 | Label shown in the hover tooltip before the numeric value. The value is | ||||||||||||||||||||||||||||||||||||||||||
| 1890 | JavaScript-escaped before being embedded in the page. | ||||||||||||||||||||||||||||||||||||||||||
| 1891 | |||||||||||||||||||||||||||||||||||||||||||
| 1892 | =item C<x_label> (string, default C<''>) | ||||||||||||||||||||||||||||||||||||||||||
| 1893 | |||||||||||||||||||||||||||||||||||||||||||
| 1894 | When non-empty, a text label is rendered below the bottom axis. The value | ||||||||||||||||||||||||||||||||||||||||||
| 1895 | is JavaScript-escaped before being embedded in the page. | ||||||||||||||||||||||||||||||||||||||||||
| 1896 | |||||||||||||||||||||||||||||||||||||||||||
| 1897 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1898 | |||||||||||||||||||||||||||||||||||||||||||
| 1899 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 1900 | |||||||||||||||||||||||||||||||||||||||||||
| 1901 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1902 | |||||||||||||||||||||||||||||||||||||||||||
| 1903 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an | ||||||||||||||||||||||||||||||||||||||||||
| 1904 | ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 1905 | |||||||||||||||||||||||||||||||||||||||||||
| 1906 | =item * Throws C<Each data point must be an array reference> when an element | ||||||||||||||||||||||||||||||||||||||||||
| 1907 | is not an array reference. | ||||||||||||||||||||||||||||||||||||||||||
| 1908 | |||||||||||||||||||||||||||||||||||||||||||
| 1909 | =item * Throws C<Each data point must have at least 2 elements> when an | ||||||||||||||||||||||||||||||||||||||||||
| 1910 | element has fewer than 2 items. | ||||||||||||||||||||||||||||||||||||||||||
| 1911 | |||||||||||||||||||||||||||||||||||||||||||
| 1912 | =item * Throws C<Value must be numeric> when a C<$value> is not a number. | ||||||||||||||||||||||||||||||||||||||||||
| 1913 | |||||||||||||||||||||||||||||||||||||||||||
| 1914 | =item * Throws C<orientation must be 'vertical' or 'horizontal'> on an | ||||||||||||||||||||||||||||||||||||||||||
| 1915 | invalid orientation value. | ||||||||||||||||||||||||||||||||||||||||||
| 1916 | |||||||||||||||||||||||||||||||||||||||||||
| 1917 | =item * Throws C<sort_bars must be 'value', 'label', or 'none'> on an | ||||||||||||||||||||||||||||||||||||||||||
| 1918 | invalid sort_bars value. | ||||||||||||||||||||||||||||||||||||||||||
| 1919 | |||||||||||||||||||||||||||||||||||||||||||
| 1920 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1921 | |||||||||||||||||||||||||||||||||||||||||||
| 1922 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 1923 | |||||||||||||||||||||||||||||||||||||||||||
| 1924 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 1925 | |||||||||||||||||||||||||||||||||||||||||||
| 1926 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1927 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 1928 | opts => { type => 'hashref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 1929 | orientation => { type => 'string', memberof => [ 'vertical', 'horizontal' ], optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 1930 | sort_bars => { type => 'string', memberof => [ 'value', 'label', 'none' ], optional => 1 } | ||||||||||||||||||||||||||||||||||||||||||
| 1931 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1932 | |||||||||||||||||||||||||||||||||||||||||||
| 1933 | Each element of C<$data>: C<[ Str, Num ]> or C<[ Str, Num, HashRef ]>; | ||||||||||||||||||||||||||||||||||||||||||
| 1934 | undef C<$value> silently skipped; negative C<$value> becomes positive. | ||||||||||||||||||||||||||||||||||||||||||
| 1935 | |||||||||||||||||||||||||||||||||||||||||||
| 1936 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 1937 | |||||||||||||||||||||||||||||||||||||||||||
| 1938 | HashRef -- { svg_id => 'bar_chart', html => Str } | ||||||||||||||||||||||||||||||||||||||||||
| 1939 | html is a Perl character string (UTF-8 flag set, or pure ASCII). | ||||||||||||||||||||||||||||||||||||||||||
| 1940 | |||||||||||||||||||||||||||||||||||||||||||
| 1941 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1942 | |||||||||||||||||||||||||||||||||||||||||||
| 1943 | 114 | ▶ | sub render_bar_chart_snippet { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_bar_chart_snippet: called CC=50 SCAR=39.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1944 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1945 | 114 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 1946 | 114 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1947 | 111 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 1948 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 1949 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 1950 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 1951 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1952 | 111 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1953 | 111 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 1954 | |||||||||||||||||||||||||||||||||||||||||||
| 1955 | 111 | ▶ | my $orientation = $opts->{orientation} // 'vertical'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"orientation"} // "vertical"
MC/DC100%1/1
$opts->{"orientation"} // "vertical"
$opts->{"orientation"}
Truth table100%2/2
A
A= $opts->{"orientation"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1956 | 111 | ▶ | die "orientation must be 'vertical' or 'horizontal'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$orientation eq "vertical" or $orientation eq "horizontal"
MC/DC100%2/2
$orientation eq "vertical" or $orientation eq "horizontal"
$orientation eq "vertical"
$orientation eq "horizontal"
Truth table100%3/3
A or B
A= $orientation eq "vertical"B= $orientation eq "horizontal"
| |||||||||||||||||||||||||||||||||||||||||||
| 1957 | unless $orientation eq 'vertical' || $orientation eq 'horizontal'; | ||||||||||||||||||||||||||||||||||||||||||
| 1958 | |||||||||||||||||||||||||||||||||||||||||||
| 1959 | 108 | ▶ | my $sort_bars = $opts->{sort_bars} // 'none'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"sort_bars"} // "none"
MC/DC100%1/1
$opts->{"sort_bars"} // "none"
$opts->{"sort_bars"}
Truth table100%2/2
A
A= $opts->{"sort_bars"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1960 | die "sort_bars must be 'value', 'label', or 'none'" | ||||||||||||||||||||||||||||||||||||||||||
| 1961 | 108 | ▶ | unless grep { $sort_bars eq $_ } qw(value label none); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1962 | |||||||||||||||||||||||||||||||||||||||||||
| 1963 | 105 | ▶ | my $max_bars = int($opts->{max_bars} // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"max_bars"} // 0
MC/DC100%1/1
$opts->{"max_bars"} // 0
$opts->{"max_bars"}
Truth table100%2/2
A
A= $opts->{"max_bars"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1964 | 105 | ▶ | my $color = _css_colour($opts->{color}) // 'steelblue'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_css_colour($opts->{"color"}) // "steelblue"
MC/DC100%1/1
_css_colour($opts->{"color"}) // "steelblue"
_css_colour($opts->{"color"})
Truth table100%2/2
A
A= _css_colour($opts->{"color"})
| |||||||||||||||||||||||||||||||||||||||||||
| 1965 | 105 | ▶ | my $show_values = $opts->{show_values} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1966 | 105 | ▶ | my $animated = $opts->{animated} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1967 | 105 | ▶ | my $value_label = $opts->{value_label} // 'Value'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"value_label"} // "Value"
MC/DC100%1/1
$opts->{"value_label"} // "Value"
$opts->{"value_label"}
Truth table100%2/2
A
A= $opts->{"value_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1968 | 105 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1969 | |||||||||||||||||||||||||||||||||||||||||||
| 1970 | # Normalise data points; negative values become positive (bars show magnitude) | ||||||||||||||||||||||||||||||||||||||||||
| 1971 | 105 | my @bars; | |||||||||||||||||||||||||||||||||||||||||
| 1972 | 105 | for my $pt (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 1973 | 322 | ▶ | die 'Each data point must be an array reference' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1974 | unless ref($pt) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 1975 | 319 | ▶ | die 'Each data point must have at least 2 elements' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1976 | unless scalar(@$pt) >= 2; | ||||||||||||||||||||||||||||||||||||||||||
| 1977 | 316 | ▶ | next unless defined $pt->[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1978 | 313 | ▶ | die 'Value must be numeric' unless looks_like_number($pt->[1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1979 | 310 | ▶ | push @bars, { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
scalar @$pt >= 3 and ref $pt->[2] eq "HASH"
MC/DC50%1/2
scalar @$pt >= 3 and ref $pt->[2] eq "HASH"
scalar @$pt >= 3
ref $pt->[2] eq "HASH"
Truth table66%2/3
A and B
A= scalar @$pt >= 3B= ref $pt->[2] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 1980 | label => defined($pt->[0]) ? "$pt->[0]" : '', | ||||||||||||||||||||||||||||||||||||||||||
| 1981 | value => abs($pt->[1] + 0), | ||||||||||||||||||||||||||||||||||||||||||
| 1982 | (scalar(@$pt) >= 3 && ref($pt->[2]) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 1983 | ? (extra => $pt->[2]) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 1984 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1985 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1986 | |||||||||||||||||||||||||||||||||||||||||||
| 1987 | # Sort before optional truncation | ||||||||||||||||||||||||||||||||||||||||||
| 1988 | 96 | ▶ | if ($sort_bars eq 'value') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1989 | 7 | @bars = sort { $b->{value} <=> $a->{value} } @bars; | |||||||||||||||||||||||||||||||||||||||||
| 1990 | } elsif ($sort_bars eq 'label') { | ||||||||||||||||||||||||||||||||||||||||||
| 1991 | 2 | @bars = sort { $a->{label} cmp $b->{label} } @bars; | |||||||||||||||||||||||||||||||||||||||||
| 1992 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1993 | |||||||||||||||||||||||||||||||||||||||||||
| 1994 | # Truncate and collapse the tail into an "Other" bar | ||||||||||||||||||||||||||||||||||||||||||
| 1995 | 96 | ▶ | if ($max_bars && scalar(@bars) > $max_bars) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$max_bars and scalar @bars > $max_bars
MC/DC50%1/2
$max_bars and scalar @bars > $max_bars
$max_bars
scalar @bars > $max_bars
Truth table66%2/3
A and B
A= $max_barsB= scalar @bars > $max_bars
| |||||||||||||||||||||||||||||||||||||||||||
| 1996 | 4 | my @rest = splice @bars, $max_bars; | |||||||||||||||||||||||||||||||||||||||||
| 1997 | 4 | my $other = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1998 | 4 | $other += $_->{value} for @rest; | |||||||||||||||||||||||||||||||||||||||||
| 1999 | 4 | ▶ | push @bars, { label => 'Other', value => $other } if @rest; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2000 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2001 | |||||||||||||||||||||||||||||||||||||||||||
| 2002 | # Serialise to JSON for embedding in the <script> block | ||||||||||||||||||||||||||||||||||||||||||
| 2003 | my $json_data = _script_json([map { | ||||||||||||||||||||||||||||||||||||||||||
| 2004 | 96 | my %h = (label => $_->{label}, value => $_->{value} + 0); | |||||||||||||||||||||||||||||||||||||||||
| 2005 | 306 | ▶ | $h{extra} = $_->{extra} if exists $_->{extra}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2006 | 306 | \%h; | |||||||||||||||||||||||||||||||||||||||||
| 2007 | } @bars]); | ||||||||||||||||||||||||||||||||||||||||||
| 2008 | |||||||||||||||||||||||||||||||||||||||||||
| 2009 | # Escape strings for safe embedding inside JS double-quoted string literals | ||||||||||||||||||||||||||||||||||||||||||
| 2010 | 96 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 2011 | 96 | my $value_label_esc = $js_esc->($value_label); | |||||||||||||||||||||||||||||||||||||||||
| 2012 | 96 | my $x_label_esc = $js_esc->($x_label); | |||||||||||||||||||||||||||||||||||||||||
| 2013 | |||||||||||||||||||||||||||||||||||||||||||
| 2014 | 96 | ▶ | my $is_categorical = ($color eq 'categorical') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2015 | 96 | ▶ | my $color_esc = $is_categorical ? 'steelblue' : $js_esc->($color); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2016 | |||||||||||||||||||||||||||||||||||||||||||
| 2017 | 96 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'bar_chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "bar_chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "bar_chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 2018 | 96 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 2019 | |||||||||||||||||||||||||||||||||||||||||||
| 2020 | 96 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 2021 | 96 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 2022 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 2023 | 96 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 2024 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 2025 | |||||||||||||||||||||||||||||||||||||||||||
| 2026 | # Rotate x-axis labels when there are more than 8 vertical bars | ||||||||||||||||||||||||||||||||||||||||||
| 2027 | 96 | ▶ | my $rotate_labels = ($orientation eq 'vertical' && scalar(@bars) > 8) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$orientation eq "vertical" and scalar @bars > 8
MC/DC100%2/2
$orientation eq "vertical" and scalar @bars > 8
$orientation eq "vertical"
scalar @bars > 8
Truth table100%3/3
A and B
A= $orientation eq "vertical"B= scalar @bars > 8
| |||||||||||||||||||||||||||||||||||||||||||
| 2028 | |||||||||||||||||||||||||||||||||||||||||||
| 2029 | # Margins depend on orientation, label rotation, and optional features | ||||||||||||||||||||||||||||||||||||||||||
| 2030 | 96 | my ($margin_top, $margin_right, $margin_bottom, $margin_left); | |||||||||||||||||||||||||||||||||||||||||
| 2031 | 96 | ▶ | if ($orientation eq 'vertical') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2032 | 92 | $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 2033 | 92 | $margin_right = 20; | |||||||||||||||||||||||||||||||||||||||||
| 2034 | 92 | ▶ | $margin_bottom = $rotate_labels | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 2035 | ? ($x_label ? 105 : 85) | ||||||||||||||||||||||||||||||||||||||||||
| 2036 | : ($x_label ? 55 : 40); | ||||||||||||||||||||||||||||||||||||||||||
| 2037 | 92 | $margin_left = 55; | |||||||||||||||||||||||||||||||||||||||||
| 2038 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2039 | 4 | $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 2040 | 4 | ▶ | $margin_right = $show_values ? 60 : 20; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2041 | 4 | ▶ | $margin_bottom = $x_label ? 55 : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2042 | 4 | $margin_left = 130; | |||||||||||||||||||||||||||||||||||||||||
| 2043 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2044 | 96 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 2045 | 96 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 2046 | |||||||||||||||||||||||||||||||||||||||||||
| 2047 | # JS colour initialiser and per-bar fill callback | ||||||||||||||||||||||||||||||||||||||||||
| 2048 | 96 | ▶ | my $color_init_js = $is_categorical | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2049 | ? 'var colorOf = d3.scaleOrdinal(' . _default_scheme_js() . ')' | ||||||||||||||||||||||||||||||||||||||||||
| 2050 | . ".domain(data.map(function(d){return d.label;}));" | ||||||||||||||||||||||||||||||||||||||||||
| 2051 | : "var colorOf = function() { return \"$color_esc\"; };"; | ||||||||||||||||||||||||||||||||||||||||||
| 2052 | 96 | my $color_func_js = 'function(d) { return colorOf(d.label); }'; | |||||||||||||||||||||||||||||||||||||||||
| 2053 | |||||||||||||||||||||||||||||||||||||||||||
| 2054 | # Rotate x-axis tick labels (vertical orientation with many bars only) | ||||||||||||||||||||||||||||||||||||||||||
| 2055 | 96 | ▶ | my $rotate_block = $rotate_labels ? <<"ROT" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2056 | g.selectAll(".bc-x-axis text") | ||||||||||||||||||||||||||||||||||||||||||
| 2057 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 2058 | .attr("text-anchor", "end") | ||||||||||||||||||||||||||||||||||||||||||
| 2059 | .attr("dx", "-0.6em") | ||||||||||||||||||||||||||||||||||||||||||
| 2060 | .attr("dy", "0.15em"); | ||||||||||||||||||||||||||||||||||||||||||
| 2061 | ROT | ||||||||||||||||||||||||||||||||||||||||||
| 2062 | |||||||||||||||||||||||||||||||||||||||||||
| 2063 | # Optional inline value labels rendered on each bar | ||||||||||||||||||||||||||||||||||||||||||
| 2064 | 96 | my $show_values_block = ''; | |||||||||||||||||||||||||||||||||||||||||
| 2065 | 96 | ▶ | if ($show_values) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2066 | 4 | ▶ | if ($orientation eq 'vertical') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2067 | 3 | $show_values_block = <<"SVB"; | |||||||||||||||||||||||||||||||||||||||||
| 2068 | g.selectAll(".bc-val-text").data(data).join("text") | ||||||||||||||||||||||||||||||||||||||||||
| 2069 | .attr("class", "bc-val-text") | ||||||||||||||||||||||||||||||||||||||||||
| 2070 | .attr("x", function(d) { return xScale(d.label) + xScale.bandwidth() / 2; }) | ||||||||||||||||||||||||||||||||||||||||||
| 2071 | .attr("y", function(d) { return yScale(d.value) - 4; }) | ||||||||||||||||||||||||||||||||||||||||||
| 2072 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 2073 | .attr("font-size", "11px") | ||||||||||||||||||||||||||||||||||||||||||
| 2074 | .attr("fill", "#333") | ||||||||||||||||||||||||||||||||||||||||||
| 2075 | .text(function(d) { return d.value.toLocaleString(); }); | ||||||||||||||||||||||||||||||||||||||||||
| 2076 | SVB | ||||||||||||||||||||||||||||||||||||||||||
| 2077 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2078 | 1 | $show_values_block = <<"SVB"; | |||||||||||||||||||||||||||||||||||||||||
| 2079 | g.selectAll(".bc-val-text").data(data).join("text") | ||||||||||||||||||||||||||||||||||||||||||
| 2080 | .attr("class", "bc-val-text") | ||||||||||||||||||||||||||||||||||||||||||
| 2081 | .attr("x", function(d) { return xScale(d.value) + 4; }) | ||||||||||||||||||||||||||||||||||||||||||
| 2082 | .attr("y", function(d) { return yScale(d.label) + yScale.bandwidth() / 2; }) | ||||||||||||||||||||||||||||||||||||||||||
| 2083 | .attr("dominant-baseline", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 2084 | .attr("font-size", "11px") | ||||||||||||||||||||||||||||||||||||||||||
| 2085 | .attr("fill", "#333") | ||||||||||||||||||||||||||||||||||||||||||
| 2086 | .text(function(d) { return d.value.toLocaleString(); }); | ||||||||||||||||||||||||||||||||||||||||||
| 2087 | SVB | ||||||||||||||||||||||||||||||||||||||||||
| 2088 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2089 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2090 | |||||||||||||||||||||||||||||||||||||||||||
| 2091 | # Optional text label below the bottom axis | ||||||||||||||||||||||||||||||||||||||||||
| 2092 | 96 | my $x_label_block = ''; | |||||||||||||||||||||||||||||||||||||||||
| 2093 | 96 | ▶ | if ($x_label) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2094 | 7 | my $lx = $margin_left + int($inner_w / 2); | |||||||||||||||||||||||||||||||||||||||||
| 2095 | 7 | my $ly = $height - 5; | |||||||||||||||||||||||||||||||||||||||||
| 2096 | 7 | $x_label_block = <<"XLB"; | |||||||||||||||||||||||||||||||||||||||||
| 2097 | svg.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 2098 | .attr("x", $lx) | ||||||||||||||||||||||||||||||||||||||||||
| 2099 | .attr("y", $ly) | ||||||||||||||||||||||||||||||||||||||||||
| 2100 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 2101 | .attr("font-size", "12px") | ||||||||||||||||||||||||||||||||||||||||||
| 2102 | .attr("fill", "#555") | ||||||||||||||||||||||||||||||||||||||||||
| 2103 | .text("$x_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 2104 | XLB | ||||||||||||||||||||||||||||||||||||||||||
| 2105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2106 | |||||||||||||||||||||||||||||||||||||||||||
| 2107 | # Optional grow-from-baseline animation on page load | ||||||||||||||||||||||||||||||||||||||||||
| 2108 | 96 | my $anim_block = ''; | |||||||||||||||||||||||||||||||||||||||||
| 2109 | 96 | ▶ | if ($animated) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2110 | 4 | ▶ | if ($orientation eq 'vertical') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2111 | 3 | $anim_block = <<"ANIM"; | |||||||||||||||||||||||||||||||||||||||||
| 2112 | var noAnim = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; | ||||||||||||||||||||||||||||||||||||||||||
| 2113 | if (!noAnim) { | ||||||||||||||||||||||||||||||||||||||||||
| 2114 | var delayPer = data.length > 1 ? Math.min(100, 600 / (data.length - 1)) : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2115 | bars.attr("height", 0).attr("y", innerH) | ||||||||||||||||||||||||||||||||||||||||||
| 2116 | .transition().duration(800) | ||||||||||||||||||||||||||||||||||||||||||
| 2117 | .delay(function(d, i) { return i * delayPer; }) | ||||||||||||||||||||||||||||||||||||||||||
| 2118 | .attr("height", function(d) { return Math.max(0, innerH - yScale(d.value)); }) | ||||||||||||||||||||||||||||||||||||||||||
| 2119 | .attr("y", function(d) { return yScale(d.value); }); | ||||||||||||||||||||||||||||||||||||||||||
| 2120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2121 | ANIM | ||||||||||||||||||||||||||||||||||||||||||
| 2122 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2123 | 1 | $anim_block = <<"ANIM"; | |||||||||||||||||||||||||||||||||||||||||
| 2124 | var noAnim = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; | ||||||||||||||||||||||||||||||||||||||||||
| 2125 | if (!noAnim) { | ||||||||||||||||||||||||||||||||||||||||||
| 2126 | var delayPer = data.length > 1 ? Math.min(100, 600 / (data.length - 1)) : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2127 | bars.attr("width", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 2128 | .transition().duration(800) | ||||||||||||||||||||||||||||||||||||||||||
| 2129 | .delay(function(d, i) { return i * delayPer; }) | ||||||||||||||||||||||||||||||||||||||||||
| 2130 | .attr("width", function(d) { return Math.max(0, xScale(d.value)); }); | ||||||||||||||||||||||||||||||||||||||||||
| 2131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2132 | ANIM | ||||||||||||||||||||||||||||||||||||||||||
| 2133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2135 | |||||||||||||||||||||||||||||||||||||||||||
| 2136 | # Orientation-specific D3 scale/axis/bar JavaScript | ||||||||||||||||||||||||||||||||||||||||||
| 2137 | 96 | my $chart_js; | |||||||||||||||||||||||||||||||||||||||||
| 2138 | 96 | ▶ | if ($orientation eq 'vertical') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2139 | 92 | $chart_js = <<"JS"; | |||||||||||||||||||||||||||||||||||||||||
| 2140 | var g = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2141 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 2142 | |||||||||||||||||||||||||||||||||||||||||||
| 2143 | var xScale = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 2144 | .domain(data.map(function(d) { return d.label; })) | ||||||||||||||||||||||||||||||||||||||||||
| 2145 | .range([0, innerW]) | ||||||||||||||||||||||||||||||||||||||||||
| 2146 | .padding(0.2); | ||||||||||||||||||||||||||||||||||||||||||
| 2147 | var yMax = d3.max(data, function(d) { return d.value; }) || 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2148 | var yScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 2149 | .domain([0, yMax]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 2150 | .range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 2151 | |||||||||||||||||||||||||||||||||||||||||||
| 2152 | g.append("g").attr("class", "bc-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 2153 | .attr("transform", "translate(0," + innerH + ")") | ||||||||||||||||||||||||||||||||||||||||||
| 2154 | .call(d3.axisBottom(xScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 2155 | g.append("g").attr("class", "bc-y-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 2156 | .call(d3.axisLeft(yScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 2157 | |||||||||||||||||||||||||||||||||||||||||||
| 2158 | $rotate_block var bars = g.selectAll(".bc-bar").data(data).join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 2159 | .attr("class", "bc-bar") | ||||||||||||||||||||||||||||||||||||||||||
| 2160 | .attr("x", function(d) { return xScale(d.label); }) | ||||||||||||||||||||||||||||||||||||||||||
| 2161 | .attr("y", function(d) { return yScale(d.value); }) | ||||||||||||||||||||||||||||||||||||||||||
| 2162 | .attr("width", xScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 2163 | .attr("height", function(d) { return Math.max(0, innerH - yScale(d.value)); }) | ||||||||||||||||||||||||||||||||||||||||||
| 2164 | .attr("fill", $color_func_js) | ||||||||||||||||||||||||||||||||||||||||||
| 2165 | .attr("rx", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 2166 | .on("mouseover", function(event, d) { | ||||||||||||||||||||||||||||||||||||||||||
| 2167 | var tc = "<strong>" + esc(d.label) + "<\\/strong><br>" + esc(valLabel) + ": " + d.value.toLocaleString(); | ||||||||||||||||||||||||||||||||||||||||||
| 2168 | if (d.extra) { | ||||||||||||||||||||||||||||||||||||||||||
| 2169 | Object.entries(d.extra).forEach(function(kv) { | ||||||||||||||||||||||||||||||||||||||||||
| 2170 | tc += "<br>" + esc(kv[0]) + ": " + esc(String(kv[1])); | ||||||||||||||||||||||||||||||||||||||||||
| 2171 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2173 | tip.html(tc) | ||||||||||||||||||||||||||||||||||||||||||
| 2174 | .style("display", "block") | ||||||||||||||||||||||||||||||||||||||||||
| 2175 | .style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2176 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2177 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2178 | .on("mousemove", function(event) { | ||||||||||||||||||||||||||||||||||||||||||
| 2179 | tip.style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2180 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2181 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2182 | .on("mouseout", function() { tip.style("display", "none"); }); | ||||||||||||||||||||||||||||||||||||||||||
| 2183 | |||||||||||||||||||||||||||||||||||||||||||
| 2184 | $anim_block$show_values_block$x_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 2185 | JS | ||||||||||||||||||||||||||||||||||||||||||
| 2186 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2187 | 4 | $chart_js = <<"JS"; | |||||||||||||||||||||||||||||||||||||||||
| 2188 | var g = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2189 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 2190 | |||||||||||||||||||||||||||||||||||||||||||
| 2191 | var yScale = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 2192 | .domain(data.map(function(d) { return d.label; })) | ||||||||||||||||||||||||||||||||||||||||||
| 2193 | .range([0, innerH]) | ||||||||||||||||||||||||||||||||||||||||||
| 2194 | .padding(0.2); | ||||||||||||||||||||||||||||||||||||||||||
| 2195 | var xMax = d3.max(data, function(d) { return d.value; }) || 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2196 | var xScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 2197 | .domain([0, xMax]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 2198 | .range([0, innerW]); | ||||||||||||||||||||||||||||||||||||||||||
| 2199 | |||||||||||||||||||||||||||||||||||||||||||
| 2200 | g.append("g").attr("class", "bc-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 2201 | .attr("transform", "translate(0," + innerH + ")") | ||||||||||||||||||||||||||||||||||||||||||
| 2202 | .call(d3.axisBottom(xScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 2203 | g.append("g").attr("class", "bc-y-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 2204 | .call(d3.axisLeft(yScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 2205 | |||||||||||||||||||||||||||||||||||||||||||
| 2206 | var bars = g.selectAll(".bc-bar").data(data).join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 2207 | .attr("class", "bc-bar") | ||||||||||||||||||||||||||||||||||||||||||
| 2208 | .attr("x", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 2209 | .attr("y", function(d) { return yScale(d.label); }) | ||||||||||||||||||||||||||||||||||||||||||
| 2210 | .attr("width", function(d) { return Math.max(0, xScale(d.value)); }) | ||||||||||||||||||||||||||||||||||||||||||
| 2211 | .attr("height", yScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 2212 | .attr("fill", $color_func_js) | ||||||||||||||||||||||||||||||||||||||||||
| 2213 | .attr("rx", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 2214 | .on("mouseover", function(event, d) { | ||||||||||||||||||||||||||||||||||||||||||
| 2215 | var tc = "<strong>" + esc(d.label) + "<\\/strong><br>" + esc(valLabel) + ": " + d.value.toLocaleString(); | ||||||||||||||||||||||||||||||||||||||||||
| 2216 | if (d.extra) { | ||||||||||||||||||||||||||||||||||||||||||
| 2217 | Object.entries(d.extra).forEach(function(kv) { | ||||||||||||||||||||||||||||||||||||||||||
| 2218 | tc += "<br>" + esc(kv[0]) + ": " + esc(String(kv[1])); | ||||||||||||||||||||||||||||||||||||||||||
| 2219 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2221 | tip.html(tc) | ||||||||||||||||||||||||||||||||||||||||||
| 2222 | .style("display", "block") | ||||||||||||||||||||||||||||||||||||||||||
| 2223 | .style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2224 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2225 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2226 | .on("mousemove", function(event) { | ||||||||||||||||||||||||||||||||||||||||||
| 2227 | tip.style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2228 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2229 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2230 | .on("mouseout", function() { tip.style("display", "none"); }); | ||||||||||||||||||||||||||||||||||||||||||
| 2231 | |||||||||||||||||||||||||||||||||||||||||||
| 2232 | $anim_block$show_values_block$x_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 2233 | JS | ||||||||||||||||||||||||||||||||||||||||||
| 2234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2235 | |||||||||||||||||||||||||||||||||||||||||||
| 2236 | 96 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 2237 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 2238 | #$svg_id { display: block; } | ||||||||||||||||||||||||||||||||||||||||||
| 2239 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 2240 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 2241 | background: rgba(255,255,255,0.95); | ||||||||||||||||||||||||||||||||||||||||||
| 2242 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 2243 | border-radius: 4px; | ||||||||||||||||||||||||||||||||||||||||||
| 2244 | padding: 6px 10px; | ||||||||||||||||||||||||||||||||||||||||||
| 2245 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 2246 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 2247 | display: none; | ||||||||||||||||||||||||||||||||||||||||||
| 2248 | line-height: 1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 2249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2250 | .bc-bar { cursor: default; } | ||||||||||||||||||||||||||||||||||||||||||
| 2251 | .bc-bar:hover { opacity: 0.8; } | ||||||||||||||||||||||||||||||||||||||||||
| 2252 | .bc-val-text { pointer-events: none; } | ||||||||||||||||||||||||||||||||||||||||||
| 2253 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 2254 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 2255 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 2256 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 2257 | (function() { | ||||||||||||||||||||||||||||||||||||||||||
| 2258 | var data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 2259 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 2260 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 2261 | var valLabel = "$value_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 2262 | var margin = { top: $margin_top, right: $margin_right, bottom: $margin_bottom, left: $margin_left }; | ||||||||||||||||||||||||||||||||||||||||||
| 2263 | $color_init_js | ||||||||||||||||||||||||||||||||||||||||||
| 2264 | |||||||||||||||||||||||||||||||||||||||||||
| 2265 | function esc(s) { | ||||||||||||||||||||||||||||||||||||||||||
| 2266 | return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||||||||||||||||||||||||||||||||||||||||||
| 2267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2268 | |||||||||||||||||||||||||||||||||||||||||||
| 2269 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2270 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2271 | |||||||||||||||||||||||||||||||||||||||||||
| 2272 | $chart_js})(); | ||||||||||||||||||||||||||||||||||||||||||
| 2273 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 2274 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 2275 | |||||||||||||||||||||||||||||||||||||||||||
| 2276 | 96 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 2277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2278 | |||||||||||||||||||||||||||||||||||||||||||
| 2279 | =head2 render_line_chart_with_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 2280 | |||||||||||||||||||||||||||||||||||||||||||
| 2281 | $html = $chart->render_line_chart_with_tooltips($data); | ||||||||||||||||||||||||||||||||||||||||||
| 2282 | |||||||||||||||||||||||||||||||||||||||||||
| 2283 | Generates HTML and JavaScript code to render a line chart with mouseover tooltips. | ||||||||||||||||||||||||||||||||||||||||||
| 2284 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 2285 | |||||||||||||||||||||||||||||||||||||||||||
| 2286 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2287 | |||||||||||||||||||||||||||||||||||||||||||
| 2288 | =item * C<$data> - An array reference containing data points. Each data point should | ||||||||||||||||||||||||||||||||||||||||||
| 2289 | be an array reference with two elements: the label (string) and the value (numeric). | ||||||||||||||||||||||||||||||||||||||||||
| 2290 | |||||||||||||||||||||||||||||||||||||||||||
| 2291 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2292 | |||||||||||||||||||||||||||||||||||||||||||
| 2293 | Returns a string containing the HTML and JavaScript code for the chart. | ||||||||||||||||||||||||||||||||||||||||||
| 2294 | The JavaScript tooltip strings use C<< <\/b> >> (with a backslash) rather than | ||||||||||||||||||||||||||||||||||||||||||
| 2295 | C<< </b> >> to satisfy html-tidy's requirement that C<< </ >> followed by a | ||||||||||||||||||||||||||||||||||||||||||
| 2296 | letter not appear literally inside C<< <script> >> blocks. | ||||||||||||||||||||||||||||||||||||||||||
| 2297 | |||||||||||||||||||||||||||||||||||||||||||
| 2298 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 2299 | |||||||||||||||||||||||||||||||||||||||||||
| 2300 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2301 | |||||||||||||||||||||||||||||||||||||||||||
| 2302 | =item * Throws C<Data must be an array of arrays> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 2303 | |||||||||||||||||||||||||||||||||||||||||||
| 2304 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2305 | |||||||||||||||||||||||||||||||||||||||||||
| 2306 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 2307 | |||||||||||||||||||||||||||||||||||||||||||
| 2308 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 2309 | |||||||||||||||||||||||||||||||||||||||||||
| 2310 | { | ||||||||||||||||||||||||||||||||||||||||||
| 2311 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 2312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2313 | |||||||||||||||||||||||||||||||||||||||||||
| 2314 | Each element of C<$data> is C<[ Str, Num ]>; passing C<undef> or a | ||||||||||||||||||||||||||||||||||||||||||
| 2315 | non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 2316 | |||||||||||||||||||||||||||||||||||||||||||
| 2317 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 2318 | |||||||||||||||||||||||||||||||||||||||||||
| 2319 | Str -- complete HTML5 document; mouseover tooltip reveals label and value. | ||||||||||||||||||||||||||||||||||||||||||
| 2320 | Tooltip strings use C<< <\/b> >> not C<< </b> >>. | ||||||||||||||||||||||||||||||||||||||||||
| 2321 | |||||||||||||||||||||||||||||||||||||||||||
| 2322 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2323 | |||||||||||||||||||||||||||||||||||||||||||
| 2324 | sub render_line_chart_with_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 2325 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_line_chart_with_tooltips: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2326 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2327 | 7 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 2328 | 7 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2329 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 2330 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 2331 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 2332 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 2333 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2334 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 2335 | |||||||||||||||||||||||||||||||||||||||||||
| 2336 | # Generate JSON for data | ||||||||||||||||||||||||||||||||||||||||||
| 2337 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 2338 | 5 | map { { label => $_->[0], value => $_->[1] } } @$data | |||||||||||||||||||||||||||||||||||||||||
| 2339 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 2340 | |||||||||||||||||||||||||||||||||||||||||||
| 2341 | # Generate HTML and D3.js code | ||||||||||||||||||||||||||||||||||||||||||
| 2342 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 2343 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 2344 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 2345 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||||||||||||
| 2346 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||||||||||||||||||||||||||||||||||||||
| 2347 | 5 | <title>@{[ $self->_title_html() ]}</title> | |||||||||||||||||||||||||||||||||||||||||
| 2348 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 2349 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 2350 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 2351 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 2352 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 2353 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 2354 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 2355 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 2356 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 2357 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2358 | transition: opacity 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 2359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2360 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 2361 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 2362 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 2363 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 2364 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 2365 | <div class="tooltip" id="tooltip"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 2366 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 2367 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 2368 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 2369 | |||||||||||||||||||||||||||||||||||||||||||
| 2370 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 2371 | const tooltip = d3.select("#tooltip"); | ||||||||||||||||||||||||||||||||||||||||||
| 2372 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 2373 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 2374 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 2375 | |||||||||||||||||||||||||||||||||||||||||||
| 2376 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 2377 | .domain(data.map(d => d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2378 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 2379 | |||||||||||||||||||||||||||||||||||||||||||
| 2380 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 2381 | .domain([0, d3.max(data, d => d.value)]) | ||||||||||||||||||||||||||||||||||||||||||
| 2382 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 2383 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 2384 | |||||||||||||||||||||||||||||||||||||||||||
| 2385 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2386 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 2387 | |||||||||||||||||||||||||||||||||||||||||||
| 2388 | // Draw line | ||||||||||||||||||||||||||||||||||||||||||
| 2389 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 2390 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2391 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 2392 | |||||||||||||||||||||||||||||||||||||||||||
| 2393 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 2394 | .datum(data) | ||||||||||||||||||||||||||||||||||||||||||
| 2395 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 2396 | .attr("stroke", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2397 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 2398 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 2399 | |||||||||||||||||||||||||||||||||||||||||||
| 2400 | // Add points to the line | ||||||||||||||||||||||||||||||||||||||||||
| 2401 | chart.selectAll("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2402 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 2403 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2404 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2405 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 2406 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 2407 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2408 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2409 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 2410 | .html(`Label: <b>\${esc(d.label)}<\\/b><br>Value: <b>\${esc(d.value)}<\\/b>`) | ||||||||||||||||||||||||||||||||||||||||||
| 2411 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2412 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2413 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2414 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2415 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2416 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2417 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2418 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 2419 | tooltip.style("opacity", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 2420 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2421 | |||||||||||||||||||||||||||||||||||||||||||
| 2422 | // Add axes | ||||||||||||||||||||||||||||||||||||||||||
| 2423 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2424 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 2425 | |||||||||||||||||||||||||||||||||||||||||||
| 2426 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2427 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 2428 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 2429 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 2430 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 2431 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 2432 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 2433 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 2434 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 2435 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 2436 | |||||||||||||||||||||||||||||||||||||||||||
| 2437 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 2438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2439 | |||||||||||||||||||||||||||||||||||||||||||
| 2440 | =head2 render_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 2441 | |||||||||||||||||||||||||||||||||||||||||||
| 2442 | my $fragment = $chart->render_line_chart_snippet($data); | ||||||||||||||||||||||||||||||||||||||||||
| 2443 | my $fragment = $chart->render_line_chart_snippet($data, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 2444 | # $fragment->{svg_id} - the id attribute of the <svg> element | ||||||||||||||||||||||||||||||||||||||||||
| 2445 | # $fragment->{html} - embeddable HTML fragment (style + svg + script) | ||||||||||||||||||||||||||||||||||||||||||
| 2446 | |||||||||||||||||||||||||||||||||||||||||||
| 2447 | Generates an embeddable HTML fragment for a line chart with mouseover tooltips. | ||||||||||||||||||||||||||||||||||||||||||
| 2448 | Unlike C<render_line_chart_with_tooltips>, this method returns a fragment with | ||||||||||||||||||||||||||||||||||||||||||
| 2449 | no C<<!DOCTYPE>>, C<<html>>, C<<head>>, or C<<body>> wrapper, suitable for | ||||||||||||||||||||||||||||||||||||||||||
| 2450 | splicing directly into a Mojolicious TT (or any other) layout. | ||||||||||||||||||||||||||||||||||||||||||
| 2451 | |||||||||||||||||||||||||||||||||||||||||||
| 2452 | The caller is responsible for loading D3 in the page C<<head>>, e.g.: | ||||||||||||||||||||||||||||||||||||||||||
| 2453 | |||||||||||||||||||||||||||||||||||||||||||
| 2454 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 2455 | |||||||||||||||||||||||||||||||||||||||||||
| 2456 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 2457 | |||||||||||||||||||||||||||||||||||||||||||
| 2458 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2459 | |||||||||||||||||||||||||||||||||||||||||||
| 2460 | =item * C<$data> - An array reference of data points. Each point is an array | ||||||||||||||||||||||||||||||||||||||||||
| 2461 | reference with two required elements - the label (string) and the value | ||||||||||||||||||||||||||||||||||||||||||
| 2462 | (numeric) - and an optional third element: a hash reference of extra key/value | ||||||||||||||||||||||||||||||||||||||||||
| 2463 | pairs to display in the tooltip after the label and value rows. | ||||||||||||||||||||||||||||||||||||||||||
| 2464 | |||||||||||||||||||||||||||||||||||||||||||
| 2465 | [$x, $y] # basic point | ||||||||||||||||||||||||||||||||||||||||||
| 2466 | [$x, $y, \%row] # point with extra tooltip data | ||||||||||||||||||||||||||||||||||||||||||
| 2467 | |||||||||||||||||||||||||||||||||||||||||||
| 2468 | =item * C<\%opts> - Optional hash reference of rendering options: | ||||||||||||||||||||||||||||||||||||||||||
| 2469 | |||||||||||||||||||||||||||||||||||||||||||
| 2470 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2471 | |||||||||||||||||||||||||||||||||||||||||||
| 2472 | =item * C<id> (string, default C<'chart'>) - Override the C<id> attribute of | ||||||||||||||||||||||||||||||||||||||||||
| 2473 | the C<<svg>> element. Use this when embedding multiple charts on the same page. | ||||||||||||||||||||||||||||||||||||||||||
| 2474 | |||||||||||||||||||||||||||||||||||||||||||
| 2475 | =item * C<responsive> (boolean, default C<0>) - If true, emit | ||||||||||||||||||||||||||||||||||||||||||
| 2476 | C<viewBox="0 0 W H" width="100%">, with no C<height>, instead of fixed pixel | ||||||||||||||||||||||||||||||||||||||||||
| 2477 | dimensions, so the chart scales with its container. Falls back to | ||||||||||||||||||||||||||||||||||||||||||
| 2478 | C<< $self->{responsive} >> when not set per call. | ||||||||||||||||||||||||||||||||||||||||||
| 2479 | |||||||||||||||||||||||||||||||||||||||||||
| 2480 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2481 | |||||||||||||||||||||||||||||||||||||||||||
| 2482 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2483 | |||||||||||||||||||||||||||||||||||||||||||
| 2484 | =head3 Return value | ||||||||||||||||||||||||||||||||||||||||||
| 2485 | |||||||||||||||||||||||||||||||||||||||||||
| 2486 | A hash reference with: | ||||||||||||||||||||||||||||||||||||||||||
| 2487 | |||||||||||||||||||||||||||||||||||||||||||
| 2488 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2489 | |||||||||||||||||||||||||||||||||||||||||||
| 2490 | =item * C<svg_id> - The C<id> attribute used on the C<<svg>> element. | ||||||||||||||||||||||||||||||||||||||||||
| 2491 | |||||||||||||||||||||||||||||||||||||||||||
| 2492 | =item * C<html> - The embeddable fragment string (Perl character string). | ||||||||||||||||||||||||||||||||||||||||||
| 2493 | |||||||||||||||||||||||||||||||||||||||||||
| 2494 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2495 | |||||||||||||||||||||||||||||||||||||||||||
| 2496 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 2497 | |||||||||||||||||||||||||||||||||||||||||||
| 2498 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2499 | |||||||||||||||||||||||||||||||||||||||||||
| 2500 | =item * Dies with C<'Data must be an array of arrays'> when C<$data> is not an | ||||||||||||||||||||||||||||||||||||||||||
| 2501 | ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 2502 | |||||||||||||||||||||||||||||||||||||||||||
| 2503 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2504 | |||||||||||||||||||||||||||||||||||||||||||
| 2505 | =head3 Side effects | ||||||||||||||||||||||||||||||||||||||||||
| 2506 | |||||||||||||||||||||||||||||||||||||||||||
| 2507 | None. The method is read-only. | ||||||||||||||||||||||||||||||||||||||||||
| 2508 | |||||||||||||||||||||||||||||||||||||||||||
| 2509 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 2510 | |||||||||||||||||||||||||||||||||||||||||||
| 2511 | { | ||||||||||||||||||||||||||||||||||||||||||
| 2512 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 2513 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 2514 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 2515 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 2516 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 2517 | id => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 2518 | responsive => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 2519 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 2520 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 2521 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2522 | |||||||||||||||||||||||||||||||||||||||||||
| 2523 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2524 | |||||||||||||||||||||||||||||||||||||||||||
| 2525 | sub render_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 2526 | 37 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_line_chart_snippet: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2527 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2528 | 37 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 2529 | 37 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2530 | 34 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 2531 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 2532 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 2533 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 2534 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2535 | 34 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 2536 | 34 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 2537 | |||||||||||||||||||||||||||||||||||||||||||
| 2538 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 2539 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 2540 | 34 | my $point = { label => $_->[0], value => $_->[1] }; | |||||||||||||||||||||||||||||||||||||||||
| 2541 | 79 | ▶ | $point->{extra} = $_->[2] if ref($_->[2]) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2542 | 79 | $point | |||||||||||||||||||||||||||||||||||||||||
| 2543 | } @$data | ||||||||||||||||||||||||||||||||||||||||||
| 2544 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 2545 | |||||||||||||||||||||||||||||||||||||||||||
| 2546 | 34 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 2547 | 34 | my $tip_id = $svg_id . '_tooltip'; | |||||||||||||||||||||||||||||||||||||||||
| 2548 | 34 | my $w = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 2549 | 34 | my $h = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 2550 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 2551 | 34 | ▶ | ? qq{viewBox="0 0 $w $h" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 2552 | : qq{width="$w" height="$h" style="border: 1px solid black;"}; | ||||||||||||||||||||||||||||||||||||||||||
| 2553 | |||||||||||||||||||||||||||||||||||||||||||
| 2554 | 34 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 2555 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 2556 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 2557 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 2558 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 2559 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 2560 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 2561 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 2562 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 2563 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2564 | transition: opacity 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 2565 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2566 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 2567 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 2568 | <div class="tooltip" id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 2569 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 2570 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 2571 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 2572 | |||||||||||||||||||||||||||||||||||||||||||
| 2573 | const svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2574 | const tooltip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2575 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 2576 | const width = $w - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 2577 | const height = $h - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 2578 | |||||||||||||||||||||||||||||||||||||||||||
| 2579 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 2580 | .domain(data.map(d => d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2581 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 2582 | |||||||||||||||||||||||||||||||||||||||||||
| 2583 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 2584 | .domain([0, d3.max(data, d => d.value)]) | ||||||||||||||||||||||||||||||||||||||||||
| 2585 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 2586 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 2587 | |||||||||||||||||||||||||||||||||||||||||||
| 2588 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2589 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 2590 | |||||||||||||||||||||||||||||||||||||||||||
| 2591 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 2592 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2593 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 2594 | |||||||||||||||||||||||||||||||||||||||||||
| 2595 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 2596 | .datum(data) | ||||||||||||||||||||||||||||||||||||||||||
| 2597 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 2598 | .attr("stroke", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2599 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 2600 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 2601 | |||||||||||||||||||||||||||||||||||||||||||
| 2602 | chart.selectAll("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2603 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 2604 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2605 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2606 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 2607 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 2608 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2609 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2610 | let ttHtml = `Label: <b>\${esc(d.label)}<\\/b><br>Value: <b>\${esc(d.value)}<\\/b>`; | ||||||||||||||||||||||||||||||||||||||||||
| 2611 | if (d.extra) { | ||||||||||||||||||||||||||||||||||||||||||
| 2612 | Object.entries(d.extra).forEach(([k, v]) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2613 | ttHtml += `<br>\${esc(k)}: <b>\${esc(v)}<\\/b>`; | ||||||||||||||||||||||||||||||||||||||||||
| 2614 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2615 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2616 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 2617 | .html(ttHtml) | ||||||||||||||||||||||||||||||||||||||||||
| 2618 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2619 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2620 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2621 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2622 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2623 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2624 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2625 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 2626 | tooltip.style("opacity", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 2627 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2628 | |||||||||||||||||||||||||||||||||||||||||||
| 2629 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2630 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 2631 | |||||||||||||||||||||||||||||||||||||||||||
| 2632 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2633 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 2634 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 2635 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 2636 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 2637 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 2638 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 2639 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 2640 | |||||||||||||||||||||||||||||||||||||||||||
| 2641 | 34 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 2642 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2643 | |||||||||||||||||||||||||||||||||||||||||||
| 2644 | =head2 render_zoomable_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 2645 | |||||||||||||||||||||||||||||||||||||||||||
| 2646 | my $fragment = $chart->render_zoomable_line_chart_snippet($data); | ||||||||||||||||||||||||||||||||||||||||||
| 2647 | my $fragment = $chart->render_zoomable_line_chart_snippet($data, { animated => 1 }); | ||||||||||||||||||||||||||||||||||||||||||
| 2648 | # $fragment->{svg_id} - the id attribute of the <svg> element | ||||||||||||||||||||||||||||||||||||||||||
| 2649 | # $fragment->{html} - embeddable HTML fragment (style + button + svg + script) | ||||||||||||||||||||||||||||||||||||||||||
| 2650 | |||||||||||||||||||||||||||||||||||||||||||
| 2651 | Like C<render_line_chart_snippet>, but adds brush-to-zoom: the user can drag | ||||||||||||||||||||||||||||||||||||||||||
| 2652 | across a range of the x-axis to zoom into that region. A I<Reset zoom> button | ||||||||||||||||||||||||||||||||||||||||||
| 2653 | (hidden until a zoom is active) returns the chart to its original extent. | ||||||||||||||||||||||||||||||||||||||||||
| 2654 | Subsequent brushes on the zoomed view zoom in further; Reset always returns to | ||||||||||||||||||||||||||||||||||||||||||
| 2655 | the full dataset. | ||||||||||||||||||||||||||||||||||||||||||
| 2656 | |||||||||||||||||||||||||||||||||||||||||||
| 2657 | The caller is responsible for loading D3 in the page C<<head>>. | ||||||||||||||||||||||||||||||||||||||||||
| 2658 | |||||||||||||||||||||||||||||||||||||||||||
| 2659 | Accepts the same arguments as C<render_line_chart_snippet>: an array reference | ||||||||||||||||||||||||||||||||||||||||||
| 2660 | of data points, each C<[$x, $y]> or C<[$x, $y, \%extra]>, plus an optional | ||||||||||||||||||||||||||||||||||||||||||
| 2661 | second argument C<$opts> (hashref). | ||||||||||||||||||||||||||||||||||||||||||
| 2662 | |||||||||||||||||||||||||||||||||||||||||||
| 2663 | =head3 Options | ||||||||||||||||||||||||||||||||||||||||||
| 2664 | |||||||||||||||||||||||||||||||||||||||||||
| 2665 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2666 | |||||||||||||||||||||||||||||||||||||||||||
| 2667 | =item * C<animated> (boolean, default C<0>) - when true, the initial page load | ||||||||||||||||||||||||||||||||||||||||||
| 2668 | animates the line drawing left-to-right via the C<stroke-dashoffset> technique | ||||||||||||||||||||||||||||||||||||||||||
| 2669 | (1200 ms, C<d3.easeLinear>), then fades in data-point circles after the line | ||||||||||||||||||||||||||||||||||||||||||
| 2670 | finishes (300 ms after a 1200 ms delay). Respects | ||||||||||||||||||||||||||||||||||||||||||
| 2671 | C<prefers-reduced-motion>: when the user has requested reduced motion the line | ||||||||||||||||||||||||||||||||||||||||||
| 2672 | is drawn immediately at full opacity. Subsequent zoom and reset redraws are | ||||||||||||||||||||||||||||||||||||||||||
| 2673 | never animated regardless of this flag. | ||||||||||||||||||||||||||||||||||||||||||
| 2674 | |||||||||||||||||||||||||||||||||||||||||||
| 2675 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2676 | |||||||||||||||||||||||||||||||||||||||||||
| 2677 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 2678 | |||||||||||||||||||||||||||||||||||||||||||
| 2679 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 2680 | |||||||||||||||||||||||||||||||||||||||||||
| 2681 | { | ||||||||||||||||||||||||||||||||||||||||||
| 2682 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 2683 | opts => { type => 'hashref', optional => 1, default => {} }, | ||||||||||||||||||||||||||||||||||||||||||
| 2684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2685 | |||||||||||||||||||||||||||||||||||||||||||
| 2686 | Each element of C<$data> is C<[ Str, Num ]> or C<[ Str, Num, HashRef ]>; | ||||||||||||||||||||||||||||||||||||||||||
| 2687 | passing C<undef> or a non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 2688 | Recognised C<opts> key: C<animated> (boolean, default C<0>). | ||||||||||||||||||||||||||||||||||||||||||
| 2689 | |||||||||||||||||||||||||||||||||||||||||||
| 2690 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 2691 | |||||||||||||||||||||||||||||||||||||||||||
| 2692 | HashRef -- C<{ svg_id =E<gt> 'chart', html =E<gt> Str }>; | ||||||||||||||||||||||||||||||||||||||||||
| 2693 | embeddable fragment; no DOCTYPE, no page shell, no D3 CDN tag. | ||||||||||||||||||||||||||||||||||||||||||
| 2694 | |||||||||||||||||||||||||||||||||||||||||||
| 2695 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 2696 | |||||||||||||||||||||||||||||||||||||||||||
| 2697 | Dies with I<Data must be an array of arrays> if C<$data> is not an arrayref. | ||||||||||||||||||||||||||||||||||||||||||
| 2698 | |||||||||||||||||||||||||||||||||||||||||||
| 2699 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2700 | |||||||||||||||||||||||||||||||||||||||||||
| 2701 | sub render_zoomable_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 2702 | 44 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_zoomable_line_chart_snippet: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2703 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2704 | 44 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 2705 | 44 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2706 | 39 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 2707 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 2708 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 2709 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 2710 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2711 | 39 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 2712 | 39 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 2713 | |||||||||||||||||||||||||||||||||||||||||||
| 2714 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 2715 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 2716 | 39 | my $point = { label => $_->[0], value => $_->[1] }; | |||||||||||||||||||||||||||||||||||||||||
| 2717 | 102 | ▶ | $point->{extra} = $_->[2] if ref($_->[2]) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2718 | 102 | $point | |||||||||||||||||||||||||||||||||||||||||
| 2719 | } @$data | ||||||||||||||||||||||||||||||||||||||||||
| 2720 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 2721 | |||||||||||||||||||||||||||||||||||||||||||
| 2722 | 39 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 2723 | 39 | my $tip_id = $svg_id . '_tooltip'; | |||||||||||||||||||||||||||||||||||||||||
| 2724 | 39 | my $rst_id = $svg_id . '_reset'; | |||||||||||||||||||||||||||||||||||||||||
| 2725 | 39 | my $w = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 2726 | 39 | my $h = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 2727 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 2728 | 39 | ▶ | ? qq{viewBox="0 0 $w $h" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 2729 | : qq{width="$w" height="$h" style="border: 1px solid black;"}; | ||||||||||||||||||||||||||||||||||||||||||
| 2730 | |||||||||||||||||||||||||||||||||||||||||||
| 2731 | # Single-quote heredoc so <\/b> is preserved verbatim in the output — | ||||||||||||||||||||||||||||||||||||||||||
| 2732 | # double-quote would require <\\/b> to survive Perl interpolation. | ||||||||||||||||||||||||||||||||||||||||||
| 2733 | 39 | my $circle_handlers = <<'HANDLERS'; | |||||||||||||||||||||||||||||||||||||||||
| 2734 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2735 | let ttHtml = `Label: <b>${esc(d.label)}<\/b><br>Value: <b>${esc(d.value)}<\/b>`; | ||||||||||||||||||||||||||||||||||||||||||
| 2736 | if (d.extra) { | ||||||||||||||||||||||||||||||||||||||||||
| 2737 | Object.entries(d.extra).forEach(([k, v]) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2738 | ttHtml += `<br>${esc(k)}: <b>${esc(v)}<\/b>`; | ||||||||||||||||||||||||||||||||||||||||||
| 2739 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2740 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2741 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 2742 | .html(ttHtml) | ||||||||||||||||||||||||||||||||||||||||||
| 2743 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2744 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2745 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2746 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 2747 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 2748 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 2749 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2750 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 2751 | tooltip.style("opacity", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 2752 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 2753 | HANDLERS | ||||||||||||||||||||||||||||||||||||||||||
| 2754 | |||||||||||||||||||||||||||||||||||||||||||
| 2755 | 39 | ▶ | my $init_flag = $opts->{animated} ? 'let initialDrawDone = false;' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2756 | |||||||||||||||||||||||||||||||||||||||||||
| 2757 | 39 | my $redraw_body; | |||||||||||||||||||||||||||||||||||||||||
| 2758 | 39 | ▶ | if ($opts->{animated}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2759 | 4 | $redraw_body = <<"ANIM"; | |||||||||||||||||||||||||||||||||||||||||
| 2760 | if (!initialDrawDone) { | ||||||||||||||||||||||||||||||||||||||||||
| 2761 | var prefersReduced = window.matchMedia && | ||||||||||||||||||||||||||||||||||||||||||
| 2762 | window.matchMedia('(prefers-reduced-motion: reduce)').matches; | ||||||||||||||||||||||||||||||||||||||||||
| 2763 | linePath.datum(newData).attr("d", lineFn); | ||||||||||||||||||||||||||||||||||||||||||
| 2764 | if (prefersReduced) { | ||||||||||||||||||||||||||||||||||||||||||
| 2765 | linePath.attr("stroke-dasharray", null).attr("stroke-dashoffset", null); | ||||||||||||||||||||||||||||||||||||||||||
| 2766 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2767 | const L = linePath.node().getTotalLength(); | ||||||||||||||||||||||||||||||||||||||||||
| 2768 | linePath | ||||||||||||||||||||||||||||||||||||||||||
| 2769 | .attr("stroke-dasharray", L) | ||||||||||||||||||||||||||||||||||||||||||
| 2770 | .attr("stroke-dashoffset", L) | ||||||||||||||||||||||||||||||||||||||||||
| 2771 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 2772 | .duration(1200) | ||||||||||||||||||||||||||||||||||||||||||
| 2773 | .ease(d3.easeLinear) | ||||||||||||||||||||||||||||||||||||||||||
| 2774 | .attr("stroke-dashoffset", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 2775 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2776 | chart.selectAll("circle.pt") | ||||||||||||||||||||||||||||||||||||||||||
| 2777 | .data(newData, d => d.label) | ||||||||||||||||||||||||||||||||||||||||||
| 2778 | .join( | ||||||||||||||||||||||||||||||||||||||||||
| 2779 | enter => enter.append("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2780 | .attr("class", "pt") | ||||||||||||||||||||||||||||||||||||||||||
| 2781 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 2782 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2783 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2784 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 2785 | .attr("opacity", prefersReduced ? 1 : 0) | ||||||||||||||||||||||||||||||||||||||||||
| 2786 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 2787 | $circle_handlers .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 2788 | .duration(prefersReduced ? 0 : 300) | ||||||||||||||||||||||||||||||||||||||||||
| 2789 | .delay(prefersReduced ? 0 : 1200) | ||||||||||||||||||||||||||||||||||||||||||
| 2790 | .attr("opacity", 1); | ||||||||||||||||||||||||||||||||||||||||||
| 2791 | initialDrawDone = true; | ||||||||||||||||||||||||||||||||||||||||||
| 2792 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2793 | linePath.datum(newData).transition(t).attr("d", lineFn); | ||||||||||||||||||||||||||||||||||||||||||
| 2794 | chart.selectAll("circle.pt") | ||||||||||||||||||||||||||||||||||||||||||
| 2795 | .data(newData, d => d.label) | ||||||||||||||||||||||||||||||||||||||||||
| 2796 | .join( | ||||||||||||||||||||||||||||||||||||||||||
| 2797 | enter => enter.append("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2798 | .attr("class", "pt") | ||||||||||||||||||||||||||||||||||||||||||
| 2799 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 2800 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2801 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2802 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 2803 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 2804 | $circle_handlers .transition(t) | ||||||||||||||||||||||||||||||||||||||||||
| 2805 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2806 | .attr("cy", d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 2807 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2808 | ANIM | ||||||||||||||||||||||||||||||||||||||||||
| 2809 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2810 | 35 | $redraw_body = <<"PLAIN"; | |||||||||||||||||||||||||||||||||||||||||
| 2811 | linePath.datum(newData).transition(t).attr("d", lineFn); | ||||||||||||||||||||||||||||||||||||||||||
| 2812 | |||||||||||||||||||||||||||||||||||||||||||
| 2813 | chart.selectAll("circle.pt") | ||||||||||||||||||||||||||||||||||||||||||
| 2814 | .data(newData, d => d.label) | ||||||||||||||||||||||||||||||||||||||||||
| 2815 | .join( | ||||||||||||||||||||||||||||||||||||||||||
| 2816 | enter => enter.append("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 2817 | .attr("class", "pt") | ||||||||||||||||||||||||||||||||||||||||||
| 2818 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 2819 | .attr("fill", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2820 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2821 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 2822 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 2823 | $circle_handlers .transition(t) | ||||||||||||||||||||||||||||||||||||||||||
| 2824 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2825 | .attr("cy", d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 2826 | PLAIN | ||||||||||||||||||||||||||||||||||||||||||
| 2827 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2828 | |||||||||||||||||||||||||||||||||||||||||||
| 2829 | 39 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 2830 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 2831 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 2832 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 2833 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 2834 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 2835 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 2836 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 2837 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 2838 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 2839 | transition: opacity 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 2840 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2841 | #$rst_id { | ||||||||||||||||||||||||||||||||||||||||||
| 2842 | display: none; | ||||||||||||||||||||||||||||||||||||||||||
| 2843 | margin-bottom: 4px; | ||||||||||||||||||||||||||||||||||||||||||
| 2844 | cursor: pointer; | ||||||||||||||||||||||||||||||||||||||||||
| 2845 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2846 | .brush .selection { | ||||||||||||||||||||||||||||||||||||||||||
| 2847 | fill: steelblue; | ||||||||||||||||||||||||||||||||||||||||||
| 2848 | fill-opacity: 0.15; | ||||||||||||||||||||||||||||||||||||||||||
| 2849 | stroke: steelblue; | ||||||||||||||||||||||||||||||||||||||||||
| 2850 | stroke-width: 1; | ||||||||||||||||||||||||||||||||||||||||||
| 2851 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2852 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 2853 | <button id="$rst_id">Reset zoom</button> | ||||||||||||||||||||||||||||||||||||||||||
| 2854 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 2855 | <div class="tooltip" id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 2856 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 2857 | const allData = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 2858 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 2859 | let currentData = allData.slice(); | ||||||||||||||||||||||||||||||||||||||||||
| 2860 | |||||||||||||||||||||||||||||||||||||||||||
| 2861 | const svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2862 | const tooltip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2863 | const resetBtn = d3.select("#$rst_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 2864 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 2865 | const width = $w - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 2866 | const height = $h - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 2867 | |||||||||||||||||||||||||||||||||||||||||||
| 2868 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 2869 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 2870 | |||||||||||||||||||||||||||||||||||||||||||
| 2871 | // Scales (domain set in redraw) | ||||||||||||||||||||||||||||||||||||||||||
| 2872 | const x = d3.scalePoint().range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 2873 | const y = d3.scaleLinear().range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 2874 | |||||||||||||||||||||||||||||||||||||||||||
| 2875 | const lineFn = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 2876 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 2877 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 2878 | |||||||||||||||||||||||||||||||||||||||||||
| 2879 | // Brush appended first so circles sit above it and receive mouse events | ||||||||||||||||||||||||||||||||||||||||||
| 2880 | const brush = d3.brushX() | ||||||||||||||||||||||||||||||||||||||||||
| 2881 | .extent([[0, 0], [width, height]]) | ||||||||||||||||||||||||||||||||||||||||||
| 2882 | .on("end", brushed); | ||||||||||||||||||||||||||||||||||||||||||
| 2883 | const brushGroup = chart.append("g").attr("class", "brush").call(brush); | ||||||||||||||||||||||||||||||||||||||||||
| 2884 | |||||||||||||||||||||||||||||||||||||||||||
| 2885 | const linePath = chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 2886 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 2887 | .attr("stroke", "steelblue") | ||||||||||||||||||||||||||||||||||||||||||
| 2888 | .attr("stroke-width", 2); | ||||||||||||||||||||||||||||||||||||||||||
| 2889 | |||||||||||||||||||||||||||||||||||||||||||
| 2890 | const yAxisG = chart.append("g"); | ||||||||||||||||||||||||||||||||||||||||||
| 2891 | const xAxisG = chart.append("g").attr("transform", `translate(0,\${height})`); | ||||||||||||||||||||||||||||||||||||||||||
| 2892 | |||||||||||||||||||||||||||||||||||||||||||
| 2893 | $init_flag | ||||||||||||||||||||||||||||||||||||||||||
| 2894 | |||||||||||||||||||||||||||||||||||||||||||
| 2895 | function redraw(newData, ms) { | ||||||||||||||||||||||||||||||||||||||||||
| 2896 | x.domain(newData.map(d => d.label)); | ||||||||||||||||||||||||||||||||||||||||||
| 2897 | y.domain([Math.min(0, d3.min(newData, d => d.value)), d3.max(newData, d => d.value)]).nice(); | ||||||||||||||||||||||||||||||||||||||||||
| 2898 | |||||||||||||||||||||||||||||||||||||||||||
| 2899 | const t = svg.transition().duration(ms); | ||||||||||||||||||||||||||||||||||||||||||
| 2900 | |||||||||||||||||||||||||||||||||||||||||||
| 2901 | xAxisG.transition(t) | ||||||||||||||||||||||||||||||||||||||||||
| 2902 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 2903 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 2904 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 2905 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 2906 | |||||||||||||||||||||||||||||||||||||||||||
| 2907 | yAxisG.transition(t).call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 2908 | |||||||||||||||||||||||||||||||||||||||||||
| 2909 | $redraw_body } | ||||||||||||||||||||||||||||||||||||||||||
| 2910 | |||||||||||||||||||||||||||||||||||||||||||
| 2911 | redraw(currentData, 0); | ||||||||||||||||||||||||||||||||||||||||||
| 2912 | |||||||||||||||||||||||||||||||||||||||||||
| 2913 | function brushed(event) { | ||||||||||||||||||||||||||||||||||||||||||
| 2914 | if (!event.selection) return; | ||||||||||||||||||||||||||||||||||||||||||
| 2915 | const [x0, x1] = event.selection; | ||||||||||||||||||||||||||||||||||||||||||
| 2916 | const zoomed = currentData.filter(d => { | ||||||||||||||||||||||||||||||||||||||||||
| 2917 | const px = x(d.label); | ||||||||||||||||||||||||||||||||||||||||||
| 2918 | return px >= x0 - 1 && px <= x1 + 1; | ||||||||||||||||||||||||||||||||||||||||||
| 2919 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2920 | brushGroup.call(brush.move, null); // clear brush rectangle | ||||||||||||||||||||||||||||||||||||||||||
| 2921 | if (zoomed.length < 2) return; | ||||||||||||||||||||||||||||||||||||||||||
| 2922 | currentData = zoomed; | ||||||||||||||||||||||||||||||||||||||||||
| 2923 | redraw(currentData, 500); | ||||||||||||||||||||||||||||||||||||||||||
| 2924 | resetBtn.style("display", "inline-block"); | ||||||||||||||||||||||||||||||||||||||||||
| 2925 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2926 | |||||||||||||||||||||||||||||||||||||||||||
| 2927 | resetBtn.on("click", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 2928 | currentData = allData.slice(); | ||||||||||||||||||||||||||||||||||||||||||
| 2929 | redraw(currentData, 500); | ||||||||||||||||||||||||||||||||||||||||||
| 2930 | resetBtn.style("display", "none"); | ||||||||||||||||||||||||||||||||||||||||||
| 2931 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2932 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 2933 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 2934 | |||||||||||||||||||||||||||||||||||||||||||
| 2935 | 39 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 2936 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2937 | |||||||||||||||||||||||||||||||||||||||||||
| 2938 | =head2 render_multi_series_line_chart_with_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 2939 | |||||||||||||||||||||||||||||||||||||||||||
| 2940 | $html = $chart->render_multi_series_line_chart_with_tooltips($data); | ||||||||||||||||||||||||||||||||||||||||||
| 2941 | |||||||||||||||||||||||||||||||||||||||||||
| 2942 | Generates HTML and JavaScript code to render a chart of many lines with mouseover tooltips. | ||||||||||||||||||||||||||||||||||||||||||
| 2943 | |||||||||||||||||||||||||||||||||||||||||||
| 2944 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 2945 | |||||||||||||||||||||||||||||||||||||||||||
| 2946 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2947 | |||||||||||||||||||||||||||||||||||||||||||
| 2948 | =item * C<$data> - An array reference of series hashes. Each element is a hashref | ||||||||||||||||||||||||||||||||||||||||||
| 2949 | with a C<name> key (string) and a C<data> key (array reference of C<< {label, value} >> | ||||||||||||||||||||||||||||||||||||||||||
| 2950 | hashrefs). | ||||||||||||||||||||||||||||||||||||||||||
| 2951 | |||||||||||||||||||||||||||||||||||||||||||
| 2952 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 2953 | { name => 'Series A', data => [{ label => 'Jan', value => 100 }, ...] }, | ||||||||||||||||||||||||||||||||||||||||||
| 2954 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 2955 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 2956 | |||||||||||||||||||||||||||||||||||||||||||
| 2957 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2958 | |||||||||||||||||||||||||||||||||||||||||||
| 2959 | Returns a string containing the HTML and JavaScript code for the chart. | ||||||||||||||||||||||||||||||||||||||||||
| 2960 | Tooltip strings use C<< <\/b> >> rather than C<< </b> >> for html-tidy compliance. | ||||||||||||||||||||||||||||||||||||||||||
| 2961 | |||||||||||||||||||||||||||||||||||||||||||
| 2962 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 2963 | |||||||||||||||||||||||||||||||||||||||||||
| 2964 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2965 | |||||||||||||||||||||||||||||||||||||||||||
| 2966 | =item * Throws C<Data must be an array of hashes> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 2967 | |||||||||||||||||||||||||||||||||||||||||||
| 2968 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2969 | |||||||||||||||||||||||||||||||||||||||||||
| 2970 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 2971 | |||||||||||||||||||||||||||||||||||||||||||
| 2972 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 2973 | |||||||||||||||||||||||||||||||||||||||||||
| 2974 | { | ||||||||||||||||||||||||||||||||||||||||||
| 2975 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 2976 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2977 | |||||||||||||||||||||||||||||||||||||||||||
| 2978 | Each element of C<$data> is a hashref with keys C<name> (string) and | ||||||||||||||||||||||||||||||||||||||||||
| 2979 | C<data> (arrayref of hashrefs with C<label> and C<value> keys); | ||||||||||||||||||||||||||||||||||||||||||
| 2980 | passing C<undef> or a non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 2981 | |||||||||||||||||||||||||||||||||||||||||||
| 2982 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 2983 | |||||||||||||||||||||||||||||||||||||||||||
| 2984 | Str -- complete HTML5 document; one coloured line per series with mouseover tooltips. | ||||||||||||||||||||||||||||||||||||||||||
| 2985 | |||||||||||||||||||||||||||||||||||||||||||
| 2986 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2987 | |||||||||||||||||||||||||||||||||||||||||||
| 2988 | sub render_multi_series_line_chart_with_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 2989 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_multi_series_line_chart_with_tooltips: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2990 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2991 | 7 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 2992 | 7 | ▶ | die 'Data must be an array of hashes' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2993 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 2994 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 2995 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of hashes' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 2996 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 2997 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 2998 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 2999 | |||||||||||||||||||||||||||||||||||||||||||
| 3000 | 5 | my $json_data = _script_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 3001 | |||||||||||||||||||||||||||||||||||||||||||
| 3002 | # Generate HTML and D3.js code | ||||||||||||||||||||||||||||||||||||||||||
| 3003 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 3004 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 3005 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 3006 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||||||||||||
| 3007 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||||||||||||||||||||||||||||||||||||||
| 3008 | 5 | <title>@{[ $self->_title_html() ]}</title> | |||||||||||||||||||||||||||||||||||||||||
| 3009 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 3010 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 3011 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 3012 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 3013 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 3014 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3015 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 3016 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 3017 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 3018 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 3019 | transition: opacity 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 3020 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3021 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 3022 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 3023 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 3024 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 3025 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 3026 | <div class="tooltip" id="tooltip"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 3027 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 3028 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 3029 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 3030 | |||||||||||||||||||||||||||||||||||||||||||
| 3031 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 3032 | const tooltip = d3.select("#tooltip"); | ||||||||||||||||||||||||||||||||||||||||||
| 3033 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 3034 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 3035 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 3036 | |||||||||||||||||||||||||||||||||||||||||||
| 3037 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3038 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 3039 | |||||||||||||||||||||||||||||||||||||||||||
| 3040 | // Extract all labels and flatten them into a unique array | ||||||||||||||||||||||||||||||||||||||||||
| 3041 | const allLabels = Array.from(new Set(data.flatMap(series => series.data.map(d => d.label)))); | ||||||||||||||||||||||||||||||||||||||||||
| 3042 | |||||||||||||||||||||||||||||||||||||||||||
| 3043 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 3044 | .domain(allLabels) | ||||||||||||||||||||||||||||||||||||||||||
| 3045 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 3046 | |||||||||||||||||||||||||||||||||||||||||||
| 3047 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 3048 | .domain([0, d3.max(data.flatMap(series => series.data.map(d => d.value)))]) | ||||||||||||||||||||||||||||||||||||||||||
| 3049 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 3050 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 3051 | |||||||||||||||||||||||||||||||||||||||||||
| 3052 | // Define color scale for series | ||||||||||||||||||||||||||||||||||||||||||
| 3053 | const color = d3.scaleOrdinal($CATEGORY10_JS); | ||||||||||||||||||||||||||||||||||||||||||
| 3054 | |||||||||||||||||||||||||||||||||||||||||||
| 3055 | // Add axes | ||||||||||||||||||||||||||||||||||||||||||
| 3056 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3057 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 3058 | |||||||||||||||||||||||||||||||||||||||||||
| 3059 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3060 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 3061 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 3062 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3063 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 3064 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 3065 | |||||||||||||||||||||||||||||||||||||||||||
| 3066 | // Draw lines for each series | ||||||||||||||||||||||||||||||||||||||||||
| 3067 | data.forEach((series, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3068 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 3069 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3070 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 3071 | |||||||||||||||||||||||||||||||||||||||||||
| 3072 | // Add line | ||||||||||||||||||||||||||||||||||||||||||
| 3073 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 3074 | .datum(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3075 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 3076 | .attr("stroke", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3077 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 3078 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 3079 | |||||||||||||||||||||||||||||||||||||||||||
| 3080 | // Add points and tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 3081 | chart.selectAll(\`circle.series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3082 | .data(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3083 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 3084 | .attr("class", \`series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3085 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3086 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 3087 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 3088 | .attr("fill", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3089 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3090 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 3091 | .html(\`Series: <b>\${esc(series.name)}<\\/b><br>Label: <b>\${esc(d.label)}<\\/b><br>Value: <b>\${esc(d.value)}<\\/b>\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3092 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3093 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3094 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3095 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3096 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3097 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3098 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3099 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 3100 | tooltip.style("opacity", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 3101 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3102 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3103 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 3104 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 3105 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 3106 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 3107 | |||||||||||||||||||||||||||||||||||||||||||
| 3108 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 3109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3110 | |||||||||||||||||||||||||||||||||||||||||||
| 3111 | =head2 render_multi_series_line_chart_with_animated_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 3112 | |||||||||||||||||||||||||||||||||||||||||||
| 3113 | $html = $chart->render_multi_series_line_chart_with_animated_tooltips($data); | ||||||||||||||||||||||||||||||||||||||||||
| 3114 | |||||||||||||||||||||||||||||||||||||||||||
| 3115 | Generates HTML and JavaScript code to render a chart of many lines with animated mouseover tooltips. | ||||||||||||||||||||||||||||||||||||||||||
| 3116 | |||||||||||||||||||||||||||||||||||||||||||
| 3117 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 3118 | |||||||||||||||||||||||||||||||||||||||||||
| 3119 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3120 | |||||||||||||||||||||||||||||||||||||||||||
| 3121 | =item * C<$data> - Same format as C<render_multi_series_line_chart_with_tooltips>: | ||||||||||||||||||||||||||||||||||||||||||
| 3122 | an array reference of C<< { name, data } >> series hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 3123 | |||||||||||||||||||||||||||||||||||||||||||
| 3124 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3125 | |||||||||||||||||||||||||||||||||||||||||||
| 3126 | Returns a string containing the complete HTML5 document. | ||||||||||||||||||||||||||||||||||||||||||
| 3127 | The tooltip appears with a CSS C<translateY> slide-in animation. | ||||||||||||||||||||||||||||||||||||||||||
| 3128 | Tooltip strings use C<< <\/b> >> for html-tidy compliance. | ||||||||||||||||||||||||||||||||||||||||||
| 3129 | |||||||||||||||||||||||||||||||||||||||||||
| 3130 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 3131 | |||||||||||||||||||||||||||||||||||||||||||
| 3132 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3133 | |||||||||||||||||||||||||||||||||||||||||||
| 3134 | =item * Throws C<Data must be an array of hashes> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 3135 | |||||||||||||||||||||||||||||||||||||||||||
| 3136 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3137 | |||||||||||||||||||||||||||||||||||||||||||
| 3138 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 3139 | |||||||||||||||||||||||||||||||||||||||||||
| 3140 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 3141 | |||||||||||||||||||||||||||||||||||||||||||
| 3142 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3143 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3145 | |||||||||||||||||||||||||||||||||||||||||||
| 3146 | Each element of C<$data> is a hashref with keys C<name> (string) and | ||||||||||||||||||||||||||||||||||||||||||
| 3147 | C<data> (arrayref of hashrefs with C<label> and C<value> keys); | ||||||||||||||||||||||||||||||||||||||||||
| 3148 | passing C<undef> or a non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 3149 | |||||||||||||||||||||||||||||||||||||||||||
| 3150 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 3151 | |||||||||||||||||||||||||||||||||||||||||||
| 3152 | Str -- complete HTML5 document; animated tooltip uses CSS translateY transition. | ||||||||||||||||||||||||||||||||||||||||||
| 3153 | |||||||||||||||||||||||||||||||||||||||||||
| 3154 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 3155 | |||||||||||||||||||||||||||||||||||||||||||
| 3156 | sub render_multi_series_line_chart_with_animated_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 3157 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_multi_series_line_chart_with_animated_tooltips: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 3158 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 3159 | 7 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 3160 | 7 | ▶ | die 'Data must be an array of hashes' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3161 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 3162 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 3163 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of hashes' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 3164 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 3165 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3166 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 3167 | |||||||||||||||||||||||||||||||||||||||||||
| 3168 | # Generate JSON for data | ||||||||||||||||||||||||||||||||||||||||||
| 3169 | 5 | my $json_data = _script_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 3170 | |||||||||||||||||||||||||||||||||||||||||||
| 3171 | # Generate HTML and D3.js code | ||||||||||||||||||||||||||||||||||||||||||
| 3172 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 3173 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 3174 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 3175 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||||||||||||
| 3176 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||||||||||||||||||||||||||||||||||||||
| 3177 | 5 | <title>@{[ $self->_title_html() ]}</title> | |||||||||||||||||||||||||||||||||||||||||
| 3178 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 3179 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 3180 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 3181 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 3182 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 3183 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3184 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 3185 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 3186 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 3187 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 3188 | transform: translateY(-10px); | ||||||||||||||||||||||||||||||||||||||||||
| 3189 | transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 3190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3191 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 3192 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 3193 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 3194 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 3195 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 3196 | <div class="tooltip" id="tooltip"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 3197 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 3198 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 3199 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 3200 | |||||||||||||||||||||||||||||||||||||||||||
| 3201 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 3202 | const tooltip = d3.select("#tooltip"); | ||||||||||||||||||||||||||||||||||||||||||
| 3203 | const margin = { top: 20, right: 30, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 3204 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 3205 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 3206 | |||||||||||||||||||||||||||||||||||||||||||
| 3207 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3208 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 3209 | |||||||||||||||||||||||||||||||||||||||||||
| 3210 | // Extract all labels and flatten them into a unique array | ||||||||||||||||||||||||||||||||||||||||||
| 3211 | const allLabels = Array.from(new Set(data.flatMap(series => series.data.map(d => d.label)))); | ||||||||||||||||||||||||||||||||||||||||||
| 3212 | |||||||||||||||||||||||||||||||||||||||||||
| 3213 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 3214 | .domain(allLabels) | ||||||||||||||||||||||||||||||||||||||||||
| 3215 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 3216 | |||||||||||||||||||||||||||||||||||||||||||
| 3217 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 3218 | .domain([0, d3.max(data.flatMap(series => series.data.map(d => d.value)))]) | ||||||||||||||||||||||||||||||||||||||||||
| 3219 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 3220 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 3221 | |||||||||||||||||||||||||||||||||||||||||||
| 3222 | // Define color scale for series | ||||||||||||||||||||||||||||||||||||||||||
| 3223 | const color = d3.scaleOrdinal($CATEGORY10_JS); | ||||||||||||||||||||||||||||||||||||||||||
| 3224 | |||||||||||||||||||||||||||||||||||||||||||
| 3225 | // Add axes | ||||||||||||||||||||||||||||||||||||||||||
| 3226 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3227 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 3228 | |||||||||||||||||||||||||||||||||||||||||||
| 3229 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3230 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 3231 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 3232 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3233 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 3234 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 3235 | |||||||||||||||||||||||||||||||||||||||||||
| 3236 | // Draw lines for each series | ||||||||||||||||||||||||||||||||||||||||||
| 3237 | data.forEach((series, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3238 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 3239 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3240 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 3241 | |||||||||||||||||||||||||||||||||||||||||||
| 3242 | // Add line | ||||||||||||||||||||||||||||||||||||||||||
| 3243 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 3244 | .datum(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3245 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 3246 | .attr("stroke", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3247 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 3248 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 3249 | |||||||||||||||||||||||||||||||||||||||||||
| 3250 | // Add points and tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 3251 | chart.selectAll(\`circle.series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3252 | .data(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3253 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 3254 | .attr("class", \`series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3255 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3256 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 3257 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 3258 | .attr("fill", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3259 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3260 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 3261 | .style("transform", "translateY(0)") | ||||||||||||||||||||||||||||||||||||||||||
| 3262 | .html(\`Series: <b>\${esc(series.name)}<\\/b><br>Label: <b>\${esc(d.label)}<\\/b><br>Value: <b>\${esc(d.value)}<\\/b>\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3263 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3264 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3265 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3266 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3267 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3268 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3269 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3270 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 3271 | tooltip.style("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 3272 | .style("transform", "translateY(-10px)"); | ||||||||||||||||||||||||||||||||||||||||||
| 3273 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3274 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3275 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 3276 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 3277 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 3278 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 3279 | |||||||||||||||||||||||||||||||||||||||||||
| 3280 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 3281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3282 | |||||||||||||||||||||||||||||||||||||||||||
| 3283 | =head2 render_multi_series_line_chart_with_legends | ||||||||||||||||||||||||||||||||||||||||||
| 3284 | |||||||||||||||||||||||||||||||||||||||||||
| 3285 | $html = $chart->render_multi_series_line_chart_with_legends($data); | ||||||||||||||||||||||||||||||||||||||||||
| 3286 | |||||||||||||||||||||||||||||||||||||||||||
| 3287 | Generates HTML and JavaScript code to render a chart of many lines with a static | ||||||||||||||||||||||||||||||||||||||||||
| 3288 | colour legend. Each series gets a labelled colour swatch in the legend area. | ||||||||||||||||||||||||||||||||||||||||||
| 3289 | |||||||||||||||||||||||||||||||||||||||||||
| 3290 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 3291 | |||||||||||||||||||||||||||||||||||||||||||
| 3292 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3293 | |||||||||||||||||||||||||||||||||||||||||||
| 3294 | =item * C<$data> - Same format as C<render_multi_series_line_chart_with_tooltips>: | ||||||||||||||||||||||||||||||||||||||||||
| 3295 | an array reference of C<< { name, data } >> series hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 3296 | |||||||||||||||||||||||||||||||||||||||||||
| 3297 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3298 | |||||||||||||||||||||||||||||||||||||||||||
| 3299 | Returns a string containing the complete HTML5 document. The stylesheet defines | ||||||||||||||||||||||||||||||||||||||||||
| 3300 | a C<.legend> CSS class used by the D3-generated legend elements. | ||||||||||||||||||||||||||||||||||||||||||
| 3301 | |||||||||||||||||||||||||||||||||||||||||||
| 3302 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 3303 | |||||||||||||||||||||||||||||||||||||||||||
| 3304 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3305 | |||||||||||||||||||||||||||||||||||||||||||
| 3306 | =item * Throws C<Data must be an array of hashes> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 3307 | |||||||||||||||||||||||||||||||||||||||||||
| 3308 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3309 | |||||||||||||||||||||||||||||||||||||||||||
| 3310 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 3311 | |||||||||||||||||||||||||||||||||||||||||||
| 3312 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 3313 | |||||||||||||||||||||||||||||||||||||||||||
| 3314 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3315 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3317 | |||||||||||||||||||||||||||||||||||||||||||
| 3318 | Each element of C<$data> is a hashref with keys C<name> (string) and | ||||||||||||||||||||||||||||||||||||||||||
| 3319 | C<data> (arrayref of hashrefs with C<label> and C<value> keys); | ||||||||||||||||||||||||||||||||||||||||||
| 3320 | passing C<undef> or a non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 3321 | |||||||||||||||||||||||||||||||||||||||||||
| 3322 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 3323 | |||||||||||||||||||||||||||||||||||||||||||
| 3324 | Str -- complete HTML5 document; static colour legend rendered as SVG C<g> elements | ||||||||||||||||||||||||||||||||||||||||||
| 3325 | with the C<.legend> CSS class applied via D3 C<.attr("class", "legend")>. | ||||||||||||||||||||||||||||||||||||||||||
| 3326 | |||||||||||||||||||||||||||||||||||||||||||
| 3327 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 3328 | |||||||||||||||||||||||||||||||||||||||||||
| 3329 | 7 | ▶ | sub render_multi_series_line_chart_with_legends { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_multi_series_line_chart_with_legends: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 3330 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 3331 | 7 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 3332 | 7 | ▶ | die 'Data must be an array of hashes' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3333 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 3334 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 3335 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of hashes' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 3336 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 3337 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3338 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 3339 | |||||||||||||||||||||||||||||||||||||||||||
| 3340 | # Generate JSON for data | ||||||||||||||||||||||||||||||||||||||||||
| 3341 | 5 | my $json_data = _script_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 3342 | |||||||||||||||||||||||||||||||||||||||||||
| 3343 | # Generate HTML and D3.js code | ||||||||||||||||||||||||||||||||||||||||||
| 3344 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 3345 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 3346 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 3347 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||||||||||||
| 3348 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||||||||||||||||||||||||||||||||||||||
| 3349 | 5 | <title>@{[ $self->_title_html() ]}</title> | |||||||||||||||||||||||||||||||||||||||||
| 3350 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 3351 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 3352 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 3353 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 3354 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 3355 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3356 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 3357 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 3358 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 3359 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 3360 | transform: translateY(-10px); | ||||||||||||||||||||||||||||||||||||||||||
| 3361 | transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 3362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3363 | .legend { | ||||||||||||||||||||||||||||||||||||||||||
| 3364 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 3365 | cursor: pointer; | ||||||||||||||||||||||||||||||||||||||||||
| 3366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3367 | .legend rect { | ||||||||||||||||||||||||||||||||||||||||||
| 3368 | stroke-width: 1; | ||||||||||||||||||||||||||||||||||||||||||
| 3369 | stroke: #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3371 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 3372 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 3373 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 3374 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 3375 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 3376 | <div class="tooltip" id="tooltip"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 3377 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 3378 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 3379 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 3380 | |||||||||||||||||||||||||||||||||||||||||||
| 3381 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 3382 | const tooltip = d3.select("#tooltip"); | ||||||||||||||||||||||||||||||||||||||||||
| 3383 | const margin = { top: 20, right: 120, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 3384 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 3385 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 3386 | |||||||||||||||||||||||||||||||||||||||||||
| 3387 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3388 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 3389 | |||||||||||||||||||||||||||||||||||||||||||
| 3390 | const legendArea = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3391 | .attr("transform", `translate(\${width + margin.left + 20},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 3392 | |||||||||||||||||||||||||||||||||||||||||||
| 3393 | // Extract all labels and flatten them into a unique array | ||||||||||||||||||||||||||||||||||||||||||
| 3394 | const allLabels = Array.from(new Set(data.flatMap(series => series.data.map(d => d.label)))); | ||||||||||||||||||||||||||||||||||||||||||
| 3395 | |||||||||||||||||||||||||||||||||||||||||||
| 3396 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 3397 | .domain(allLabels) | ||||||||||||||||||||||||||||||||||||||||||
| 3398 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 3399 | |||||||||||||||||||||||||||||||||||||||||||
| 3400 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 3401 | .domain([0, d3.max(data.flatMap(series => series.data.map(d => d.value)))]) | ||||||||||||||||||||||||||||||||||||||||||
| 3402 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 3403 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 3404 | |||||||||||||||||||||||||||||||||||||||||||
| 3405 | // Define color scale for series | ||||||||||||||||||||||||||||||||||||||||||
| 3406 | const color = d3.scaleOrdinal($CATEGORY10_JS); | ||||||||||||||||||||||||||||||||||||||||||
| 3407 | |||||||||||||||||||||||||||||||||||||||||||
| 3408 | // Add axes | ||||||||||||||||||||||||||||||||||||||||||
| 3409 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3410 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 3411 | |||||||||||||||||||||||||||||||||||||||||||
| 3412 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3413 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 3414 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 3415 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3416 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 3417 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 3418 | |||||||||||||||||||||||||||||||||||||||||||
| 3419 | // Draw lines for each series | ||||||||||||||||||||||||||||||||||||||||||
| 3420 | data.forEach((series, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3421 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 3422 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3423 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 3424 | |||||||||||||||||||||||||||||||||||||||||||
| 3425 | // Add line | ||||||||||||||||||||||||||||||||||||||||||
| 3426 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 3427 | .datum(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3428 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 3429 | .attr("stroke", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3430 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 3431 | .attr("class", \`line-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3432 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 3433 | |||||||||||||||||||||||||||||||||||||||||||
| 3434 | // Add points and tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 3435 | chart.selectAll(\`circle.series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3436 | .data(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3437 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 3438 | .attr("class", \`series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3439 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3440 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 3441 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 3442 | .attr("fill", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3443 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3444 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 3445 | .style("transform", "translateY(0)") | ||||||||||||||||||||||||||||||||||||||||||
| 3446 | .html(\`Series: <b>\${esc(series.name)}<\\/b><br>Label: <b>\${esc(d.label)}<\\/b><br>Value: <b>\${esc(d.value)}<\\/b>\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3447 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3448 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3449 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3450 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3451 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3452 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3453 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3454 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 3455 | tooltip.style("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 3456 | .style("transform", "translateY(-10px)"); | ||||||||||||||||||||||||||||||||||||||||||
| 3457 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3458 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3459 | |||||||||||||||||||||||||||||||||||||||||||
| 3460 | // Add legend | ||||||||||||||||||||||||||||||||||||||||||
| 3461 | data.forEach((series, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3462 | const legend = legendArea.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3463 | .attr("transform", `translate(0, \${i * 20})`) | ||||||||||||||||||||||||||||||||||||||||||
| 3464 | .attr("class", "legend"); | ||||||||||||||||||||||||||||||||||||||||||
| 3465 | |||||||||||||||||||||||||||||||||||||||||||
| 3466 | legend.append("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 3467 | .attr("width", 12) | ||||||||||||||||||||||||||||||||||||||||||
| 3468 | .attr("height", 12) | ||||||||||||||||||||||||||||||||||||||||||
| 3469 | .attr("fill", color(i)); | ||||||||||||||||||||||||||||||||||||||||||
| 3470 | |||||||||||||||||||||||||||||||||||||||||||
| 3471 | legend.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3472 | .attr("x", 20) | ||||||||||||||||||||||||||||||||||||||||||
| 3473 | .attr("y", 10) | ||||||||||||||||||||||||||||||||||||||||||
| 3474 | .text(series.name) | ||||||||||||||||||||||||||||||||||||||||||
| 3475 | .style("alignment-baseline", "middle"); | ||||||||||||||||||||||||||||||||||||||||||
| 3476 | |||||||||||||||||||||||||||||||||||||||||||
| 3477 | // Optional: Interactive legend for toggling visibility (uncomment to use) | ||||||||||||||||||||||||||||||||||||||||||
| 3478 | // legend.on("click", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 3479 | // const visible = d3.selectAll(\`path.line-\${i}\`).style("opacity") === "1" ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||
| 3480 | // d3.selectAll(\`path.line-\${i}\`).style("opacity", visible); | ||||||||||||||||||||||||||||||||||||||||||
| 3481 | // d3.selectAll(\`circle.series-\${i}\`).style("opacity", visible); | ||||||||||||||||||||||||||||||||||||||||||
| 3482 | // }); | ||||||||||||||||||||||||||||||||||||||||||
| 3483 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3484 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 3485 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 3486 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 3487 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 3488 | |||||||||||||||||||||||||||||||||||||||||||
| 3489 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 3490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3491 | |||||||||||||||||||||||||||||||||||||||||||
| 3492 | =head2 render_multi_series_line_chart_with_interactive_legends | ||||||||||||||||||||||||||||||||||||||||||
| 3493 | |||||||||||||||||||||||||||||||||||||||||||
| 3494 | $html = $chart->render_multi_series_line_chart_with_interactive_legends($data); | ||||||||||||||||||||||||||||||||||||||||||
| 3495 | |||||||||||||||||||||||||||||||||||||||||||
| 3496 | Generates HTML and JavaScript code to render a chart of many lines with interactive legends to filter, highlight or modify elements based on legend selections. | ||||||||||||||||||||||||||||||||||||||||||
| 3497 | |||||||||||||||||||||||||||||||||||||||||||
| 3498 | Accepts the following arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 3499 | |||||||||||||||||||||||||||||||||||||||||||
| 3500 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3501 | |||||||||||||||||||||||||||||||||||||||||||
| 3502 | =item * C<$data> - Same format as C<render_multi_series_line_chart_with_tooltips>: | ||||||||||||||||||||||||||||||||||||||||||
| 3503 | an array reference of C<< { name, data } >> series hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 3504 | |||||||||||||||||||||||||||||||||||||||||||
| 3505 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3506 | |||||||||||||||||||||||||||||||||||||||||||
| 3507 | Returns a string containing the complete HTML5 document. Clicking a legend entry | ||||||||||||||||||||||||||||||||||||||||||
| 3508 | toggles that series' opacity using an C<isVisible> boolean flag in the D3 click | ||||||||||||||||||||||||||||||||||||||||||
| 3509 | handler (opacity is set to C<isVisible ? 0 : 1> on each click). | ||||||||||||||||||||||||||||||||||||||||||
| 3510 | |||||||||||||||||||||||||||||||||||||||||||
| 3511 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 3512 | |||||||||||||||||||||||||||||||||||||||||||
| 3513 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3514 | |||||||||||||||||||||||||||||||||||||||||||
| 3515 | =item * Throws C<Data must be an array of hashes> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 3516 | |||||||||||||||||||||||||||||||||||||||||||
| 3517 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3518 | |||||||||||||||||||||||||||||||||||||||||||
| 3519 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 3520 | |||||||||||||||||||||||||||||||||||||||||||
| 3521 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 3522 | |||||||||||||||||||||||||||||||||||||||||||
| 3523 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3524 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3526 | |||||||||||||||||||||||||||||||||||||||||||
| 3527 | Each element of C<$data> is a hashref with keys C<name> (string) and | ||||||||||||||||||||||||||||||||||||||||||
| 3528 | C<data> (arrayref of hashrefs with C<label> and C<value> keys); | ||||||||||||||||||||||||||||||||||||||||||
| 3529 | passing C<undef> or a non-arrayref dies. | ||||||||||||||||||||||||||||||||||||||||||
| 3530 | |||||||||||||||||||||||||||||||||||||||||||
| 3531 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 3532 | |||||||||||||||||||||||||||||||||||||||||||
| 3533 | Str -- complete HTML5 document; legend clicks toggle series visibility. | ||||||||||||||||||||||||||||||||||||||||||
| 3534 | The C<isVisible> JS variable tracks current visibility state. | ||||||||||||||||||||||||||||||||||||||||||
| 3535 | Opacity toggled by C<isVisible ? 0 : 1>. | ||||||||||||||||||||||||||||||||||||||||||
| 3536 | |||||||||||||||||||||||||||||||||||||||||||
| 3537 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 3538 | |||||||||||||||||||||||||||||||||||||||||||
| 3539 | sub render_multi_series_line_chart_with_interactive_legends | ||||||||||||||||||||||||||||||||||||||||||
| 3540 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_multi_series_line_chart_with_interactive_legends: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 3541 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 3542 | 7 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 3543 | 7 | ▶ | die 'Data must be an array of hashes' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3544 | 5 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 3545 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 3546 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of hashes' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 3547 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 3548 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3549 | 5 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 3550 | |||||||||||||||||||||||||||||||||||||||||||
| 3551 | # Generate JSON for data | ||||||||||||||||||||||||||||||||||||||||||
| 3552 | 5 | my $json_data = _script_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 3553 | |||||||||||||||||||||||||||||||||||||||||||
| 3554 | # Generate HTML and D3.js code | ||||||||||||||||||||||||||||||||||||||||||
| 3555 | 5 | my $html = $self->_preamble(); | |||||||||||||||||||||||||||||||||||||||||
| 3556 | 5 | $html .= <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 3557 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 3558 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||||||||||||
| 3559 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||||||||||||||||||||||||||||||||||||||
| 3560 | 5 | <title>@{[ $self->_title_html() ]}</title> | |||||||||||||||||||||||||||||||||||||||||
| 3561 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 3562 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 3563 | .tooltip { | ||||||||||||||||||||||||||||||||||||||||||
| 3564 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 3565 | background-color: white; | ||||||||||||||||||||||||||||||||||||||||||
| 3566 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3567 | padding: 5px; | ||||||||||||||||||||||||||||||||||||||||||
| 3568 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 3569 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 3570 | opacity: 0; | ||||||||||||||||||||||||||||||||||||||||||
| 3571 | transform: translateY(-10px); | ||||||||||||||||||||||||||||||||||||||||||
| 3572 | transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out; | ||||||||||||||||||||||||||||||||||||||||||
| 3573 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3574 | .legend { | ||||||||||||||||||||||||||||||||||||||||||
| 3575 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 3576 | cursor: pointer; | ||||||||||||||||||||||||||||||||||||||||||
| 3577 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3578 | .legend rect { | ||||||||||||||||||||||||||||||||||||||||||
| 3579 | stroke-width: 1; | ||||||||||||||||||||||||||||||||||||||||||
| 3580 | stroke: #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3581 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3582 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 3583 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 3584 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 3585 | 5 | <h1 style="text-align: center;">@{[ $self->_title_html() ]}</h1> | |||||||||||||||||||||||||||||||||||||||||
| 3586 | 5 | <svg id="chart" ${\ $self->_svg_attrs() }></svg> | |||||||||||||||||||||||||||||||||||||||||
| 3587 | <div class="tooltip" id="tooltip"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 3588 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 3589 | const data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 3590 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 3591 | |||||||||||||||||||||||||||||||||||||||||||
| 3592 | const svg = d3.select("#chart"); | ||||||||||||||||||||||||||||||||||||||||||
| 3593 | const tooltip = d3.select("#tooltip"); | ||||||||||||||||||||||||||||||||||||||||||
| 3594 | const margin = { top: 20, right: 150, bottom: 40, left: 40 }; | ||||||||||||||||||||||||||||||||||||||||||
| 3595 | const width = $self->{width} - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 3596 | const height = $self->{height} - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 3597 | |||||||||||||||||||||||||||||||||||||||||||
| 3598 | const chart = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3599 | .attr("transform", `translate(\${margin.left},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 3600 | |||||||||||||||||||||||||||||||||||||||||||
| 3601 | const legendArea = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3602 | .attr("transform", `translate(\${width + margin.left + 20},\${margin.top})`); | ||||||||||||||||||||||||||||||||||||||||||
| 3603 | |||||||||||||||||||||||||||||||||||||||||||
| 3604 | // Extract all labels and flatten them into a unique array | ||||||||||||||||||||||||||||||||||||||||||
| 3605 | const allLabels = Array.from(new Set(data.flatMap(series => series.data.map(d => d.label)))); | ||||||||||||||||||||||||||||||||||||||||||
| 3606 | |||||||||||||||||||||||||||||||||||||||||||
| 3607 | const x = d3.scalePoint() | ||||||||||||||||||||||||||||||||||||||||||
| 3608 | .domain(allLabels) | ||||||||||||||||||||||||||||||||||||||||||
| 3609 | .range([0, width]); | ||||||||||||||||||||||||||||||||||||||||||
| 3610 | |||||||||||||||||||||||||||||||||||||||||||
| 3611 | const y = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 3612 | .domain([0, d3.max(data.flatMap(series => series.data.map(d => d.value)))]) | ||||||||||||||||||||||||||||||||||||||||||
| 3613 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 3614 | .range([height, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 3615 | |||||||||||||||||||||||||||||||||||||||||||
| 3616 | // Define color scale for series | ||||||||||||||||||||||||||||||||||||||||||
| 3617 | const color = d3.scaleOrdinal($CATEGORY10_JS); | ||||||||||||||||||||||||||||||||||||||||||
| 3618 | |||||||||||||||||||||||||||||||||||||||||||
| 3619 | // Add axes | ||||||||||||||||||||||||||||||||||||||||||
| 3620 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3621 | .call(d3.axisLeft(y)); | ||||||||||||||||||||||||||||||||||||||||||
| 3622 | |||||||||||||||||||||||||||||||||||||||||||
| 3623 | chart.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3624 | .attr("transform", `translate(0,\${height})`) | ||||||||||||||||||||||||||||||||||||||||||
| 3625 | .call(d3.axisBottom(x)) | ||||||||||||||||||||||||||||||||||||||||||
| 3626 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3627 | .attr("transform", "rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 3628 | .style("text-anchor", "end"); | ||||||||||||||||||||||||||||||||||||||||||
| 3629 | |||||||||||||||||||||||||||||||||||||||||||
| 3630 | // Draw lines for each series | ||||||||||||||||||||||||||||||||||||||||||
| 3631 | data.forEach((series, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3632 | const line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 3633 | .x(d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3634 | .y(d => y(d.value)); | ||||||||||||||||||||||||||||||||||||||||||
| 3635 | |||||||||||||||||||||||||||||||||||||||||||
| 3636 | // Add line | ||||||||||||||||||||||||||||||||||||||||||
| 3637 | chart.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 3638 | .datum(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3639 | .attr("fill", "none") | ||||||||||||||||||||||||||||||||||||||||||
| 3640 | .attr("stroke", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3641 | .attr("stroke-width", 2) | ||||||||||||||||||||||||||||||||||||||||||
| 3642 | .attr("class", \`line-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3643 | .attr("d", line); | ||||||||||||||||||||||||||||||||||||||||||
| 3644 | |||||||||||||||||||||||||||||||||||||||||||
| 3645 | // Add points and tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 3646 | chart.selectAll(\`circle.series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3647 | .data(series.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3648 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 3649 | .attr("class", \`series-\${i}\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3650 | .attr("cx", d => x(d.label)) | ||||||||||||||||||||||||||||||||||||||||||
| 3651 | .attr("cy", d => y(d.value)) | ||||||||||||||||||||||||||||||||||||||||||
| 3652 | .attr("r", 4) | ||||||||||||||||||||||||||||||||||||||||||
| 3653 | .attr("fill", color(i)) | ||||||||||||||||||||||||||||||||||||||||||
| 3654 | .on("mouseover", (event, d) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3655 | tooltip.style("opacity", 1) | ||||||||||||||||||||||||||||||||||||||||||
| 3656 | .style("transform", "translateY(0)") | ||||||||||||||||||||||||||||||||||||||||||
| 3657 | .html(\`Series: <b>\${esc(series.name)}<\\/b><br>Label: <b>\${esc(d.label)}<\\/b><br>Value: <b>\${esc(d.value)}<\\/b>\`) | ||||||||||||||||||||||||||||||||||||||||||
| 3658 | .style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3659 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3660 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3661 | .on("mousemove", (event) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3662 | tooltip.style("left", (event.pageX + 10) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 3663 | .style("top", (event.pageY - 30) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 3664 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 3665 | .on("mouseout", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 3666 | tooltip.style("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 3667 | .style("transform", "translateY(-10px)"); | ||||||||||||||||||||||||||||||||||||||||||
| 3668 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3669 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3670 | |||||||||||||||||||||||||||||||||||||||||||
| 3671 | // Add legend with interactivity | ||||||||||||||||||||||||||||||||||||||||||
| 3672 | data.forEach((series, i) => { | ||||||||||||||||||||||||||||||||||||||||||
| 3673 | const legend = legendArea.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 3674 | .attr("transform", `translate(0, \${i * 20})`) | ||||||||||||||||||||||||||||||||||||||||||
| 3675 | .attr("class", "legend") | ||||||||||||||||||||||||||||||||||||||||||
| 3676 | .on("click", () => { | ||||||||||||||||||||||||||||||||||||||||||
| 3677 | const isVisible = d3.selectAll(\`path.line-\${i}\`).style("opacity") === "1"; | ||||||||||||||||||||||||||||||||||||||||||
| 3678 | |||||||||||||||||||||||||||||||||||||||||||
| 3679 | // Toggle visibility | ||||||||||||||||||||||||||||||||||||||||||
| 3680 | d3.selectAll(\`path.line-\${i}\`).style("opacity", isVisible ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||||
| 3681 | d3.selectAll(\`circle.series-\${i}\`).style("opacity", isVisible ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||||
| 3682 | |||||||||||||||||||||||||||||||||||||||||||
| 3683 | // Dim legend if series is hidden | ||||||||||||||||||||||||||||||||||||||||||
| 3684 | legend.select("text").style("opacity", isVisible ? 0.5 : 1); | ||||||||||||||||||||||||||||||||||||||||||
| 3685 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3686 | |||||||||||||||||||||||||||||||||||||||||||
| 3687 | legend.append("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 3688 | .attr("width", 12) | ||||||||||||||||||||||||||||||||||||||||||
| 3689 | .attr("height", 12) | ||||||||||||||||||||||||||||||||||||||||||
| 3690 | .attr("fill", color(i)); | ||||||||||||||||||||||||||||||||||||||||||
| 3691 | |||||||||||||||||||||||||||||||||||||||||||
| 3692 | legend.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3693 | .attr("x", 20) | ||||||||||||||||||||||||||||||||||||||||||
| 3694 | .attr("y", 10) | ||||||||||||||||||||||||||||||||||||||||||
| 3695 | .text(series.name) | ||||||||||||||||||||||||||||||||||||||||||
| 3696 | .style("alignment-baseline", "middle"); | ||||||||||||||||||||||||||||||||||||||||||
| 3697 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3698 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 3699 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 3700 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 3701 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 3702 | |||||||||||||||||||||||||||||||||||||||||||
| 3703 | 5 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 3704 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3705 | |||||||||||||||||||||||||||||||||||||||||||
| 3706 | =head2 render_multi_series_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 3707 | |||||||||||||||||||||||||||||||||||||||||||
| 3708 | my $fragment = $chart->render_multi_series_line_chart_snippet(\@series); | ||||||||||||||||||||||||||||||||||||||||||
| 3709 | my $fragment = $chart->render_multi_series_line_chart_snippet(\@series, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 3710 | # $fragment->{svg_id} - the id attribute of the <svg> element | ||||||||||||||||||||||||||||||||||||||||||
| 3711 | # $fragment->{html} - embeddable HTML fragment (style + svg + script) | ||||||||||||||||||||||||||||||||||||||||||
| 3712 | |||||||||||||||||||||||||||||||||||||||||||
| 3713 | Generates an embeddable multi-series line chart fragment. Unlike | ||||||||||||||||||||||||||||||||||||||||||
| 3714 | C<render_multi_series_line_chart_with_interactive_legends>, this method returns | ||||||||||||||||||||||||||||||||||||||||||
| 3715 | a bare fragment with no C<< <html> >>, C<< <head> >>, or C<< <body> >> wrapper; | ||||||||||||||||||||||||||||||||||||||||||
| 3716 | D3.js must be loaded by the host page. | ||||||||||||||||||||||||||||||||||||||||||
| 3717 | |||||||||||||||||||||||||||||||||||||||||||
| 3718 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 3719 | |||||||||||||||||||||||||||||||||||||||||||
| 3720 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3721 | |||||||||||||||||||||||||||||||||||||||||||
| 3722 | =item * C<\@series> - An array reference of series. Each series is a hash | ||||||||||||||||||||||||||||||||||||||||||
| 3723 | reference with a C<name> (string) and a C<data> array reference of | ||||||||||||||||||||||||||||||||||||||||||
| 3724 | C<< { label => Str, value => Num } >> points: | ||||||||||||||||||||||||||||||||||||||||||
| 3725 | |||||||||||||||||||||||||||||||||||||||||||
| 3726 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 3727 | { name => 'North', data => [ { label => 'Jan', value => 12 }, ... ] }, | ||||||||||||||||||||||||||||||||||||||||||
| 3728 | { name => 'South', data => [ { label => 'Jan', value => 8 }, ... ] }, | ||||||||||||||||||||||||||||||||||||||||||
| 3729 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 3730 | |||||||||||||||||||||||||||||||||||||||||||
| 3731 | Series need not share labels: the X domain is the union of every series' labels, | ||||||||||||||||||||||||||||||||||||||||||
| 3732 | in the order C<x_order> gives. One line and one set of dots is drawn per series, and a | ||||||||||||||||||||||||||||||||||||||||||
| 3733 | clickable legend toggles each series on and off. | ||||||||||||||||||||||||||||||||||||||||||
| 3734 | |||||||||||||||||||||||||||||||||||||||||||
| 3735 | =item * C<\%opts> - Optional hash reference: | ||||||||||||||||||||||||||||||||||||||||||
| 3736 | |||||||||||||||||||||||||||||||||||||||||||
| 3737 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3738 | |||||||||||||||||||||||||||||||||||||||||||
| 3739 | =item * C<scheme> (string, default C<'tableau10'>) - D3 categorical colour | ||||||||||||||||||||||||||||||||||||||||||
| 3740 | scheme (for example C<'category10'>, C<'set2'>, C<'set3'>, C<'paired'>). | ||||||||||||||||||||||||||||||||||||||||||
| 3741 | C<categorical_schemes()> lists the names, and C<palette()> hands a Perl caller | ||||||||||||||||||||||||||||||||||||||||||
| 3742 | the colours of one. | ||||||||||||||||||||||||||||||||||||||||||
| 3743 | |||||||||||||||||||||||||||||||||||||||||||
| 3744 | =item * C<x_label> (string, default C<''>) - Label shown below the X axis. | ||||||||||||||||||||||||||||||||||||||||||
| 3745 | |||||||||||||||||||||||||||||||||||||||||||
| 3746 | =item * C<y_label> (string, default C<''>) - Label shown beside the Y axis. | ||||||||||||||||||||||||||||||||||||||||||
| 3747 | |||||||||||||||||||||||||||||||||||||||||||
| 3748 | =item * C<animated> (boolean, default 0) - If true each line draws itself left | ||||||||||||||||||||||||||||||||||||||||||
| 3749 | to right by animating its stroke dash offset. Respects | ||||||||||||||||||||||||||||||||||||||||||
| 3750 | C<prefers-reduced-motion>. | ||||||||||||||||||||||||||||||||||||||||||
| 3751 | |||||||||||||||||||||||||||||||||||||||||||
| 3752 | =item * C<x_order> (C<'auto'>, C<'data'> or C<'sort'>, default C<'auto'>) - the | ||||||||||||||||||||||||||||||||||||||||||
| 3753 | order of the X domain. C<'data'> takes the labels in the order they are met, | ||||||||||||||||||||||||||||||||||||||||||
| 3754 | series by series, as before 0.29. C<'sort'> sorts them: numerically when every | ||||||||||||||||||||||||||||||||||||||||||
| 3755 | label is a number, otherwise as strings. C<'auto'> sorts them when every label | ||||||||||||||||||||||||||||||||||||||||||
| 3756 | is a number or an ISO 8601 date or date-time (C<2026-01-15>, C<2026-01>, | ||||||||||||||||||||||||||||||||||||||||||
| 3757 | C<2026-01-15T09:30>), and otherwise keeps the order they are met in, so that | ||||||||||||||||||||||||||||||||||||||||||
| 3758 | C<Jan>, C<Feb>, C<Mar> stay in the caller's order. Taken as met, two series of | ||||||||||||||||||||||||||||||||||||||||||
| 3759 | dates gave an axis such as C<15, 19, 16, 17>. Whenever the labels are sorted, | ||||||||||||||||||||||||||||||||||||||||||
| 3760 | each line's points are drawn in the axis order. The caller's series are not | ||||||||||||||||||||||||||||||||||||||||||
| 3761 | changed. | ||||||||||||||||||||||||||||||||||||||||||
| 3762 | |||||||||||||||||||||||||||||||||||||||||||
| 3763 | =item * C<responsive> (boolean, optional) - SVG responsive sizing override. | ||||||||||||||||||||||||||||||||||||||||||
| 3764 | |||||||||||||||||||||||||||||||||||||||||||
| 3765 | =item * C<id> (string, default C<'chart'>) - C<id> of the C<<svg>> element. | ||||||||||||||||||||||||||||||||||||||||||
| 3766 | |||||||||||||||||||||||||||||||||||||||||||
| 3767 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3768 | |||||||||||||||||||||||||||||||||||||||||||
| 3769 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3770 | |||||||||||||||||||||||||||||||||||||||||||
| 3771 | Returns C<< { svg_id => 'chart', html => Str } >>. | ||||||||||||||||||||||||||||||||||||||||||
| 3772 | |||||||||||||||||||||||||||||||||||||||||||
| 3773 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 3774 | |||||||||||||||||||||||||||||||||||||||||||
| 3775 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 3776 | |||||||||||||||||||||||||||||||||||||||||||
| 3777 | =item * Throws C<Data must be an array of series> when C<\@series> is not an | ||||||||||||||||||||||||||||||||||||||||||
| 3778 | ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 3779 | |||||||||||||||||||||||||||||||||||||||||||
| 3780 | =item * Throws C<Each series must be a hashref with name and data> when a | ||||||||||||||||||||||||||||||||||||||||||
| 3781 | series is not a hashref, has no C<name>, or has a C<data> that is not an ARRAY | ||||||||||||||||||||||||||||||||||||||||||
| 3782 | reference. | ||||||||||||||||||||||||||||||||||||||||||
| 3783 | |||||||||||||||||||||||||||||||||||||||||||
| 3784 | =item * Throws C<Unknown x_order: E<lt>valueE<gt>> when C<x_order> is not | ||||||||||||||||||||||||||||||||||||||||||
| 3785 | C<auto>, C<data> or C<sort>. | ||||||||||||||||||||||||||||||||||||||||||
| 3786 | |||||||||||||||||||||||||||||||||||||||||||
| 3787 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 3788 | |||||||||||||||||||||||||||||||||||||||||||
| 3789 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 3790 | |||||||||||||||||||||||||||||||||||||||||||
| 3791 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 3792 | |||||||||||||||||||||||||||||||||||||||||||
| 3793 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3794 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3795 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 3796 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 3797 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 3798 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 3799 | scheme => { type => 'string', optional => 1, default => 'tableau10' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3800 | x_label => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3801 | y_label => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3802 | x_order => { type => 'string', optional => 1, default => 'auto' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3803 | animated => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 3804 | responsive => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 3805 | id => { type => 'string', optional => 1, default => 'chart' }, | ||||||||||||||||||||||||||||||||||||||||||
| 3806 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 3807 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 3808 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3809 | |||||||||||||||||||||||||||||||||||||||||||
| 3810 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 3811 | |||||||||||||||||||||||||||||||||||||||||||
| 3812 | HashRef -- { svg_id => 'chart', html => Str } | ||||||||||||||||||||||||||||||||||||||||||
| 3813 | |||||||||||||||||||||||||||||||||||||||||||
| 3814 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 3815 | |||||||||||||||||||||||||||||||||||||||||||
| 3816 | sub render_multi_series_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 3817 | 39 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_multi_series_line_chart_snippet: called CC=33 SCAR=35.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 3818 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 3819 | 39 | ▶ | my $raw = Params::Get::get_params('series_data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("series_data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("series_data", \@_) // {}
Params::Get::get_params("series_data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("series_data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 3820 | 39 | ▶ | die 'Data must be an array of series' unless ref($raw->{series_data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3821 | 38 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 3822 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 3823 | schema => { series_data => { type => 'arrayref', error_msg => 'Data must be an array of series' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 3824 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 3825 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3826 | 38 | my $series_data = $raw->{series_data}; | |||||||||||||||||||||||||||||||||||||||||
| 3827 | 38 | my $opts = {%$raw}; delete $opts->{series_data}; | |||||||||||||||||||||||||||||||||||||||||
| 3828 | 38 | for my $s (@$series_data) { | |||||||||||||||||||||||||||||||||||||||||
| 3829 | die 'Each series must be a hashref with name and data' | ||||||||||||||||||||||||||||||||||||||||||
| 3830 | 52 | ▶ | unless ref($s) eq 'HASH' && defined $s->{name} && ref($s->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $s eq "HASH" and defined $s->{"name"} and ref $s->{"data"} eq "ARRAY"
ref $s eq "HASH" and defined $s->{"name"}
MC/DC100%3/3
ref $s eq "HASH" and defined $s->{"name"} and ref $s->{"data"} eq "ARRAY"
ref $s eq "HASH"
defined $s->{"name"}
ref $s->{"data"} eq "ARRAY"
Truth table100%4/4
A and B and C
A= ref $s eq "HASH"B= defined $s->{"name"}C= ref $s->{"data"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 3831 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3832 | |||||||||||||||||||||||||||||||||||||||||||
| 3833 | 35 | ▶ | my $x_order = $opts->{x_order} // 'auto'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_order"} // "auto"
MC/DC100%1/1
$opts->{"x_order"} // "auto"
$opts->{"x_order"}
Truth table100%2/2
A
A= $opts->{"x_order"}
| |||||||||||||||||||||||||||||||||||||||||||
| 3834 | 35 | ▶ | die "Unknown x_order: $x_order" unless $x_order =~ /\A(?:auto|data|sort)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3835 | |||||||||||||||||||||||||||||||||||||||||||
| 3836 | # The x axis is every series' labels. Taken as they are met, one series | ||||||||||||||||||||||||||||||||||||||||||
| 3837 | # after another, two series of dates give an axis that runs 15, 19, 16, 17: | ||||||||||||||||||||||||||||||||||||||||||
| 3838 | # so labels that order themselves -- all numbers, or all ISO dates and | ||||||||||||||||||||||||||||||||||||||||||
| 3839 | # times -- are sorted, and the points of each line follow the axis. | ||||||||||||||||||||||||||||||||||||||||||
| 3840 | # Anything else keeps the order it is met in, which is the order a caller | ||||||||||||||||||||||||||||||||||||||||||
| 3841 | # chose (Jan, Feb, Mar). The labels are strings in the script, so a 1 in | ||||||||||||||||||||||||||||||||||||||||||
| 3842 | # one series and a "1" in another are one point, as they are here. | ||||||||||||||||||||||||||||||||||||||||||
| 3843 | 34 | ▶ | my $label_of = sub { my $p = shift; ref($p) eq 'HASH' && defined($p->{label}) ? "$p->{label}" : '' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
Branch: 1 branch
Condition33%1/3
ref $p eq "HASH" and defined $p->{"label"}
MC/DC0%0/2
ref $p eq "HASH" and defined $p->{"label"}
ref $p eq "HASH"
defined $p->{"label"}
Truth table33%1/3
A and B
A= ref $p eq "HASH"B= defined $p->{"label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 3844 | 34 | my (@labels, %seen); | |||||||||||||||||||||||||||||||||||||||||
| 3845 | 34 | for my $s (@$series_data) { | |||||||||||||||||||||||||||||||||||||||||
| 3846 | 47 | for my $p (@{ $s->{data} }) { | |||||||||||||||||||||||||||||||||||||||||
| 3847 | 71 | my $l = $label_of->($p); | |||||||||||||||||||||||||||||||||||||||||
| 3848 | 71 | ▶ | push @labels, $l unless $seen{$l}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3849 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3850 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3851 | 34 | ▶ | my $all_numeric = @labels && !grep { !looks_like_number($_) || /\A\s*[-+]?(?:nan|inf)/i } @labels; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
@labels && !grep({not &looks_like_number($_) or /\A\s*[-+]?(?:nan|inf)/i;} @labels)
!&looks_like_number($_) || /\A\s*[-+]?(?:nan|inf)/i
MC/DC50%1/2
@labels && !grep({not &looks_like_number($_) or /\A\s*[-+]?(?:nan|inf)/i;} @labels)
@labels
!grep({not &looks_like_number($_) or /\A\s*[-+]?(?:nan|inf)/i;} @labels)
MC/DC50%1/2
!&looks_like_number($_) || /\A\s*[-+]?(?:nan|inf)/i
!&looks_like_number($_)
/\A\s*[-+]?(?:nan|inf)/i
Truth table66%2/3
A && not(B)
A= @labelsB= !grep({not &looks_like_number($_) or /\A\s*[-+]?(?:nan|inf)/i;} @labels)
Truth table66%2/3
not(A) || B
A= !&looks_like_number($_)B= /\A\s*[-+]?(?:nan|inf)/i
| |||||||||||||||||||||||||||||||||||||||||||
| 3852 | my $all_dates = @labels && !grep { | ||||||||||||||||||||||||||||||||||||||||||
| 3853 | 34 | ▶ | !/\A\d{4}-\d{2}(?:-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[-+]\d{2}:?\d{2})?)?)?\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
@labels && !grep({not /\A\d{4}-\d{2}(?:-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[-+]\d{2}:?\d{2})?)?)?\z/;} @labels)
MC/DC50%1/2
@labels && !grep({not /\A\d{4}-\d{2}(?:-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[-+]\d{2}:?\d{2})?)?)?\z/;} @labels)
@labels
!grep({not /\A\d{4}-\d{2}(?:-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[-+]\d{2}:?\d{2})?)?)?\z/;} @labels)
Truth table66%2/3
A && not(B)
A= @labelsB= !grep({not /\A\d{4}-\d{2}(?:-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[-+]\d{2}:?\d{2})?)?)?\z/;} @labels)
| |||||||||||||||||||||||||||||||||||||||||||
| 3854 | } @labels; | ||||||||||||||||||||||||||||||||||||||||||
| 3855 | 34 | my $sorted = 1; | |||||||||||||||||||||||||||||||||||||||||
| 3856 | 34 | ▶ | if ($x_order ne 'data' && $all_numeric) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$x_order ne "data" and $all_numeric
MC/DC100%2/2
$x_order ne "data" and $all_numeric
$x_order ne "data"
$all_numeric
Truth table100%3/3
A and B
A= $x_order ne "data"B= $all_numeric
| |||||||||||||||||||||||||||||||||||||||||||
| 3857 | 1 | @labels = sort { $a <=> $b } @labels; | |||||||||||||||||||||||||||||||||||||||||
| 3858 | ▶ | } elsif ($x_order eq 'sort' || ($x_order eq 'auto' && $all_dates)) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$x_order eq "sort" or $x_order eq "auto" and $all_dates
$x_order eq "auto" and $all_dates
MC/DC100%3/3
$x_order eq "sort" or $x_order eq "auto" and $all_dates
$x_order eq "sort"
$x_order eq "auto"
$all_dates
Truth table100%4/4
A or B and C
A= $x_order eq "sort"B= $x_order eq "auto"C= $all_dates
| |||||||||||||||||||||||||||||||||||||||||||
| 3859 | 2 | @labels = sort @labels; | |||||||||||||||||||||||||||||||||||||||||
| 3860 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 3861 | 31 | $sorted = 0; | |||||||||||||||||||||||||||||||||||||||||
| 3862 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3863 | 34 | my %position; | |||||||||||||||||||||||||||||||||||||||||
| 3864 | 34 | @position{@labels} = (0 .. $#labels); | |||||||||||||||||||||||||||||||||||||||||
| 3865 | # Copies: the caller's series are not reordered. Unsorted, each line keeps | ||||||||||||||||||||||||||||||||||||||||||
| 3866 | # its points in the order given, as it always has. | ||||||||||||||||||||||||||||||||||||||||||
| 3867 | my @drawn = !$sorted ? @$series_data : map { | ||||||||||||||||||||||||||||||||||||||||||
| 3868 | 34 | ▶ | my $s = $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3869 | 7 | +{ %$s, data => [ sort { $position{ $label_of->($a) } <=> $position{ $label_of->($b) } } @{ $s->{data} } ] }; | |||||||||||||||||||||||||||||||||||||||||
| 3870 | } @$series_data; | ||||||||||||||||||||||||||||||||||||||||||
| 3871 | |||||||||||||||||||||||||||||||||||||||||||
| 3872 | 34 | my $json_data = _script_json(\@drawn); | |||||||||||||||||||||||||||||||||||||||||
| 3873 | 34 | my $labels_json = _script_json(\@labels); | |||||||||||||||||||||||||||||||||||||||||
| 3874 | |||||||||||||||||||||||||||||||||||||||||||
| 3875 | 34 | ▶ | my $scheme = $opts->{scheme} // $DEFAULT_CATEGORICAL_SCHEME; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$opts->{"scheme"} // $DEFAULT_CATEGORICAL_SCHEME
MC/DC0%0/2
$opts->{"scheme"} // $DEFAULT_CATEGORICAL_SCHEME
$opts->{"scheme"}
$DEFAULT_CATEGORICAL_SCHEME
Truth table66%2/3
A // B
A= $opts->{"scheme"}B= $DEFAULT_CATEGORICAL_SCHEME
| |||||||||||||||||||||||||||||||||||||||||||
| 3876 | 34 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 3877 | 34 | ▶ | my $y_label = $opts->{y_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"y_label"} // ""
MC/DC100%1/1
$opts->{"y_label"} // ""
$opts->{"y_label"}
Truth table100%2/2
A
A= $opts->{"y_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 3878 | 34 | ▶ | my $animated = $opts->{animated} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"animated"} // 0
MC/DC100%1/1
$opts->{"animated"} // 0
$opts->{"animated"}
Truth table100%2/2
A
A= $opts->{"animated"}
| |||||||||||||||||||||||||||||||||||||||||||
| 3879 | 34 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 3880 | 34 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 3881 | |||||||||||||||||||||||||||||||||||||||||||
| 3882 | 34 | my $scheme_js = _scheme_js($scheme); | |||||||||||||||||||||||||||||||||||||||||
| 3883 | |||||||||||||||||||||||||||||||||||||||||||
| 3884 | 34 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 3885 | |||||||||||||||||||||||||||||||||||||||||||
| 3886 | 34 | my $x_label_esc = $js_esc->($x_label); | |||||||||||||||||||||||||||||||||||||||||
| 3887 | 34 | my $y_label_esc = $js_esc->($y_label); | |||||||||||||||||||||||||||||||||||||||||
| 3888 | |||||||||||||||||||||||||||||||||||||||||||
| 3889 | 34 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 3890 | 34 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 3891 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 3892 | 34 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 3893 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 3894 | |||||||||||||||||||||||||||||||||||||||||||
| 3895 | 34 | my $margin_right = 150; # room for legend | |||||||||||||||||||||||||||||||||||||||||
| 3896 | 34 | ▶ | my $margin_left = $y_label ? 60 : 45; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3897 | 34 | my $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 3898 | 34 | ▶ | my $margin_bottom = $x_label ? 60 : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3899 | 34 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 3900 | 34 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 3901 | |||||||||||||||||||||||||||||||||||||||||||
| 3902 | 34 | ▶ | my $x_label_block = $x_label ? <<"XLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3903 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3904 | .attr("class","msl-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 3905 | .attr("x", innerW / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 3906 | .attr("y", innerH + (margin.bottom - 10)) | ||||||||||||||||||||||||||||||||||||||||||
| 3907 | .attr("text-anchor","middle") | ||||||||||||||||||||||||||||||||||||||||||
| 3908 | .text("$x_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 3909 | XLB | ||||||||||||||||||||||||||||||||||||||||||
| 3910 | |||||||||||||||||||||||||||||||||||||||||||
| 3911 | 34 | ▶ | my $y_label_block = $y_label ? <<"YLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3912 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 3913 | .attr("class","msl-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 3914 | .attr("transform","rotate(-90)") | ||||||||||||||||||||||||||||||||||||||||||
| 3915 | .attr("x", -innerH / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 3916 | .attr("y", -(margin.left - 14)) | ||||||||||||||||||||||||||||||||||||||||||
| 3917 | .attr("text-anchor","middle") | ||||||||||||||||||||||||||||||||||||||||||
| 3918 | .text("$y_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 3919 | YLB | ||||||||||||||||||||||||||||||||||||||||||
| 3920 | |||||||||||||||||||||||||||||||||||||||||||
| 3921 | 34 | ▶ | my $anim_block = $animated ? <<"ANIM" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 3922 | var noAnim = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; | ||||||||||||||||||||||||||||||||||||||||||
| 3923 | if (!noAnim) { | ||||||||||||||||||||||||||||||||||||||||||
| 3924 | g.selectAll(".msl-line") | ||||||||||||||||||||||||||||||||||||||||||
| 3925 | .each(function() { | ||||||||||||||||||||||||||||||||||||||||||
| 3926 | var totalLen = this.getTotalLength(); | ||||||||||||||||||||||||||||||||||||||||||
| 3927 | d3.select(this) | ||||||||||||||||||||||||||||||||||||||||||
| 3928 | .attr("stroke-dasharray", totalLen) | ||||||||||||||||||||||||||||||||||||||||||
| 3929 | .attr("stroke-dashoffset", totalLen) | ||||||||||||||||||||||||||||||||||||||||||
| 3930 | .transition().duration(1200).ease(d3.easeLinear) | ||||||||||||||||||||||||||||||||||||||||||
| 3931 | .attr("stroke-dashoffset", 0); | ||||||||||||||||||||||||||||||||||||||||||
| 3932 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 3933 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3934 | ANIM | ||||||||||||||||||||||||||||||||||||||||||
| 3935 | |||||||||||||||||||||||||||||||||||||||||||
| 3936 | 34 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 3937 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 3938 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 3939 | position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 3940 | border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none; | ||||||||||||||||||||||||||||||||||||||||||
| 3941 | display:none;line-height:1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 3942 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3943 | .msl-axis-label{font-size:11px;fill:#555;} | ||||||||||||||||||||||||||||||||||||||||||
| 3944 | .msl-line{fill:none;stroke-width:2;} | ||||||||||||||||||||||||||||||||||||||||||
| 3945 | .msl-dot{cursor:default;} | ||||||||||||||||||||||||||||||||||||||||||
| 3946 | .msl-leg-item{cursor:pointer;} | ||||||||||||||||||||||||||||||||||||||||||
| 3947 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 3948 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 3949 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 3950 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 3951 | (function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 3952 | var series = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 3953 | var margin = {top:$margin_top,right:$margin_right,bottom:$margin_bottom,left:$margin_left}; | ||||||||||||||||||||||||||||||||||||||||||
| 3954 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 3955 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 3956 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 3957 | var g = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 3958 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 3959 | |||||||||||||||||||||||||||||||||||||||||||
| 3960 | function esc(s) { | ||||||||||||||||||||||||||||||||||||||||||
| 3961 | return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||||||||||||||||||||||||||||||||||||||||||
| 3962 | } | ||||||||||||||||||||||||||||||||||||||||||
| 3963 | |||||||||||||||||||||||||||||||||||||||||||
| 3964 | var allLabels = $labels_json; | ||||||||||||||||||||||||||||||||||||||||||
| 3965 | var allVals = series.flatMap(function(s){return s.data.map(function(d){return d.value;});}); | ||||||||||||||||||||||||||||||||||||||||||
| 3966 | |||||||||||||||||||||||||||||||||||||||||||
| 3967 | var xScale = d3.scalePoint().domain(allLabels).range([0,innerW]); | ||||||||||||||||||||||||||||||||||||||||||
| 3968 | var yScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 3969 | .domain([Math.min(0,d3.min(allVals)), d3.max(allVals)]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 3970 | .range([innerH,0]); | ||||||||||||||||||||||||||||||||||||||||||
| 3971 | |||||||||||||||||||||||||||||||||||||||||||
| 3972 | var color = d3.scaleOrdinal($scheme_js).domain(series.map(function(s){return s.name;})); | ||||||||||||||||||||||||||||||||||||||||||
| 3973 | |||||||||||||||||||||||||||||||||||||||||||
| 3974 | g.append("g").attr("transform","translate(0,"+innerH+")") | ||||||||||||||||||||||||||||||||||||||||||
| 3975 | .call(d3.axisBottom(xScale)) | ||||||||||||||||||||||||||||||||||||||||||
| 3976 | .selectAll("text").attr("transform","rotate(-45)").style("text-anchor","end"); | ||||||||||||||||||||||||||||||||||||||||||
| 3977 | g.append("g").call(d3.axisLeft(yScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 3978 | |||||||||||||||||||||||||||||||||||||||||||
| 3979 | $x_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 3980 | $y_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 3981 | |||||||||||||||||||||||||||||||||||||||||||
| 3982 | var lineGen = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 3983 | .x(function(d){return xScale(d.label == null ? "" : String(d.label));}) | ||||||||||||||||||||||||||||||||||||||||||
| 3984 | .y(function(d){return yScale(d.value);}); | ||||||||||||||||||||||||||||||||||||||||||
| 3985 | |||||||||||||||||||||||||||||||||||||||||||
| 3986 | series.forEach(function(s,i){ | ||||||||||||||||||||||||||||||||||||||||||
| 3987 | g.append("path") | ||||||||||||||||||||||||||||||||||||||||||
| 3988 | .datum(s.data) | ||||||||||||||||||||||||||||||||||||||||||
| 3989 | .attr("class","msl-line") | ||||||||||||||||||||||||||||||||||||||||||
| 3990 | .attr("stroke",color(s.name)) | ||||||||||||||||||||||||||||||||||||||||||
| 3991 | .attr("d",lineGen); | ||||||||||||||||||||||||||||||||||||||||||
| 3992 | |||||||||||||||||||||||||||||||||||||||||||
| 3993 | g.selectAll(".msl-dot-"+i) | ||||||||||||||||||||||||||||||||||||||||||
| 3994 | .data(s.data).join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 3995 | .attr("class","msl-dot msl-dot-"+i) | ||||||||||||||||||||||||||||||||||||||||||
| 3996 | .attr("cx",function(d){return xScale(d.label == null ? "" : String(d.label));}) | ||||||||||||||||||||||||||||||||||||||||||
| 3997 | .attr("cy",function(d){return yScale(d.value);}) | ||||||||||||||||||||||||||||||||||||||||||
| 3998 | .attr("r",4) | ||||||||||||||||||||||||||||||||||||||||||
| 3999 | .attr("fill",color(s.name)) | ||||||||||||||||||||||||||||||||||||||||||
| 4000 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 4001 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 4002 | .html("<b>"+esc(d.label)+"<\\/b><br\/>"+esc(s.name)+": <b>"+esc(d.value)+"<\\/b>"); | ||||||||||||||||||||||||||||||||||||||||||
| 4003 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 4004 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 4005 | tip.style("left",(event.pageX+12)+"px").style("top",(event.pageY-24)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 4006 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 4007 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 4008 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4009 | |||||||||||||||||||||||||||||||||||||||||||
| 4010 | // Legend | ||||||||||||||||||||||||||||||||||||||||||
| 4011 | var legG = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 4012 | .attr("transform","translate("+(margin.left+innerW+12)+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 4013 | series.forEach(function(s,i){ | ||||||||||||||||||||||||||||||||||||||||||
| 4014 | var row = legG.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 4015 | .attr("class","msl-leg-item") | ||||||||||||||||||||||||||||||||||||||||||
| 4016 | .attr("transform","translate(0,"+(i*18)+")") | ||||||||||||||||||||||||||||||||||||||||||
| 4017 | .on("click",function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 4018 | var lines = g.selectAll(".msl-line").filter(function(d,j){return j===i;}); | ||||||||||||||||||||||||||||||||||||||||||
| 4019 | var dots = g.selectAll(".msl-dot-"+i); | ||||||||||||||||||||||||||||||||||||||||||
| 4020 | var vis = lines.style("opacity") !== "0"; | ||||||||||||||||||||||||||||||||||||||||||
| 4021 | lines.style("opacity", vis ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||||
| 4022 | dots.style("opacity", vis ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||||
| 4023 | row.select("text").style("opacity", vis ? 0.4 : 1); | ||||||||||||||||||||||||||||||||||||||||||
| 4024 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4025 | row.append("rect").attr("width",11).attr("height",11).attr("fill",color(s.name)).attr("rx",2); | ||||||||||||||||||||||||||||||||||||||||||
| 4026 | row.append("text").attr("x",15).attr("y",10) | ||||||||||||||||||||||||||||||||||||||||||
| 4027 | .style("font-size","11px").style("fill","#444") | ||||||||||||||||||||||||||||||||||||||||||
| 4028 | .text(s.name.length>14?s.name.slice(0,13)+"…":s.name); | ||||||||||||||||||||||||||||||||||||||||||
| 4029 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4030 | |||||||||||||||||||||||||||||||||||||||||||
| 4031 | $anim_block | ||||||||||||||||||||||||||||||||||||||||||
| 4032 | })(); | ||||||||||||||||||||||||||||||||||||||||||
| 4033 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 4034 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 4035 | |||||||||||||||||||||||||||||||||||||||||||
| 4036 | 34 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 4037 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4038 | |||||||||||||||||||||||||||||||||||||||||||
| 4039 | =head2 render_scatter_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4040 | |||||||||||||||||||||||||||||||||||||||||||
| 4041 | my $fragment = $chart->render_scatter_chart_snippet($data); | ||||||||||||||||||||||||||||||||||||||||||
| 4042 | my $fragment = $chart->render_scatter_chart_snippet($data, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 4043 | # $fragment->{svg_id} - the id attribute of the <svg> element | ||||||||||||||||||||||||||||||||||||||||||
| 4044 | # $fragment->{html} - embeddable HTML fragment (style + svg + script) | ||||||||||||||||||||||||||||||||||||||||||
| 4045 | |||||||||||||||||||||||||||||||||||||||||||
| 4046 | Generates an embeddable HTML fragment for a scatter plot with mouseover | ||||||||||||||||||||||||||||||||||||||||||
| 4047 | tooltips. Returns a fragment with no C<<!DOCTYPE>>, C<<html>>, C<<head>>, or | ||||||||||||||||||||||||||||||||||||||||||
| 4048 | C<<body>> wrapper; the caller is responsible for loading D3 in the page. | ||||||||||||||||||||||||||||||||||||||||||
| 4049 | |||||||||||||||||||||||||||||||||||||||||||
| 4050 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 4051 | |||||||||||||||||||||||||||||||||||||||||||
| 4052 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4053 | |||||||||||||||||||||||||||||||||||||||||||
| 4054 | =item * C<$data> - An array reference of data points. Each point is an array | ||||||||||||||||||||||||||||||||||||||||||
| 4055 | reference with two required numeric elements (x, y), an optional third hash | ||||||||||||||||||||||||||||||||||||||||||
| 4056 | reference of extra key/value pairs shown as extra tooltip rows, and an optional | ||||||||||||||||||||||||||||||||||||||||||
| 4057 | fourth hash reference carrying the C<series> (colour group) and C<size> (bubble | ||||||||||||||||||||||||||||||||||||||||||
| 4058 | radius) dimensions: | ||||||||||||||||||||||||||||||||||||||||||
| 4059 | |||||||||||||||||||||||||||||||||||||||||||
| 4060 | [$x, $y] # basic point | ||||||||||||||||||||||||||||||||||||||||||
| 4061 | [$x, $y, \%row] # point with extra tooltip data | ||||||||||||||||||||||||||||||||||||||||||
| 4062 | [$x, $y, \%row, { series => 'North', size => 42 }] # colour and size | ||||||||||||||||||||||||||||||||||||||||||
| 4063 | |||||||||||||||||||||||||||||||||||||||||||
| 4064 | =item * C<\%opts> - Optional hash reference: | ||||||||||||||||||||||||||||||||||||||||||
| 4065 | |||||||||||||||||||||||||||||||||||||||||||
| 4066 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4067 | |||||||||||||||||||||||||||||||||||||||||||
| 4068 | =item * C<id> (string, default C<'scatter_chart'>) - C<id> of the C<<svg>> element. | ||||||||||||||||||||||||||||||||||||||||||
| 4069 | |||||||||||||||||||||||||||||||||||||||||||
| 4070 | =item * C<color> (CSS colour or C<'categorical'>, default C<'steelblue'>) - | ||||||||||||||||||||||||||||||||||||||||||
| 4071 | Fill colour for the data points. C<'categorical'> uses the Tableau-10 palette. | ||||||||||||||||||||||||||||||||||||||||||
| 4072 | A value holding anything a CSS colour cannot, such as a quote or C<E<lt>>, is | ||||||||||||||||||||||||||||||||||||||||||
| 4073 | ignored and the default used. | ||||||||||||||||||||||||||||||||||||||||||
| 4074 | |||||||||||||||||||||||||||||||||||||||||||
| 4075 | =item * C<x_label> (string, default C<''>) - Label for the X axis. | ||||||||||||||||||||||||||||||||||||||||||
| 4076 | |||||||||||||||||||||||||||||||||||||||||||
| 4077 | =item * C<y_label> (string, default C<''>) - Label for the Y axis. | ||||||||||||||||||||||||||||||||||||||||||
| 4078 | |||||||||||||||||||||||||||||||||||||||||||
| 4079 | =item * C<value_label> (string, default C<'Value'>) - Prefix for the tooltip value row. | ||||||||||||||||||||||||||||||||||||||||||
| 4080 | |||||||||||||||||||||||||||||||||||||||||||
| 4081 | =item * C<colour_by_series> (boolean, default 0) - Colour each point by its | ||||||||||||||||||||||||||||||||||||||||||
| 4082 | C<series> value instead of by point index, and draw a legend listing the series | ||||||||||||||||||||||||||||||||||||||||||
| 4083 | to the right of the plot. Ignored for points with no C<series> value. | ||||||||||||||||||||||||||||||||||||||||||
| 4084 | |||||||||||||||||||||||||||||||||||||||||||
| 4085 | =item * C<scheme> (string, default C<'tableau10'>) - D3 categorical colour | ||||||||||||||||||||||||||||||||||||||||||
| 4086 | scheme used when C<colour_by_series> is set, for example C<'category10'>, | ||||||||||||||||||||||||||||||||||||||||||
| 4087 | C<'set2'>, C<'set3'>, or C<'paired'>. | ||||||||||||||||||||||||||||||||||||||||||
| 4088 | C<categorical_schemes()> lists the names, and C<palette()> hands a Perl caller | ||||||||||||||||||||||||||||||||||||||||||
| 4089 | the colours of one. | ||||||||||||||||||||||||||||||||||||||||||
| 4090 | |||||||||||||||||||||||||||||||||||||||||||
| 4091 | =item * C<series_col> (string, default C<'Series'>) - Row label for the series | ||||||||||||||||||||||||||||||||||||||||||
| 4092 | value in the tooltip. | ||||||||||||||||||||||||||||||||||||||||||
| 4093 | |||||||||||||||||||||||||||||||||||||||||||
| 4094 | =item * C<size_col> (string, default C<'Size'>) - Row label for the size value | ||||||||||||||||||||||||||||||||||||||||||
| 4095 | in the tooltip. | ||||||||||||||||||||||||||||||||||||||||||
| 4096 | |||||||||||||||||||||||||||||||||||||||||||
| 4097 | =item * C<radius_range> (arrayref of two numbers, default C<[3, 15]>) - Pixel | ||||||||||||||||||||||||||||||||||||||||||
| 4098 | radii mapped from the smallest and largest observed C<size> value. The mapping | ||||||||||||||||||||||||||||||||||||||||||
| 4099 | is by area (C<d3.scaleSqrt>), so a point twice as large is drawn with four | ||||||||||||||||||||||||||||||||||||||||||
| 4100 | times the area. A point with no C<size> value is drawn at the lower radius. | ||||||||||||||||||||||||||||||||||||||||||
| 4101 | |||||||||||||||||||||||||||||||||||||||||||
| 4102 | =item * C<animated> (boolean, default 0) - If true, circles fade in from | ||||||||||||||||||||||||||||||||||||||||||
| 4103 | opacity 0 on page load (400 ms). Respects C<prefers-reduced-motion>. | ||||||||||||||||||||||||||||||||||||||||||
| 4104 | |||||||||||||||||||||||||||||||||||||||||||
| 4105 | =item * C<responsive> (boolean, default 0) - See C<render_bar_chart_snippet>. | ||||||||||||||||||||||||||||||||||||||||||
| 4106 | |||||||||||||||||||||||||||||||||||||||||||
| 4107 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4108 | |||||||||||||||||||||||||||||||||||||||||||
| 4109 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4110 | |||||||||||||||||||||||||||||||||||||||||||
| 4111 | =head3 Return value | ||||||||||||||||||||||||||||||||||||||||||
| 4112 | |||||||||||||||||||||||||||||||||||||||||||
| 4113 | A hash reference with C<svg_id> (string) and C<html> (Perl character string). | ||||||||||||||||||||||||||||||||||||||||||
| 4114 | |||||||||||||||||||||||||||||||||||||||||||
| 4115 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 4116 | |||||||||||||||||||||||||||||||||||||||||||
| 4117 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4118 | |||||||||||||||||||||||||||||||||||||||||||
| 4119 | =item * Dies with C<'Data must be an array of arrays'> when C<$data> is not an ARRAY ref. | ||||||||||||||||||||||||||||||||||||||||||
| 4120 | |||||||||||||||||||||||||||||||||||||||||||
| 4121 | =item * Dies with C<'Each data point must be an array reference'> when an element is not an ARRAY ref. | ||||||||||||||||||||||||||||||||||||||||||
| 4122 | |||||||||||||||||||||||||||||||||||||||||||
| 4123 | =item * Dies with C<'Each data point must have at least 2 elements'> when a point has fewer than 2 items. | ||||||||||||||||||||||||||||||||||||||||||
| 4124 | |||||||||||||||||||||||||||||||||||||||||||
| 4125 | =item * Dies with C<'X value must be numeric'> when the x element is not a number. | ||||||||||||||||||||||||||||||||||||||||||
| 4126 | |||||||||||||||||||||||||||||||||||||||||||
| 4127 | =item * Dies with C<'Y value must be numeric'> when the y element is not a number. | ||||||||||||||||||||||||||||||||||||||||||
| 4128 | |||||||||||||||||||||||||||||||||||||||||||
| 4129 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4130 | |||||||||||||||||||||||||||||||||||||||||||
| 4131 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 4132 | |||||||||||||||||||||||||||||||||||||||||||
| 4133 | { | ||||||||||||||||||||||||||||||||||||||||||
| 4134 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4135 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 4136 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 4137 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 4138 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 4139 | id => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4140 | color => { type => 'string', optional => 1, default => 'steelblue' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4141 | x_label => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4142 | y_label => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4143 | value_label => { type => 'string', optional => 1, default => 'Value' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4144 | colour_by_series => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4145 | scheme => { type => 'string', optional => 1, default => 'tableau10' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4146 | series_col => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4147 | size_col => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4148 | radius_range => { type => 'arrayref', optional => 1, default => [3, 15] }, | ||||||||||||||||||||||||||||||||||||||||||
| 4149 | animated => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4150 | responsive => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4151 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4152 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4154 | |||||||||||||||||||||||||||||||||||||||||||
| 4155 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 4156 | |||||||||||||||||||||||||||||||||||||||||||
| 4157 | sub render_scatter_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4158 | 74 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_scatter_chart_snippet: called CC=58 SCAR=40.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 4159 | 74 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 4160 | 74 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 4161 | 74 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4162 | 71 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 4163 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 4164 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 4165 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 4166 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4167 | 71 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 4168 | 71 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 4169 | |||||||||||||||||||||||||||||||||||||||||||
| 4170 | 71 | ▶ | my $color = _css_colour($opts->{color}) // 'steelblue'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_css_colour($opts->{"color"}) // "steelblue"
MC/DC100%1/1
_css_colour($opts->{"color"}) // "steelblue"
_css_colour($opts->{"color"})
Truth table100%2/2
A
A= _css_colour($opts->{"color"})
| |||||||||||||||||||||||||||||||||||||||||||
| 4171 | 71 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4172 | 71 | ▶ | my $y_label = $opts->{y_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"y_label"} // ""
MC/DC100%1/1
$opts->{"y_label"} // ""
$opts->{"y_label"}
Truth table100%2/2
A
A= $opts->{"y_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4173 | 71 | ▶ | my $value_label = $opts->{value_label} // 'Value'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"value_label"} // "Value"
MC/DC0%0/1
$opts->{"value_label"} // "Value"
$opts->{"value_label"}
Truth table50%1/2
A
A= $opts->{"value_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4174 | 71 | ▶ | my $animated = $opts->{animated} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"animated"} // 0
MC/DC100%1/1
$opts->{"animated"} // 0
$opts->{"animated"}
Truth table100%2/2
A
A= $opts->{"animated"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4175 | 71 | ▶ | my $colour_by_series = $opts->{colour_by_series} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"colour_by_series"} // 0
MC/DC100%1/1
$opts->{"colour_by_series"} // 0
$opts->{"colour_by_series"}
Truth table100%2/2
A
A= $opts->{"colour_by_series"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4176 | 71 | ▶ | my $scheme = $opts->{scheme} // $DEFAULT_CATEGORICAL_SCHEME; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$opts->{"scheme"} // $DEFAULT_CATEGORICAL_SCHEME
MC/DC0%0/2
$opts->{"scheme"} // $DEFAULT_CATEGORICAL_SCHEME
$opts->{"scheme"}
$DEFAULT_CATEGORICAL_SCHEME
Truth table66%2/3
A // B
A= $opts->{"scheme"}B= $DEFAULT_CATEGORICAL_SCHEME
| |||||||||||||||||||||||||||||||||||||||||||
| 4177 | 71 | ▶ | my $series_col = $opts->{series_col} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"series_col"} // ""
MC/DC100%1/1
$opts->{"series_col"} // ""
$opts->{"series_col"}
Truth table100%2/2
A
A= $opts->{"series_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4178 | 71 | ▶ | my $size_col = $opts->{size_col} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"size_col"} // ""
MC/DC100%1/1
$opts->{"size_col"} // ""
$opts->{"size_col"}
Truth table100%2/2
A
A= $opts->{"size_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4179 | 71 | ▶ | my $radius_range = $opts->{radius_range} // [3, 15]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"radius_range"} // [3, 15]
MC/DC100%1/1
$opts->{"radius_range"} // [3, 15]
$opts->{"radius_range"}
Truth table100%2/2
A
A= $opts->{"radius_range"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4180 | |||||||||||||||||||||||||||||||||||||||||||
| 4181 | 71 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 4182 | |||||||||||||||||||||||||||||||||||||||||||
| 4183 | # The scheme's colours as a JS array, from the one definition rather than | ||||||||||||||||||||||||||||||||||||||||||
| 4184 | # from the spelling of the name. | ||||||||||||||||||||||||||||||||||||||||||
| 4185 | 71 | my $scheme_js = _scheme_js($scheme); | |||||||||||||||||||||||||||||||||||||||||
| 4186 | |||||||||||||||||||||||||||||||||||||||||||
| 4187 | 71 | my @points; | |||||||||||||||||||||||||||||||||||||||||
| 4188 | 71 | for my $pt (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 4189 | 142 | ▶ | die 'Each data point must be an array reference' unless ref($pt) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4190 | 139 | ▶ | die 'Each data point must have at least 2 elements' unless scalar(@$pt) >= 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4191 | 136 | ▶ | die 'X value must be numeric' unless looks_like_number($pt->[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4192 | 133 | ▶ | die 'Y value must be numeric' unless looks_like_number($pt->[1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4193 | 130 | my $p = { x => $pt->[0] + 0, y => $pt->[1] + 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 4194 | 130 | ▶ | $p->{extra} = $pt->[2] if ref($pt->[2]) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4195 | 130 | ▶ | if (ref($pt->[3]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4196 | 13 | ▶ | $p->{series} = $pt->[3]{series} if defined $pt->[3]{series}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4197 | 13 | ▶ | $p->{size} = $pt->[3]{size}+0 if defined $pt->[3]{size} && looks_like_number($pt->[3]{size}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $pt->[3]{"size"} and &looks_like_number($pt->[3]{"size"})
MC/DC100%2/2
defined $pt->[3]{"size"} and &looks_like_number($pt->[3]{"size"})
defined $pt->[3]{"size"}
&looks_like_number($pt->[3]{"size"})
Truth table100%3/3
A and B
A= defined $pt->[3]{"size"}B= &looks_like_number($pt->[3]{"size"})
| |||||||||||||||||||||||||||||||||||||||||||
| 4198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4199 | 130 | push @points, $p; | |||||||||||||||||||||||||||||||||||||||||
| 4200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4201 | |||||||||||||||||||||||||||||||||||||||||||
| 4202 | # Collect series names and size range for JS initialisation. | ||||||||||||||||||||||||||||||||||||||||||
| 4203 | 59 | my %seen_series; | |||||||||||||||||||||||||||||||||||||||||
| 4204 | my @series_names; | ||||||||||||||||||||||||||||||||||||||||||
| 4205 | 59 | my ($size_min, $size_max); | |||||||||||||||||||||||||||||||||||||||||
| 4206 | 59 | for my $p (@points) { | |||||||||||||||||||||||||||||||||||||||||
| 4207 | 130 | ▶ | if (defined $p->{series} && !$seen_series{$p->{series}}++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $p->{"series"} and not $seen_series{$p->{"series"}}++
MC/DC50%1/2
defined $p->{"series"} and not $seen_series{$p->{"series"}}++
defined $p->{"series"}
not $seen_series{$p->{"series"}}++
Truth table66%2/3
A and not(B)
A= defined $p->{"series"}B= not $seen_series{$p->{"series"}}++
| |||||||||||||||||||||||||||||||||||||||||||
| 4208 | 10 | push @series_names, $p->{series}; | |||||||||||||||||||||||||||||||||||||||||
| 4209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4210 | 130 | ▶ | if (defined $p->{size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4211 | 4 | ▶ | $size_min = $p->{size} if !defined $size_min || $p->{size} < $size_min; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $size_min or $p->{"size"} < $size_min
MC/DC50%1/2
not defined $size_min or $p->{"size"} < $size_min
not defined $size_min
$p->{"size"} < $size_min
Truth table66%2/3
not(A) or B
A= not defined $size_minB= $p->{"size"} < $size_min
| |||||||||||||||||||||||||||||||||||||||||||
| 4212 | 4 | ▶ | $size_max = $p->{size} if !defined $size_max || $p->{size} > $size_max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $size_max or $p->{"size"} > $size_max
MC/DC0%0/2
not defined $size_max or $p->{"size"} > $size_max
not defined $size_max
$p->{"size"} > $size_max
Truth table66%2/3
not(A) or B
A= not defined $size_maxB= $p->{"size"} > $size_max
| |||||||||||||||||||||||||||||||||||||||||||
| 4213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4215 | # Colour and size are independent dimensions: a point may carry both. | ||||||||||||||||||||||||||||||||||||||||||
| 4216 | 59 | ▶ | my $has_series = $colour_by_series && @series_names; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$colour_by_series && @series_names
MC/DC100%2/2
$colour_by_series && @series_names
$colour_by_series
@series_names
Truth table100%3/3
A && B
A= $colour_by_seriesB= @series_names
| |||||||||||||||||||||||||||||||||||||||||||
| 4217 | 59 | my $has_size = defined $size_min; | |||||||||||||||||||||||||||||||||||||||||
| 4218 | |||||||||||||||||||||||||||||||||||||||||||
| 4219 | 59 | my $json_data = _script_json(\@points); | |||||||||||||||||||||||||||||||||||||||||
| 4220 | 59 | my $json_series = _script_json(\@series_names); | |||||||||||||||||||||||||||||||||||||||||
| 4221 | 59 | my $x_label_esc = $js_esc->($x_label); | |||||||||||||||||||||||||||||||||||||||||
| 4222 | 59 | my $y_label_esc = $js_esc->($y_label); | |||||||||||||||||||||||||||||||||||||||||
| 4223 | 59 | my $val_label_esc = $js_esc->($value_label); | |||||||||||||||||||||||||||||||||||||||||
| 4224 | 59 | ▶ | my $series_lbl_esc = $js_esc->($series_col || 'Series'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$series_col || 'Series'
MC/DC100%1/1
$series_col || 'Series'
$series_col
Truth table100%2/2
A
A= $series_col
| |||||||||||||||||||||||||||||||||||||||||||
| 4225 | 59 | ▶ | my $size_lbl_esc = $js_esc->($size_col || 'Size'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$size_col || 'Size'
MC/DC100%1/1
$size_col || 'Size'
$size_col
Truth table100%2/2
A
A= $size_col
| |||||||||||||||||||||||||||||||||||||||||||
| 4226 | |||||||||||||||||||||||||||||||||||||||||||
| 4227 | 59 | ▶ | my $is_categorical = ($color eq 'categorical') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4228 | 59 | ▶ | my $color_esc = ($is_categorical || $has_series) ? 'steelblue' : $js_esc->($color); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_categorical or $has_series
MC/DC50%1/2
$is_categorical or $has_series
$is_categorical
$has_series
Truth table66%2/3
A or B
A= $is_categoricalB= $has_series
| |||||||||||||||||||||||||||||||||||||||||||
| 4229 | |||||||||||||||||||||||||||||||||||||||||||
| 4230 | 59 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'scatter_chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "scatter_chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "scatter_chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 4231 | 59 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 4232 | 59 | my $leg_id = $svg_id . '_leg'; | |||||||||||||||||||||||||||||||||||||||||
| 4233 | |||||||||||||||||||||||||||||||||||||||||||
| 4234 | 59 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 4235 | 59 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 4236 | |||||||||||||||||||||||||||||||||||||||||||
| 4237 | # Reserve right margin for legend when series colouring is active. | ||||||||||||||||||||||||||||||||||||||||||
| 4238 | 59 | ▶ | my $margin_right = $has_series ? 140 : 20; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4239 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 4240 | 59 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4241 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 4242 | |||||||||||||||||||||||||||||||||||||||||||
| 4243 | 59 | ▶ | my $margin_bottom = $x_label ? 60 : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4244 | 59 | ▶ | my $margin_left = $y_label ? 70 : 50; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4245 | 59 | my $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 4246 | 59 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 4247 | 59 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 4248 | |||||||||||||||||||||||||||||||||||||||||||
| 4249 | # Color initialisation JS. | ||||||||||||||||||||||||||||||||||||||||||
| 4250 | 59 | my $color_init_js; | |||||||||||||||||||||||||||||||||||||||||
| 4251 | 59 | ▶ | if ($has_series) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 4252 | 8 | $color_init_js = "var seriesNames = $json_series;\n" | |||||||||||||||||||||||||||||||||||||||||
| 4253 | . " var color = d3.scaleOrdinal(${scheme_js}).domain(seriesNames);"; | ||||||||||||||||||||||||||||||||||||||||||
| 4254 | } elsif ($is_categorical) { | ||||||||||||||||||||||||||||||||||||||||||
| 4255 | 0 | $color_init_js = 'var color = d3.scaleOrdinal(' . _default_scheme_js() . ').domain(data.map(function(d,i){return i;}));'; | |||||||||||||||||||||||||||||||||||||||||
| 4256 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 4257 | 51 | $color_init_js = "var color = function() { return \"$color_esc\"; };"; | |||||||||||||||||||||||||||||||||||||||||
| 4258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4259 | |||||||||||||||||||||||||||||||||||||||||||
| 4260 | # Size scale JS (bubble mode). | ||||||||||||||||||||||||||||||||||||||||||
| 4261 | 59 | ▶ | my $r_min = $radius_range->[0] // 3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$radius_range->[0] // 3
MC/DC0%0/1
$radius_range->[0] // 3
$radius_range->[0]
Truth table50%1/2
A
A= $radius_range->[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 4262 | 59 | ▶ | my $r_max = $radius_range->[1] // 15; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$radius_range->[1] // 15
MC/DC0%0/1
$radius_range->[1] // 15
$radius_range->[1]
Truth table50%1/2
A
A= $radius_range->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 4263 | 59 | my $size_init_js = ''; | |||||||||||||||||||||||||||||||||||||||||
| 4264 | 59 | my $radius_js = '5'; | |||||||||||||||||||||||||||||||||||||||||
| 4265 | 59 | ▶ | if ($has_size && defined $size_min) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$has_size and defined $size_min
MC/DC50%1/2
$has_size and defined $size_min
$has_size
defined $size_min
Truth table66%2/3
A and B
A= $has_sizeB= defined $size_min
| |||||||||||||||||||||||||||||||||||||||||||
| 4266 | 2 | my $smin = $size_min + 0; | |||||||||||||||||||||||||||||||||||||||||
| 4267 | 2 | my $smax = $size_max + 0; | |||||||||||||||||||||||||||||||||||||||||
| 4268 | 2 | $size_init_js = "var rScale = d3.scaleSqrt().domain([$smin,$smax]).range([$r_min,$r_max]).clamp(true);"; | |||||||||||||||||||||||||||||||||||||||||
| 4269 | 2 | $radius_js = 'function(d){return d.size!=null?rScale(d.size):' . $r_min . ';}'; | |||||||||||||||||||||||||||||||||||||||||
| 4270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4271 | |||||||||||||||||||||||||||||||||||||||||||
| 4272 | # Series colouring: color by d.series; fallback to default. | ||||||||||||||||||||||||||||||||||||||||||
| 4273 | 59 | ▶ | my $fill_js = $has_series | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4274 | ? 'function(d){return d.series!=null?color(d.series):"' . $color_esc . '";}' | ||||||||||||||||||||||||||||||||||||||||||
| 4275 | : 'function(d,i){return color(i);}'; | ||||||||||||||||||||||||||||||||||||||||||
| 4276 | |||||||||||||||||||||||||||||||||||||||||||
| 4277 | 59 | ▶ | my $anim_block = $animated ? <<"ANIM" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4278 | var noAnim = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; | ||||||||||||||||||||||||||||||||||||||||||
| 4279 | if (!noAnim) { | ||||||||||||||||||||||||||||||||||||||||||
| 4280 | circles.attr("opacity", 0) | ||||||||||||||||||||||||||||||||||||||||||
| 4281 | .transition() | ||||||||||||||||||||||||||||||||||||||||||
| 4282 | .duration(400) | ||||||||||||||||||||||||||||||||||||||||||
| 4283 | .delay(function(d, i) { return i * 10; }) | ||||||||||||||||||||||||||||||||||||||||||
| 4284 | .attr("opacity", 0.8); | ||||||||||||||||||||||||||||||||||||||||||
| 4285 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 4286 | circles.attr("opacity", 0.8); | ||||||||||||||||||||||||||||||||||||||||||
| 4287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4288 | ANIM | ||||||||||||||||||||||||||||||||||||||||||
| 4289 | |||||||||||||||||||||||||||||||||||||||||||
| 4290 | 59 | ▶ | my $x_label_block = $x_label ? <<"XLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4291 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 4292 | .attr("class", "sc-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 4293 | .attr("x", $inner_w / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 4294 | .attr("y", $inner_h + ($margin_bottom - 10)) | ||||||||||||||||||||||||||||||||||||||||||
| 4295 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 4296 | .text("$x_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 4297 | XLB | ||||||||||||||||||||||||||||||||||||||||||
| 4298 | |||||||||||||||||||||||||||||||||||||||||||
| 4299 | 59 | ▶ | my $y_label_block = $y_label ? <<"YLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4300 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 4301 | .attr("class", "sc-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 4302 | .attr("transform", "rotate(-90)") | ||||||||||||||||||||||||||||||||||||||||||
| 4303 | .attr("x", -$inner_h / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 4304 | .attr("y", -($margin_left - 15)) | ||||||||||||||||||||||||||||||||||||||||||
| 4305 | .attr("text-anchor", "middle") | ||||||||||||||||||||||||||||||||||||||||||
| 4306 | .text("$y_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 4307 | YLB | ||||||||||||||||||||||||||||||||||||||||||
| 4308 | |||||||||||||||||||||||||||||||||||||||||||
| 4309 | # Legend block (only when colour_by_series is true and we have series names). | ||||||||||||||||||||||||||||||||||||||||||
| 4310 | 59 | ▶ | my $legend_block = $has_series ? <<"LEG" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4311 | var legendG = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 4312 | .attr("transform","translate(" + ($margin_left + $inner_w + 12) + "," + $margin_top + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 4313 | var seriesNamesForLeg = $json_series; | ||||||||||||||||||||||||||||||||||||||||||
| 4314 | seriesNamesForLeg.forEach(function(name, i){ | ||||||||||||||||||||||||||||||||||||||||||
| 4315 | var row = legendG.append("g").attr("transform","translate(0," + (i*18) + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 4316 | row.append("rect").attr("width",11).attr("height",11).attr("fill",color(name)).attr("rx",2); | ||||||||||||||||||||||||||||||||||||||||||
| 4317 | row.append("text").attr("x",15).attr("y",10).style("font-size","11px").style("fill","#444") | ||||||||||||||||||||||||||||||||||||||||||
| 4318 | .text(name.length>14?name.slice(0,13)+"…":name); | ||||||||||||||||||||||||||||||||||||||||||
| 4319 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4320 | LEG | ||||||||||||||||||||||||||||||||||||||||||
| 4321 | |||||||||||||||||||||||||||||||||||||||||||
| 4322 | 59 | ▶ | my $series_tip_js = $has_series | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$series_col || 'Series'
MC/DC100%1/1
$series_col || 'Series'
$series_col
Truth table100%2/2
A
A= $series_col
| |||||||||||||||||||||||||||||||||||||||||||
| 4323 | ? 'if(d.series!=null){tipHtml+="<br>"+esc("' . $js_esc->($series_col || 'Series') . '")+": "+esc(String(d.series));}' | ||||||||||||||||||||||||||||||||||||||||||
| 4324 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 4325 | 59 | ▶ | my $size_tip_js = $has_size | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$size_col || 'Size'
MC/DC0%0/1
$size_col || 'Size'
$size_col
Truth table50%1/2
A
A= $size_col
| |||||||||||||||||||||||||||||||||||||||||||
| 4326 | ? 'if(d.size!=null){tipHtml+="<br>"+esc("' . $js_esc->($size_col || 'Size') . '")+": "+esc(String(d.size));}' | ||||||||||||||||||||||||||||||||||||||||||
| 4327 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 4328 | |||||||||||||||||||||||||||||||||||||||||||
| 4329 | 59 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 4330 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 4331 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 4332 | position: absolute; | ||||||||||||||||||||||||||||||||||||||||||
| 4333 | background: rgba(255,255,255,0.95); | ||||||||||||||||||||||||||||||||||||||||||
| 4334 | border: 1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 4335 | border-radius: 4px; | ||||||||||||||||||||||||||||||||||||||||||
| 4336 | padding: 6px 10px; | ||||||||||||||||||||||||||||||||||||||||||
| 4337 | font-size: 12px; | ||||||||||||||||||||||||||||||||||||||||||
| 4338 | pointer-events: none; | ||||||||||||||||||||||||||||||||||||||||||
| 4339 | display: none; | ||||||||||||||||||||||||||||||||||||||||||
| 4340 | line-height: 1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 4341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4342 | .sc-circle { cursor: default; } | ||||||||||||||||||||||||||||||||||||||||||
| 4343 | .sc-axis-label { font-size: 11px; fill: #555; } | ||||||||||||||||||||||||||||||||||||||||||
| 4344 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 4345 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 4346 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 4347 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 4348 | (function() { | ||||||||||||||||||||||||||||||||||||||||||
| 4349 | var data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 4350 | var margin = { top: $margin_top, right: $margin_right, bottom: $margin_bottom, left: $margin_left }; | ||||||||||||||||||||||||||||||||||||||||||
| 4351 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 4352 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 4353 | var valLabel = "$val_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 4354 | $color_init_js | ||||||||||||||||||||||||||||||||||||||||||
| 4355 | $size_init_js | ||||||||||||||||||||||||||||||||||||||||||
| 4356 | |||||||||||||||||||||||||||||||||||||||||||
| 4357 | function esc(s) { | ||||||||||||||||||||||||||||||||||||||||||
| 4358 | return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||||||||||||||||||||||||||||||||||||||||||
| 4359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4360 | |||||||||||||||||||||||||||||||||||||||||||
| 4361 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 4362 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 4363 | |||||||||||||||||||||||||||||||||||||||||||
| 4364 | var g = svg.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 4365 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | ||||||||||||||||||||||||||||||||||||||||||
| 4366 | |||||||||||||||||||||||||||||||||||||||||||
| 4367 | var xExt = d3.extent(data, function(d) { return d.x; }); | ||||||||||||||||||||||||||||||||||||||||||
| 4368 | var yExt = d3.extent(data, function(d) { return d.y; }); | ||||||||||||||||||||||||||||||||||||||||||
| 4369 | var xPad = (xExt[1] - xExt[0]) * 0.05 || 1; | ||||||||||||||||||||||||||||||||||||||||||
| 4370 | var yPad = (yExt[1] - yExt[0]) * 0.05 || 1; | ||||||||||||||||||||||||||||||||||||||||||
| 4371 | |||||||||||||||||||||||||||||||||||||||||||
| 4372 | var xScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 4373 | .domain([xExt[0] - xPad, xExt[1] + xPad]) | ||||||||||||||||||||||||||||||||||||||||||
| 4374 | .range([0, innerW]); | ||||||||||||||||||||||||||||||||||||||||||
| 4375 | |||||||||||||||||||||||||||||||||||||||||||
| 4376 | var yScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 4377 | .domain([yExt[0] - yPad, yExt[1] + yPad]) | ||||||||||||||||||||||||||||||||||||||||||
| 4378 | .nice() | ||||||||||||||||||||||||||||||||||||||||||
| 4379 | .range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 4380 | |||||||||||||||||||||||||||||||||||||||||||
| 4381 | g.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 4382 | .attr("transform", "translate(0," + innerH + ")") | ||||||||||||||||||||||||||||||||||||||||||
| 4383 | .call(d3.axisBottom(xScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 4384 | |||||||||||||||||||||||||||||||||||||||||||
| 4385 | g.append("g") | ||||||||||||||||||||||||||||||||||||||||||
| 4386 | .call(d3.axisLeft(yScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 4387 | |||||||||||||||||||||||||||||||||||||||||||
| 4388 | var circles = g.selectAll(".sc-circle") | ||||||||||||||||||||||||||||||||||||||||||
| 4389 | .data(data) | ||||||||||||||||||||||||||||||||||||||||||
| 4390 | .join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 4391 | .attr("class", "sc-circle") | ||||||||||||||||||||||||||||||||||||||||||
| 4392 | .attr("cx", function(d) { return xScale(d.x); }) | ||||||||||||||||||||||||||||||||||||||||||
| 4393 | .attr("cy", function(d) { return yScale(d.y); }) | ||||||||||||||||||||||||||||||||||||||||||
| 4394 | .attr("r", $radius_js) | ||||||||||||||||||||||||||||||||||||||||||
| 4395 | .attr("fill", $fill_js) | ||||||||||||||||||||||||||||||||||||||||||
| 4396 | .attr("stroke", "white") | ||||||||||||||||||||||||||||||||||||||||||
| 4397 | .attr("stroke-width", 0.5) | ||||||||||||||||||||||||||||||||||||||||||
| 4398 | .on("mouseover", function(event, d) { | ||||||||||||||||||||||||||||||||||||||||||
| 4399 | var tipHtml = "<b>" + esc(String(d.x)) + "<\\/b>, <b>" + esc(String(d.y)) + "<\\/b>"; | ||||||||||||||||||||||||||||||||||||||||||
| 4400 | if (d.extra) { | ||||||||||||||||||||||||||||||||||||||||||
| 4401 | Object.entries(d.extra).forEach(function(kv) { | ||||||||||||||||||||||||||||||||||||||||||
| 4402 | tipHtml += "<br>" + esc(kv[0]) + ": " + esc(kv[1]); | ||||||||||||||||||||||||||||||||||||||||||
| 4403 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4405 | $series_tip_js | ||||||||||||||||||||||||||||||||||||||||||
| 4406 | $size_tip_js | ||||||||||||||||||||||||||||||||||||||||||
| 4407 | tip.html(tipHtml) | ||||||||||||||||||||||||||||||||||||||||||
| 4408 | .style("display", "block") | ||||||||||||||||||||||||||||||||||||||||||
| 4409 | .style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 4410 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 4411 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 4412 | .on("mousemove", function(event) { | ||||||||||||||||||||||||||||||||||||||||||
| 4413 | tip.style("left", (event.pageX + 12) + "px") | ||||||||||||||||||||||||||||||||||||||||||
| 4414 | .style("top", (event.pageY - 24) + "px"); | ||||||||||||||||||||||||||||||||||||||||||
| 4415 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 4416 | .on("mouseout", function() { | ||||||||||||||||||||||||||||||||||||||||||
| 4417 | tip.style("display", "none"); | ||||||||||||||||||||||||||||||||||||||||||
| 4418 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4419 | |||||||||||||||||||||||||||||||||||||||||||
| 4420 | $anim_block$x_label_block$y_label_block$legend_block})(); | ||||||||||||||||||||||||||||||||||||||||||
| 4421 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 4422 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 4423 | |||||||||||||||||||||||||||||||||||||||||||
| 4424 | 59 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 4425 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4426 | |||||||||||||||||||||||||||||||||||||||||||
| 4427 | =head2 render_table_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4428 | |||||||||||||||||||||||||||||||||||||||||||
| 4429 | my $fragment = $chart->render_table_snippet($data); | ||||||||||||||||||||||||||||||||||||||||||
| 4430 | my $fragment = $chart->render_table_snippet($data, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 4431 | # $fragment->{table_id} - the id attribute of the <table> element | ||||||||||||||||||||||||||||||||||||||||||
| 4432 | # $fragment->{html} - embeddable HTML fragment (style + table + script) | ||||||||||||||||||||||||||||||||||||||||||
| 4433 | |||||||||||||||||||||||||||||||||||||||||||
| 4434 | Generates an embeddable HTML fragment for a sortable, filterable data table. | ||||||||||||||||||||||||||||||||||||||||||
| 4435 | Returns a fragment with no page-shell wrapper; the caller loads D3 if needed. | ||||||||||||||||||||||||||||||||||||||||||
| 4436 | |||||||||||||||||||||||||||||||||||||||||||
| 4437 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 4438 | |||||||||||||||||||||||||||||||||||||||||||
| 4439 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4440 | |||||||||||||||||||||||||||||||||||||||||||
| 4441 | =item * C<$data> - An array reference of row array references. The first row | ||||||||||||||||||||||||||||||||||||||||||
| 4442 | is treated as the header row. Every subsequent row must have the same number | ||||||||||||||||||||||||||||||||||||||||||
| 4443 | of columns as the header. | ||||||||||||||||||||||||||||||||||||||||||
| 4444 | |||||||||||||||||||||||||||||||||||||||||||
| 4445 | [ ['Name', 'Value', 'Category'], # header | ||||||||||||||||||||||||||||||||||||||||||
| 4446 | ['Alpha', 300, 'A'], | ||||||||||||||||||||||||||||||||||||||||||
| 4447 | ['Beta', 150, 'B'], | ||||||||||||||||||||||||||||||||||||||||||
| 4448 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 4449 | |||||||||||||||||||||||||||||||||||||||||||
| 4450 | =item * C<\%opts> - Optional hash reference: | ||||||||||||||||||||||||||||||||||||||||||
| 4451 | |||||||||||||||||||||||||||||||||||||||||||
| 4452 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4453 | |||||||||||||||||||||||||||||||||||||||||||
| 4454 | =item * C<id> (string, default C<'data_table'>) - C<id> of the C<<table>> element. | ||||||||||||||||||||||||||||||||||||||||||
| 4455 | |||||||||||||||||||||||||||||||||||||||||||
| 4456 | =item * C<sortable> (boolean, default 1) - If true, clicking a column header | ||||||||||||||||||||||||||||||||||||||||||
| 4457 | sorts the table by that column (toggle ascending/descending). | ||||||||||||||||||||||||||||||||||||||||||
| 4458 | |||||||||||||||||||||||||||||||||||||||||||
| 4459 | =item * C<caption> (string, default C<''>) - Optional C<<caption>> element text. | ||||||||||||||||||||||||||||||||||||||||||
| 4460 | |||||||||||||||||||||||||||||||||||||||||||
| 4461 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4462 | |||||||||||||||||||||||||||||||||||||||||||
| 4463 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4464 | |||||||||||||||||||||||||||||||||||||||||||
| 4465 | =head3 Return value | ||||||||||||||||||||||||||||||||||||||||||
| 4466 | |||||||||||||||||||||||||||||||||||||||||||
| 4467 | A hash reference with C<table_id> (string) and C<html> (Perl character string). | ||||||||||||||||||||||||||||||||||||||||||
| 4468 | Note: returns C<table_id>, not C<svg_id>, because this method renders an HTML | ||||||||||||||||||||||||||||||||||||||||||
| 4469 | table rather than an SVG chart. | ||||||||||||||||||||||||||||||||||||||||||
| 4470 | |||||||||||||||||||||||||||||||||||||||||||
| 4471 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 4472 | |||||||||||||||||||||||||||||||||||||||||||
| 4473 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4474 | |||||||||||||||||||||||||||||||||||||||||||
| 4475 | =item * Dies with C<'Data must be an array of arrays'> when C<$data> is not an ARRAY ref. | ||||||||||||||||||||||||||||||||||||||||||
| 4476 | |||||||||||||||||||||||||||||||||||||||||||
| 4477 | =item * Dies with C<'Data must have at least one row (header row)'> when C<$data> is empty. | ||||||||||||||||||||||||||||||||||||||||||
| 4478 | |||||||||||||||||||||||||||||||||||||||||||
| 4479 | =item * Dies with C<'Each row must be an array reference'> when a row is not an ARRAY ref. | ||||||||||||||||||||||||||||||||||||||||||
| 4480 | |||||||||||||||||||||||||||||||||||||||||||
| 4481 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4482 | |||||||||||||||||||||||||||||||||||||||||||
| 4483 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 4484 | |||||||||||||||||||||||||||||||||||||||||||
| 4485 | { | ||||||||||||||||||||||||||||||||||||||||||
| 4486 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4487 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 4488 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 4489 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 4490 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 4491 | id => { type => 'string', optional => 1, default => 'data_table' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4492 | sortable => { type => 'boolean', optional => 1, default => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4493 | caption => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4494 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4495 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4497 | |||||||||||||||||||||||||||||||||||||||||||
| 4498 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 4499 | |||||||||||||||||||||||||||||||||||||||||||
| 4500 | sub render_table_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4501 | 40 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_table_snippet: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 4502 | 40 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 4503 | 40 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 4504 | 40 | ▶ | die 'Data must be an array of arrays' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4505 | 37 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 4506 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 4507 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of arrays' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 4508 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 4509 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4510 | 37 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 4511 | 37 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 4512 | 37 | ▶ | die 'Data must have at least one row (header row)' unless @$data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4513 | 34 | ▶ | die 'Each row must be an array reference' unless ref($data->[0]) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4514 | |||||||||||||||||||||||||||||||||||||||||||
| 4515 | 31 | ▶ | my $sortable = exists($opts->{sortable}) ? $opts->{sortable} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4516 | 31 | ▶ | my $caption = $opts->{caption} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"caption"} // ""
MC/DC100%1/1
$opts->{"caption"} // ""
$opts->{"caption"}
Truth table100%2/2
A
A= $opts->{"caption"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4517 | 31 | ▶ | my $table_id = _chart_id($opts->{id}) // 'data_table'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "data_table"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "data_table"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 4518 | |||||||||||||||||||||||||||||||||||||||||||
| 4519 | 31 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 4520 | |||||||||||||||||||||||||||||||||||||||||||
| 4521 | 172 | ▶ | my $html_esc = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4522 | 172 | ▶ | my $s = shift // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 4523 | 172 | $s =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 4524 | 172 | $s =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 4525 | 172 | $s =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 4526 | 172 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 4527 | 31 | }; | |||||||||||||||||||||||||||||||||||||||||
| 4528 | |||||||||||||||||||||||||||||||||||||||||||
| 4529 | 31 | my $headers = $data->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 4530 | 31 | my @rows = @{$data}[1 .. $#$data]; | |||||||||||||||||||||||||||||||||||||||||
| 4531 | |||||||||||||||||||||||||||||||||||||||||||
| 4532 | 31 | for my $row (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 4533 | 50 | ▶ | die 'Each row must be an array reference' unless ref($row) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4535 | |||||||||||||||||||||||||||||||||||||||||||
| 4536 | my $json_data = _script_json([ | ||||||||||||||||||||||||||||||||||||||||||
| 4537 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 4538 | 29 | my @row = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 4539 | \@row | ||||||||||||||||||||||||||||||||||||||||||
| 4540 | 77 | } @$data | |||||||||||||||||||||||||||||||||||||||||
| 4541 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 4542 | |||||||||||||||||||||||||||||||||||||||||||
| 4543 | 29 | ▶ | my $caption_html = $caption | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4544 | ? '<caption>' . $html_esc->($caption) . '</caption>' | ||||||||||||||||||||||||||||||||||||||||||
| 4545 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 4546 | |||||||||||||||||||||||||||||||||||||||||||
| 4547 | 29 | ▶ | my $sort_th_class = $sortable ? ' class="dt-sortable"' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4548 | 29 | ▶ | my $sort_cursor_css = $sortable ? 'cursor:pointer; user-select:none;' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4549 | |||||||||||||||||||||||||||||||||||||||||||
| 4550 | 29 | my @th_cells = map { '<th' . $sort_th_class . '>' . $html_esc->($_) . '</th>' } @$headers; | |||||||||||||||||||||||||||||||||||||||||
| 4551 | 29 | my $thead_html = '<tr>' . join('', @th_cells) . '</tr>'; | |||||||||||||||||||||||||||||||||||||||||
| 4552 | |||||||||||||||||||||||||||||||||||||||||||
| 4553 | 29 | my @tbody_rows; | |||||||||||||||||||||||||||||||||||||||||
| 4554 | 29 | for my $row (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 4555 | 48 | my @tds = map { '<td>' . $html_esc->($_) . '</td>' } @$row; | |||||||||||||||||||||||||||||||||||||||||
| 4556 | 48 | push @tbody_rows, '<tr>' . join('', @tds) . '</tr>'; | |||||||||||||||||||||||||||||||||||||||||
| 4557 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4558 | 29 | my $tbody_html = join("\n ", @tbody_rows); | |||||||||||||||||||||||||||||||||||||||||
| 4559 | |||||||||||||||||||||||||||||||||||||||||||
| 4560 | 29 | ▶ | my $sort_script = $sortable ? <<"SORT" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4561 | var sortState = { col: -1, asc: true }; | ||||||||||||||||||||||||||||||||||||||||||
| 4562 | var tbody = table.select('tbody'); | ||||||||||||||||||||||||||||||||||||||||||
| 4563 | table.selectAll('th.dt-sortable').on('click', function(event, colIdx) { | ||||||||||||||||||||||||||||||||||||||||||
| 4564 | if (sortState.col === colIdx) { | ||||||||||||||||||||||||||||||||||||||||||
| 4565 | sortState.asc = !sortState.asc; | ||||||||||||||||||||||||||||||||||||||||||
| 4566 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 4567 | sortState.col = colIdx; | ||||||||||||||||||||||||||||||||||||||||||
| 4568 | sortState.asc = true; | ||||||||||||||||||||||||||||||||||||||||||
| 4569 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4570 | var rows = tbody.selectAll('tr').data(); | ||||||||||||||||||||||||||||||||||||||||||
| 4571 | rows.sort(function(a, b) { | ||||||||||||||||||||||||||||||||||||||||||
| 4572 | var va = a[colIdx], vb = b[colIdx]; | ||||||||||||||||||||||||||||||||||||||||||
| 4573 | var na = parseFloat(va), nb = parseFloat(vb); | ||||||||||||||||||||||||||||||||||||||||||
| 4574 | if (!isNaN(na) && !isNaN(nb)) { va = na; vb = nb; } | ||||||||||||||||||||||||||||||||||||||||||
| 4575 | if (va < vb) return sortState.asc ? -1 : 1; | ||||||||||||||||||||||||||||||||||||||||||
| 4576 | if (va > vb) return sortState.asc ? 1 : -1; | ||||||||||||||||||||||||||||||||||||||||||
| 4577 | return 0; | ||||||||||||||||||||||||||||||||||||||||||
| 4578 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4579 | tbody.selectAll('tr').data(rows).join('tr') | ||||||||||||||||||||||||||||||||||||||||||
| 4580 | .selectAll('td').data(function(d) { return d.slice(1); }).join('td') | ||||||||||||||||||||||||||||||||||||||||||
| 4581 | .text(function(d) { return d; }); | ||||||||||||||||||||||||||||||||||||||||||
| 4582 | table.selectAll('th.dt-sortable') | ||||||||||||||||||||||||||||||||||||||||||
| 4583 | .attr('data-sort', function(d, i) { | ||||||||||||||||||||||||||||||||||||||||||
| 4584 | return i === sortState.col ? (sortState.asc ? 'asc' : 'desc') : null; | ||||||||||||||||||||||||||||||||||||||||||
| 4585 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4586 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4587 | SORT | ||||||||||||||||||||||||||||||||||||||||||
| 4588 | |||||||||||||||||||||||||||||||||||||||||||
| 4589 | 29 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 4590 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 4591 | #$table_id { | ||||||||||||||||||||||||||||||||||||||||||
| 4592 | border-collapse: collapse; | ||||||||||||||||||||||||||||||||||||||||||
| 4593 | width: 100%; | ||||||||||||||||||||||||||||||||||||||||||
| 4594 | font-size: 13px; | ||||||||||||||||||||||||||||||||||||||||||
| 4595 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4596 | #$table_id caption { font-weight: bold; margin-bottom: 6px; text-align: left; } | ||||||||||||||||||||||||||||||||||||||||||
| 4597 | #$table_id th, #$table_id td { border: 1px solid #ccc; padding: 6px 10px; text-align: left; } | ||||||||||||||||||||||||||||||||||||||||||
| 4598 | #$table_id th { background: #f5f5f5; $sort_cursor_css } | ||||||||||||||||||||||||||||||||||||||||||
| 4599 | #$table_id th[data-sort="asc"]::after { content: " \\25B2"; font-size: 10px; } | ||||||||||||||||||||||||||||||||||||||||||
| 4600 | #$table_id th[data-sort="desc"]::after { content: " \\25BC"; font-size: 10px; } | ||||||||||||||||||||||||||||||||||||||||||
| 4601 | #$table_id tr:nth-child(even) { background: #fafafa; } | ||||||||||||||||||||||||||||||||||||||||||
| 4602 | #$table_id tr:hover { background: #f0f4ff; } | ||||||||||||||||||||||||||||||||||||||||||
| 4603 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 4604 | <table id="$table_id"> | ||||||||||||||||||||||||||||||||||||||||||
| 4605 | $caption_html<thead> | ||||||||||||||||||||||||||||||||||||||||||
| 4606 | $thead_html</thead> | ||||||||||||||||||||||||||||||||||||||||||
| 4607 | <tbody> | ||||||||||||||||||||||||||||||||||||||||||
| 4608 | $tbody_html</tbody> | ||||||||||||||||||||||||||||||||||||||||||
| 4609 | </table> | ||||||||||||||||||||||||||||||||||||||||||
| 4610 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 4611 | (function() { | ||||||||||||||||||||||||||||||||||||||||||
| 4612 | var allData = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 4613 | var table = d3.select("#$table_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 4614 | var headers = allData[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 4615 | $sort_script})(); | ||||||||||||||||||||||||||||||||||||||||||
| 4616 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 4617 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 4618 | |||||||||||||||||||||||||||||||||||||||||||
| 4619 | 29 | return { table_id => $table_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 4620 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4621 | |||||||||||||||||||||||||||||||||||||||||||
| 4622 | =head2 render_stacked_bar_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4623 | |||||||||||||||||||||||||||||||||||||||||||
| 4624 | my $result = $chart->render_stacked_bar_snippet(\@rows, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 4625 | |||||||||||||||||||||||||||||||||||||||||||
| 4626 | Generates an embeddable stacked bar chart (no page shell, no CDN tag). | ||||||||||||||||||||||||||||||||||||||||||
| 4627 | |||||||||||||||||||||||||||||||||||||||||||
| 4628 | Each element of C<\@rows> is a hashref with three keys named by C<opts>: | ||||||||||||||||||||||||||||||||||||||||||
| 4629 | C<cat_col> (the X-axis grouping), C<seg_col> (the stacking segment), and | ||||||||||||||||||||||||||||||||||||||||||
| 4630 | C<val_col> (the numeric measure). Multiple rows sharing the same | ||||||||||||||||||||||||||||||||||||||||||
| 4631 | cat+seg combination are summed. | ||||||||||||||||||||||||||||||||||||||||||
| 4632 | |||||||||||||||||||||||||||||||||||||||||||
| 4633 | Accepted C<\%opts> keys: | ||||||||||||||||||||||||||||||||||||||||||
| 4634 | |||||||||||||||||||||||||||||||||||||||||||
| 4635 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4636 | |||||||||||||||||||||||||||||||||||||||||||
| 4637 | =item * C<cat_col> (string, default C<'cat'>) - row key for the X-axis category. | ||||||||||||||||||||||||||||||||||||||||||
| 4638 | |||||||||||||||||||||||||||||||||||||||||||
| 4639 | =item * C<seg_col> (string, default C<'seg'>) - row key for the stacking segment. | ||||||||||||||||||||||||||||||||||||||||||
| 4640 | |||||||||||||||||||||||||||||||||||||||||||
| 4641 | =item * C<val_col> (string, default C<'val'>) - row key for the numeric value. | ||||||||||||||||||||||||||||||||||||||||||
| 4642 | |||||||||||||||||||||||||||||||||||||||||||
| 4643 | =item * C<orient> (string C<'v'> or C<'h'>, default C<'v'>) - bar orientation. | ||||||||||||||||||||||||||||||||||||||||||
| 4644 | |||||||||||||||||||||||||||||||||||||||||||
| 4645 | =item * C<scheme> (string, default C<'tableau10'>) - D3 categorical colour scheme. | ||||||||||||||||||||||||||||||||||||||||||
| 4646 | Supported: C<tableau10>, C<category10>, C<set2>, C<set3>, C<paired> - the names | ||||||||||||||||||||||||||||||||||||||||||
| 4647 | C<categorical_schemes()> returns, whose colours C<palette()> makes available to | ||||||||||||||||||||||||||||||||||||||||||
| 4648 | Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 4649 | |||||||||||||||||||||||||||||||||||||||||||
| 4650 | =item * C<x_label> (string, optional) - axis label shown below the category axis. | ||||||||||||||||||||||||||||||||||||||||||
| 4651 | |||||||||||||||||||||||||||||||||||||||||||
| 4652 | =item * C<sort> (string, optional) - sort categories: C<'value'> (desc total) | ||||||||||||||||||||||||||||||||||||||||||
| 4653 | or C<'label'> (alpha). | ||||||||||||||||||||||||||||||||||||||||||
| 4654 | |||||||||||||||||||||||||||||||||||||||||||
| 4655 | =item * C<id> (string, optional) - override the default C<svg_id> value. | ||||||||||||||||||||||||||||||||||||||||||
| 4656 | |||||||||||||||||||||||||||||||||||||||||||
| 4657 | =item * C<responsive> (boolean, optional) - SVG responsive sizing override. | ||||||||||||||||||||||||||||||||||||||||||
| 4658 | |||||||||||||||||||||||||||||||||||||||||||
| 4659 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4660 | |||||||||||||||||||||||||||||||||||||||||||
| 4661 | Returns C<{ svg_id =E<gt> 'stacked_bar', html =E<gt> Str }>. | ||||||||||||||||||||||||||||||||||||||||||
| 4662 | |||||||||||||||||||||||||||||||||||||||||||
| 4663 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 4664 | |||||||||||||||||||||||||||||||||||||||||||
| 4665 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4666 | |||||||||||||||||||||||||||||||||||||||||||
| 4667 | =item * Throws C<Data must be an array of hashes> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 4668 | |||||||||||||||||||||||||||||||||||||||||||
| 4669 | =item * Throws C<Each data point must be a hash reference> when a row is not a hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 4670 | |||||||||||||||||||||||||||||||||||||||||||
| 4671 | =item * Throws C<Value must be numeric> when a value field is not a number. | ||||||||||||||||||||||||||||||||||||||||||
| 4672 | |||||||||||||||||||||||||||||||||||||||||||
| 4673 | =item * Throws C<orient must be 'v' or 'h'> for an invalid C<orient> value. | ||||||||||||||||||||||||||||||||||||||||||
| 4674 | |||||||||||||||||||||||||||||||||||||||||||
| 4675 | =item * Throws C<sort must be 'value' or 'label'> for an invalid C<sort> value. | ||||||||||||||||||||||||||||||||||||||||||
| 4676 | |||||||||||||||||||||||||||||||||||||||||||
| 4677 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4678 | |||||||||||||||||||||||||||||||||||||||||||
| 4679 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 4680 | |||||||||||||||||||||||||||||||||||||||||||
| 4681 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 4682 | |||||||||||||||||||||||||||||||||||||||||||
| 4683 | { | ||||||||||||||||||||||||||||||||||||||||||
| 4684 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4685 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 4686 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 4687 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 4688 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 4689 | cat_col => { type => 'string', optional => 1, default => 'cat' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4690 | seg_col => { type => 'string', optional => 1, default => 'seg' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4691 | val_col => { type => 'string', optional => 1, default => 'val' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4692 | orient => { type => 'string', optional => 1, default => 'v' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4693 | scheme => { type => 'string', optional => 1, default => 'tableau10' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4694 | x_label => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4695 | sort => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4696 | id => { type => 'string', optional => 1, default => 'stacked_bar' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4697 | responsive => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4698 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4699 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4700 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4701 | |||||||||||||||||||||||||||||||||||||||||||
| 4702 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 4703 | |||||||||||||||||||||||||||||||||||||||||||
| 4704 | HashRef -- { svg_id => Str, html => Str } | ||||||||||||||||||||||||||||||||||||||||||
| 4705 | |||||||||||||||||||||||||||||||||||||||||||
| 4706 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 4707 | |||||||||||||||||||||||||||||||||||||||||||
| 4708 | sub render_stacked_bar_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4709 | 43 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_stacked_bar_snippet: called CC=40 SCAR=37.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 4710 | 43 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 4711 | 43 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 4712 | 43 | ▶ | die 'Data must be an array of hashes' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4713 | 42 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 4714 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 4715 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of hashes' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 4716 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 4717 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4718 | 42 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 4719 | 42 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 4720 | |||||||||||||||||||||||||||||||||||||||||||
| 4721 | 42 | ▶ | my $cat_col = $opts->{cat_col} // 'cat'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"cat_col"} // "cat"
MC/DC0%0/1
$opts->{"cat_col"} // "cat"
$opts->{"cat_col"}
Truth table50%1/2
A
A= $opts->{"cat_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4722 | 42 | ▶ | my $seg_col = $opts->{seg_col} // 'seg'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"seg_col"} // "seg"
MC/DC0%0/1
$opts->{"seg_col"} // "seg"
$opts->{"seg_col"}
Truth table50%1/2
A
A= $opts->{"seg_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4723 | 42 | ▶ | my $val_col = $opts->{val_col} // 'val'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"val_col"} // "val"
MC/DC0%0/1
$opts->{"val_col"} // "val"
$opts->{"val_col"}
Truth table50%1/2
A
A= $opts->{"val_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4724 | 42 | ▶ | my $orient = $opts->{orient} // 'v'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"orient"} // "v"
MC/DC100%1/1
$opts->{"orient"} // "v"
$opts->{"orient"}
Truth table100%2/2
A
A= $opts->{"orient"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4725 | 42 | my $scheme = _scheme_name($opts->{scheme}); | |||||||||||||||||||||||||||||||||||||||||
| 4726 | 42 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4727 | 42 | my $sort_by = $opts->{sort}; | |||||||||||||||||||||||||||||||||||||||||
| 4728 | 42 | my $schemes_js = $self->_schemes_js('var'); | |||||||||||||||||||||||||||||||||||||||||
| 4729 | 42 | my $default_js = _default_scheme_js(); | |||||||||||||||||||||||||||||||||||||||||
| 4730 | |||||||||||||||||||||||||||||||||||||||||||
| 4731 | 42 | ▶ | die "orient must be 'v' or 'h'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$orient eq "v" or $orient eq "h"
MC/DC100%2/2
$orient eq "v" or $orient eq "h"
$orient eq "v"
$orient eq "h"
Truth table100%3/3
A or B
A= $orient eq "v"B= $orient eq "h"
| |||||||||||||||||||||||||||||||||||||||||||
| 4732 | unless $orient eq 'v' || $orient eq 'h'; | ||||||||||||||||||||||||||||||||||||||||||
| 4733 | 41 | ▶ | die "sort must be 'value' or 'label'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $sort_by and $sort_by ne "value" and $sort_by ne "label"
defined $sort_by and $sort_by ne "value"
MC/DC100%3/3
defined $sort_by and $sort_by ne "value" and $sort_by ne "label"
defined $sort_by
$sort_by ne "value"
$sort_by ne "label"
Truth table100%4/4
A and B and C
A= defined $sort_byB= $sort_by ne "value"C= $sort_by ne "label"
| |||||||||||||||||||||||||||||||||||||||||||
| 4734 | if defined($sort_by) && $sort_by ne 'value' && $sort_by ne 'label'; | ||||||||||||||||||||||||||||||||||||||||||
| 4735 | |||||||||||||||||||||||||||||||||||||||||||
| 4736 | 40 | my (%cats_seen, %segs_seen, %pivot, @cat_order); | |||||||||||||||||||||||||||||||||||||||||
| 4737 | 40 | for my $row (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 4738 | 91 | ▶ | die 'Each data point must be a hash reference' unless ref($row) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4739 | 90 | ▶ | die "Value must be numeric" unless looks_like_number($row->{$val_col} // 'x'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$row->{$val_col} // "x"
MC/DC0%0/1
$row->{$val_col} // "x"
$row->{$val_col}
Truth table50%1/2
A
A= $row->{$val_col}
| |||||||||||||||||||||||||||||||||||||||||||
| 4740 | 89 | ▶ | my ($cat, $seg, $val) = ($row->{$cat_col} // '', $row->{$seg_col} // '', $row->{$val_col} + 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$row->{$cat_col} // ""
$row->{$seg_col} // ""
MC/DC0%0/1
$row->{$cat_col} // ""
$row->{$cat_col}
MC/DC0%0/1
$row->{$seg_col} // ""
$row->{$seg_col}
Truth table50%1/2
A
A= $row->{$cat_col}
Truth table50%1/2
A
A= $row->{$seg_col}
| |||||||||||||||||||||||||||||||||||||||||||
| 4741 | 89 | ▶ | push @cat_order, $cat unless $cats_seen{$cat}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4742 | 89 | $segs_seen{$seg} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 4743 | 89 | ▶ | $pivot{$cat}{$seg} = ($pivot{$cat}{$seg} // 0) + $val; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pivot{$cat}{$seg} // 0
MC/DC0%0/1
$pivot{$cat}{$seg} // 0
$pivot{$cat}{$seg}
Truth table50%1/2
A
A= $pivot{$cat}{$seg}
| |||||||||||||||||||||||||||||||||||||||||||
| 4744 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4745 | |||||||||||||||||||||||||||||||||||||||||||
| 4746 | 38 | my @segs = keys %segs_seen; | |||||||||||||||||||||||||||||||||||||||||
| 4747 | 38 | my @cats = @cat_order; | |||||||||||||||||||||||||||||||||||||||||
| 4748 | |||||||||||||||||||||||||||||||||||||||||||
| 4749 | 38 | ▶ | if (defined $sort_by && $sort_by eq 'label') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $sort_by and $sort_by eq "label"
MC/DC100%2/2
defined $sort_by and $sort_by eq "label"
defined $sort_by
$sort_by eq "label"
Truth table100%3/3
A and B
A= defined $sort_byB= $sort_by eq "label"
| |||||||||||||||||||||||||||||||||||||||||||
| 4750 | 1 | @cats = sort @cats; | |||||||||||||||||||||||||||||||||||||||||
| 4751 | ▶ | } elsif (defined $sort_by && $sort_by eq 'value') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $sort_by and $sort_by eq "value"
MC/DC50%1/2
defined $sort_by and $sort_by eq "value"
defined $sort_by
$sort_by eq "value"
Truth table66%2/3
A and B
A= defined $sort_byB= $sort_by eq "value"
| |||||||||||||||||||||||||||||||||||||||||||
| 4752 | @cats = sort { | ||||||||||||||||||||||||||||||||||||||||||
| 4753 | 1 | my ($ta, $tb) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 4754 | 3 | ▶ | $ta += ($pivot{$a}{$_} // 0) for @segs; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pivot{$a}{$_} // 0
MC/DC0%0/1
$pivot{$a}{$_} // 0
$pivot{$a}{$_}
Truth table50%1/2
A
A= $pivot{$a}{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 4755 | 3 | ▶ | $tb += ($pivot{$b}{$_} // 0) for @segs; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pivot{$b}{$_} // 0
MC/DC0%0/1
$pivot{$b}{$_} // 0
$pivot{$b}{$_}
Truth table50%1/2
A
A= $pivot{$b}{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 4756 | 3 | $tb <=> $ta | |||||||||||||||||||||||||||||||||||||||||
| 4757 | } @cats; | ||||||||||||||||||||||||||||||||||||||||||
| 4758 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4759 | |||||||||||||||||||||||||||||||||||||||||||
| 4760 | my @rows = map { | ||||||||||||||||||||||||||||||||||||||||||
| 4761 | 38 | my $cat = $_; | |||||||||||||||||||||||||||||||||||||||||
| 4762 | 59 | my %r = (cat => $cat); | |||||||||||||||||||||||||||||||||||||||||
| 4763 | 59 | ▶ | $r{$_} = ($pivot{$cat}{$_} // 0) for @segs; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$pivot{$cat}{$_} // 0
MC/DC100%1/1
$pivot{$cat}{$_} // 0
$pivot{$cat}{$_}
Truth table100%2/2
A
A= $pivot{$cat}{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 4764 | 59 | \%r | |||||||||||||||||||||||||||||||||||||||||
| 4765 | } @cats; | ||||||||||||||||||||||||||||||||||||||||||
| 4766 | |||||||||||||||||||||||||||||||||||||||||||
| 4767 | 38 | my $json_data = _script_json(\@rows); | |||||||||||||||||||||||||||||||||||||||||
| 4768 | 38 | my $json_segs = _script_json(\@segs); | |||||||||||||||||||||||||||||||||||||||||
| 4769 | |||||||||||||||||||||||||||||||||||||||||||
| 4770 | 38 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 4771 | |||||||||||||||||||||||||||||||||||||||||||
| 4772 | 38 | my $x_label_esc = $js_esc->($x_label); | |||||||||||||||||||||||||||||||||||||||||
| 4773 | 38 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'stacked_bar'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "stacked_bar"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "stacked_bar"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 4774 | 38 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 4775 | 38 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 4776 | 38 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 4777 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 4778 | 38 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 4779 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 4780 | |||||||||||||||||||||||||||||||||||||||||||
| 4781 | 38 | ▶ | my $is_v = ($orient eq 'v') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4782 | 38 | my $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 4783 | 38 | my $margin_right = 130; | |||||||||||||||||||||||||||||||||||||||||
| 4784 | 38 | ▶ | my $margin_bottom = $is_v ? ($x_label ? 60 : 40) : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 4785 | 38 | ▶ | my $margin_left = $is_v ? 55 : 130; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4786 | 38 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 4787 | 38 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 4788 | 38 | my $legend_x = $margin_left + $inner_w + 12; | |||||||||||||||||||||||||||||||||||||||||
| 4789 | |||||||||||||||||||||||||||||||||||||||||||
| 4790 | 38 | ▶ | my $x_label_block = $x_label ? <<"XLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4791 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 4792 | .attr("class","sb-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 4793 | .attr("x", innerW / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 4794 | .attr("y", innerH + (margin.bottom - 10)) | ||||||||||||||||||||||||||||||||||||||||||
| 4795 | .attr("text-anchor","middle") | ||||||||||||||||||||||||||||||||||||||||||
| 4796 | .text("$x_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 4797 | XLB | ||||||||||||||||||||||||||||||||||||||||||
| 4798 | |||||||||||||||||||||||||||||||||||||||||||
| 4799 | 38 | my ($scales_js, $axes_js, $rect_attrs_js); | |||||||||||||||||||||||||||||||||||||||||
| 4800 | 38 | ▶ | if ($is_v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 4801 | 37 | $scales_js = <<'SCALES'; | |||||||||||||||||||||||||||||||||||||||||
| 4802 | var xScale = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 4803 | .domain(data.map(function(d){return d.cat;})) | ||||||||||||||||||||||||||||||||||||||||||
| 4804 | .range([0, innerW]).padding(0.2); | ||||||||||||||||||||||||||||||||||||||||||
| 4805 | var yScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 4806 | .domain([0, d3.max(stackedData[stackedData.length-1], function(d){return d[1];})]) | ||||||||||||||||||||||||||||||||||||||||||
| 4807 | .nice().range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 4808 | SCALES | ||||||||||||||||||||||||||||||||||||||||||
| 4809 | 37 | $axes_js = <<'AXES'; | |||||||||||||||||||||||||||||||||||||||||
| 4810 | g.append("g").attr("class","sb-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 4811 | .attr("transform","translate(0,"+innerH+")").call(d3.axisBottom(xScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 4812 | g.append("g").attr("class","sb-y-axis").call(d3.axisLeft(yScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 4813 | AXES | ||||||||||||||||||||||||||||||||||||||||||
| 4814 | 37 | $rect_attrs_js = <<'RECTS'; | |||||||||||||||||||||||||||||||||||||||||
| 4815 | .attr("x", function(d){return xScale(d.data.cat);}) | ||||||||||||||||||||||||||||||||||||||||||
| 4816 | .attr("y", function(d){return yScale(d[1]);}) | ||||||||||||||||||||||||||||||||||||||||||
| 4817 | .attr("height", function(d){return Math.max(0, yScale(d[0]) - yScale(d[1]));}) | ||||||||||||||||||||||||||||||||||||||||||
| 4818 | .attr("width", xScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 4819 | RECTS | ||||||||||||||||||||||||||||||||||||||||||
| 4820 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 4821 | 1 | $scales_js = <<'SCALES'; | |||||||||||||||||||||||||||||||||||||||||
| 4822 | var yScale = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 4823 | .domain(data.map(function(d){return d.cat;})) | ||||||||||||||||||||||||||||||||||||||||||
| 4824 | .range([0, innerH]).padding(0.2); | ||||||||||||||||||||||||||||||||||||||||||
| 4825 | var xScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 4826 | .domain([0, d3.max(stackedData[stackedData.length-1], function(d){return d[1];})]) | ||||||||||||||||||||||||||||||||||||||||||
| 4827 | .nice().range([0, innerW]); | ||||||||||||||||||||||||||||||||||||||||||
| 4828 | SCALES | ||||||||||||||||||||||||||||||||||||||||||
| 4829 | 1 | $axes_js = <<'AXES'; | |||||||||||||||||||||||||||||||||||||||||
| 4830 | g.append("g").attr("class","sb-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 4831 | .attr("transform","translate(0,"+innerH+")").call(d3.axisBottom(xScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 4832 | g.append("g").attr("class","sb-y-axis").call(d3.axisLeft(yScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 4833 | AXES | ||||||||||||||||||||||||||||||||||||||||||
| 4834 | 1 | $rect_attrs_js = <<'RECTS'; | |||||||||||||||||||||||||||||||||||||||||
| 4835 | .attr("y", function(d){return yScale(d.data.cat);}) | ||||||||||||||||||||||||||||||||||||||||||
| 4836 | .attr("x", function(d){return xScale(d[0]);}) | ||||||||||||||||||||||||||||||||||||||||||
| 4837 | .attr("width", function(d){return Math.max(0, xScale(d[1]) - xScale(d[0]));}) | ||||||||||||||||||||||||||||||||||||||||||
| 4838 | .attr("height", yScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 4839 | RECTS | ||||||||||||||||||||||||||||||||||||||||||
| 4840 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4841 | |||||||||||||||||||||||||||||||||||||||||||
| 4842 | 38 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 4843 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 4844 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 4845 | position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 4846 | border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none; | ||||||||||||||||||||||||||||||||||||||||||
| 4847 | display:none;line-height:1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 4848 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4849 | .sb-axis-label{font-size:11px;fill:#555;} | ||||||||||||||||||||||||||||||||||||||||||
| 4850 | .sb-legend-item{cursor:pointer;} | ||||||||||||||||||||||||||||||||||||||||||
| 4851 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 4852 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 4853 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 4854 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 4855 | (function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 4856 | var data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 4857 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 4858 | var segs = $json_segs; | ||||||||||||||||||||||||||||||||||||||||||
| 4859 | var margin = {top:$margin_top,right:$margin_right,bottom:$margin_bottom,left:$margin_left}; | ||||||||||||||||||||||||||||||||||||||||||
| 4860 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 4861 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 4862 | $schemes_js | ||||||||||||||||||||||||||||||||||||||||||
| 4863 | var color = d3.scaleOrdinal(SCHEMES['$scheme'] || $default_js).domain(segs); | ||||||||||||||||||||||||||||||||||||||||||
| 4864 | var hidden = {}; | ||||||||||||||||||||||||||||||||||||||||||
| 4865 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 4866 | var g = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 4867 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 4868 | var stackFn = d3.stack().order(d3.stackOrderNone).offset(d3.stackOffsetNone); | ||||||||||||||||||||||||||||||||||||||||||
| 4869 | var stackedData = stackFn.keys(segs)(data); | ||||||||||||||||||||||||||||||||||||||||||
| 4870 | $scales_js | ||||||||||||||||||||||||||||||||||||||||||
| 4871 | $axes_js | ||||||||||||||||||||||||||||||||||||||||||
| 4872 | $x_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 4873 | function redraw(){ | ||||||||||||||||||||||||||||||||||||||||||
| 4874 | var vis = segs.filter(function(s){return !hidden[s];}); | ||||||||||||||||||||||||||||||||||||||||||
| 4875 | var sd = stackFn.keys(vis)(data); | ||||||||||||||||||||||||||||||||||||||||||
| 4876 | var layers = g.selectAll(".sb-layer").data(sd, function(d){return d.key;}); | ||||||||||||||||||||||||||||||||||||||||||
| 4877 | var entered = layers.enter().append("g").attr("class","sb-layer") | ||||||||||||||||||||||||||||||||||||||||||
| 4878 | .attr("fill",function(d){return color(d.key);}); | ||||||||||||||||||||||||||||||||||||||||||
| 4879 | entered.merge(layers) | ||||||||||||||||||||||||||||||||||||||||||
| 4880 | .selectAll(".sb-rect").data(function(d){return d;}) | ||||||||||||||||||||||||||||||||||||||||||
| 4881 | .join("rect").attr("class","sb-rect") | ||||||||||||||||||||||||||||||||||||||||||
| 4882 | $rect_attrs_js | ||||||||||||||||||||||||||||||||||||||||||
| 4883 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 4884 | var seg = d3.select(this.parentNode).datum().key; | ||||||||||||||||||||||||||||||||||||||||||
| 4885 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 4886 | .html("<b>"+esc(d.data.cat)+"<\\/b><br\/>"+esc(seg)+": "+(d[1]-d[0])); | ||||||||||||||||||||||||||||||||||||||||||
| 4887 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 4888 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 4889 | tip.style("left",(event.offsetX+12)+"px").style("top",(event.offsetY-28)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 4890 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 4891 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 4892 | layers.exit().remove(); | ||||||||||||||||||||||||||||||||||||||||||
| 4893 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4894 | redraw(); | ||||||||||||||||||||||||||||||||||||||||||
| 4895 | var legend = svg.append("g").attr("transform","translate($legend_x,$margin_top)"); | ||||||||||||||||||||||||||||||||||||||||||
| 4896 | segs.forEach(function(seg,i){ | ||||||||||||||||||||||||||||||||||||||||||
| 4897 | var item = legend.append("g").attr("class","sb-legend-item") | ||||||||||||||||||||||||||||||||||||||||||
| 4898 | .attr("transform","translate(0,"+(i*20)+")") | ||||||||||||||||||||||||||||||||||||||||||
| 4899 | .on("click",function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 4900 | hidden[seg] = !hidden[seg]; | ||||||||||||||||||||||||||||||||||||||||||
| 4901 | d3.select(this).select("rect").attr("opacity",hidden[seg]?0.3:1); | ||||||||||||||||||||||||||||||||||||||||||
| 4902 | d3.select(this).select("text").attr("opacity",hidden[seg]?0.4:1); | ||||||||||||||||||||||||||||||||||||||||||
| 4903 | redraw(); | ||||||||||||||||||||||||||||||||||||||||||
| 4904 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4905 | item.append("rect").attr("width",12).attr("height",12).attr("y",-10) | ||||||||||||||||||||||||||||||||||||||||||
| 4906 | .attr("fill",color(seg)); | ||||||||||||||||||||||||||||||||||||||||||
| 4907 | item.append("text").attr("x",16).attr("font-size","11px").attr("fill","#333") | ||||||||||||||||||||||||||||||||||||||||||
| 4908 | .text(seg); | ||||||||||||||||||||||||||||||||||||||||||
| 4909 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 4910 | })(); | ||||||||||||||||||||||||||||||||||||||||||
| 4911 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 4912 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 4913 | |||||||||||||||||||||||||||||||||||||||||||
| 4914 | 38 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 4915 | } | ||||||||||||||||||||||||||||||||||||||||||
| 4916 | |||||||||||||||||||||||||||||||||||||||||||
| 4917 | =head2 render_combo_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 4918 | |||||||||||||||||||||||||||||||||||||||||||
| 4919 | my $result = $chart->render_combo_snippet(\@rows, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 4920 | |||||||||||||||||||||||||||||||||||||||||||
| 4921 | Generates an embeddable combo chart: bars for one measure on the left Y | ||||||||||||||||||||||||||||||||||||||||||
| 4922 | axis, a line for another measure on the right Y axis, sharing the same | ||||||||||||||||||||||||||||||||||||||||||
| 4923 | X (category) axis. | ||||||||||||||||||||||||||||||||||||||||||
| 4924 | |||||||||||||||||||||||||||||||||||||||||||
| 4925 | Each element of C<\@rows> is a hashref with at least three keys named by | ||||||||||||||||||||||||||||||||||||||||||
| 4926 | C<opts>: C<x_col> (X-axis label), C<bar_col> (bar measure), and | ||||||||||||||||||||||||||||||||||||||||||
| 4927 | C<line_col> (line measure). | ||||||||||||||||||||||||||||||||||||||||||
| 4928 | |||||||||||||||||||||||||||||||||||||||||||
| 4929 | Accepted C<\%opts> keys: | ||||||||||||||||||||||||||||||||||||||||||
| 4930 | |||||||||||||||||||||||||||||||||||||||||||
| 4931 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4932 | |||||||||||||||||||||||||||||||||||||||||||
| 4933 | =item * C<x_col> (string, default C<'x'>) - row key for the X-axis label. | ||||||||||||||||||||||||||||||||||||||||||
| 4934 | |||||||||||||||||||||||||||||||||||||||||||
| 4935 | =item * C<bar_col> (string, default C<'bar_val'>) - row key for the bar measure. | ||||||||||||||||||||||||||||||||||||||||||
| 4936 | |||||||||||||||||||||||||||||||||||||||||||
| 4937 | =item * C<line_col> (string, default C<'line_val'>) - row key for the line measure. | ||||||||||||||||||||||||||||||||||||||||||
| 4938 | |||||||||||||||||||||||||||||||||||||||||||
| 4939 | =item * C<bar_label> (string, default C<'Bar'>) - legend/axis label for bars. | ||||||||||||||||||||||||||||||||||||||||||
| 4940 | |||||||||||||||||||||||||||||||||||||||||||
| 4941 | =item * C<line_label> (string, default C<'Line'>) - legend/axis label for line. | ||||||||||||||||||||||||||||||||||||||||||
| 4942 | |||||||||||||||||||||||||||||||||||||||||||
| 4943 | =item * C<bar_color> (string, optional CSS colour, default C<'steelblue'>) - bar fill. | ||||||||||||||||||||||||||||||||||||||||||
| 4944 | A value holding anything a CSS colour cannot, such as a quote or C<E<lt>>, is | ||||||||||||||||||||||||||||||||||||||||||
| 4945 | ignored and the default used. | ||||||||||||||||||||||||||||||||||||||||||
| 4946 | |||||||||||||||||||||||||||||||||||||||||||
| 4947 | =item * C<line_color> (string, optional CSS colour, default C<'#e15759'>) - line stroke. | ||||||||||||||||||||||||||||||||||||||||||
| 4948 | Checked as C<bar_color> is. | ||||||||||||||||||||||||||||||||||||||||||
| 4949 | |||||||||||||||||||||||||||||||||||||||||||
| 4950 | =item * C<scheme> (string, optional) - a categorical scheme, as | ||||||||||||||||||||||||||||||||||||||||||
| 4951 | C<categorical_schemes()> lists them. The bars take its first colour and the | ||||||||||||||||||||||||||||||||||||||||||
| 4952 | line its second, as C<colour_at($scheme, 0)> and C<colour_at($scheme, 1)>. | ||||||||||||||||||||||||||||||||||||||||||
| 4953 | C<bar_color> and C<line_color> still win when given. Any other name is | ||||||||||||||||||||||||||||||||||||||||||
| 4954 | ignored and the defaults used. | ||||||||||||||||||||||||||||||||||||||||||
| 4955 | |||||||||||||||||||||||||||||||||||||||||||
| 4956 | =item * C<id> (string, optional) - override the default C<svg_id> value. | ||||||||||||||||||||||||||||||||||||||||||
| 4957 | |||||||||||||||||||||||||||||||||||||||||||
| 4958 | =item * C<responsive> (boolean, optional) - SVG responsive sizing override. | ||||||||||||||||||||||||||||||||||||||||||
| 4959 | |||||||||||||||||||||||||||||||||||||||||||
| 4960 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4961 | |||||||||||||||||||||||||||||||||||||||||||
| 4962 | Returns C<{ svg_id =E<gt> 'combo_chart', html =E<gt> Str }>. | ||||||||||||||||||||||||||||||||||||||||||
| 4963 | |||||||||||||||||||||||||||||||||||||||||||
| 4964 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 4965 | |||||||||||||||||||||||||||||||||||||||||||
| 4966 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 4967 | |||||||||||||||||||||||||||||||||||||||||||
| 4968 | =item * Throws C<Data must be an array of hashes> when C<$data> is not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 4969 | |||||||||||||||||||||||||||||||||||||||||||
| 4970 | =item * Throws C<Each data point must be a hash reference> when a row is not a hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 4971 | |||||||||||||||||||||||||||||||||||||||||||
| 4972 | =item * Throws C<Bar value must be numeric> when a bar value field is not a number. | ||||||||||||||||||||||||||||||||||||||||||
| 4973 | |||||||||||||||||||||||||||||||||||||||||||
| 4974 | =item * Throws C<Line value must be numeric> when a line value field is not a number. | ||||||||||||||||||||||||||||||||||||||||||
| 4975 | |||||||||||||||||||||||||||||||||||||||||||
| 4976 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 4977 | |||||||||||||||||||||||||||||||||||||||||||
| 4978 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 4979 | |||||||||||||||||||||||||||||||||||||||||||
| 4980 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 4981 | |||||||||||||||||||||||||||||||||||||||||||
| 4982 | { | ||||||||||||||||||||||||||||||||||||||||||
| 4983 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4984 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 4985 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 4986 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 4987 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 4988 | x_col => { type => 'string', optional => 1, default => 'x' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4989 | bar_col => { type => 'string', optional => 1, default => 'bar_val' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4990 | line_col => { type => 'string', optional => 1, default => 'line_val' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4991 | bar_label => { type => 'string', optional => 1, default => 'Bar' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4992 | line_label => { type => 'string', optional => 1, default => 'Line' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4993 | bar_color => { type => 'string', optional => 1, default => 'steelblue' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4994 | line_color => { type => 'string', optional => 1, default => '#e15759' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4995 | scheme => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4996 | id => { type => 'string', optional => 1, default => 'combo_chart' }, | ||||||||||||||||||||||||||||||||||||||||||
| 4997 | responsive => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 4998 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 4999 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 5000 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5001 | |||||||||||||||||||||||||||||||||||||||||||
| 5002 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 5003 | |||||||||||||||||||||||||||||||||||||||||||
| 5004 | HashRef -- { svg_id => Str, html => Str } | ||||||||||||||||||||||||||||||||||||||||||
| 5005 | |||||||||||||||||||||||||||||||||||||||||||
| 5006 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 5007 | |||||||||||||||||||||||||||||||||||||||||||
| 5008 | sub render_combo_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 5009 | 62 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_combo_snippet: called CC=23 SCAR=31.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 5010 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5011 | 62 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 5012 | 62 | ▶ | die 'Data must be an array of hashes' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5013 | 61 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 5014 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 5015 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of hashes' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 5016 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 5017 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5018 | 61 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 5019 | 61 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 5020 | |||||||||||||||||||||||||||||||||||||||||||
| 5021 | 61 | ▶ | my $x_col = $opts->{x_col} // 'x'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_col"} // "x"
MC/DC100%1/1
$opts->{"x_col"} // "x"
$opts->{"x_col"}
Truth table100%2/2
A
A= $opts->{"x_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5022 | 61 | ▶ | my $bar_col = $opts->{bar_col} // 'bar_val'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"bar_col"} // "bar_val"
MC/DC100%1/1
$opts->{"bar_col"} // "bar_val"
$opts->{"bar_col"}
Truth table100%2/2
A
A= $opts->{"bar_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5023 | 61 | ▶ | my $line_col = $opts->{line_col} // 'line_val'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"line_col"} // "line_val"
MC/DC100%1/1
$opts->{"line_col"} // "line_val"
$opts->{"line_col"}
Truth table100%2/2
A
A= $opts->{"line_col"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5024 | 61 | ▶ | my $bar_label = $opts->{bar_label} // 'Bar'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"bar_label"} // "Bar"
MC/DC100%1/1
$opts->{"bar_label"} // "Bar"
$opts->{"bar_label"}
Truth table100%2/2
A
A= $opts->{"bar_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5025 | 61 | ▶ | my $line_label = $opts->{line_label} // 'Line'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"line_label"} // "Line"
MC/DC100%1/1
$opts->{"line_label"} // "Line"
$opts->{"line_label"}
Truth table100%2/2
A
A= $opts->{"line_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5026 | # A scheme gives both colours at once -- its first for the bars, its second | ||||||||||||||||||||||||||||||||||||||||||
| 5027 | # for the line -- so a combo matches the other charts of a page drawn with | ||||||||||||||||||||||||||||||||||||||||||
| 5028 | # the same scheme. An explicit colour still wins, and a name that is not a | ||||||||||||||||||||||||||||||||||||||||||
| 5029 | # scheme leaves the defaults, as an unknown colour does. | ||||||||||||||||||||||||||||||||||||||||||
| 5030 | 61 | my $scheme = $opts->{scheme}; | |||||||||||||||||||||||||||||||||||||||||
| 5031 | my ($scheme_bar, $scheme_line) = (defined($scheme) && !ref($scheme) && exists($CATEGORICAL_SCHEMES{$scheme})) | ||||||||||||||||||||||||||||||||||||||||||
| 5032 | 61 | ▶ | ? @{ $CATEGORICAL_SCHEMES{$scheme}{colours} }[0, 1] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $scheme and not ref $scheme and exists $CATEGORICAL_SCHEMES{$scheme}
defined $scheme and not ref $scheme
MC/DC100%3/3
defined $scheme and not ref $scheme and exists $CATEGORICAL_SCHEMES{$scheme}
defined $scheme
not ref $scheme
exists $CATEGORICAL_SCHEMES{$scheme}
Truth table100%4/4
A and not(B) and C
A= defined $schemeB= not ref $schemeC= exists $CATEGORICAL_SCHEMES{$scheme}
| |||||||||||||||||||||||||||||||||||||||||||
| 5033 | : ('steelblue', '#e15759'); | ||||||||||||||||||||||||||||||||||||||||||
| 5034 | 61 | ▶ | my $bar_color = _css_colour($opts->{bar_color}) // $scheme_bar; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
_css_colour($opts->{"bar_color"}) // $scheme_bar
MC/DC0%0/2
_css_colour($opts->{"bar_color"}) // $scheme_bar
_css_colour($opts->{"bar_color"})
$scheme_bar
Truth table66%2/3
A // B
A= _css_colour($opts->{"bar_color"})B= $scheme_bar
| |||||||||||||||||||||||||||||||||||||||||||
| 5035 | 61 | ▶ | my $line_color = _css_colour($opts->{line_color}) // $scheme_line; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
_css_colour($opts->{"line_color"}) // $scheme_line
MC/DC0%0/2
_css_colour($opts->{"line_color"}) // $scheme_line
_css_colour($opts->{"line_color"})
$scheme_line
Truth table66%2/3
A // B
A= _css_colour($opts->{"line_color"})B= $scheme_line
| |||||||||||||||||||||||||||||||||||||||||||
| 5036 | |||||||||||||||||||||||||||||||||||||||||||
| 5037 | 61 | my @points; | |||||||||||||||||||||||||||||||||||||||||
| 5038 | 61 | for my $row (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 5039 | 104 | ▶ | die 'Each data point must be a hash reference' unless ref($row) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5040 | 103 | ▶ | die 'Bar value must be numeric' unless looks_like_number($row->{$bar_col} // 'x'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$row->{$bar_col} // "x"
MC/DC0%0/1
$row->{$bar_col} // "x"
$row->{$bar_col}
Truth table50%1/2
A
A= $row->{$bar_col}
| |||||||||||||||||||||||||||||||||||||||||||
| 5041 | 102 | ▶ | die 'Line value must be numeric' unless looks_like_number($row->{$line_col} // 'x'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$row->{$line_col} // "x"
MC/DC0%0/1
$row->{$line_col} // "x"
$row->{$line_col}
Truth table50%1/2
A
A= $row->{$line_col}
| |||||||||||||||||||||||||||||||||||||||||||
| 5042 | push @points, { | ||||||||||||||||||||||||||||||||||||||||||
| 5043 | x => defined($row->{$x_col}) ? "$row->{$x_col}" : '', | ||||||||||||||||||||||||||||||||||||||||||
| 5044 | bar => $row->{$bar_col} + 0, | ||||||||||||||||||||||||||||||||||||||||||
| 5045 | 101 | ▶ | line => $row->{$line_col} + 0, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5046 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 5047 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5048 | |||||||||||||||||||||||||||||||||||||||||||
| 5049 | 58 | my $json_data = _script_json(\@points); | |||||||||||||||||||||||||||||||||||||||||
| 5050 | |||||||||||||||||||||||||||||||||||||||||||
| 5051 | 58 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 5052 | |||||||||||||||||||||||||||||||||||||||||||
| 5053 | 58 | my $bar_label_esc = $js_esc->($bar_label); | |||||||||||||||||||||||||||||||||||||||||
| 5054 | 58 | my $line_label_esc = $js_esc->($line_label); | |||||||||||||||||||||||||||||||||||||||||
| 5055 | 58 | my $bar_color_esc = $js_esc->($bar_color); | |||||||||||||||||||||||||||||||||||||||||
| 5056 | 58 | my $line_color_esc = $js_esc->($line_color); | |||||||||||||||||||||||||||||||||||||||||
| 5057 | |||||||||||||||||||||||||||||||||||||||||||
| 5058 | 58 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'combo_chart'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "combo_chart"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "combo_chart"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5059 | 58 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 5060 | 58 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 5061 | 58 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 5062 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 5063 | 58 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5064 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 5065 | |||||||||||||||||||||||||||||||||||||||||||
| 5066 | 58 | my $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 5067 | 58 | my $margin_right = 70; | |||||||||||||||||||||||||||||||||||||||||
| 5068 | 58 | my $margin_bottom = 55; | |||||||||||||||||||||||||||||||||||||||||
| 5069 | 58 | my $margin_left = 60; | |||||||||||||||||||||||||||||||||||||||||
| 5070 | 58 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 5071 | 58 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 5072 | |||||||||||||||||||||||||||||||||||||||||||
| 5073 | 58 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 5074 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 5075 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 5076 | position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 5077 | border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none; | ||||||||||||||||||||||||||||||||||||||||||
| 5078 | display:none;line-height:1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 5079 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5080 | .cc-axis-label{font-size:11px;fill:#555;} | ||||||||||||||||||||||||||||||||||||||||||
| 5081 | .cc-dot{cursor:default;} | ||||||||||||||||||||||||||||||||||||||||||
| 5082 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 5083 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 5084 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 5085 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 5086 | (function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 5087 | var data = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 5088 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 5089 | var margin = {top:$margin_top,right:$margin_right,bottom:$margin_bottom,left:$margin_left}; | ||||||||||||||||||||||||||||||||||||||||||
| 5090 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 5091 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 5092 | var barLabel = "$bar_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 5093 | var lineLabel = "$line_label_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 5094 | var barColor = "$bar_color_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 5095 | var lineColor = "$line_color_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 5096 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5097 | var g = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 5098 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5099 | |||||||||||||||||||||||||||||||||||||||||||
| 5100 | var xScale = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 5101 | .domain(data.map(function(d){return d.x;})) | ||||||||||||||||||||||||||||||||||||||||||
| 5102 | .range([0, innerW]).padding(0.25); | ||||||||||||||||||||||||||||||||||||||||||
| 5103 | var yLeft = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 5104 | .domain([0, d3.max(data,function(d){return d.bar;})]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 5105 | .range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 5106 | var yRight = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 5107 | .domain([d3.min(data,function(d){return d.line;}), d3.max(data,function(d){return d.line;})]) | ||||||||||||||||||||||||||||||||||||||||||
| 5108 | .nice().range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 5109 | |||||||||||||||||||||||||||||||||||||||||||
| 5110 | g.append("g").attr("class","cc-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 5111 | .attr("transform","translate(0,"+innerH+")").call(d3.axisBottom(xScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 5112 | g.append("g").attr("class","cc-y-left").call(d3.axisLeft(yLeft).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 5113 | g.append("g").attr("class","cc-y-right") | ||||||||||||||||||||||||||||||||||||||||||
| 5114 | .attr("transform","translate("+innerW+",0)").call(d3.axisRight(yRight).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 5115 | |||||||||||||||||||||||||||||||||||||||||||
| 5116 | // Left axis label | ||||||||||||||||||||||||||||||||||||||||||
| 5117 | g.append("text").attr("class","cc-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 5118 | .attr("transform","rotate(-90)") | ||||||||||||||||||||||||||||||||||||||||||
| 5119 | .attr("x",-innerH/2).attr("y",-(margin.left-15)) | ||||||||||||||||||||||||||||||||||||||||||
| 5120 | .attr("text-anchor","middle").text(barLabel); | ||||||||||||||||||||||||||||||||||||||||||
| 5121 | // Right axis label | ||||||||||||||||||||||||||||||||||||||||||
| 5122 | svg.append("text").attr("class","cc-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 5123 | .attr("transform","rotate(90)") | ||||||||||||||||||||||||||||||||||||||||||
| 5124 | .attr("x",(margin.top+innerH/2)).attr("y",-(margin.left+innerW+margin.right-15)) | ||||||||||||||||||||||||||||||||||||||||||
| 5125 | .attr("text-anchor","middle").text(lineLabel); | ||||||||||||||||||||||||||||||||||||||||||
| 5126 | // X axis label | ||||||||||||||||||||||||||||||||||||||||||
| 5127 | g.append("text").attr("class","cc-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 5128 | .attr("x",innerW/2).attr("y",innerH+(margin.bottom-12)) | ||||||||||||||||||||||||||||||||||||||||||
| 5129 | .attr("text-anchor","middle").text(barLabel+" vs "+lineLabel); | ||||||||||||||||||||||||||||||||||||||||||
| 5130 | |||||||||||||||||||||||||||||||||||||||||||
| 5131 | // Bars | ||||||||||||||||||||||||||||||||||||||||||
| 5132 | g.selectAll(".cc-bar").data(data).join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 5133 | .attr("class","cc-bar") | ||||||||||||||||||||||||||||||||||||||||||
| 5134 | .attr("x",function(d){return xScale(d.x);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5135 | .attr("y",function(d){return yLeft(d.bar);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5136 | .attr("width",xScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 5137 | .attr("height",function(d){return innerH - yLeft(d.bar);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5138 | .attr("fill",barColor).attr("opacity",0.8) | ||||||||||||||||||||||||||||||||||||||||||
| 5139 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5140 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 5141 | .html("<b>"+esc(d.x)+"<\\/b><br\/>"+esc(barLabel)+": "+d.bar+"<br\/>"+esc(lineLabel)+": "+d.line); | ||||||||||||||||||||||||||||||||||||||||||
| 5142 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5143 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 5144 | tip.style("left",(event.offsetX+12)+"px").style("top",(event.offsetY-28)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 5145 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5146 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 5147 | |||||||||||||||||||||||||||||||||||||||||||
| 5148 | // Line path | ||||||||||||||||||||||||||||||||||||||||||
| 5149 | var line = d3.line() | ||||||||||||||||||||||||||||||||||||||||||
| 5150 | .x(function(d){return xScale(d.x) + xScale.bandwidth()/2;}) | ||||||||||||||||||||||||||||||||||||||||||
| 5151 | .y(function(d){return yRight(d.line);}); | ||||||||||||||||||||||||||||||||||||||||||
| 5152 | g.append("path").datum(data) | ||||||||||||||||||||||||||||||||||||||||||
| 5153 | .attr("fill","none").attr("stroke",lineColor).attr("stroke-width",2.5) | ||||||||||||||||||||||||||||||||||||||||||
| 5154 | .attr("d",line); | ||||||||||||||||||||||||||||||||||||||||||
| 5155 | |||||||||||||||||||||||||||||||||||||||||||
| 5156 | // Dots | ||||||||||||||||||||||||||||||||||||||||||
| 5157 | g.selectAll(".cc-dot").data(data).join("circle") | ||||||||||||||||||||||||||||||||||||||||||
| 5158 | .attr("class","cc-dot") | ||||||||||||||||||||||||||||||||||||||||||
| 5159 | .attr("cx",function(d){return xScale(d.x) + xScale.bandwidth()/2;}) | ||||||||||||||||||||||||||||||||||||||||||
| 5160 | .attr("cy",function(d){return yRight(d.line);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5161 | .attr("r",4).attr("fill",lineColor).attr("stroke","#fff").attr("stroke-width",1.5) | ||||||||||||||||||||||||||||||||||||||||||
| 5162 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5163 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 5164 | .html("<b>"+esc(d.x)+"<\\/b><br\/>"+esc(barLabel)+": "+d.bar+"<br\/>"+esc(lineLabel)+": "+d.line); | ||||||||||||||||||||||||||||||||||||||||||
| 5165 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5166 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 5167 | tip.style("left",(event.offsetX+12)+"px").style("top",(event.offsetY-28)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 5168 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5169 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 5170 | |||||||||||||||||||||||||||||||||||||||||||
| 5171 | // Legend | ||||||||||||||||||||||||||||||||||||||||||
| 5172 | var lg = svg.append("g").attr("transform","translate("+(margin.left+8)+","+(margin.top+8)+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 5173 | lg.append("rect").attr("width",12).attr("height",12).attr("fill",barColor).attr("opacity",0.8); | ||||||||||||||||||||||||||||||||||||||||||
| 5174 | lg.append("text").attr("x",16).attr("y",10).attr("font-size","11px").attr("fill","#333").text(barLabel); | ||||||||||||||||||||||||||||||||||||||||||
| 5175 | var lg2 = svg.append("g").attr("transform","translate("+(margin.left+8)+","+(margin.top+26)+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 5176 | lg2.append("line").attr("x1",0).attr("y1",6).attr("x2",12).attr("y2",6) | ||||||||||||||||||||||||||||||||||||||||||
| 5177 | .attr("stroke",lineColor).attr("stroke-width",2.5); | ||||||||||||||||||||||||||||||||||||||||||
| 5178 | lg2.append("circle").attr("cx",6).attr("cy",6).attr("r",4).attr("fill",lineColor); | ||||||||||||||||||||||||||||||||||||||||||
| 5179 | lg2.append("text").attr("x",16).attr("y",10).attr("font-size","11px").attr("fill","#333").text(lineLabel); | ||||||||||||||||||||||||||||||||||||||||||
| 5180 | })(); | ||||||||||||||||||||||||||||||||||||||||||
| 5181 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 5182 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 5183 | |||||||||||||||||||||||||||||||||||||||||||
| 5184 | 58 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 5185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5186 | |||||||||||||||||||||||||||||||||||||||||||
| 5187 | =head2 render_treemap_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 5188 | |||||||||||||||||||||||||||||||||||||||||||
| 5189 | my $result = $chart->render_treemap_snippet(\@rows_or_tree, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 5190 | |||||||||||||||||||||||||||||||||||||||||||
| 5191 | Generates an embeddable treemap (no page shell, no CDN tag). | ||||||||||||||||||||||||||||||||||||||||||
| 5192 | Leaf area is proportional to the numeric measure. | ||||||||||||||||||||||||||||||||||||||||||
| 5193 | |||||||||||||||||||||||||||||||||||||||||||
| 5194 | Accepts two data formats: | ||||||||||||||||||||||||||||||||||||||||||
| 5195 | |||||||||||||||||||||||||||||||||||||||||||
| 5196 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 5197 | |||||||||||||||||||||||||||||||||||||||||||
| 5198 | =item * B<Flat path> (arrayref of hashrefs): each row must have a C<path> | ||||||||||||||||||||||||||||||||||||||||||
| 5199 | key (e.g. C<'Root/A/X'>) and a C<value> key. The module converts this | ||||||||||||||||||||||||||||||||||||||||||
| 5200 | to a D3 stratify hierarchy in JS. Override the path separator with | ||||||||||||||||||||||||||||||||||||||||||
| 5201 | C<opts-E<gt>{path_sep}>. | ||||||||||||||||||||||||||||||||||||||||||
| 5202 | |||||||||||||||||||||||||||||||||||||||||||
| 5203 | =item * B<Nested tree> (hashref): a pre-built D3-compatible hierarchy | ||||||||||||||||||||||||||||||||||||||||||
| 5204 | with C<name>/C<children>/C<value> structure. Passed directly to | ||||||||||||||||||||||||||||||||||||||||||
| 5205 | C<d3.hierarchy()>. | ||||||||||||||||||||||||||||||||||||||||||
| 5206 | |||||||||||||||||||||||||||||||||||||||||||
| 5207 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 5208 | |||||||||||||||||||||||||||||||||||||||||||
| 5209 | B<Colour by value.> A flat row, or a node of the tree, may also carry | ||||||||||||||||||||||||||||||||||||||||||
| 5210 | C<color_value>, a second number that colours its tile: the area says how much | ||||||||||||||||||||||||||||||||||||||||||
| 5211 | and the colour says how well -- sales sized by revenue and coloured by margin. | ||||||||||||||||||||||||||||||||||||||||||
| 5212 | As soon as one tile has one, every tile is shaded from the sequential | ||||||||||||||||||||||||||||||||||||||||||
| 5213 | C<color_scheme> over the smallest to the largest C<color_value>, in place of | ||||||||||||||||||||||||||||||||||||||||||
| 5214 | its top-level group's colour; a tile without one is grey. The tooltip gives | ||||||||||||||||||||||||||||||||||||||||||
| 5215 | the value under C<color_label>, a legend at the foot of the chart gives the | ||||||||||||||||||||||||||||||||||||||||||
| 5216 | range, and labels on light tiles are drawn dark. When every tile has the same | ||||||||||||||||||||||||||||||||||||||||||
| 5217 | value they all take the middle of the scheme. | ||||||||||||||||||||||||||||||||||||||||||
| 5218 | |||||||||||||||||||||||||||||||||||||||||||
| 5219 | ->render_treemap_snippet([ | ||||||||||||||||||||||||||||||||||||||||||
| 5220 | { path => 'All/North', value => 1200, color_value => 0.18 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5221 | { path => 'All/South', value => 800, color_value => 0.05 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5222 | ], { color_scheme => 'Blues', color_label => 'Margin' }); | ||||||||||||||||||||||||||||||||||||||||||
| 5223 | |||||||||||||||||||||||||||||||||||||||||||
| 5224 | Accepted C<\%opts> keys: | ||||||||||||||||||||||||||||||||||||||||||
| 5225 | |||||||||||||||||||||||||||||||||||||||||||
| 5226 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 5227 | |||||||||||||||||||||||||||||||||||||||||||
| 5228 | =item * C<path_sep> (string, default C<'/'>) - path separator (flat format only). | ||||||||||||||||||||||||||||||||||||||||||
| 5229 | |||||||||||||||||||||||||||||||||||||||||||
| 5230 | =item * C<scheme> (string, default C<'tableau10'>) - D3 categorical colour scheme. | ||||||||||||||||||||||||||||||||||||||||||
| 5231 | Supported: C<tableau10>, C<category10>, C<set2>, C<set3>, C<paired> - the names | ||||||||||||||||||||||||||||||||||||||||||
| 5232 | C<categorical_schemes()> returns, whose colours C<palette()> makes available to | ||||||||||||||||||||||||||||||||||||||||||
| 5233 | Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 5234 | |||||||||||||||||||||||||||||||||||||||||||
| 5235 | =item * C<label> (boolean, default C<1>) - show node labels inside cells. | ||||||||||||||||||||||||||||||||||||||||||
| 5236 | |||||||||||||||||||||||||||||||||||||||||||
| 5237 | =item * C<color_scheme> (string, default C<'YlOrRd'>) - the sequential scheme | ||||||||||||||||||||||||||||||||||||||||||
| 5238 | tiles are coloured from when they carry C<color_value>: C<YlOrRd>, C<Blues>, | ||||||||||||||||||||||||||||||||||||||||||
| 5239 | C<Greens>, C<Purples>, C<RdPu> or C<YlGnBu>, as for C<render_heatmap_snippet>. | ||||||||||||||||||||||||||||||||||||||||||
| 5240 | Not read when no tile has a C<color_value>. | ||||||||||||||||||||||||||||||||||||||||||
| 5241 | |||||||||||||||||||||||||||||||||||||||||||
| 5242 | =item * C<color_label> (string, default C<'Colour'>) - what C<color_value> | ||||||||||||||||||||||||||||||||||||||||||
| 5243 | measures, for the tooltip and the legend. | ||||||||||||||||||||||||||||||||||||||||||
| 5244 | |||||||||||||||||||||||||||||||||||||||||||
| 5245 | =item * C<id> (string, optional) - override the default C<svg_id> value. | ||||||||||||||||||||||||||||||||||||||||||
| 5246 | |||||||||||||||||||||||||||||||||||||||||||
| 5247 | =item * C<responsive> (boolean, optional) - SVG responsive sizing override. | ||||||||||||||||||||||||||||||||||||||||||
| 5248 | |||||||||||||||||||||||||||||||||||||||||||
| 5249 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 5250 | |||||||||||||||||||||||||||||||||||||||||||
| 5251 | Returns C<{ svg_id =E<gt> 'treemap', html =E<gt> Str }>. | ||||||||||||||||||||||||||||||||||||||||||
| 5252 | |||||||||||||||||||||||||||||||||||||||||||
| 5253 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 5254 | |||||||||||||||||||||||||||||||||||||||||||
| 5255 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 5256 | |||||||||||||||||||||||||||||||||||||||||||
| 5257 | =item * Throws C<Data must be an array of hashes or a hash tree> | ||||||||||||||||||||||||||||||||||||||||||
| 5258 | when C<$data> is neither an ARRAY nor a HASH reference. | ||||||||||||||||||||||||||||||||||||||||||
| 5259 | |||||||||||||||||||||||||||||||||||||||||||
| 5260 | =item * Throws C<Each flat row must be a hash reference> when a flat row is not a hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 5261 | |||||||||||||||||||||||||||||||||||||||||||
| 5262 | =item * Throws C<Each flat row must have a 'path' key> when a flat row lacks C<path>. | ||||||||||||||||||||||||||||||||||||||||||
| 5263 | |||||||||||||||||||||||||||||||||||||||||||
| 5264 | =item * Throws C<Each flat row must have a numeric 'value' key> when a value is not numeric. | ||||||||||||||||||||||||||||||||||||||||||
| 5265 | |||||||||||||||||||||||||||||||||||||||||||
| 5266 | =item * Throws C<Each flat row's 'color_value' must be numeric> when a flat | ||||||||||||||||||||||||||||||||||||||||||
| 5267 | row's C<color_value> is defined but not a finite number. | ||||||||||||||||||||||||||||||||||||||||||
| 5268 | |||||||||||||||||||||||||||||||||||||||||||
| 5269 | =item * Throws C<Each node's 'color_value' must be numeric> when a node of the | ||||||||||||||||||||||||||||||||||||||||||
| 5270 | tree has a C<color_value> that is defined but not a finite number. | ||||||||||||||||||||||||||||||||||||||||||
| 5271 | |||||||||||||||||||||||||||||||||||||||||||
| 5272 | =item * Throws C<Unknown color_scheme: E<lt>nameE<gt>> when a tile has a | ||||||||||||||||||||||||||||||||||||||||||
| 5273 | C<color_value> and C<color_scheme> is not one of the sequential schemes. | ||||||||||||||||||||||||||||||||||||||||||
| 5274 | |||||||||||||||||||||||||||||||||||||||||||
| 5275 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 5276 | |||||||||||||||||||||||||||||||||||||||||||
| 5277 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 5278 | |||||||||||||||||||||||||||||||||||||||||||
| 5279 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 5280 | |||||||||||||||||||||||||||||||||||||||||||
| 5281 | { | ||||||||||||||||||||||||||||||||||||||||||
| 5282 | data => { type => 'arrayref | hashref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5283 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 5284 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 5285 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 5286 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 5287 | path_sep => { type => 'string', optional => 1, default => '/' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5288 | scheme => { type => 'string', optional => 1, default => 'tableau10' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5289 | label => { type => 'boolean', optional => 1, default => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5290 | color_scheme => { type => 'string', optional => 1, default => 'YlOrRd' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5291 | color_label => { type => 'string', optional => 1, default => 'Colour' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5292 | id => { type => 'string', optional => 1, default => 'treemap' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5293 | responsive => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5294 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 5295 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 5296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5297 | |||||||||||||||||||||||||||||||||||||||||||
| 5298 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 5299 | |||||||||||||||||||||||||||||||||||||||||||
| 5300 | HashRef -- { svg_id => Str, html => Str } | ||||||||||||||||||||||||||||||||||||||||||
| 5301 | |||||||||||||||||||||||||||||||||||||||||||
| 5302 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 5303 | |||||||||||||||||||||||||||||||||||||||||||
| 5304 | sub render_treemap_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 5305 | 52 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_treemap_snippet: called CC=30 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 5306 | 52 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5307 | 52 | ▶ | my $first = @_ ? $_[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5308 | 52 | my $raw; | |||||||||||||||||||||||||||||||||||||||||
| 5309 | # A lone hashref argument is valid treemap data (hash tree), not named args | ||||||||||||||||||||||||||||||||||||||||||
| 5310 | 52 | ▶ | if (ref($first) eq 'HASH' && @_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $first eq "HASH" and @_ == 1
MC/DC50%1/2
ref $first eq "HASH" and @_ == 1
ref $first eq "HASH"
@_ == 1
Truth table66%2/3
A and B
A= ref $first eq "HASH"B= @_ == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 5311 | 3 | $raw = { data => $first }; | |||||||||||||||||||||||||||||||||||||||||
| 5312 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 5313 | 49 | ▶ | $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 5314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5315 | die 'Data must be an array of hashes or a hash tree' | ||||||||||||||||||||||||||||||||||||||||||
| 5316 | 52 | ▶ | unless ref($raw->{data}) eq 'ARRAY' || ref($raw->{data}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $raw->{"data"} eq "ARRAY" or ref $raw->{"data"} eq "HASH"
MC/DC100%2/2
ref $raw->{"data"} eq "ARRAY" or ref $raw->{"data"} eq "HASH"
ref $raw->{"data"} eq "ARRAY"
ref $raw->{"data"} eq "HASH"
Truth table100%3/3
A or B
A= ref $raw->{"data"} eq "ARRAY"B= ref $raw->{"data"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 5317 | 51 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 5318 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 5319 | schema => { data => { type => ['arrayref', 'hashref'], error_msg => 'Data must be an array of hashes or a hash tree' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 5320 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 5321 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5322 | 51 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 5323 | 51 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 5324 | |||||||||||||||||||||||||||||||||||||||||||
| 5325 | 51 | ▶ | my $path_sep = $opts->{path_sep} // '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"path_sep"} // "/"
MC/DC100%1/1
$opts->{"path_sep"} // "/"
$opts->{"path_sep"}
Truth table100%2/2
A
A= $opts->{"path_sep"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5326 | 51 | my $scheme = _scheme_name($opts->{scheme}); | |||||||||||||||||||||||||||||||||||||||||
| 5327 | 51 | ▶ | my $label = exists($opts->{label}) ? ($opts->{label} ? 1 : 0) : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 5328 | 51 | my $schemes_js = $self->_schemes_js('var'); | |||||||||||||||||||||||||||||||||||||||||
| 5329 | 51 | my $default_js = _default_scheme_js(); | |||||||||||||||||||||||||||||||||||||||||
| 5330 | |||||||||||||||||||||||||||||||||||||||||||
| 5331 | 51 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 5332 | |||||||||||||||||||||||||||||||||||||||||||
| 5333 | # A tile may carry a second measure, color_value, which shades it instead of | ||||||||||||||||||||||||||||||||||||||||||
| 5334 | # the colour of its top-level group: the area says how much, the colour how | ||||||||||||||||||||||||||||||||||||||||||
| 5335 | # well (sales sized by revenue, coloured by margin). It is on as soon as one | ||||||||||||||||||||||||||||||||||||||||||
| 5336 | # tile has one; a tile without one is grey. | ||||||||||||||||||||||||||||||||||||||||||
| 5337 | 51 | my $by_value = 0; | |||||||||||||||||||||||||||||||||||||||||
| 5338 | 101 | ▶ | my $check_cv = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 5339 | 101 | my ($node, $what) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 5340 | 101 | ▶ | return unless defined $node->{color_value}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5341 | die "Each ${what}'s 'color_value' must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 5342 | 16 | ▶ | unless looks_like_number($node->{color_value}) && $node->{color_value} !~ /\A\s*[-+]?(?:nan|inf)/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&looks_like_number($node->{"color_value"}) and not $node->{"color_value"} =~ /\A\s*[-+]?(?:nan|inf)/i
MC/DC100%2/2
&looks_like_number($node->{"color_value"}) and not $node->{"color_value"} =~ /\A\s*[-+]?(?:nan|inf)/i
&looks_like_number($node->{"color_value"})
not $node->{"color_value"} =~ /\A\s*[-+]?(?:nan|inf)/i
Truth table100%3/3
A and not(B)
A= &looks_like_number($node->{"color_value"})B= not $node->{"color_value"} =~ /\A\s*[-+]?(?:nan|inf)/i
| |||||||||||||||||||||||||||||||||||||||||||
| 5343 | 11 | $by_value = 1; | |||||||||||||||||||||||||||||||||||||||||
| 5344 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||
| 5345 | |||||||||||||||||||||||||||||||||||||||||||
| 5346 | 51 | my ($json_data, $build_js); | |||||||||||||||||||||||||||||||||||||||||
| 5347 | 51 | ▶ | if (ref($data) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5348 | 48 | for my $row (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 5349 | 91 | ▶ | die 'Each flat row must be a hash reference' unless ref($row) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5350 | 90 | ▶ | die "Each flat row must have a 'path' key" unless exists $row->{path}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5351 | die "Each flat row must have a numeric 'value' key" | ||||||||||||||||||||||||||||||||||||||||||
| 5352 | 89 | ▶ | unless looks_like_number($row->{value} // 'x'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$row->{"value"} // "x"
MC/DC0%0/1
$row->{"value"} // "x"
$row->{"value"}
Truth table50%1/2
A
A= $row->{"value"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5353 | 88 | $check_cv->($row, 'flat row'); | |||||||||||||||||||||||||||||||||||||||||
| 5354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5355 | my @flat = map { | ||||||||||||||||||||||||||||||||||||||||||
| 5356 | 41 | +{ path => "$_->{path}", value => $_->{value} + 0, | |||||||||||||||||||||||||||||||||||||||||
| 5357 | 84 | ▶ | (defined $_->{color_value} ? (color_value => $_->{color_value} + 0) : ()) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5358 | } @$data; | ||||||||||||||||||||||||||||||||||||||||||
| 5359 | 41 | $json_data = _script_json(\@flat); | |||||||||||||||||||||||||||||||||||||||||
| 5360 | 41 | my $sep_esc = $js_esc->($path_sep); | |||||||||||||||||||||||||||||||||||||||||
| 5361 | 41 | $build_js = <<"BUILDJS"; | |||||||||||||||||||||||||||||||||||||||||
| 5362 | var sep = "$sep_esc"; | ||||||||||||||||||||||||||||||||||||||||||
| 5363 | var nodeMap = {}; | ||||||||||||||||||||||||||||||||||||||||||
| 5364 | rawData.forEach(function(r){ | ||||||||||||||||||||||||||||||||||||||||||
| 5365 | var parts = r.path.split(sep); | ||||||||||||||||||||||||||||||||||||||||||
| 5366 | var id = r.path; | ||||||||||||||||||||||||||||||||||||||||||
| 5367 | if(!nodeMap[id]){ nodeMap[id] = {id:id, value:r.value, color_value:r.color_value}; } | ||||||||||||||||||||||||||||||||||||||||||
| 5368 | for(var i = parts.length-1; i >= 1; i--){ | ||||||||||||||||||||||||||||||||||||||||||
| 5369 | var pid = parts.slice(0,i).join(sep); | ||||||||||||||||||||||||||||||||||||||||||
| 5370 | if(!nodeMap[pid]){ nodeMap[pid] = {id:pid, value:0}; } | ||||||||||||||||||||||||||||||||||||||||||
| 5371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5372 | var root0 = parts[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 5373 | if(!nodeMap[root0]){ nodeMap[root0] = {id:root0, value:0}; } | ||||||||||||||||||||||||||||||||||||||||||
| 5374 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5375 | var nodes = Object.values(nodeMap).map(function(n){ | ||||||||||||||||||||||||||||||||||||||||||
| 5376 | var parts = n.id.split(sep); | ||||||||||||||||||||||||||||||||||||||||||
| 5377 | return {id:n.id, parentId: parts.length>1 ? parts.slice(0,parts.length-1).join(sep) : null, value:n.value, color_value:n.color_value}; | ||||||||||||||||||||||||||||||||||||||||||
| 5378 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5379 | var root = d3.stratify().id(function(d){return d.id;}) | ||||||||||||||||||||||||||||||||||||||||||
| 5380 | .parentId(function(d){return d.parentId;})(nodes) | ||||||||||||||||||||||||||||||||||||||||||
| 5381 | .sum(function(d){return d.value || 0;}) | ||||||||||||||||||||||||||||||||||||||||||
| 5382 | .sort(function(a,b){return b.value - a.value;}); | ||||||||||||||||||||||||||||||||||||||||||
| 5383 | BUILDJS | ||||||||||||||||||||||||||||||||||||||||||
| 5384 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 5385 | # The tree's leaves are what is drawn, but any node's colour value is | ||||||||||||||||||||||||||||||||||||||||||
| 5386 | # checked: a bad one is the caller's mistake wherever it is. | ||||||||||||||||||||||||||||||||||||||||||
| 5387 | 3 | my @todo = ($data); | |||||||||||||||||||||||||||||||||||||||||
| 5388 | 3 | ▶ | while (my $node = shift @todo) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5389 | 13 | ▶ | next unless ref($node) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5390 | 13 | $check_cv->($node, 'node'); | |||||||||||||||||||||||||||||||||||||||||
| 5391 | 12 | ▶ | push @todo, @{ $node->{children} } if ref($node->{children}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5393 | 2 | $json_data = _script_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 5394 | 2 | $build_js = <<'BUILDJS'; | |||||||||||||||||||||||||||||||||||||||||
| 5395 | var root = d3.hierarchy(rawData) | ||||||||||||||||||||||||||||||||||||||||||
| 5396 | .sum(function(d){return d.value || 0;}) | ||||||||||||||||||||||||||||||||||||||||||
| 5397 | .sort(function(a,b){return b.value - a.value;}); | ||||||||||||||||||||||||||||||||||||||||||
| 5398 | BUILDJS | ||||||||||||||||||||||||||||||||||||||||||
| 5399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5400 | |||||||||||||||||||||||||||||||||||||||||||
| 5401 | 43 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'treemap'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "treemap"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "treemap"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5402 | 43 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 5403 | 43 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 5404 | 43 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 5405 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 5406 | 43 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5407 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 5408 | |||||||||||||||||||||||||||||||||||||||||||
| 5409 | # Coloured by value: the tiles leave room at the foot for a legend that | ||||||||||||||||||||||||||||||||||||||||||
| 5410 | # says which colour is which number. | ||||||||||||||||||||||||||||||||||||||||||
| 5411 | 43 | my ($fill_js, $cv_tip_js, $legend_js, $layout_h) = (<<'FILL', '', '', $height); | |||||||||||||||||||||||||||||||||||||||||
| 5412 | function fillOf(d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5413 | var anc = d; while(anc.depth > 1){ anc = anc.parent; } | ||||||||||||||||||||||||||||||||||||||||||
| 5414 | return color(anc.id || anc.data.name || ""); | ||||||||||||||||||||||||||||||||||||||||||
| 5415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5416 | FILL | ||||||||||||||||||||||||||||||||||||||||||
| 5417 | 43 | ▶ | if ($by_value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5418 | 4 | ▶ | my $color_scheme = $opts->{color_scheme} // 'YlOrRd'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"color_scheme"} // "YlOrRd"
MC/DC100%1/1
$opts->{"color_scheme"} // "YlOrRd"
$opts->{"color_scheme"}
Truth table100%2/2
A
A= $opts->{"color_scheme"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5419 | die "Unknown color_scheme: $color_scheme" | ||||||||||||||||||||||||||||||||||||||||||
| 5420 | 4 | ▶ | unless exists $SEQUENTIAL_SCHEMES{$color_scheme}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5421 | 3 | my $interp = $SEQUENTIAL_SCHEMES{$color_scheme}; | |||||||||||||||||||||||||||||||||||||||||
| 5422 | 3 | ▶ | my $cv_label = $js_esc->($opts->{color_label} // 'Colour'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"color_label"} // "Colour"
MC/DC100%1/1
$opts->{"color_label"} // "Colour"
$opts->{"color_label"}
Truth table100%2/2
A
A= $opts->{"color_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5423 | 3 | my $grad_id = "${svg_id}_cgrad"; | |||||||||||||||||||||||||||||||||||||||||
| 5424 | 3 | my $legend_h = 34; | |||||||||||||||||||||||||||||||||||||||||
| 5425 | 3 | $layout_h = $height - $legend_h; | |||||||||||||||||||||||||||||||||||||||||
| 5426 | 3 | my $leg_y = $height - $legend_h + 10; | |||||||||||||||||||||||||||||||||||||||||
| 5427 | 3 | my $leg_x = $width - 190; | |||||||||||||||||||||||||||||||||||||||||
| 5428 | 3 | $fill_js = <<"FILL"; | |||||||||||||||||||||||||||||||||||||||||
| 5429 | var colorLabel = "$cv_label"; | ||||||||||||||||||||||||||||||||||||||||||
| 5430 | function cvOf(d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5431 | var v = d.data ? d.data.color_value : undefined; | ||||||||||||||||||||||||||||||||||||||||||
| 5432 | return (v === null || v === undefined || !isFinite(v)) ? null : +v; | ||||||||||||||||||||||||||||||||||||||||||
| 5433 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5434 | var cvals = root.leaves().map(cvOf).filter(function(v){ return v !== null; }); | ||||||||||||||||||||||||||||||||||||||||||
| 5435 | var cext = cvals.length ? d3.extent(cvals) : [0, 1]; | ||||||||||||||||||||||||||||||||||||||||||
| 5436 | // One value shades every tile alike, from the middle of the scheme. | ||||||||||||||||||||||||||||||||||||||||||
| 5437 | if (cext[0] === cext[1]) { cext = [cext[0] - 1, cext[1] + 1]; } | ||||||||||||||||||||||||||||||||||||||||||
| 5438 | var cscale = d3.scaleSequential($interp).domain(cext); | ||||||||||||||||||||||||||||||||||||||||||
| 5439 | function fillOf(d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5440 | var v = cvOf(d); | ||||||||||||||||||||||||||||||||||||||||||
| 5441 | return v === null ? "#ccc" : cscale(v); | ||||||||||||||||||||||||||||||||||||||||||
| 5442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5443 | FILL | ||||||||||||||||||||||||||||||||||||||||||
| 5444 | 3 | $cv_tip_js = '(cvOf(d) !== null ? "<br\/>"+esc(colorLabel)+": "+esc(cvOf(d)) : "")'; | |||||||||||||||||||||||||||||||||||||||||
| 5445 | 3 | $legend_js = <<"LEGEND"; | |||||||||||||||||||||||||||||||||||||||||
| 5446 | var defs = svg.append("defs"); | ||||||||||||||||||||||||||||||||||||||||||
| 5447 | var grad = defs.append("linearGradient").attr("id","$grad_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5448 | d3.range(0, 1.0001, 0.1).forEach(function(t){ | ||||||||||||||||||||||||||||||||||||||||||
| 5449 | grad.append("stop").attr("offset",(t*100)+"%") | ||||||||||||||||||||||||||||||||||||||||||
| 5450 | .attr("stop-color",cscale(cext[0] + t*(cext[1]-cext[0]))); | ||||||||||||||||||||||||||||||||||||||||||
| 5451 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5452 | var leg = svg.append("g").attr("class","tm-legend") | ||||||||||||||||||||||||||||||||||||||||||
| 5453 | .attr("transform","translate($leg_x,$leg_y)"); | ||||||||||||||||||||||||||||||||||||||||||
| 5454 | leg.append("text").attr("x",-6).attr("y",9).attr("text-anchor","end") | ||||||||||||||||||||||||||||||||||||||||||
| 5455 | .attr("font-size","11px").attr("fill","#555").text(colorLabel); | ||||||||||||||||||||||||||||||||||||||||||
| 5456 | leg.append("rect").attr("width",180).attr("height",10) | ||||||||||||||||||||||||||||||||||||||||||
| 5457 | .attr("fill","url(#$grad_id)"); | ||||||||||||||||||||||||||||||||||||||||||
| 5458 | var fmt = d3.format("~g"); | ||||||||||||||||||||||||||||||||||||||||||
| 5459 | leg.append("text").attr("x",0).attr("y",22).attr("font-size","10px") | ||||||||||||||||||||||||||||||||||||||||||
| 5460 | .attr("fill","#555").text(fmt(cext[0])); | ||||||||||||||||||||||||||||||||||||||||||
| 5461 | leg.append("text").attr("x",180).attr("y",22).attr("text-anchor","end") | ||||||||||||||||||||||||||||||||||||||||||
| 5462 | .attr("font-size","10px").attr("fill","#555").text(fmt(cext[1])); | ||||||||||||||||||||||||||||||||||||||||||
| 5463 | LEGEND | ||||||||||||||||||||||||||||||||||||||||||
| 5464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5465 | |||||||||||||||||||||||||||||||||||||||||||
| 5466 | # Labels are white. Coloured by value they are dark on the light tiles a | ||||||||||||||||||||||||||||||||||||||||||
| 5467 | # sequential scheme starts from, where white cannot be read. | ||||||||||||||||||||||||||||||||||||||||||
| 5468 | 42 | ▶ | my $label_fill = $by_value | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5469 | ? q{function(d){ return d3.lab(fillOf(d)).l > 65 ? "#333" : "#fff"; }} | ||||||||||||||||||||||||||||||||||||||||||
| 5470 | : q{"#fff"}; | ||||||||||||||||||||||||||||||||||||||||||
| 5471 | 42 | ▶ | my $label_block = $label ? <<"LBLJS" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5472 | cell.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 5473 | .attr("class","tm-label") | ||||||||||||||||||||||||||||||||||||||||||
| 5474 | .attr("x",4).attr("y",14) | ||||||||||||||||||||||||||||||||||||||||||
| 5475 | .attr("font-size","11px") | ||||||||||||||||||||||||||||||||||||||||||
| 5476 | .attr("fill",$label_fill) | ||||||||||||||||||||||||||||||||||||||||||
| 5477 | .text(function(d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5478 | var w = d.x1 - d.x0, h = d.y1 - d.y0; | ||||||||||||||||||||||||||||||||||||||||||
| 5479 | if(w < 24 || h < 14){ return ""; } | ||||||||||||||||||||||||||||||||||||||||||
| 5480 | var name = d.id ? d.id.split(sep||"/").pop() : (d.data.name||""); | ||||||||||||||||||||||||||||||||||||||||||
| 5481 | return name; | ||||||||||||||||||||||||||||||||||||||||||
| 5482 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5483 | .each(function(d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5484 | var w = d.x1 - d.x0 - 6; | ||||||||||||||||||||||||||||||||||||||||||
| 5485 | var self2 = d3.select(this); | ||||||||||||||||||||||||||||||||||||||||||
| 5486 | var text = self2.text(); | ||||||||||||||||||||||||||||||||||||||||||
| 5487 | while(self2.node().getComputedTextLength && self2.node().getComputedTextLength() > w && text.length > 1){ | ||||||||||||||||||||||||||||||||||||||||||
| 5488 | text = text.slice(0,-1); | ||||||||||||||||||||||||||||||||||||||||||
| 5489 | self2.text(text + "…"); | ||||||||||||||||||||||||||||||||||||||||||
| 5490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5491 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5492 | LBLJS | ||||||||||||||||||||||||||||||||||||||||||
| 5493 | |||||||||||||||||||||||||||||||||||||||||||
| 5494 | 42 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 5495 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 5496 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 5497 | position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 5498 | border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none; | ||||||||||||||||||||||||||||||||||||||||||
| 5499 | display:none;line-height:1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 5500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5501 | .tm-cell{stroke:#fff;stroke-width:1;} | ||||||||||||||||||||||||||||||||||||||||||
| 5502 | .tm-label{pointer-events:none;} | ||||||||||||||||||||||||||||||||||||||||||
| 5503 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 5504 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 5505 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 5506 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 5507 | (function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 5508 | var rawData = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 5509 | $ESC_JS | ||||||||||||||||||||||||||||||||||||||||||
| 5510 | $schemes_js | ||||||||||||||||||||||||||||||||||||||||||
| 5511 | var color = d3.scaleOrdinal(SCHEMES['$scheme'] || $default_js); | ||||||||||||||||||||||||||||||||||||||||||
| 5512 | 42 | var sep = "${\$js_esc->($path_sep)}"; | |||||||||||||||||||||||||||||||||||||||||
| 5513 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5514 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5515 | $build_js | ||||||||||||||||||||||||||||||||||||||||||
| 5516 | $fill_js | ||||||||||||||||||||||||||||||||||||||||||
| 5517 | var layout = d3.treemap().tile(d3.treemapSquarify) | ||||||||||||||||||||||||||||||||||||||||||
| 5518 | .size([$width, $layout_h]).paddingOuter(2).paddingInner(1)(root); | ||||||||||||||||||||||||||||||||||||||||||
| 5519 | var leaves = root.leaves(); | ||||||||||||||||||||||||||||||||||||||||||
| 5520 | var cell = svg.selectAll(".tm-cell-g") | ||||||||||||||||||||||||||||||||||||||||||
| 5521 | .data(leaves).join("g").attr("class","tm-cell-g") | ||||||||||||||||||||||||||||||||||||||||||
| 5522 | .attr("transform",function(d){return "translate("+d.x0+","+d.y0+")";}); | ||||||||||||||||||||||||||||||||||||||||||
| 5523 | cell.append("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 5524 | .attr("class","tm-cell") | ||||||||||||||||||||||||||||||||||||||||||
| 5525 | .attr("width",function(d){return Math.max(0,d.x1-d.x0);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5526 | .attr("height",function(d){return Math.max(0,d.y1-d.y0);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5527 | .attr("fill",fillOf) | ||||||||||||||||||||||||||||||||||||||||||
| 5528 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5529 | var name = d.id ? d.id.split(sep).pop() : (d.data.name||""); | ||||||||||||||||||||||||||||||||||||||||||
| 5530 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 5531 | 42 | ▶ | .html("<b>"+esc(name)+"<\\/b><br\/>"+d.value${\ ($cv_tip_js ? "+$cv_tip_js" : '')}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5532 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5533 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 5534 | tip.style("left",(event.offsetX+12)+"px").style("top",(event.offsetY-28)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 5535 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5536 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 5537 | $label_block | ||||||||||||||||||||||||||||||||||||||||||
| 5538 | $legend_js | ||||||||||||||||||||||||||||||||||||||||||
| 5539 | })(); | ||||||||||||||||||||||||||||||||||||||||||
| 5540 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 5541 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 5542 | |||||||||||||||||||||||||||||||||||||||||||
| 5543 | 42 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 5544 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5545 | |||||||||||||||||||||||||||||||||||||||||||
| 5546 | =head2 render_histogram_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 5547 | |||||||||||||||||||||||||||||||||||||||||||
| 5548 | my $result = $chart->render_histogram_snippet(\@values, \%opts); | ||||||||||||||||||||||||||||||||||||||||||
| 5549 | |||||||||||||||||||||||||||||||||||||||||||
| 5550 | Generates an embeddable frequency histogram (no page shell, no CDN tag). | ||||||||||||||||||||||||||||||||||||||||||
| 5551 | C<\@values> is an arrayref of numeric scalars (already extracted and | ||||||||||||||||||||||||||||||||||||||||||
| 5552 | coerced by the caller). | ||||||||||||||||||||||||||||||||||||||||||
| 5553 | |||||||||||||||||||||||||||||||||||||||||||
| 5554 | Accepted C<\%opts> keys: | ||||||||||||||||||||||||||||||||||||||||||
| 5555 | |||||||||||||||||||||||||||||||||||||||||||
| 5556 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 5557 | |||||||||||||||||||||||||||||||||||||||||||
| 5558 | =item * C<bins> (integer or C<undef>, default C<20>) - number of bins. | ||||||||||||||||||||||||||||||||||||||||||
| 5559 | Pass C<undef> for automatic bin count using Sturges' rule: | ||||||||||||||||||||||||||||||||||||||||||
| 5560 | C<ceil(log2(n) + 1)>. Ignored when C<binned> is set. | ||||||||||||||||||||||||||||||||||||||||||
| 5561 | |||||||||||||||||||||||||||||||||||||||||||
| 5562 | =item * C<binned> (boolean, default 0) - treat C<$data> as pre-computed bins | ||||||||||||||||||||||||||||||||||||||||||
| 5563 | rather than as raw values. Each element is a hashref with a C<count> and | ||||||||||||||||||||||||||||||||||||||||||
| 5564 | either a C<label> or a numeric C<lo>/C<hi> pair from which the label C<[lo,hi)> | ||||||||||||||||||||||||||||||||||||||||||
| 5565 | is derived: | ||||||||||||||||||||||||||||||||||||||||||
| 5566 | |||||||||||||||||||||||||||||||||||||||||||
| 5567 | { label => '[0,10)', count => 3 } | ||||||||||||||||||||||||||||||||||||||||||
| 5568 | { lo => 10, hi => 20, count => 5 } | ||||||||||||||||||||||||||||||||||||||||||
| 5569 | |||||||||||||||||||||||||||||||||||||||||||
| 5570 | The caller owns the binning, so C<d3.bin()> is never called: one bar is drawn | ||||||||||||||||||||||||||||||||||||||||||
| 5571 | per entry, in the order supplied, and the supplied counts are authoritative. | ||||||||||||||||||||||||||||||||||||||||||
| 5572 | |||||||||||||||||||||||||||||||||||||||||||
| 5573 | The bin labels are drawn rotated. The plot gives up as much room as they need, | ||||||||||||||||||||||||||||||||||||||||||
| 5574 | up to half the chart's height and a quarter of its width, so that they and | ||||||||||||||||||||||||||||||||||||||||||
| 5575 | C<x_label> do not overlap and stay inside the chart. | ||||||||||||||||||||||||||||||||||||||||||
| 5576 | |||||||||||||||||||||||||||||||||||||||||||
| 5577 | =item * C<scheme> (string, optional) - D3 categorical colour scheme (for example | ||||||||||||||||||||||||||||||||||||||||||
| 5578 | C<'tableau10'> or C<'category10'>) whose first colour is used as the bar fill | ||||||||||||||||||||||||||||||||||||||||||
| 5579 | when C<color> is not given. | ||||||||||||||||||||||||||||||||||||||||||
| 5580 | That colour is C<colour_at($scheme, 0)>, so a caller shading the same data by | ||||||||||||||||||||||||||||||||||||||||||
| 5581 | hand starts from the same colour the bars do. | ||||||||||||||||||||||||||||||||||||||||||
| 5582 | |||||||||||||||||||||||||||||||||||||||||||
| 5583 | =item * C<x_label> (string, optional) - label shown below the X axis. | ||||||||||||||||||||||||||||||||||||||||||
| 5584 | |||||||||||||||||||||||||||||||||||||||||||
| 5585 | =item * C<color> (string, default C<'steelblue'>) - bar fill colour. | ||||||||||||||||||||||||||||||||||||||||||
| 5586 | A value holding anything a CSS colour cannot, such as a quote or C<E<lt>>, is | ||||||||||||||||||||||||||||||||||||||||||
| 5587 | ignored and the default used. | ||||||||||||||||||||||||||||||||||||||||||
| 5588 | |||||||||||||||||||||||||||||||||||||||||||
| 5589 | =item * C<id> (string, optional) - override the default C<svg_id> value. | ||||||||||||||||||||||||||||||||||||||||||
| 5590 | |||||||||||||||||||||||||||||||||||||||||||
| 5591 | =item * C<responsive> (boolean, optional) - SVG responsive sizing override. | ||||||||||||||||||||||||||||||||||||||||||
| 5592 | |||||||||||||||||||||||||||||||||||||||||||
| 5593 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 5594 | |||||||||||||||||||||||||||||||||||||||||||
| 5595 | Returns C<{ svg_id =E<gt> 'histogram', html =E<gt> Str }>. | ||||||||||||||||||||||||||||||||||||||||||
| 5596 | |||||||||||||||||||||||||||||||||||||||||||
| 5597 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 5598 | |||||||||||||||||||||||||||||||||||||||||||
| 5599 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 5600 | |||||||||||||||||||||||||||||||||||||||||||
| 5601 | =item * Throws C<Data must be an array of numeric values> when C<$data> is | ||||||||||||||||||||||||||||||||||||||||||
| 5602 | not an ARRAY reference. | ||||||||||||||||||||||||||||||||||||||||||
| 5603 | |||||||||||||||||||||||||||||||||||||||||||
| 5604 | =item * Throws C<Each value must be numeric> when any element is non-numeric. | ||||||||||||||||||||||||||||||||||||||||||
| 5605 | |||||||||||||||||||||||||||||||||||||||||||
| 5606 | =item * Throws C<bins must be a positive integer> when C<bins> is defined | ||||||||||||||||||||||||||||||||||||||||||
| 5607 | but not a positive integer. | ||||||||||||||||||||||||||||||||||||||||||
| 5608 | |||||||||||||||||||||||||||||||||||||||||||
| 5609 | =item * Throws C<Data must contain at least one bin> when C<binned> is set and | ||||||||||||||||||||||||||||||||||||||||||
| 5610 | C<$data> holds no bins. | ||||||||||||||||||||||||||||||||||||||||||
| 5611 | |||||||||||||||||||||||||||||||||||||||||||
| 5612 | =item * Throws C<Each pre-binned entry must be a hash reference> when a | ||||||||||||||||||||||||||||||||||||||||||
| 5613 | pre-binned element is not a hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 5614 | |||||||||||||||||||||||||||||||||||||||||||
| 5615 | =item * Throws C<Bin count must be numeric> when a pre-binned C<count> is | ||||||||||||||||||||||||||||||||||||||||||
| 5616 | missing or not numeric. | ||||||||||||||||||||||||||||||||||||||||||
| 5617 | |||||||||||||||||||||||||||||||||||||||||||
| 5618 | =item * Throws C<Bin count must not be negative> when a pre-binned C<count> is | ||||||||||||||||||||||||||||||||||||||||||
| 5619 | negative. | ||||||||||||||||||||||||||||||||||||||||||
| 5620 | |||||||||||||||||||||||||||||||||||||||||||
| 5621 | =item * Throws C<Each pre-binned entry must have a label or numeric lo and hi> | ||||||||||||||||||||||||||||||||||||||||||
| 5622 | when a pre-binned entry has neither a C<label> nor a numeric C<lo>/C<hi> pair. | ||||||||||||||||||||||||||||||||||||||||||
| 5623 | |||||||||||||||||||||||||||||||||||||||||||
| 5624 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 5625 | |||||||||||||||||||||||||||||||||||||||||||
| 5626 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 5627 | |||||||||||||||||||||||||||||||||||||||||||
| 5628 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 5629 | |||||||||||||||||||||||||||||||||||||||||||
| 5630 | { | ||||||||||||||||||||||||||||||||||||||||||
| 5631 | data => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5632 | opts => { | ||||||||||||||||||||||||||||||||||||||||||
| 5633 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 5634 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 5635 | keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 5636 | bins => { type => 'integer', optional => 1, default => 20 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5637 | binned => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5638 | x_label => { type => 'string', optional => 1, default => '' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5639 | color => { type => 'string', optional => 1, default => 'steelblue' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5640 | scheme => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5641 | id => { type => 'string', optional => 1, default => 'histogram' }, | ||||||||||||||||||||||||||||||||||||||||||
| 5642 | responsive => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 5643 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 5644 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 5645 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5646 | |||||||||||||||||||||||||||||||||||||||||||
| 5647 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 5648 | |||||||||||||||||||||||||||||||||||||||||||
| 5649 | HashRef -- { svg_id => Str, html => Str } | ||||||||||||||||||||||||||||||||||||||||||
| 5650 | |||||||||||||||||||||||||||||||||||||||||||
| 5651 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 5652 | |||||||||||||||||||||||||||||||||||||||||||
| 5653 | # _render_histogram_binned($self, \@bins, \%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 5654 | # Internal helper for the binned => 1 path of render_histogram_snippet. | ||||||||||||||||||||||||||||||||||||||||||
| 5655 | # $bins: [ {label=>'[0,10)',count=>N}, ... ] or [ {lo=>0,hi=>10,count=>N}, ... ] | ||||||||||||||||||||||||||||||||||||||||||
| 5656 | sub _render_histogram_binned | ||||||||||||||||||||||||||||||||||||||||||
| 5657 | 26 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_histogram_binned: called CC=24 SCAR=31.8
| |||||||||||||||||||||||||||||||||||||||||||
| 5658 | 26 | my ($self, $bins, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 5659 | |||||||||||||||||||||||||||||||||||||||||||
| 5660 | 26 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 5661 | |||||||||||||||||||||||||||||||||||||||||||
| 5662 | 26 | ▶ | die 'Data must contain at least one bin' unless @$bins; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5663 | |||||||||||||||||||||||||||||||||||||||||||
| 5664 | 25 | my @norm; | |||||||||||||||||||||||||||||||||||||||||
| 5665 | 25 | for my $b (@$bins) { | |||||||||||||||||||||||||||||||||||||||||
| 5666 | 37 | ▶ | die 'Each pre-binned entry must be a hash reference' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5667 | unless ref($b) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 5668 | die 'Bin count must be numeric' | ||||||||||||||||||||||||||||||||||||||||||
| 5669 | 36 | ▶ | unless defined($b->{count}) && looks_like_number($b->{count}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $b->{"count"} and &looks_like_number($b->{"count"})
MC/DC100%2/2
defined $b->{"count"} and &looks_like_number($b->{"count"})
defined $b->{"count"}
&looks_like_number($b->{"count"})
Truth table100%3/3
A and B
A= defined $b->{"count"}B= &looks_like_number($b->{"count"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5670 | 34 | ▶ | die 'Bin count must not be negative' unless $b->{count} + 0 >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5671 | |||||||||||||||||||||||||||||||||||||||||||
| 5672 | 33 | ▶ | my $lo = (defined($b->{lo}) && looks_like_number($b->{lo})) ? $b->{lo} + 0 : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $b->{"lo"} and &looks_like_number($b->{"lo"})
MC/DC50%1/2
defined $b->{"lo"} and &looks_like_number($b->{"lo"})
defined $b->{"lo"}
&looks_like_number($b->{"lo"})
Truth table66%2/3
A and B
A= defined $b->{"lo"}B= &looks_like_number($b->{"lo"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5673 | 33 | ▶ | my $hi = (defined($b->{hi}) && looks_like_number($b->{hi})) ? $b->{hi} + 0 : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $b->{"hi"} and &looks_like_number($b->{"hi"})
MC/DC50%1/2
defined $b->{"hi"} and &looks_like_number($b->{"hi"})
defined $b->{"hi"}
&looks_like_number($b->{"hi"})
Truth table66%2/3
A and B
A= defined $b->{"hi"}B= &looks_like_number($b->{"hi"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5674 | |||||||||||||||||||||||||||||||||||||||||||
| 5675 | 33 | my $label = $b->{label}; | |||||||||||||||||||||||||||||||||||||||||
| 5676 | 33 | ▶ | if(!defined($label) || !length($label)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $label and length $label
MC/DC50%1/2
defined $label and length $label
defined $label
length $label
Truth table66%2/3
A and B
A= defined $labelB= length $label
| |||||||||||||||||||||||||||||||||||||||||||
| 5677 | 4 | ▶ | die 'Each pre-binned entry must have a label or numeric lo and hi' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $lo and defined $hi
MC/DC100%2/2
defined $lo and defined $hi
defined $lo
defined $hi
Truth table100%3/3
A and B
A= defined $loB= defined $hi
| |||||||||||||||||||||||||||||||||||||||||||
| 5678 | unless defined($lo) && defined($hi); | ||||||||||||||||||||||||||||||||||||||||||
| 5679 | 2 | $label = sprintf('[%g,%g)', $lo, $hi); | |||||||||||||||||||||||||||||||||||||||||
| 5680 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5681 | |||||||||||||||||||||||||||||||||||||||||||
| 5682 | 31 | push @norm, { label => "$label", count => $b->{count} + 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 5683 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5684 | |||||||||||||||||||||||||||||||||||||||||||
| 5685 | 19 | my $json_data = _script_json(\@norm); | |||||||||||||||||||||||||||||||||||||||||
| 5686 | |||||||||||||||||||||||||||||||||||||||||||
| 5687 | 19 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5688 | 19 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'histogram'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "histogram"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "histogram"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5689 | 19 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 5690 | 19 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 5691 | 19 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 5692 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 5693 | 19 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5694 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 5695 | |||||||||||||||||||||||||||||||||||||||||||
| 5696 | 19 | ▶ | my $margin_bottom = $x_label ? 60 : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5697 | 19 | my $margin_left = 55; | |||||||||||||||||||||||||||||||||||||||||
| 5698 | 19 | my $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 5699 | 19 | my $margin_right = 20; | |||||||||||||||||||||||||||||||||||||||||
| 5700 | 19 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 5701 | 19 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 5702 | |||||||||||||||||||||||||||||||||||||||||||
| 5703 | 19 | my $x_label_esc = $js_esc->($x_label); | |||||||||||||||||||||||||||||||||||||||||
| 5704 | |||||||||||||||||||||||||||||||||||||||||||
| 5705 | # The rotated tick labels hang below the axis and out to its left by however | ||||||||||||||||||||||||||||||||||||||||||
| 5706 | # long they are, which is only known once they are drawn, so the script | ||||||||||||||||||||||||||||||||||||||||||
| 5707 | # measures them and takes the room out of the plot. This is what keeps the | ||||||||||||||||||||||||||||||||||||||||||
| 5708 | # axis label clear of them. | ||||||||||||||||||||||||||||||||||||||||||
| 5709 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5710 | # The label also carries its font as attributes, because an SVG saved from the | ||||||||||||||||||||||||||||||||||||||||||
| 5711 | # page leaves the stylesheet behind. | ||||||||||||||||||||||||||||||||||||||||||
| 5712 | 19 | ▶ | my $x_label_room = $x_label ? 24 : 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5713 | |||||||||||||||||||||||||||||||||||||||||||
| 5714 | # Bar fill: an explicit colour wins, otherwise the first colour of the | ||||||||||||||||||||||||||||||||||||||||||
| 5715 | # requested D3 categorical scheme, otherwise the default colour. | ||||||||||||||||||||||||||||||||||||||||||
| 5716 | 19 | my $fill_js = '"steelblue"'; | |||||||||||||||||||||||||||||||||||||||||
| 5717 | 19 | my $colour = _css_colour($opts->{color}); | |||||||||||||||||||||||||||||||||||||||||
| 5718 | 19 | ▶ | if(defined $colour) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 5719 | 2 | $fill_js = '"' . $js_esc->($colour) . '"'; | |||||||||||||||||||||||||||||||||||||||||
| 5720 | } elsif(defined $opts->{scheme}) { | ||||||||||||||||||||||||||||||||||||||||||
| 5721 | # The scheme's first colour, which is the one colour_at($scheme, 0) | ||||||||||||||||||||||||||||||||||||||||||
| 5722 | # reports to a Perl caller colouring the same data by hand. | ||||||||||||||||||||||||||||||||||||||||||
| 5723 | 8 | $fill_js = _scheme_js($opts->{scheme}) . '[0]'; | |||||||||||||||||||||||||||||||||||||||||
| 5724 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5725 | |||||||||||||||||||||||||||||||||||||||||||
| 5726 | 19 | ▶ | my $x_label_block = $x_label ? <<"XLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5727 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 5728 | .attr("class","hg-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 5729 | .attr("font-size","11px") | ||||||||||||||||||||||||||||||||||||||||||
| 5730 | .attr("font-family","sans-serif") | ||||||||||||||||||||||||||||||||||||||||||
| 5731 | .attr("x", innerW / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 5732 | .attr("y", innerH + (margin.bottom - 10)) | ||||||||||||||||||||||||||||||||||||||||||
| 5733 | .attr("text-anchor","middle") | ||||||||||||||||||||||||||||||||||||||||||
| 5734 | .text("$x_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 5735 | XLB | ||||||||||||||||||||||||||||||||||||||||||
| 5736 | |||||||||||||||||||||||||||||||||||||||||||
| 5737 | 19 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 5738 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 5739 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 5740 | position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 5741 | border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none; | ||||||||||||||||||||||||||||||||||||||||||
| 5742 | display:none;line-height:1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 5743 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5744 | .hg-axis-label{font-size:11px;fill:#555;} | ||||||||||||||||||||||||||||||||||||||||||
| 5745 | .hg-bar{cursor:default;} | ||||||||||||||||||||||||||||||||||||||||||
| 5746 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 5747 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 5748 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 5749 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 5750 | (function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 5751 | var bins = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 5752 | var margin = {top:$margin_top,right:$margin_right,bottom:$margin_bottom,left:$margin_left}; | ||||||||||||||||||||||||||||||||||||||||||
| 5753 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 5754 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 5755 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5756 | var g = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 5757 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5758 | |||||||||||||||||||||||||||||||||||||||||||
| 5759 | function esc(s) { | ||||||||||||||||||||||||||||||||||||||||||
| 5760 | return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||||||||||||||||||||||||||||||||||||||||||
| 5761 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5762 | |||||||||||||||||||||||||||||||||||||||||||
| 5763 | var xScale = d3.scaleBand() | ||||||||||||||||||||||||||||||||||||||||||
| 5764 | .domain(bins.map(function(d){return d.label;})) | ||||||||||||||||||||||||||||||||||||||||||
| 5765 | .range([0, innerW]) | ||||||||||||||||||||||||||||||||||||||||||
| 5766 | .padding(0.05); | ||||||||||||||||||||||||||||||||||||||||||
| 5767 | |||||||||||||||||||||||||||||||||||||||||||
| 5768 | var xAxis = g.append("g").attr("class","hg-x-axis"); | ||||||||||||||||||||||||||||||||||||||||||
| 5769 | function drawXAxis() { | ||||||||||||||||||||||||||||||||||||||||||
| 5770 | xAxis.attr("transform","translate(0,"+innerH+")") | ||||||||||||||||||||||||||||||||||||||||||
| 5771 | .call(d3.axisBottom(xScale)) | ||||||||||||||||||||||||||||||||||||||||||
| 5772 | .selectAll("text") | ||||||||||||||||||||||||||||||||||||||||||
| 5773 | .attr("transform","rotate(-45)") | ||||||||||||||||||||||||||||||||||||||||||
| 5774 | .style("text-anchor","end"); | ||||||||||||||||||||||||||||||||||||||||||
| 5775 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5776 | drawXAxis(); | ||||||||||||||||||||||||||||||||||||||||||
| 5777 | |||||||||||||||||||||||||||||||||||||||||||
| 5778 | var axisBox = xAxis.node().getBBox(); | ||||||||||||||||||||||||||||||||||||||||||
| 5779 | margin.bottom = Math.max(margin.bottom, Math.min(Math.ceil(axisBox.height) + $x_label_room, $height / 2)); | ||||||||||||||||||||||||||||||||||||||||||
| 5780 | margin.left = Math.max(margin.left, Math.min(Math.ceil(-axisBox.x) + 4, $width / 4)); | ||||||||||||||||||||||||||||||||||||||||||
| 5781 | innerW = $width - margin.left - margin.right; | ||||||||||||||||||||||||||||||||||||||||||
| 5782 | innerH = $height - margin.top - margin.bottom; | ||||||||||||||||||||||||||||||||||||||||||
| 5783 | g.attr("transform","translate("+margin.left+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 5784 | xScale.range([0, innerW]); | ||||||||||||||||||||||||||||||||||||||||||
| 5785 | drawXAxis(); | ||||||||||||||||||||||||||||||||||||||||||
| 5786 | |||||||||||||||||||||||||||||||||||||||||||
| 5787 | var yScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 5788 | .domain([0, d3.max(bins, function(d){return d.count;})]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 5789 | .range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 5790 | |||||||||||||||||||||||||||||||||||||||||||
| 5791 | g.append("g").attr("class","hg-y-axis").call(d3.axisLeft(yScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 5792 | |||||||||||||||||||||||||||||||||||||||||||
| 5793 | $x_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 5794 | g.selectAll(".hg-bar").data(bins).join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 5795 | .attr("class","hg-bar") | ||||||||||||||||||||||||||||||||||||||||||
| 5796 | .attr("x",function(d){return xScale(d.label);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5797 | .attr("y",function(d){return yScale(d.count);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5798 | .attr("width",xScale.bandwidth()) | ||||||||||||||||||||||||||||||||||||||||||
| 5799 | .attr("height",function(d){return innerH - yScale(d.count);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5800 | .attr("fill",$fill_js) | ||||||||||||||||||||||||||||||||||||||||||
| 5801 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5802 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 5803 | .html("<b>"+esc(d.label)+"<\\/b><br\/>Count: "+d.count); | ||||||||||||||||||||||||||||||||||||||||||
| 5804 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5805 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 5806 | tip.style("left",(event.offsetX+12)+"px").style("top",(event.offsetY-28)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 5807 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5808 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 5809 | })(); | ||||||||||||||||||||||||||||||||||||||||||
| 5810 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 5811 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 5812 | |||||||||||||||||||||||||||||||||||||||||||
| 5813 | 19 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 5814 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5815 | |||||||||||||||||||||||||||||||||||||||||||
| 5816 | sub render_histogram_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 5817 | 69 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_histogram_snippet: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 5818 | 69 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5819 | 69 | ▶ | my $raw = Params::Get::get_params('data', \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params("data", \@_) // {}
MC/DC0%0/1
Params::Get::get_params("data", \@_) // {}
Params::Get::get_params("data", \@_)
Truth table50%1/2
A
A= Params::Get::get_params("data", \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 5820 | 69 | ▶ | die 'Data must be an array of numeric values' unless ref($raw->{data}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5821 | 67 | Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 5822 | args => $raw, | ||||||||||||||||||||||||||||||||||||||||||
| 5823 | schema => { data => { type => 'arrayref', error_msg => 'Data must be an array of numeric values' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 5824 | unknown_parameter_handler => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||
| 5825 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 5826 | 67 | my $data = $raw->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 5827 | 67 | my $opts = {%$raw}; delete $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 5828 | |||||||||||||||||||||||||||||||||||||||||||
| 5829 | # Pre-binned path: the caller has already computed the bins. | ||||||||||||||||||||||||||||||||||||||||||
| 5830 | 67 | ▶ | if ($opts->{binned}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5831 | 26 | return $self->_render_histogram_binned($data, $opts); | |||||||||||||||||||||||||||||||||||||||||
| 5832 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5833 | |||||||||||||||||||||||||||||||||||||||||||
| 5834 | 41 | ▶ | my $bins_opt = exists($opts->{bins}) ? $opts->{bins} : 20; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5835 | 41 | ▶ | if (defined $bins_opt) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5836 | 40 | ▶ | die 'bins must be a positive integer' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
&looks_like_number($bins_opt) and $bins_opt == int $bins_opt and $bins_opt >= 1
&looks_like_number($bins_opt) and $bins_opt == int $bins_opt
MC/DC66%2/3
&looks_like_number($bins_opt) and $bins_opt == int $bins_opt and $bins_opt >= 1
&looks_like_number($bins_opt)
$bins_opt == int $bins_opt
$bins_opt >= 1
Truth table75%3/4
A and B and C
A= &looks_like_number($bins_opt)B= $bins_opt == int $bins_optC= $bins_opt >= 1
| |||||||||||||||||||||||||||||||||||||||||||
| 5837 | unless looks_like_number($bins_opt) && $bins_opt == int($bins_opt) && $bins_opt >= 1; | ||||||||||||||||||||||||||||||||||||||||||
| 5838 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5839 | |||||||||||||||||||||||||||||||||||||||||||
| 5840 | 38 | my @values; | |||||||||||||||||||||||||||||||||||||||||
| 5841 | 38 | for my $v (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 5842 | 307 | ▶ | die 'Each value must be numeric' unless looks_like_number($v // 'x'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$v // "x"
MC/DC0%0/1
$v // "x"
$v
Truth table50%1/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||
| 5843 | 306 | push @values, $v + 0; | |||||||||||||||||||||||||||||||||||||||||
| 5844 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5845 | |||||||||||||||||||||||||||||||||||||||||||
| 5846 | 37 | my $json_data = _script_json(\@values); | |||||||||||||||||||||||||||||||||||||||||
| 5847 | |||||||||||||||||||||||||||||||||||||||||||
| 5848 | 37 | my $js_esc = \&_js_string; | |||||||||||||||||||||||||||||||||||||||||
| 5849 | |||||||||||||||||||||||||||||||||||||||||||
| 5850 | 37 | ▶ | my $x_label = $opts->{x_label} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"x_label"} // ""
MC/DC100%1/1
$opts->{"x_label"} // ""
$opts->{"x_label"}
Truth table100%2/2
A
A= $opts->{"x_label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5851 | 37 | ▶ | my $color = _css_colour($opts->{color}) // 'steelblue'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_css_colour($opts->{"color"}) // "steelblue"
MC/DC100%1/1
_css_colour($opts->{"color"}) // "steelblue"
_css_colour($opts->{"color"})
Truth table100%2/2
A
A= _css_colour($opts->{"color"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5852 | |||||||||||||||||||||||||||||||||||||||||||
| 5853 | 37 | my $x_label_esc = $js_esc->($x_label); | |||||||||||||||||||||||||||||||||||||||||
| 5854 | 37 | my $color_esc = $js_esc->($color); | |||||||||||||||||||||||||||||||||||||||||
| 5855 | |||||||||||||||||||||||||||||||||||||||||||
| 5856 | 37 | ▶ | my $svg_id = _chart_id($opts->{id}) // 'histogram'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_chart_id($opts->{"id"}) // "histogram"
MC/DC100%1/1
_chart_id($opts->{"id"}) // "histogram"
_chart_id($opts->{"id"})
Truth table100%2/2
A
A= _chart_id($opts->{"id"})
| |||||||||||||||||||||||||||||||||||||||||||
| 5857 | 37 | my $tip_id = $svg_id . '_tip'; | |||||||||||||||||||||||||||||||||||||||||
| 5858 | 37 | my $width = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 5859 | 37 | my $height = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 5860 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 5861 | 37 | ▶ | ? qq{viewBox="0 0 $width $height" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table66%2/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 5862 | : qq{width="$width" height="$height"}; | ||||||||||||||||||||||||||||||||||||||||||
| 5863 | |||||||||||||||||||||||||||||||||||||||||||
| 5864 | 37 | my $margin_top = 20; | |||||||||||||||||||||||||||||||||||||||||
| 5865 | 37 | my $margin_right = 20; | |||||||||||||||||||||||||||||||||||||||||
| 5866 | 37 | ▶ | my $margin_bottom = $x_label ? 60 : 40; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5867 | 37 | my $margin_left = 55; | |||||||||||||||||||||||||||||||||||||||||
| 5868 | 37 | my $inner_w = $width - $margin_left - $margin_right; | |||||||||||||||||||||||||||||||||||||||||
| 5869 | 37 | my $inner_h = $height - $margin_top - $margin_bottom; | |||||||||||||||||||||||||||||||||||||||||
| 5870 | |||||||||||||||||||||||||||||||||||||||||||
| 5871 | 37 | ▶ | my $bins_js = defined($bins_opt) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5872 | ? "var numBins = $bins_opt;" | ||||||||||||||||||||||||||||||||||||||||||
| 5873 | : 'var numBins = Math.ceil(Math.log2(values.length) + 1);'; | ||||||||||||||||||||||||||||||||||||||||||
| 5874 | |||||||||||||||||||||||||||||||||||||||||||
| 5875 | 37 | ▶ | my $x_label_block = $x_label ? <<"XLB" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5876 | g.append("text") | ||||||||||||||||||||||||||||||||||||||||||
| 5877 | .attr("class","hg-axis-label") | ||||||||||||||||||||||||||||||||||||||||||
| 5878 | .attr("x", innerW / 2) | ||||||||||||||||||||||||||||||||||||||||||
| 5879 | .attr("y", innerH + (margin.bottom - 10)) | ||||||||||||||||||||||||||||||||||||||||||
| 5880 | .attr("text-anchor","middle") | ||||||||||||||||||||||||||||||||||||||||||
| 5881 | .text("$x_label_esc"); | ||||||||||||||||||||||||||||||||||||||||||
| 5882 | XLB | ||||||||||||||||||||||||||||||||||||||||||
| 5883 | |||||||||||||||||||||||||||||||||||||||||||
| 5884 | 37 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 5885 | <style> | ||||||||||||||||||||||||||||||||||||||||||
| 5886 | #$tip_id { | ||||||||||||||||||||||||||||||||||||||||||
| 5887 | position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc; | ||||||||||||||||||||||||||||||||||||||||||
| 5888 | border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none; | ||||||||||||||||||||||||||||||||||||||||||
| 5889 | display:none;line-height:1.6; | ||||||||||||||||||||||||||||||||||||||||||
| 5890 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5891 | .hg-axis-label{font-size:11px;fill:#555;} | ||||||||||||||||||||||||||||||||||||||||||
| 5892 | .hg-bar{cursor:default;} | ||||||||||||||||||||||||||||||||||||||||||
| 5893 | </style> | ||||||||||||||||||||||||||||||||||||||||||
| 5894 | <svg id="$svg_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 5895 | <div id="$tip_id"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 5896 | <script> | ||||||||||||||||||||||||||||||||||||||||||
| 5897 | (function(){ | ||||||||||||||||||||||||||||||||||||||||||
| 5898 | var values = $json_data; | ||||||||||||||||||||||||||||||||||||||||||
| 5899 | var margin = {top:$margin_top,right:$margin_right,bottom:$margin_bottom,left:$margin_left}; | ||||||||||||||||||||||||||||||||||||||||||
| 5900 | var innerW = $inner_w; | ||||||||||||||||||||||||||||||||||||||||||
| 5901 | var innerH = $inner_h; | ||||||||||||||||||||||||||||||||||||||||||
| 5902 | $bins_js | ||||||||||||||||||||||||||||||||||||||||||
| 5903 | var svg = d3.select("#$svg_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5904 | var g = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); | ||||||||||||||||||||||||||||||||||||||||||
| 5905 | var tip = d3.select("#$tip_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 5906 | |||||||||||||||||||||||||||||||||||||||||||
| 5907 | var xScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 5908 | .domain([d3.min(values), d3.max(values)]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 5909 | .range([0, innerW]); | ||||||||||||||||||||||||||||||||||||||||||
| 5910 | |||||||||||||||||||||||||||||||||||||||||||
| 5911 | var binner = d3.bin().domain(xScale.domain()).thresholds(xScale.ticks(numBins)); | ||||||||||||||||||||||||||||||||||||||||||
| 5912 | var bins = binner(values); | ||||||||||||||||||||||||||||||||||||||||||
| 5913 | |||||||||||||||||||||||||||||||||||||||||||
| 5914 | var yScale = d3.scaleLinear() | ||||||||||||||||||||||||||||||||||||||||||
| 5915 | .domain([0, d3.max(bins, function(d){return d.length;})]).nice() | ||||||||||||||||||||||||||||||||||||||||||
| 5916 | .range([innerH, 0]); | ||||||||||||||||||||||||||||||||||||||||||
| 5917 | |||||||||||||||||||||||||||||||||||||||||||
| 5918 | g.append("g").attr("class","hg-x-axis") | ||||||||||||||||||||||||||||||||||||||||||
| 5919 | .attr("transform","translate(0,"+innerH+")").call(d3.axisBottom(xScale)); | ||||||||||||||||||||||||||||||||||||||||||
| 5920 | g.append("g").attr("class","hg-y-axis").call(d3.axisLeft(yScale).ticks(6)); | ||||||||||||||||||||||||||||||||||||||||||
| 5921 | |||||||||||||||||||||||||||||||||||||||||||
| 5922 | $x_label_block | ||||||||||||||||||||||||||||||||||||||||||
| 5923 | g.selectAll(".hg-bar").data(bins).join("rect") | ||||||||||||||||||||||||||||||||||||||||||
| 5924 | .attr("class","hg-bar") | ||||||||||||||||||||||||||||||||||||||||||
| 5925 | .attr("x",function(d){return xScale(d.x0)+1;}) | ||||||||||||||||||||||||||||||||||||||||||
| 5926 | .attr("y",function(d){return yScale(d.length);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5927 | .attr("width",function(d){return Math.max(0, xScale(d.x1)-xScale(d.x0)-2);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5928 | .attr("height",function(d){return innerH - yScale(d.length);}) | ||||||||||||||||||||||||||||||||||||||||||
| 5929 | .attr("fill","$color_esc") | ||||||||||||||||||||||||||||||||||||||||||
| 5930 | .on("mouseover",function(event,d){ | ||||||||||||||||||||||||||||||||||||||||||
| 5931 | tip.style("display","block") | ||||||||||||||||||||||||||||||||||||||||||
| 5932 | .html("<b>"+d.x0.toFixed(2)+" – "+d.x1.toFixed(2)+"<\\/b><br\/>Count: "+d.length); | ||||||||||||||||||||||||||||||||||||||||||
| 5933 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5934 | .on("mousemove",function(event){ | ||||||||||||||||||||||||||||||||||||||||||
| 5935 | tip.style("left",(event.offsetX+12)+"px").style("top",(event.offsetY-28)+"px"); | ||||||||||||||||||||||||||||||||||||||||||
| 5936 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 5937 | .on("mouseout",function(){tip.style("display","none");}); | ||||||||||||||||||||||||||||||||||||||||||
| 5938 | })(); | ||||||||||||||||||||||||||||||||||||||||||
| 5939 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 5940 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 5941 | |||||||||||||||||||||||||||||||||||||||||||
| 5942 | 37 | return { svg_id => $svg_id, html => $html }; | |||||||||||||||||||||||||||||||||||||||||
| 5943 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5944 | |||||||||||||||||||||||||||||||||||||||||||
| 5945 | # The definition of one scheme, by name, or a die that names what was asked for. | ||||||||||||||||||||||||||||||||||||||||||
| 5946 | sub _categorical_scheme | ||||||||||||||||||||||||||||||||||||||||||
| 5947 | 1142 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_categorical_scheme: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 5948 | 1142 | my $scheme = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5949 | |||||||||||||||||||||||||||||||||||||||||||
| 5950 | die 'Unknown scheme: ' . (defined($scheme) ? $scheme : '(none)') | ||||||||||||||||||||||||||||||||||||||||||
| 5951 | 1142 | ▶ | unless defined($scheme) && exists($CATEGORICAL_SCHEMES{$scheme}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $scheme and exists $CATEGORICAL_SCHEMES{$scheme}
MC/DC100%2/2
defined $scheme and exists $CATEGORICAL_SCHEMES{$scheme}
defined $scheme
exists $CATEGORICAL_SCHEMES{$scheme}
Truth table100%3/3
A and B
A= defined $schemeB= exists $CATEGORICAL_SCHEMES{$scheme}
| |||||||||||||||||||||||||||||||||||||||||||
| 5952 | |||||||||||||||||||||||||||||||||||||||||||
| 5953 | 1137 | return $CATEGORICAL_SCHEMES{$scheme}; | |||||||||||||||||||||||||||||||||||||||||
| 5954 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5955 | |||||||||||||||||||||||||||||||||||||||||||
| 5956 | # A scheme's colours as JavaScript source: a literal array, in the scheme's | ||||||||||||||||||||||||||||||||||||||||||
| 5957 | # order, built from the one definition. The snippets inline this rather than a | ||||||||||||||||||||||||||||||||||||||||||
| 5958 | # d3.scheme* expression, so the browser draws the numbers palette() handed a | ||||||||||||||||||||||||||||||||||||||||||
| 5959 | # map, and an upgrade of d3 cannot change a chart's colours. | ||||||||||||||||||||||||||||||||||||||||||
| 5960 | sub _scheme_colours_js | ||||||||||||||||||||||||||||||||||||||||||
| 5961 | 1086 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scheme_colours_js: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5962 | 1086 | my $scheme = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5963 | |||||||||||||||||||||||||||||||||||||||||||
| 5964 | 1086 | return '[' . join(', ', map { "'$_'" } @{_categorical_scheme($scheme)->{colours}}) . ']'; | |||||||||||||||||||||||||||||||||||||||||
| 5965 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5966 | |||||||||||||||||||||||||||||||||||||||||||
| 5967 | # The colours of a scheme as a JS array, or the default scheme's for a name that | ||||||||||||||||||||||||||||||||||||||||||
| 5968 | # is not one. The snippets used to capitalise the name they were handed and | ||||||||||||||||||||||||||||||||||||||||||
| 5969 | # look up the expression that spelling produced, which turned a name they did | ||||||||||||||||||||||||||||||||||||||||||
| 5970 | # not know into an undefined function in the browser; drawing with the | ||||||||||||||||||||||||||||||||||||||||||
| 5971 | # documented default is what the snippets that look their scheme up in a table | ||||||||||||||||||||||||||||||||||||||||||
| 5972 | # have always done. | ||||||||||||||||||||||||||||||||||||||||||
| 5973 | sub _scheme_js | ||||||||||||||||||||||||||||||||||||||||||
| 5974 | 113 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scheme_js: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 5975 | 113 | my $scheme = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5976 | |||||||||||||||||||||||||||||||||||||||||||
| 5977 | # exists() first: naming a scheme that is not one must not add it to the | ||||||||||||||||||||||||||||||||||||||||||
| 5978 | # vocabulary, which is what dereferencing the missing entry would do. | ||||||||||||||||||||||||||||||||||||||||||
| 5979 | 113 | ▶ | return exists($CATEGORICAL_SCHEMES{$scheme}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5980 | ? _scheme_colours_js($scheme) | ||||||||||||||||||||||||||||||||||||||||||
| 5981 | : _default_scheme_js(); | ||||||||||||||||||||||||||||||||||||||||||
| 5982 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5983 | |||||||||||||||||||||||||||||||||||||||||||
| 5984 | # The name to write into a snippet's SCHEMES['...'] lookup: the one asked for if | ||||||||||||||||||||||||||||||||||||||||||
| 5985 | # it is a scheme, otherwise the default. The name is written into a script, and | ||||||||||||||||||||||||||||||||||||||||||
| 5986 | # it often comes from a request or a saved chart, so only the names in the | ||||||||||||||||||||||||||||||||||||||||||
| 5987 | # definition above ever reach the page. An unknown name was always going to draw | ||||||||||||||||||||||||||||||||||||||||||
| 5988 | # the default colours; this makes sure it can do nothing else. | ||||||||||||||||||||||||||||||||||||||||||
| 5989 | sub _scheme_name | ||||||||||||||||||||||||||||||||||||||||||
| 5990 | 156 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scheme_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5991 | 156 | my $scheme = shift; | |||||||||||||||||||||||||||||||||||||||||
| 5992 | |||||||||||||||||||||||||||||||||||||||||||
| 5993 | 156 | ▶ | return defined($scheme) && exists($CATEGORICAL_SCHEMES{$scheme}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $scheme and exists $CATEGORICAL_SCHEMES{$scheme}
MC/DC100%2/2
defined $scheme and exists $CATEGORICAL_SCHEMES{$scheme}
defined $scheme
exists $CATEGORICAL_SCHEMES{$scheme}
Truth table100%3/3
A and B
A= defined $schemeB= exists $CATEGORICAL_SCHEMES{$scheme}
| |||||||||||||||||||||||||||||||||||||||||||
| 5994 | ? $scheme | ||||||||||||||||||||||||||||||||||||||||||
| 5995 | : $DEFAULT_CATEGORICAL_SCHEME; | ||||||||||||||||||||||||||||||||||||||||||
| 5996 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5997 | |||||||||||||||||||||||||||||||||||||||||||
| 5998 | # A chart's id, or undef for one that is not usable, so that the caller's default | ||||||||||||||||||||||||||||||||||||||||||
| 5999 | # applies. An id is written into an attribute, a stylesheet selector and a script | ||||||||||||||||||||||||||||||||||||||||||
| 6000 | # all at once; one of these characters is safe in all three and can be selected | ||||||||||||||||||||||||||||||||||||||||||
| 6001 | # with "#id". | ||||||||||||||||||||||||||||||||||||||||||
| 6002 | sub _chart_id | ||||||||||||||||||||||||||||||||||||||||||
| 6003 | 613 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_chart_id: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6004 | 613 | my $id = shift; | |||||||||||||||||||||||||||||||||||||||||
| 6005 | |||||||||||||||||||||||||||||||||||||||||||
| 6006 | 613 | ▶ | return unless defined($id); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6007 | 131 | ▶ | return $id =~ m{\A[A-Za-z_][A-Za-z0-9_-]*\z} ? $id : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6008 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6009 | |||||||||||||||||||||||||||||||||||||||||||
| 6010 | # Nothing written into a <script> may end it. The HTML parser looks for "</script" | ||||||||||||||||||||||||||||||||||||||||||
| 6011 | # and "<!--" before any JavaScript is read, so quoting cannot help: "<" has to be | ||||||||||||||||||||||||||||||||||||||||||
| 6012 | # written as an escape, which JavaScript reads back as the same character. The two | ||||||||||||||||||||||||||||||||||||||||||
| 6013 | # line separators are newlines to older JavaScript. | ||||||||||||||||||||||||||||||||||||||||||
| 6014 | sub _script_safe | ||||||||||||||||||||||||||||||||||||||||||
| 6015 | 2243 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_script_safe: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6016 | 2243 | my $text = shift; | |||||||||||||||||||||||||||||||||||||||||
| 6017 | |||||||||||||||||||||||||||||||||||||||||||
| 6018 | 2243 | $text =~ s/</\\u003c/g; | |||||||||||||||||||||||||||||||||||||||||
| 6019 | 2243 | $text =~ s/\x{2028}/\\u2028/g; | |||||||||||||||||||||||||||||||||||||||||
| 6020 | 2243 | $text =~ s/\x{2029}/\\u2029/g; | |||||||||||||||||||||||||||||||||||||||||
| 6021 | 2243 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 6022 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6023 | |||||||||||||||||||||||||||||||||||||||||||
| 6024 | # Data as JSON that is safe to write into a <script>. | ||||||||||||||||||||||||||||||||||||||||||
| 6025 | sub _script_json | ||||||||||||||||||||||||||||||||||||||||||
| 6026 | 813 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_script_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6027 | 813 | return _script_safe($_JSON->encode($_[0])); | |||||||||||||||||||||||||||||||||||||||||
| 6028 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6029 | |||||||||||||||||||||||||||||||||||||||||||
| 6030 | # Text as the inside of a double-quoted JavaScript string in a <script>. | ||||||||||||||||||||||||||||||||||||||||||
| 6031 | sub _js_string | ||||||||||||||||||||||||||||||||||||||||||
| 6032 | 1430 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_js_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6033 | 1430 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 6034 | |||||||||||||||||||||||||||||||||||||||||||
| 6035 | 1430 | $s =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||||||||||||
| 6036 | 1430 | $s =~ s/"/\\"/g; | |||||||||||||||||||||||||||||||||||||||||
| 6037 | 1430 | $s =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 6038 | 1430 | $s =~ s/\r/\\r/g; | |||||||||||||||||||||||||||||||||||||||||
| 6039 | 1430 | return _script_safe($s); | |||||||||||||||||||||||||||||||||||||||||
| 6040 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6041 | |||||||||||||||||||||||||||||||||||||||||||
| 6042 | # Text as the inside of a JavaScript string of any kind, for a value written into a | ||||||||||||||||||||||||||||||||||||||||||
| 6043 | # single-quoted string or a template literal, where "$" and the other quotes count. | ||||||||||||||||||||||||||||||||||||||||||
| 6044 | sub _js_any_string | ||||||||||||||||||||||||||||||||||||||||||
| 6045 | 86 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_js_any_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6046 | 86 | my $s = _js_string(shift); | |||||||||||||||||||||||||||||||||||||||||
| 6047 | |||||||||||||||||||||||||||||||||||||||||||
| 6048 | 86 | $s =~ s/(['`\$])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 6049 | 86 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 6050 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6051 | |||||||||||||||||||||||||||||||||||||||||||
| 6052 | # A colour fit to write into a page, or undef for one that is not, so that the | ||||||||||||||||||||||||||||||||||||||||||
| 6053 | # caller's default applies. The characters are those of a CSS colour - a name, a | ||||||||||||||||||||||||||||||||||||||||||
| 6054 | # hex value, or rgb(), hsl() and the like - and none of them can end a JavaScript | ||||||||||||||||||||||||||||||||||||||||||
| 6055 | # string, an attribute or a script element. | ||||||||||||||||||||||||||||||||||||||||||
| 6056 | sub _css_colour | ||||||||||||||||||||||||||||||||||||||||||
| 6057 | 354 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_css_colour: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6058 | 354 | my $colour = shift; | |||||||||||||||||||||||||||||||||||||||||
| 6059 | |||||||||||||||||||||||||||||||||||||||||||
| 6060 | 354 | ▶ | return unless defined($colour); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6061 | 68 | ▶ | return $colour =~ m{\A[#0-9A-Za-z_ (),.%/-]+\z} ? $colour : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6062 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6063 | |||||||||||||||||||||||||||||||||||||||||||
| 6064 | # The chart's title as text for the page. The object keeps what it was given. | ||||||||||||||||||||||||||||||||||||||||||
| 6065 | sub _title_html | ||||||||||||||||||||||||||||||||||||||||||
| 6066 | 143 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_title_html: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6067 | 143 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 6068 | |||||||||||||||||||||||||||||||||||||||||||
| 6069 | 143 | my $title = $self->{title}; | |||||||||||||||||||||||||||||||||||||||||
| 6070 | 143 | $title =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 6071 | 143 | $title =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 6072 | 143 | $title =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 6073 | 143 | $title =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||
| 6074 | 143 | $title =~ s/'/'/g; | |||||||||||||||||||||||||||||||||||||||||
| 6075 | 143 | return $title; | |||||||||||||||||||||||||||||||||||||||||
| 6076 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6077 | |||||||||||||||||||||||||||||||||||||||||||
| 6078 | sub _default_scheme_js | ||||||||||||||||||||||||||||||||||||||||||
| 6079 | 164 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_default_scheme_js: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6080 | 164 | return _scheme_colours_js($DEFAULT_CATEGORICAL_SCHEME); | |||||||||||||||||||||||||||||||||||||||||
| 6081 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6082 | |||||||||||||||||||||||||||||||||||||||||||
| 6083 | # The table the snippets look a scheme up in, built from the definition above, so | ||||||||||||||||||||||||||||||||||||||||||
| 6084 | # that a scheme added there is understood by every snippet that offers a scheme | ||||||||||||||||||||||||||||||||||||||||||
| 6085 | # rather than by the ones somebody remembered to edit. Each value is the | ||||||||||||||||||||||||||||||||||||||||||
| 6086 | # scheme's colours as a literal array: the same numbers palette() returns. | ||||||||||||||||||||||||||||||||||||||||||
| 6087 | sub _schemes_js | ||||||||||||||||||||||||||||||||||||||||||
| 6088 | 156 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_schemes_js: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6089 | 156 | my ($self, $declaration) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 6090 | |||||||||||||||||||||||||||||||||||||||||||
| 6091 | return $declaration . ' SCHEMES = { ' | ||||||||||||||||||||||||||||||||||||||||||
| 6092 | 156 | . join(', ', map { "$_: " . _scheme_colours_js($_) } sort keys %CATEGORICAL_SCHEMES) | |||||||||||||||||||||||||||||||||||||||||
| 6093 | . ' };'; | ||||||||||||||||||||||||||||||||||||||||||
| 6094 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6095 | |||||||||||||||||||||||||||||||||||||||||||
| 6096 | sub _preamble | ||||||||||||||||||||||||||||||||||||||||||
| 6097 | 72 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_preamble: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6098 | 72 | my $html = <<'HTML'; | |||||||||||||||||||||||||||||||||||||||||
| 6099 | <!DOCTYPE html> | ||||||||||||||||||||||||||||||||||||||||||
| 6100 | <html lang="en"> | ||||||||||||||||||||||||||||||||||||||||||
| 6101 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 6102 | 72 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 6103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6104 | |||||||||||||||||||||||||||||||||||||||||||
| 6105 | sub _head | ||||||||||||||||||||||||||||||||||||||||||
| 6106 | 47 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_head: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6107 | 47 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 6108 | |||||||||||||||||||||||||||||||||||||||||||
| 6109 | 47 | my $html = <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||
| 6110 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 6111 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||||||||||||
| 6112 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||||||||||||||||||||||||||||||||||||||
| 6113 | 47 | <title>@{[ $self->_title_html() ]}</title> | |||||||||||||||||||||||||||||||||||||||||
| 6114 | <script src="https://d3js.org/d3.v7.min.js"></script> | ||||||||||||||||||||||||||||||||||||||||||
| 6115 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 6116 | HTML | ||||||||||||||||||||||||||||||||||||||||||
| 6117 | 47 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 6118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6119 | |||||||||||||||||||||||||||||||||||||||||||
| 6120 | sub _svg_attrs | ||||||||||||||||||||||||||||||||||||||||||
| 6121 | 71 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_svg_attrs: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 6122 | 71 | my ($self, $override) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 6123 | 71 | ▶ | my $responsive = defined($override) ? $override : $self->{responsive}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6124 | 71 | ▶ | return $responsive | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6125 | ? qq{viewBox="0 0 $self->{width} $self->{height}" width="100%"} | ||||||||||||||||||||||||||||||||||||||||||
| 6126 | : qq{width="$self->{width}" height="$self->{height}" style="border: 1px solid black;"}; | ||||||||||||||||||||||||||||||||||||||||||
| 6127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6128 | |||||||||||||||||||||||||||||||||||||||||||
| 6129 | # _render_snippet_shell($opts, $container_id, $script) | ||||||||||||||||||||||||||||||||||||||||||
| 6130 | # Used exclusively by new snippet methods (scatter, table). | ||||||||||||||||||||||||||||||||||||||||||
| 6131 | # Returns a minimal HTML fragment with a container div and the provided script block. | ||||||||||||||||||||||||||||||||||||||||||
| 6132 | sub _render_snippet_shell | ||||||||||||||||||||||||||||||||||||||||||
| 6133 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_snippet_shell: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 6134 | 0 | my ($self, $opts, $container_id, $script) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 6135 | 0 | my $w = $self->{width}; | |||||||||||||||||||||||||||||||||||||||||
| 6136 | 0 | my $h = $self->{height}; | |||||||||||||||||||||||||||||||||||||||||
| 6137 | my $svg_attrs = ($opts->{responsive} // $self->{responsive}) | ||||||||||||||||||||||||||||||||||||||||||
| 6138 | 0 | ▶ | ? qq{viewBox="0 0 $w $h" width="100%"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts->{"responsive"} // $self->{"responsive"}
MC/DC0%0/2
$opts->{"responsive"} // $self->{"responsive"}
$opts->{"responsive"}
$self->{"responsive"}
Truth table0%0/3
A // B
A= $opts->{"responsive"}B= $self->{"responsive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 6139 | : qq{width="$w" height="$h"}; | ||||||||||||||||||||||||||||||||||||||||||
| 6140 | 0 | return <<"SHELL"; | |||||||||||||||||||||||||||||||||||||||||
| 6141 | <div id="${container_id}_wrap" style="position:relative;"> | ||||||||||||||||||||||||||||||||||||||||||
| 6142 | <svg id="$container_id" $svg_attrs></svg> | ||||||||||||||||||||||||||||||||||||||||||
| 6143 | <div id="${container_id}_tip" style="position:absolute;background:rgba(255,255,255,0.95);border:1px solid #ccc;border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none;display:none;line-height:1.6;"></div> | ||||||||||||||||||||||||||||||||||||||||||
| 6144 | </div> | ||||||||||||||||||||||||||||||||||||||||||
| 6145 | $script | ||||||||||||||||||||||||||||||||||||||||||
| 6146 | SHELL | ||||||||||||||||||||||||||||||||||||||||||
| 6147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 6148 | |||||||||||||||||||||||||||||||||||||||||||
| 6149 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 6150 | |||||||||||||||||||||||||||||||||||||||||||
| 6151 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||||||||||||||||
| 6152 | |||||||||||||||||||||||||||||||||||||||||||
| 6153 | Please report any bugs or feature requests to C<bug-html-d3 at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||||
| 6154 | or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||
| 6155 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-D3>. | ||||||||||||||||||||||||||||||||||||||||||
| 6156 | I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 6157 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 6158 | |||||||||||||||||||||||||||||||||||||||||||
| 6159 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 6160 | |||||||||||||||||||||||||||||||||||||||||||
| 6161 | perldoc HTML::D3 | ||||||||||||||||||||||||||||||||||||||||||
| 6162 | |||||||||||||||||||||||||||||||||||||||||||
| 6163 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 6164 | |||||||||||||||||||||||||||||||||||||||||||
| 6165 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 6166 | |||||||||||||||||||||||||||||||||||||||||||
| 6167 | It would help to have the render routine to return the head and body components separately. | ||||||||||||||||||||||||||||||||||||||||||
| 6168 | |||||||||||||||||||||||||||||||||||||||||||
| 6169 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 6170 | |||||||||||||||||||||||||||||||||||||||||||
| 6171 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 6172 | |||||||||||||||||||||||||||||||||||||||||||
| 6173 | =item * L<Configure an Object at Runtime|Object::Configure> | ||||||||||||||||||||||||||||||||||||||||||
| 6174 | |||||||||||||||||||||||||||||||||||||||||||
| 6175 | =item * L<Test Dashboard|https://nigelhorne.github.io/HTML-D3/coverage/> | ||||||||||||||||||||||||||||||||||||||||||
| 6176 | |||||||||||||||||||||||||||||||||||||||||||
| 6177 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 6178 | |||||||||||||||||||||||||||||||||||||||||||
| 6179 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 6180 | |||||||||||||||||||||||||||||||||||||||||||
| 6181 | Nigel Horne <njh@nigelhorne.com> | ||||||||||||||||||||||||||||||||||||||||||
| 6182 | |||||||||||||||||||||||||||||||||||||||||||
| 6183 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 6184 | |||||||||||||||||||||||||||||||||||||||||||
| 6185 | =head1 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 6186 | |||||||||||||||||||||||||||||||||||||||||||
| 6187 | =head2 render_bar_chart | ||||||||||||||||||||||||||||||||||||||||||
| 6188 | |||||||||||||||||||||||||||||||||||||||||||
| 6189 | render_bar_chart : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6190 | |||||||||||||||||||||||||||||||||||||||||||
| 6191 | pre data = undef ⇒ die "Data is not optional" | ||||||||||||||||||||||||||||||||||||||||||
| 6192 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6193 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6194 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6195 | post ∀ d ∈ data . d[0] ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6196 | |||||||||||||||||||||||||||||||||||||||||||
| 6197 | =head2 render_line_chart | ||||||||||||||||||||||||||||||||||||||||||
| 6198 | |||||||||||||||||||||||||||||||||||||||||||
| 6199 | render_line_chart : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6200 | |||||||||||||||||||||||||||||||||||||||||||
| 6201 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6202 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6203 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6204 | post "d3.scalePoint" ⊆ result ∧ "d3.line()" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6205 | |||||||||||||||||||||||||||||||||||||||||||
| 6206 | =head2 render_animated_bar_chart | ||||||||||||||||||||||||||||||||||||||||||
| 6207 | |||||||||||||||||||||||||||||||||||||||||||
| 6208 | render_animated_bar_chart : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6209 | |||||||||||||||||||||||||||||||||||||||||||
| 6210 | pre data = undef ⇒ die "Data is not optional" | ||||||||||||||||||||||||||||||||||||||||||
| 6211 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6212 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6213 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6214 | post ".transition()" ⊆ result ∧ ".delay(" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6215 | |||||||||||||||||||||||||||||||||||||||||||
| 6216 | =head2 render_animated_line_chart | ||||||||||||||||||||||||||||||||||||||||||
| 6217 | |||||||||||||||||||||||||||||||||||||||||||
| 6218 | render_animated_line_chart : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6219 | |||||||||||||||||||||||||||||||||||||||||||
| 6220 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6221 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6222 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6223 | post "stroke-dashoffset" ⊆ result ∧ "d3.easeLinear" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6224 | |||||||||||||||||||||||||||||||||||||||||||
| 6225 | =head2 render_pie_chart | ||||||||||||||||||||||||||||||||||||||||||
| 6226 | |||||||||||||||||||||||||||||||||||||||||||
| 6227 | render_pie_chart : HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6228 | |||||||||||||||||||||||||||||||||||||||||||
| 6229 | pre data = undef ⇒ die "Data is not optional" | ||||||||||||||||||||||||||||||||||||||||||
| 6230 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6231 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6232 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6233 | post "d3.pie()" ⊆ result ∧ "d3.arc()" ⊆ result ∧ "'#1f77b4'" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6234 | -- category10's colours, inlined: no d3.scheme* is read | ||||||||||||||||||||||||||||||||||||||||||
| 6235 | post opts.separator = S ⇒ " S " ⊆ result (SVG legend: label S value) | ||||||||||||||||||||||||||||||||||||||||||
| 6236 | |||||||||||||||||||||||||||||||||||||||||||
| 6237 | =head2 render_animated_pie_chart | ||||||||||||||||||||||||||||||||||||||||||
| 6238 | |||||||||||||||||||||||||||||||||||||||||||
| 6239 | render_animated_pie_chart : HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6240 | |||||||||||||||||||||||||||||||||||||||||||
| 6241 | pre data = undef ⇒ die "Data is not optional" | ||||||||||||||||||||||||||||||||||||||||||
| 6242 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6243 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6244 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6245 | post "attrTween" ⊆ result ∧ "d3.interpolate" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6246 | post opts.separator = S ⇒ " S " ⊆ result (SVG legend: label S value) | ||||||||||||||||||||||||||||||||||||||||||
| 6247 | |||||||||||||||||||||||||||||||||||||||||||
| 6248 | =head2 render_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6249 | |||||||||||||||||||||||||||||||||||||||||||
| 6250 | render_line_chart_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6251 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6252 | |||||||||||||||||||||||||||||||||||||||||||
| 6253 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6254 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6255 | post result.svg_id = opts.id // "chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6256 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6257 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6258 | |||||||||||||||||||||||||||||||||||||||||||
| 6259 | =head2 render_zoomable_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6260 | |||||||||||||||||||||||||||||||||||||||||||
| 6261 | render_zoomable_line_chart_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6262 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6263 | |||||||||||||||||||||||||||||||||||||||||||
| 6264 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6265 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6266 | post result.svg_id = opts.id // "chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6267 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6268 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6269 | post "d3.brushX()" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6270 | post opts.animated = 1 ⇒ "stroke-dashoffset" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6271 | ∧ "initialDrawDone" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6272 | |||||||||||||||||||||||||||||||||||||||||||
| 6273 | =head2 render_pie_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6274 | |||||||||||||||||||||||||||||||||||||||||||
| 6275 | render_pie_chart_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6276 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6277 | |||||||||||||||||||||||||||||||||||||||||||
| 6278 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6279 | pre ∀ d ∈ data . d[1] < 0 ⇒ d[1] := |d[1]| -- negative → absolute | ||||||||||||||||||||||||||||||||||||||||||
| 6280 | pre ∀ d ∈ data . d[1] = 0 ⇒ d ∉ result -- zero → omitted | ||||||||||||||||||||||||||||||||||||||||||
| 6281 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6282 | post result.svg_id = "pie_chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6283 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6284 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6285 | post "d3.pie()" ⊆ result.html ∧ "schemeTableau10" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6286 | post opts.color_scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6287 | ⇒ the D3 expression of S ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6288 | post opts.color_scheme ∉ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6289 | ⇒ "schemeTableau10" ⊆ result.html -- the default scheme | ||||||||||||||||||||||||||||||||||||||||||
| 6290 | post opts.animated = 1 ⇒ "attrTween" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6291 | ∧ "initialDrawDone" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6292 | post opts.donut = 1 ⇒ "innerRadius" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6293 | post opts.max_slices = N ∧ N ≥ 2 ∧ |data| > N | ||||||||||||||||||||||||||||||||||||||||||
| 6294 | ⇒ |result_slices| = N ∧ "Other" ∈ result_labels | ||||||||||||||||||||||||||||||||||||||||||
| 6295 | post opts.separator = S ⇒ " S " ⊆ result.html (HTML legend: label S value (pct%)) | ||||||||||||||||||||||||||||||||||||||||||
| 6296 | |||||||||||||||||||||||||||||||||||||||||||
| 6297 | =head2 render_heatmap_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6298 | |||||||||||||||||||||||||||||||||||||||||||
| 6299 | render_heatmap_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6300 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6301 | |||||||||||||||||||||||||||||||||||||||||||
| 6302 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6303 | pre ∃ pt ∈ data . ref(pt) ≠ 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 6304 | ⇒ die "Each data point must be an array reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6305 | pre ∃ pt ∈ data . |pt| < 3 ⇒ die "Each data point must have at least 3 elements" | ||||||||||||||||||||||||||||||||||||||||||
| 6306 | pre ∃ pt ∈ data . defined(pt[2]) ∧ ¬numeric(pt[2]) | ||||||||||||||||||||||||||||||||||||||||||
| 6307 | ⇒ die "Value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6308 | pre opts.color_scheme = S ∧ S ∉ {YlOrRd,Blues,Greens,Purples,RdPu,YlGnBu} | ||||||||||||||||||||||||||||||||||||||||||
| 6309 | ⇒ die "Unknown color_scheme: S" | ||||||||||||||||||||||||||||||||||||||||||
| 6310 | pre opts.cell_padding = N ∧ (N < 0 ∨ N > 8) | ||||||||||||||||||||||||||||||||||||||||||
| 6311 | ⇒ die "cell_padding must be between 0 and 8" | ||||||||||||||||||||||||||||||||||||||||||
| 6312 | pre ∀ pt ∈ data . pt[2] = undef ⇒ pt ∉ result -- undef rows skipped | ||||||||||||||||||||||||||||||||||||||||||
| 6313 | pre ∃ pt₁,pt₂ ∈ data . pt₁[0]=pt₂[0] ∧ pt₁[1]=pt₂[1] | ||||||||||||||||||||||||||||||||||||||||||
| 6314 | ⇒ last-write wins | ||||||||||||||||||||||||||||||||||||||||||
| 6315 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6316 | post result.svg_id = "heatmap" | ||||||||||||||||||||||||||||||||||||||||||
| 6317 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6318 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6319 | post "scaleSequential" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6320 | post opts.animated = 1 ⇒ "prefers-reduced-motion" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6321 | post opts.legend = 1 ⇒ "linearGradient" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6322 | |||||||||||||||||||||||||||||||||||||||||||
| 6323 | =head2 render_bar_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6324 | |||||||||||||||||||||||||||||||||||||||||||
| 6325 | render_bar_chart_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6326 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6327 | |||||||||||||||||||||||||||||||||||||||||||
| 6328 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6329 | pre ∃ pt ∈ data . ref(pt) ≠ 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 6330 | ⇒ die "Each data point must be an array reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6331 | pre ∃ pt ∈ data . |pt| < 2 ⇒ die "Each data point must have at least 2 elements" | ||||||||||||||||||||||||||||||||||||||||||
| 6332 | pre ∃ pt ∈ data . defined(pt[1]) ∧ ¬numeric(pt[1]) | ||||||||||||||||||||||||||||||||||||||||||
| 6333 | ⇒ die "Value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6334 | pre opts.orientation ∉ {'vertical','horizontal'} | ||||||||||||||||||||||||||||||||||||||||||
| 6335 | ⇒ die "orientation must be 'vertical' or 'horizontal'" | ||||||||||||||||||||||||||||||||||||||||||
| 6336 | pre opts.sort_bars ∉ {'value','label','none'} | ||||||||||||||||||||||||||||||||||||||||||
| 6337 | ⇒ die "sort_bars must be 'value', 'label', or 'none'" | ||||||||||||||||||||||||||||||||||||||||||
| 6338 | pre ∀ pt ∈ data . pt[1] < 0 ⇒ pt[1] := |pt[1]| -- negative → absolute | ||||||||||||||||||||||||||||||||||||||||||
| 6339 | pre ∀ pt ∈ data . pt[1] = undef ⇒ pt ∉ result -- undef rows skipped | ||||||||||||||||||||||||||||||||||||||||||
| 6340 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6341 | post result.svg_id = opts.id // "bar_chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6342 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6343 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6344 | post "d3.scaleBand" ⊆ result.html ∧ "d3.scaleLinear" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6345 | post opts.animated = 1 ⇒ "prefers-reduced-motion" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6346 | post opts.color = 'categorical' ⇒ the default scheme's expression | ||||||||||||||||||||||||||||||||||||||||||
| 6347 | ("schemeTableau10") ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6348 | post opts.max_bars = N ∧ N ≥ 2 ∧ |data| > N | ||||||||||||||||||||||||||||||||||||||||||
| 6349 | ⇒ |result_bars| = N ∧ "Other" ∈ result_labels | ||||||||||||||||||||||||||||||||||||||||||
| 6350 | |||||||||||||||||||||||||||||||||||||||||||
| 6351 | =head2 render_multi_series_line_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6352 | |||||||||||||||||||||||||||||||||||||||||||
| 6353 | render_multi_series_line_chart_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6354 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6355 | |||||||||||||||||||||||||||||||||||||||||||
| 6356 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of series" | ||||||||||||||||||||||||||||||||||||||||||
| 6357 | pre ∃ s ∈ data . ref(s) ≠ 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 6358 | ⇒ die "Each series must be a hashref with name and data" | ||||||||||||||||||||||||||||||||||||||||||
| 6359 | pre ∃ s ∈ data . ¬defined(s.name) ∨ ref(s.data) ≠ 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 6360 | pre opts.x_order ∉ {undef, 'auto', 'data', 'sort'} | ||||||||||||||||||||||||||||||||||||||||||
| 6361 | ⇒ die "Unknown x_order: <x_order>" | ||||||||||||||||||||||||||||||||||||||||||
| 6362 | ⇒ die "Each series must be a hashref with name and data" | ||||||||||||||||||||||||||||||||||||||||||
| 6363 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6364 | post result.svg_id = opts.id // "chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6365 | post L = the labels of every series, first-seen order, as strings | ||||||||||||||||||||||||||||||||||||||||||
| 6366 | post opts.x_order ≠ 'data' ∧ ∀ l ∈ L . numeric(l) | ||||||||||||||||||||||||||||||||||||||||||
| 6367 | ⇒ the X domain is L sorted numerically | ||||||||||||||||||||||||||||||||||||||||||
| 6368 | post opts.x_order = 'auto' ∧ ∀ l ∈ L . iso_date(l) | ||||||||||||||||||||||||||||||||||||||||||
| 6369 | ⇒ the X domain is L sorted as strings | ||||||||||||||||||||||||||||||||||||||||||
| 6370 | post opts.x_order = 'sort' ∧ ∃ l ∈ L . ¬numeric(l) | ||||||||||||||||||||||||||||||||||||||||||
| 6371 | ⇒ the X domain is L sorted as strings | ||||||||||||||||||||||||||||||||||||||||||
| 6372 | post otherwise ⇒ the X domain is L | ||||||||||||||||||||||||||||||||||||||||||
| 6373 | post the X domain is sorted ⇒ each series' points are in domain order | ||||||||||||||||||||||||||||||||||||||||||
| 6374 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6375 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6376 | post "d3.scalePoint" ⊆ result.html ∧ "d3.scaleLinear" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6377 | post "d3.line()" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6378 | post #{path ∈ result.html : class = "msl-line"} = |data| -- one path per series | ||||||||||||||||||||||||||||||||||||||||||
| 6379 | post opts.x_label ≠ '' ⇒ "msl-axis-label" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6380 | post opts.y_label ≠ '' ⇒ "msl-axis-label" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6381 | post opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6382 | ⇒ the colours of S, as a JS array, ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6383 | post opts.scheme ∉ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6384 | ⇒ the default scheme's colours ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6385 | -- not a d3.scheme<S> expression | ||||||||||||||||||||||||||||||||||||||||||
| 6386 | post opts.animated = 1 ⇒ "stroke-dashoffset" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6387 | ∧ "prefers-reduced-motion" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6388 | |||||||||||||||||||||||||||||||||||||||||||
| 6389 | =head2 render_scatter_chart_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6390 | |||||||||||||||||||||||||||||||||||||||||||
| 6391 | render_scatter_chart_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6392 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6393 | |||||||||||||||||||||||||||||||||||||||||||
| 6394 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6395 | pre ∃ pt ∈ data . ref(pt) ≠ 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 6396 | ⇒ die "Each data point must be an array reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6397 | pre ∃ pt ∈ data . |pt| < 2 ⇒ die "Each data point must have at least 2 elements" | ||||||||||||||||||||||||||||||||||||||||||
| 6398 | pre ∃ pt ∈ data . ¬numeric(pt[0]) | ||||||||||||||||||||||||||||||||||||||||||
| 6399 | ⇒ die "X value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6400 | pre ∃ pt ∈ data . ¬numeric(pt[1]) | ||||||||||||||||||||||||||||||||||||||||||
| 6401 | ⇒ die "Y value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6402 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6403 | post result.svg_id = opts.id // "scatter_chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6404 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6405 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6406 | post "d3.scaleLinear" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6407 | post opts.animated = 1 ⇒ "prefers-reduced-motion" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6408 | post opts.colour_by_series = 1 ∧ ∃ pt ∈ data . defined(pt[3].series) | ||||||||||||||||||||||||||||||||||||||||||
| 6409 | ⇒ "scaleOrdinal" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6410 | ∧ "seriesNames" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6411 | ∧ legend drawn (margin right = 140) | ||||||||||||||||||||||||||||||||||||||||||
| 6412 | post opts.colour_by_series = 1 ∧ opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6413 | ⇒ the D3 expression of S ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6414 | post opts.colour_by_series = 1 ∧ opts.scheme ∉ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6415 | ⇒ the default scheme's expression ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6416 | post ∃ pt ∈ data . defined(pt[3].size) | ||||||||||||||||||||||||||||||||||||||||||
| 6417 | ⇒ "d3.scaleSqrt" ⊆ result.html ∧ "rScale" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6418 | post opts.radius_range = [A,B] | ||||||||||||||||||||||||||||||||||||||||||
| 6419 | ⇒ "range([A,B])" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6420 | post ∀ pt ∈ data . pt[3].size = undef | ||||||||||||||||||||||||||||||||||||||||||
| 6421 | ⇒ "rScale" ∉ result.html -- fixed radius | ||||||||||||||||||||||||||||||||||||||||||
| 6422 | |||||||||||||||||||||||||||||||||||||||||||
| 6423 | =head2 render_table_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6424 | |||||||||||||||||||||||||||||||||||||||||||
| 6425 | render_table_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6426 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6427 | |||||||||||||||||||||||||||||||||||||||||||
| 6428 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6429 | pre |data| = 0 ⇒ die "Data must have at least one row (header row)" | ||||||||||||||||||||||||||||||||||||||||||
| 6430 | pre ∃ row ∈ data . ref(row) ≠ 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 6431 | ⇒ die "Each row must be an array reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6432 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6433 | post result.table_id = opts.id // "data_table" | ||||||||||||||||||||||||||||||||||||||||||
| 6434 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6435 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6436 | post opts.sortable ≠ 0 ⇒ "dt-sortable" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6437 | |||||||||||||||||||||||||||||||||||||||||||
| 6438 | =head2 render_stacked_bar_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6439 | |||||||||||||||||||||||||||||||||||||||||||
| 6440 | render_stacked_bar_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6441 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6442 | |||||||||||||||||||||||||||||||||||||||||||
| 6443 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of hashes" | ||||||||||||||||||||||||||||||||||||||||||
| 6444 | pre ∃ row ∈ data . ref(row) ≠ 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 6445 | ⇒ die "Each data point must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6446 | pre ∃ row ∈ data . ¬numeric(row[val_col]) | ||||||||||||||||||||||||||||||||||||||||||
| 6447 | ⇒ die "Value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6448 | pre opts.orient ∉ {'v','h'} ⇒ die "orient must be 'v' or 'h'" | ||||||||||||||||||||||||||||||||||||||||||
| 6449 | pre defined(opts.sort) ∧ opts.sort ∉ {'value','label'} | ||||||||||||||||||||||||||||||||||||||||||
| 6450 | ⇒ die "sort must be 'value' or 'label'" | ||||||||||||||||||||||||||||||||||||||||||
| 6451 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6452 | post result.svg_id = opts.id // "stacked_bar" | ||||||||||||||||||||||||||||||||||||||||||
| 6453 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6454 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6455 | post "d3.stack()" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6456 | post "SCHEMES" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6457 | post opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6458 | ⇒ the D3 expression of S ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6459 | post opts.scheme ∉ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6460 | ⇒ the default scheme's expression ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6461 | |||||||||||||||||||||||||||||||||||||||||||
| 6462 | =head2 render_combo_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6463 | |||||||||||||||||||||||||||||||||||||||||||
| 6464 | render_combo_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6465 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6466 | |||||||||||||||||||||||||||||||||||||||||||
| 6467 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of hashes" | ||||||||||||||||||||||||||||||||||||||||||
| 6468 | pre ∃ row ∈ data . ref(row) ≠ 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 6469 | ⇒ die "Each data point must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6470 | pre ∃ row ∈ data . ¬numeric(row[bar_col]) | ||||||||||||||||||||||||||||||||||||||||||
| 6471 | ⇒ die "Bar value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6472 | pre ∃ row ∈ data . ¬numeric(row[line_col]) | ||||||||||||||||||||||||||||||||||||||||||
| 6473 | ⇒ die "Line value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6474 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6475 | post result.svg_id = opts.id // "combo_chart" | ||||||||||||||||||||||||||||||||||||||||||
| 6476 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6477 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6478 | post "d3.scaleBand" ⊆ result.html ∧ "d3.scaleLinear" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6479 | post "axisRight" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6480 | post opts.bar_color = undef ∧ opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6481 | ⇒ colour_at(S, 0) is the bar fill | ||||||||||||||||||||||||||||||||||||||||||
| 6482 | post opts.line_color = undef ∧ opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6483 | ⇒ colour_at(S, 1) is the line stroke | ||||||||||||||||||||||||||||||||||||||||||
| 6484 | post "</b>" ∉ result.html ∧ "<\/b>" ∈ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6485 | |||||||||||||||||||||||||||||||||||||||||||
| 6486 | =head2 render_treemap_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6487 | |||||||||||||||||||||||||||||||||||||||||||
| 6488 | render_treemap_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6489 | HTML::D3 × (ArrayRef | HashRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6490 | |||||||||||||||||||||||||||||||||||||||||||
| 6491 | pre ref(data) ∉ {'ARRAY','HASH'} | ||||||||||||||||||||||||||||||||||||||||||
| 6492 | ⇒ die "Data must be an array of hashes or a hash tree" | ||||||||||||||||||||||||||||||||||||||||||
| 6493 | pre ref(data) = 'ARRAY' ∧ ∃ row ∈ data . ref(row) ≠ 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 6494 | ⇒ die "Each flat row must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6495 | pre ref(data) = 'ARRAY' ∧ ∃ row ∈ data . ¬exists(row.path) | ||||||||||||||||||||||||||||||||||||||||||
| 6496 | ⇒ die "Each flat row must have a 'path' key" | ||||||||||||||||||||||||||||||||||||||||||
| 6497 | pre ref(data) = 'ARRAY' ∧ ∃ row ∈ data . ¬numeric(row.value) | ||||||||||||||||||||||||||||||||||||||||||
| 6498 | ⇒ die "Each flat row must have a numeric 'value' key" | ||||||||||||||||||||||||||||||||||||||||||
| 6499 | pre ref(data) = 'ARRAY' ∧ ∃ row ∈ data . defined(row.color_value) ∧ ¬finite(row.color_value) | ||||||||||||||||||||||||||||||||||||||||||
| 6500 | ⇒ die "Each flat row's 'color_value' must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6501 | pre ref(data) = 'HASH' ∧ ∃ node ∈ tree . defined(node.color_value) ∧ ¬finite(node.color_value) | ||||||||||||||||||||||||||||||||||||||||||
| 6502 | ⇒ die "Each node's 'color_value' must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6503 | pre BV ∧ opts.color_scheme ∉ sequential schemes | ||||||||||||||||||||||||||||||||||||||||||
| 6504 | ⇒ die "Unknown color_scheme: <color_scheme>" | ||||||||||||||||||||||||||||||||||||||||||
| 6505 | where BV ≡ ∃ tile . defined(tile.color_value) | ||||||||||||||||||||||||||||||||||||||||||
| 6506 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6507 | post result.svg_id = opts.id // "treemap" | ||||||||||||||||||||||||||||||||||||||||||
| 6508 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6509 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6510 | post "d3.treemap()" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6511 | post ref(data) = 'ARRAY' ⇒ "d3.stratify()" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6512 | post ref(data) = 'HASH' ⇒ "d3.hierarchy(" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6513 | post opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6514 | ⇒ the D3 expression of S ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6515 | post BV ⇒ "d3.scaleSequential(" ⊆ result.html ∧ "tm-legend" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6516 | post ¬BV ⇒ "scaleSequential" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6517 | post opts.scheme ∉ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6518 | ⇒ the default scheme's expression ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6519 | |||||||||||||||||||||||||||||||||||||||||||
| 6520 | =head2 render_histogram_snippet | ||||||||||||||||||||||||||||||||||||||||||
| 6521 | |||||||||||||||||||||||||||||||||||||||||||
| 6522 | render_histogram_snippet : | ||||||||||||||||||||||||||||||||||||||||||
| 6523 | HTML::D3 × (ArrayRef | undef) × (HashRef | undef) → HashRef ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6524 | |||||||||||||||||||||||||||||||||||||||||||
| 6525 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of numeric values" | ||||||||||||||||||||||||||||||||||||||||||
| 6526 | pre ∃ v ∈ data . ¬numeric(v) ⇒ die "Each value must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6527 | pre defined(opts.bins) ∧ (¬integer(opts.bins) ∨ opts.bins < 1) | ||||||||||||||||||||||||||||||||||||||||||
| 6528 | ⇒ die "bins must be a positive integer" | ||||||||||||||||||||||||||||||||||||||||||
| 6529 | pre opts.binned = 1 ∧ |data| = 0 | ||||||||||||||||||||||||||||||||||||||||||
| 6530 | ⇒ die "Data must contain at least one bin" | ||||||||||||||||||||||||||||||||||||||||||
| 6531 | pre opts.binned = 1 ∧ ∃ b ∈ data . ref(b) ≠ 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 6532 | ⇒ die "Each pre-binned entry must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||||
| 6533 | pre opts.binned = 1 ∧ ∃ b ∈ data . ¬numeric(b.count) | ||||||||||||||||||||||||||||||||||||||||||
| 6534 | ⇒ die "Bin count must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 6535 | pre opts.binned = 1 ∧ ∃ b ∈ data . b.count < 0 | ||||||||||||||||||||||||||||||||||||||||||
| 6536 | ⇒ die "Bin count must not be negative" | ||||||||||||||||||||||||||||||||||||||||||
| 6537 | pre opts.binned = 1 ∧ ∃ b ∈ data . ¬defined(b.label) ∧ ¬(numeric(b.lo) ∧ numeric(b.hi)) | ||||||||||||||||||||||||||||||||||||||||||
| 6538 | ⇒ die "Each pre-binned entry must have a label or numeric lo and hi" | ||||||||||||||||||||||||||||||||||||||||||
| 6539 | pre opts.binned = 1 ∧ ¬defined(b.label) ∧ numeric(b.lo) ∧ numeric(b.hi) | ||||||||||||||||||||||||||||||||||||||||||
| 6540 | ⇒ b.label := "[lo,hi)" | ||||||||||||||||||||||||||||||||||||||||||
| 6541 | post result ∈ HashRef | ||||||||||||||||||||||||||||||||||||||||||
| 6542 | post result.svg_id = opts.id // "histogram" | ||||||||||||||||||||||||||||||||||||||||||
| 6543 | post result.html ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6544 | post "<!DOCTYPE" ∉ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6545 | post opts.binned ≠ 1 ⇒ "d3.bin()" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6546 | post opts.binned = 1 ⇒ "d3.bin()" ∉ result.html ∧ "scaleBand" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6547 | post opts.color defined ⇒ c(clone) -- explicit colour is the bar fill | ||||||||||||||||||||||||||||||||||||||||||
| 6548 | post opts.color = undef ∧ opts.scheme = S ∧ S ∈ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6549 | ⇒ the D3 expression of S subscripted "[0]" | ||||||||||||||||||||||||||||||||||||||||||
| 6550 | ⊆ result.html -- colour_at(S, 0) | ||||||||||||||||||||||||||||||||||||||||||
| 6551 | post opts.color = undef ∧ opts.scheme ∉ categorical_schemes() | ||||||||||||||||||||||||||||||||||||||||||
| 6552 | ⇒ the default scheme's first colour ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6553 | post opts.bins = undef ⇒ "Math.log2" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6554 | post opts.x_label ≠ '' ⇒ "hg-axis-label" ⊆ result.html | ||||||||||||||||||||||||||||||||||||||||||
| 6555 | |||||||||||||||||||||||||||||||||||||||||||
| 6556 | =head2 render_line_chart_with_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 6557 | |||||||||||||||||||||||||||||||||||||||||||
| 6558 | render_line_chart_with_tooltips : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6559 | |||||||||||||||||||||||||||||||||||||||||||
| 6560 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of arrays" | ||||||||||||||||||||||||||||||||||||||||||
| 6561 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6562 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6563 | post "mouseover" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6564 | post "</b>" ∉ result ∧ "<\/b>" ∈ result | ||||||||||||||||||||||||||||||||||||||||||
| 6565 | |||||||||||||||||||||||||||||||||||||||||||
| 6566 | =head2 render_multi_series_line_chart_with_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 6567 | |||||||||||||||||||||||||||||||||||||||||||
| 6568 | render_multi_series_line_chart_with_tooltips : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6569 | |||||||||||||||||||||||||||||||||||||||||||
| 6570 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of hashes" | ||||||||||||||||||||||||||||||||||||||||||
| 6571 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6572 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6573 | post "</b>" ∉ result ∧ "<\/b>" ∈ result | ||||||||||||||||||||||||||||||||||||||||||
| 6574 | |||||||||||||||||||||||||||||||||||||||||||
| 6575 | =head2 render_multi_series_line_chart_with_animated_tooltips | ||||||||||||||||||||||||||||||||||||||||||
| 6576 | |||||||||||||||||||||||||||||||||||||||||||
| 6577 | render_multi_series_line_chart_with_animated_tooltips : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6578 | |||||||||||||||||||||||||||||||||||||||||||
| 6579 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of hashes" | ||||||||||||||||||||||||||||||||||||||||||
| 6580 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6581 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6582 | post "translateY" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6583 | post "</b>" ∉ result ∧ "<\/b>" ∈ result | ||||||||||||||||||||||||||||||||||||||||||
| 6584 | |||||||||||||||||||||||||||||||||||||||||||
| 6585 | =head2 render_multi_series_line_chart_with_legends | ||||||||||||||||||||||||||||||||||||||||||
| 6586 | |||||||||||||||||||||||||||||||||||||||||||
| 6587 | render_multi_series_line_chart_with_legends : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6588 | |||||||||||||||||||||||||||||||||||||||||||
| 6589 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of hashes" | ||||||||||||||||||||||||||||||||||||||||||
| 6590 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6591 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6592 | post ".legend" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6593 | |||||||||||||||||||||||||||||||||||||||||||
| 6594 | =head2 render_multi_series_line_chart_with_interactive_legends | ||||||||||||||||||||||||||||||||||||||||||
| 6595 | |||||||||||||||||||||||||||||||||||||||||||
| 6596 | render_multi_series_line_chart_with_interactive_legends : HTML::D3 × (ArrayRef | undef) → Str ∪ ⊥ | ||||||||||||||||||||||||||||||||||||||||||
| 6597 | |||||||||||||||||||||||||||||||||||||||||||
| 6598 | pre ref(data) ≠ 'ARRAY' ⇒ die "Data must be an array of hashes" | ||||||||||||||||||||||||||||||||||||||||||
| 6599 | post result ∈ Str | ||||||||||||||||||||||||||||||||||||||||||
| 6600 | post "<!DOCTYPE" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6601 | post "isVisible" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6602 | post "isVisible ? 0 : 1" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6603 | post ".legend" ⊆ result | ||||||||||||||||||||||||||||||||||||||||||
| 6604 | |||||||||||||||||||||||||||||||||||||||||||
| 6605 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 6606 | |||||||||||||||||||||||||||||||||||||||||||
| 6607 | Copyright 2025-2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||
| 6608 | |||||||||||||||||||||||||||||||||||||||||||
| 6609 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||
| 6610 | If you use it, | ||||||||||||||||||||||||||||||||||||||||||
| 6611 | please let me know. | ||||||||||||||||||||||||||||||||||||||||||
| 6612 | |||||||||||||||||||||||||||||||||||||||||||
| 6613 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 6614 | |||||||||||||||||||||||||||||||||||||||||||
| 6615 | 32 | 1; | |||||||||||||||||||||||||||||||||||||||||