HTML-D3
view release on metacpan or search on metacpan
lib/HTML/D3.pm view on Meta::CPAN
my $sortable = exists($opts->{sortable}) ? $opts->{sortable} : 1;
my $caption = $opts->{caption} // '';
my $table_id = _chart_id($opts->{id}) // 'data_table';
my $js_esc = \&_js_string;
my $html_esc = sub {
my $s = shift // '';
$s =~ s/&/&/g;
$s =~ s/</</g;
$s =~ s/>/>/g;
return $s;
};
my $headers = $data->[0];
my @rows = @{$data}[1 .. $#$data];
for my $row (@rows) {
die 'Each row must be an array reference' unless ref($row) eq 'ARRAY';
}
my $json_data = _script_json([
map {
my @row = @$_;
\@row
} @$data
]);
my $caption_html = $caption
? '<caption>' . $html_esc->($caption) . '</caption>'
: '';
my $sort_th_class = $sortable ? ' class="dt-sortable"' : '';
my $sort_cursor_css = $sortable ? 'cursor:pointer; user-select:none;' : '';
my @th_cells = map { '<th' . $sort_th_class . '>' . $html_esc->($_) . '</th>' } @$headers;
my $thead_html = '<tr>' . join('', @th_cells) . '</tr>';
my @tbody_rows;
for my $row (@rows) {
my @tds = map { '<td>' . $html_esc->($_) . '</td>' } @$row;
push @tbody_rows, '<tr>' . join('', @tds) . '</tr>';
}
my $tbody_html = join("\n ", @tbody_rows);
my $sort_script = $sortable ? <<"SORT" : '';
var sortState = { col: -1, asc: true };
var tbody = table.select('tbody');
table.selectAll('th.dt-sortable').on('click', function(event, colIdx) {
if (sortState.col === colIdx) {
sortState.asc = !sortState.asc;
} else {
sortState.col = colIdx;
sortState.asc = true;
}
var rows = tbody.selectAll('tr').data();
rows.sort(function(a, b) {
var va = a[colIdx], vb = b[colIdx];
var na = parseFloat(va), nb = parseFloat(vb);
if (!isNaN(na) && !isNaN(nb)) { va = na; vb = nb; }
if (va < vb) return sortState.asc ? -1 : 1;
if (va > vb) return sortState.asc ? 1 : -1;
return 0;
});
tbody.selectAll('tr').data(rows).join('tr')
.selectAll('td').data(function(d) { return d.slice(1); }).join('td')
.text(function(d) { return d; });
table.selectAll('th.dt-sortable')
.attr('data-sort', function(d, i) {
return i === sortState.col ? (sortState.asc ? 'asc' : 'desc') : null;
});
});
SORT
my $html = <<"HTML";
<style>
#$table_id {
border-collapse: collapse;
width: 100%;
font-size: 13px;
}
#$table_id caption { font-weight: bold; margin-bottom: 6px; text-align: left; }
#$table_id th, #$table_id td { border: 1px solid #ccc; padding: 6px 10px; text-align: left; }
#$table_id th { background: #f5f5f5; $sort_cursor_css }
#$table_id th[data-sort="asc"]::after { content: " \\25B2"; font-size: 10px; }
#$table_id th[data-sort="desc"]::after { content: " \\25BC"; font-size: 10px; }
#$table_id tr:nth-child(even) { background: #fafafa; }
#$table_id tr:hover { background: #f0f4ff; }
</style>
<table id="$table_id">
$caption_html<thead>
$thead_html</thead>
<tbody>
$tbody_html</tbody>
</table>
<script@{[ $self->_nonce_attr ]}>
(function() {
var allData = $json_data;
var table = d3.select("#$table_id");
var headers = allData[0];
$sort_script})();
</script>
HTML
return { table_id => $table_id, html => $html };
}
=head2 render_stacked_bar_snippet
my $result = $chart->render_stacked_bar_snippet(\@rows, \%opts);
Generates an embeddable stacked bar chart (no page shell, no CDN tag).
Each element of C<\@rows> is a hashref with three keys named by C<opts>:
C<cat_col> (the X-axis grouping), C<seg_col> (the stacking segment), and
C<val_col> (the numeric measure). Multiple rows sharing the same
cat+seg combination are summed.
Accepted C<\%opts> keys:
( run in 2.067 seconds using v1.01-cache-2.11-cpan-60884a8df76 )