Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title and subtitle support #423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Scales, autoScaleRange} from "./scales.js";
import {offset} from "./style.js";

export function plot(options = {}) {
const {facet, style, caption} = options;
const {facet, style, caption, title, subtitle} = options;

// When faceting, wrap all marks in a faceting mark.
if (facet !== undefined) {
Expand Down Expand Up @@ -88,12 +88,28 @@ export function plot(options = {}) {
if (node != null) svg.appendChild(node);
}

// Wrap the plot in a figure with a caption, if desired.
if (caption == null) return svg;
// Wrap the plot in a figure with a caption, a title or a subtitle, if desired.
if (caption == null && title == null && subtitle == null) return svg;

const figure = document.createElement("figure");

if (title) {
const figtitle = figure.appendChild(document.createElement("h1"));
figtitle.appendChild(title instanceof Node ? title : document.createTextNode(title));
}

if (subtitle) {
const figsubtitle = figure.appendChild(document.createElement("h2"));
figsubtitle.appendChild(subtitle instanceof Node ? subtitle : document.createTextNode(subtitle));
}

figure.appendChild(svg);
const figcaption = figure.appendChild(document.createElement("figcaption"));
figcaption.appendChild(caption instanceof Node ? caption : document.createTextNode(caption));

if (caption) {
const figcaption = figure.appendChild(document.createElement("figcaption"));
figcaption.appendChild(caption instanceof Node ? caption : document.createTextNode(caption));
}

return figure;
}

Expand Down
169 changes: 169 additions & 0 deletions test/output/subtitle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<figure>
<h2>Relative frequency of letters in the English language</h2><svg class="plot" fill="currentColor" text-anchor="middle" width="640" height="400" viewBox="0 0 640 400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="translate(40,0)" fill="none" text-anchor="end">
<g class="tick" opacity="1" transform="translate(0,370.5)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">0</text>
</g>
<g class="tick" opacity="1" transform="translate(0,342.9452842072115)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">1</text>
</g>
<g class="tick" opacity="1" transform="translate(0,315.3905684144229)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">2</text>
</g>
<g class="tick" opacity="1" transform="translate(0,287.8358526216344)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">3</text>
</g>
<g class="tick" opacity="1" transform="translate(0,260.28113682884583)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">4</text>
</g>
<g class="tick" opacity="1" transform="translate(0,232.72642103605727)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">5</text>
</g>
<g class="tick" opacity="1" transform="translate(0,205.1717052432688)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">6</text>
</g>
<g class="tick" opacity="1" transform="translate(0,177.61698945048025)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">7</text>
</g>
<g class="tick" opacity="1" transform="translate(0,150.0622736576917)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">8</text>
</g>
<g class="tick" opacity="1" transform="translate(0,122.50755786490315)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">9</text>
</g>
<g class="tick" opacity="1" transform="translate(0,94.95284207211463)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">10</text>
</g>
<g class="tick" opacity="1" transform="translate(0,67.39812627932609)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">11</text>
</g>
<g class="tick" opacity="1" transform="translate(0,39.84341048653754)">
<line stroke="currentColor" x2="-6"></line>
<line stroke="currentColor" x2="580" stroke-opacity="0.1"></line><text fill="currentColor" x="-9" dy="0.32em">12</text>
</g><text fill="currentColor" transform="translate(-40,20)" dy="-1em" text-anchor="start">↑ Frequency (%)</text>
</g>
<g transform="translate(0,370)" fill="none" text-anchor="middle">
<g class="tick" opacity="1" transform="translate(55.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">A</text>
</g>
<g class="tick" opacity="1" transform="translate(77.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">B</text>
</g>
<g class="tick" opacity="1" transform="translate(99.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">C</text>
</g>
<g class="tick" opacity="1" transform="translate(121.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">D</text>
</g>
<g class="tick" opacity="1" transform="translate(143.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">E</text>
</g>
<g class="tick" opacity="1" transform="translate(165.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">F</text>
</g>
<g class="tick" opacity="1" transform="translate(187.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">G</text>
</g>
<g class="tick" opacity="1" transform="translate(209.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">H</text>
</g>
<g class="tick" opacity="1" transform="translate(231.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">I</text>
</g>
<g class="tick" opacity="1" transform="translate(253.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">J</text>
</g>
<g class="tick" opacity="1" transform="translate(275.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">K</text>
</g>
<g class="tick" opacity="1" transform="translate(297.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">L</text>
</g>
<g class="tick" opacity="1" transform="translate(319.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">M</text>
</g>
<g class="tick" opacity="1" transform="translate(341.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">N</text>
</g>
<g class="tick" opacity="1" transform="translate(363.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">O</text>
</g>
<g class="tick" opacity="1" transform="translate(385.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">P</text>
</g>
<g class="tick" opacity="1" transform="translate(407.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">Q</text>
</g>
<g class="tick" opacity="1" transform="translate(429.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">R</text>
</g>
<g class="tick" opacity="1" transform="translate(451.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">S</text>
</g>
<g class="tick" opacity="1" transform="translate(473.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">T</text>
</g>
<g class="tick" opacity="1" transform="translate(495.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">U</text>
</g>
<g class="tick" opacity="1" transform="translate(517.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">V</text>
</g>
<g class="tick" opacity="1" transform="translate(539.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">W</text>
</g>
<g class="tick" opacity="1" transform="translate(561.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">X</text>
</g>
<g class="tick" opacity="1" transform="translate(583.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">Y</text>
</g>
<g class="tick" opacity="1" transform="translate(605.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">Z</text>
</g>
</g>
<g>
<rect x="133" width="20" y="20" height="350"></rect>
<rect x="463" width="20" y="120.46449378050698" height="249.53550621949302"></rect>
<rect x="45" width="20" y="144.96063612029602" height="225.03936387970398"></rect>
<rect x="353" width="20" y="163.1467485435365" height="206.8532514564635"></rect>
<rect x="221" width="20" y="178.05384978743504" height="191.94615021256496"></rect>
<rect x="331" width="20" y="184.03322311447016" height="185.96677688552984"></rect>
<rect x="441" width="20" y="195.6613131790269" height="174.3386868209731"></rect>
<rect x="199" width="20" y="202.08156195874668" height="167.91843804125332"></rect>
<rect x="419" width="20" y="205.02991654857504" height="164.97008345142496"></rect>
<rect x="111" width="20" y="252.80979373327034" height="117.19020626672966"></rect>
<rect x="287" width="20" y="259.09226893402615" height="110.90773106597385"></rect>
<rect x="89" width="20" y="293.34278066446234" height="76.65721933553766"></rect>
<rect x="485" width="20" y="294.00409384348916" height="75.99590615651084"></rect>
<rect x="309" width="20" y="303.7033538025508" height="66.2966461974492"></rect>
<rect x="529" width="20" y="304.97087072901905" height="65.02912927098095"></rect>
<rect x="155" width="20" y="306.95481026609986" height="63.045189733900145"></rect>
<rect x="177" width="20" y="314.4772476775311" height="55.522752322468875"></rect>
<rect x="573" width="20" y="315.60699102503537" height="54.39300897496463"></rect>
<rect x="375" width="20" y="316.8469532357109" height="53.153046764289115"></rect>
<rect x="67" width="20" y="328.8883640371595" height="41.11163596284052"></rect>
<rect x="507" width="20" y="343.05148795465277" height="26.94851204534723"></rect>
<rect x="265" width="20" y="348.72775940796726" height="21.272240592032745"></rect>
<rect x="243" width="20" y="365.7841284837034" height="4.215871516296602"></rect>
<rect x="551" width="20" y="365.8667926310817" height="4.133207368918306"></rect>
<rect x="397" width="20" y="367.38230199968507" height="2.617698000314931"></rect>
<rect x="595" width="20" y="367.96095103133365" height="2.03904896866635"></rect>
</g>
<g stroke="currentColor" transform="translate(0,0.5)">
<line x1="40" x2="620" y1="370" y2="370"></line>
</g>
</svg>
</figure>
Loading