# NAME HTML::D3 - A simple Perl module for generating charts using D3.js. # VERSION Version 0.14 # SYNOPSIS use HTML::D3; my $chart = HTML::D3->new( width => 1024, height => 768, title => 'Sample Bar Chart' ); my $data = [ ['Category 1', 10], ['Category 2', 20], ['Category 3', 30] ]; my $html = $chart->render_bar_chart($data); print $html; $chart = HTML::D3->new(title => 'Sales Data'); $data = [ ['Product A', 100], ['Product B', 150], ['Product C', 200] ]; $html = $chart->render_line_chart($data); print $html; # DESCRIPTION HTML::D3 is a Perl module that provides functionality to create simple charts using D3.js. The module generates HTML and JavaScript code to render the chart in a web browser. # METHODS The `=head3 API SPECIFICATION` subsections use [Params::Validate::Strict](https://metacpan.org/pod/Params%3A%3AValidate%3A%3AStrict) schema syntax (`type => 'arrayref'` etc.) as a documentation convention. `Params::Validate::Strict` is not a runtime dependency of this module; the schemas describe the parameter contract in machine-readable notation and can be plumbed into a WAF or test generator if desired. ## new my $chart = HTML::D3->new(%args); Creates a new HTML::D3 object. Accepts the following optional arguments: - `width` - The width of the chart (default: 800). - `height` - The height of the chart (default: 600). - `title` - The title of the chart (default: 'Chart'). ## render\_bar\_chart my $html = $chart->render_bar_chart($data); Generates HTML and JavaScript code to render a bar chart. Accepts the following arguments: - `$data` - An array reference containing data points. Each data point should be an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the HTML and JavaScript code for the chart. ### Errors - Throws `Data is not optional` when `$data` is `undef`. - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document starting with C<< >>; D3.js loaded from CDN; bar chart rendered with C. ## render\_animated\_bar\_chart my $html = $chart->render_animated_bar_chart($data); Generates HTML and JavaScript code to render a bar chart where each bar grows upward from the baseline on page load. Bars are staggered so they rise one-after-another from left to right. Accepts the following arguments: - `$data` - An array reference of data points. Each data point is an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the complete HTML5 document. ### Errors - Throws `Data is not optional` when `$data` is `undef`. - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document; each bar animates from height=0 upward using C with a staggered per-bar delay. ## render\_line\_chart my $html = $chart->render_line_chart($data); Generates HTML and JavaScript code to render a line chart. Accepts the following arguments: - `$data` - An array reference containing data points. Each data point should be an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the HTML and JavaScript code for the chart. ### Errors - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document; line chart with C and C. ## render\_animated\_line\_chart my $html = $chart->render_animated_line_chart($data); Generates HTML and JavaScript code to render a line chart where the line draws itself from left to right on page load, followed by each data-point circle fading in once the line is complete. Accepts the following arguments: - `$data` - An array reference of data points. Each data point is an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the complete HTML5 document. ### Errors - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document; the line path animates via C with C; data-point circles fade in with C after the line transition completes. ## render\_pie\_chart my $html = $chart->render_pie_chart($data); Generates HTML and JavaScript code to render a pie chart. Each slice is coloured with `d3.schemeCategory10`; percentage labels appear inside each slice and a colour legend is shown to the right of the pie. Accepts the following arguments: - `$data` - An array reference of data points. Each data point is an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the complete HTML5 document. ### Errors - Throws `Data is not optional` when `$data` is `undef`. - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document; pie rendered with C and C; slices coloured with C; percentage label inside each slice; legend to the right. ## render\_animated\_pie\_chart my $html = $chart->render_animated_pie_chart($data); Generates HTML and JavaScript code to render an animated pie chart where each slice fans out from zero angle on page load using `attrTween` and `d3.interpolate`. Percentage labels fade in once all slices are drawn. Accepts the following arguments: - `$data` - An array reference of data points. Each data point is an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the complete HTML5 document. ### Errors - Throws `Data is not optional` when `$data` is `undef`. - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document; slices animate via C with C (1000 ms); percentage labels fade in afterwards. ## render\_pie\_chart\_snippet my $fragment = $chart->render_pie_chart_snippet(\@slices); my $fragment = $chart->render_pie_chart_snippet(\@slices, \%opts); # $fragment->{svg_id} - always 'pie_chart' # $fragment->{html} - embeddable fragment; caller must load D3 v7 Generates an embeddable pie or donut chart fragment for use in existing HTML layouts. Returns `{ svg_id => 'pie_chart', html => Str }`. The caller is responsible for loading D3 v7 before embedding the fragment. ### Data format Each element of `\@slices` is `[$label, $value]` or `[$label, $value, \%extra]`. Negative values are silently converted to their absolute value. Zero-value slices are silently omitted. `\%extra` key/value pairs are shown as additional rows in the hover tooltip. ### Options (`\%opts`) - `animated` (bool, default 0) - fan slices in from arc-length 0 on first render using `attrTween` / `d3.easeBackOut` (800 ms, staggered). Respects `prefers-reduced-motion`. - `donut` (bool, default 0) - render as a donut chart (inner radius 38% of outer radius); the total sum appears in the centre hole. - `sort_slices` (string, default `'none'`) - `'value'` for largest-first, `'label'` for alphabetical, `'none'` for input order. - `max_slices` (int, default 0) - when > 0, only the top N-1 slices are shown individually; the rest are collapsed into an `"Other"` slice. - `legend` (bool, default 1) - render an HTML legend panel beside the chart. - `color_scheme` (string, default `'tableau10'`) - D3 categorical colour scheme. Supported: `tableau10`, `category10`, `set2`, `set3`, `paired`. ### Errors - Throws `Data must be an array of arrays` when `\@slices` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, opts => { type => 'hashref', optional => 1, default => {} }, } Each element of C<$data> is C<[ Str, Num ]> or C<[ Str, Num, HashRef ]>; passing C or a non-arrayref dies. Recognised C keys: C (boolean, default C<0>), C (boolean, default C<0>), C (string: C<'value'>, C<'label'>, or C<'none'>; default C<'none'>), C (integer, default C<0>), C (boolean, default C<1>), C (string, default C<'tableau10'>). #### Output HashRef -- C<{ svg_id =E 'pie_chart', html =E Str }>; embeddable fragment; no DOCTYPE, no page shell, no D3 CDN tag. ## render\_line\_chart\_with\_tooltips $html = $chart->render_line_chart_with_tooltips($data); Generates HTML and JavaScript code to render a line chart with mouseover tooltips. Accepts the following arguments: - `$data` - An array reference containing data points. Each data point should be an array reference with two elements: the label (string) and the value (numeric). Returns a string containing the HTML and JavaScript code for the chart. The JavaScript tooltip strings use `<\/b>` (with a backslash) rather than `` to satisfy html-tidy's requirement that `` blocks. ### Errors - Throws `Data must be an array of arrays` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is C<[ Str, Num ]>; passing C or a non-arrayref dies. #### Output Str -- complete HTML5 document; mouseover tooltip reveals label and value. Tooltip strings use C<< <\/b> >> not C<< >>. ## render\_line\_chart\_snippet my $fragment = $chart->render_line_chart_snippet($data); # $fragment->{svg_id} - the id attribute of the element # $fragment->{html} - embeddable HTML fragment (style + svg + script) Generates an embeddable HTML fragment for a line chart with mouseover tooltips. Unlike `render_line_chart_with_tooltips`, this method returns a fragment with no `, `, `, or ` wrapper, suitable for splicing directly into a Mojolicious TT (or any other) layout. The caller is responsible for loading D3 in the page `, e.g.: Accepts the following arguments: - `$data` - An array reference of data points. Each point is an array reference with two required elements - the label (string) and the value (numeric) - and an optional third element: a hash reference of extra key/value pairs to display in the tooltip after the label and value rows. [$x, $y] # basic point [$x, $y, \%row] # point with extra tooltip data Returns a hash reference with: - `svg_id` - The `id` attribute used on the ` element. - `html` - The embeddable fragment string. ## render\_zoomable\_line\_chart\_snippet my $fragment = $chart->render_zoomable_line_chart_snippet($data); my $fragment = $chart->render_zoomable_line_chart_snippet($data, { animated => 1 }); # $fragment->{svg_id} - the id attribute of the element # $fragment->{html} - embeddable HTML fragment (style + button + svg + script) Like `render_line_chart_snippet`, but adds brush-to-zoom: the user can drag across a range of the x-axis to zoom into that region. A _Reset zoom_ button (hidden until a zoom is active) returns the chart to its original extent. Subsequent brushes on the zoomed view zoom in further; Reset always returns to the full dataset. The caller is responsible for loading D3 in the page `. Accepts the same arguments as `render_line_chart_snippet`: an array reference of data points, each `[$x, $y]` or `[$x, $y, \%extra]`, plus an optional second argument `$opts` (hashref). ### Options - `animated` (boolean, default `0`) - when true, the initial page load animates the line drawing left-to-right via the `stroke-dashoffset` technique (1200 ms, `d3.easeLinear`), then fades in data-point circles after the line finishes (300 ms after a 1200 ms delay). Respects `prefers-reduced-motion`: when the user has requested reduced motion the line is drawn immediately at full opacity. Subsequent zoom and reset redraws are never animated regardless of this flag. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, opts => { type => 'hashref', optional => 1, default => {} }, } Each element of C<$data> is C<[ Str, Num ]> or C<[ Str, Num, HashRef ]>; passing C or a non-arrayref dies. Recognised C key: C (boolean, default C<0>). #### Output HashRef -- C<{ svg_id =E 'chart', html =E Str }>; embeddable fragment; no DOCTYPE, no page shell, no D3 CDN tag. ### Errors Dies with _Data must be an array of arrays_ if `$data` is not an arrayref. ### Side Effects None. ## render\_multi\_series\_line\_chart\_with\_tooltips $html = $chart->render_multi_series_line_chart_with_tooltips($data); Generates HTML and JavaScript code to render a chart of many lines with mouseover tooltips. Accepts the following arguments: - `$data` - An array reference of series hashes. Each element is a hashref with a `name` key (string) and a `data` key (array reference of `{label, value}` hashrefs). [ { name => 'Series A', data => [{ label => 'Jan', value => 100 }, ...] }, ... ] Returns a string containing the HTML and JavaScript code for the chart. Tooltip strings use `<\/b>` rather than `` for html-tidy compliance. ### Errors - Throws `Data must be an array of hashes` when `$data` is not an ARRAY reference. ### Side Effects None. ### API SPECIFICATION #### Input { data => { type => 'arrayref' }, } Each element of C<$data> is a hashref with keys C (string) and C (arrayref of hashrefs with C