Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
d3plus-text
Advanced tools
A smart SVG text box with line wrapping and automatic font size scaling.
If you use NPM, run npm install d3plus-text --save
. Otherwise, download the latest release. The released bundle supports AMD, CommonJS, and vanilla environments. You can also load directly from d3plus.org:
<script src="https://d3plus.org/js/d3plus-text.v0.9.full.min.js"></script>
Without a doubt, the most commonly requested part of the d3plus-text module is textBox, which is used for intelligently wrapping SVG text. At it's core, it accepts an array of data points containing "text"
keys and adds them to the page using a set of defaults. Here is a data array containing 3 different sentences to be wrapped:
var data = [
{text: "Here is <i>some</i> sample text that has been <strong>wrapped</strong> using d3plus.textBox."},
{text: "...and here is a <b>second</b> sentence!"},
{text: "这是句3号。这也即使包装没有空格!"}
];
And finally, this is how that data array would be passed to the textBox:
new d3plus.TextBox()
.data(data)
.fontSize(16)
.width(200)
.x(function(d, i) { return i * 250; })
.render();
While textBox comes with some helpful defaults, this example shows how any of the methods can be overridden with static values or accessor functions. For more information on how the textSplit function splits strings, specifically in languages that don't use spaces, check out this blog post.
Click here to view this example live on the web.
false
if none are installed on the user's machine.true
if the HTML or body element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl".This is a global class, and extends all of the methods and functionality of BaseClass
.
BaseClass
# new TextBox()
Creates a wrapped text box for each point in an array of data. See this example for help getting started using the TextBox class.
# TextBox.render([callback]) <>
Renders the text boxes. If a callback is specified, it will be called once the shapes are done drawing.
This is a static method of TextBox
.
# TextBox.ariaHidden(value) <>
If value is specified, sets the aria-hidden attribute to the specified function or string and returns the current class instance.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the data array to the specified array. A text box will be drawn for each object in the array.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the animation delay to the specified number in milliseconds.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.duration([value]) <>
Sets the animation duration to the specified number in milliseconds.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.ellipsis([value]) <>
Sets the function that handles what to do when a line is truncated. It should return the new value for the line, and is passed 2 arguments: the String of text for the line in question, and the number of the line. By default, an ellipsis is added to the end of any line except if it is the first word that cannot fit (in that case, an empty string is returned).
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(text, line) {
return line ? text.replace(/\.|,$/g, "") + "..." : "";
}
# TextBox.fontColor([value]) <>
Sets the font color to the specified accessor function or static string, which is inferred from the DOM selection by default.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.fontFamily([value]) <>
Defines the font-family to be used. The value passed can be either a String name of a font, a comma-separated list of font-family fallbacks, an Array of fallbacks, or a Function that returns either a String or an Array. If supplying multiple fallback fonts, the fontExists function will be used to determine the first available font on the client's machine.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the maximum font size to the specified accessor function or static number (which corresponds to pixel units), which is used when dynamically resizing fonts.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the minimum font size to the specified accessor function or static number (which corresponds to pixel units), which is used when dynamically resizing fonts.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.fontOpacity([value]) <>
Sets the font opacity to the specified accessor function or static number between 0 and 1.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.fontResize([value]) <>
Toggles font resizing, which can either be defined as a static boolean for all data points, or an accessor function that returns a boolean. See this example for a side-by-side comparison.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.fontSize([value]) <>
Sets the font size to the specified accessor function or static number (which corresponds to pixel units), which is inferred from the DOM selection by default.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.fontWeight([value]) <>
Sets the font weight to the specified accessor function or static number, which is inferred from the DOM selection by default.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the height for each box to the specified accessor function or static number.
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(d) {
return d.height || 200;
}
Toggles the ability to render simple HTML tags. Currently supports <b>
, <strong>
, <i>
, and <em>
.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Defines the unique id for each box to the specified accessor function or static number.
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(d, i) {
return d.id || i + "";
}
# TextBox.lineHeight([value]) <>
Sets the line height to the specified accessor function or static number, which is 1.2 times the font size by default.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.maxLines([value]) <>
Restricts the maximum number of lines to wrap onto, which is null (unlimited) by default.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.overflow([value]) <>
Sets the text overflow to the specified accessor function or static boolean.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the padding to the specified accessor function, CSS shorthand string, or static number, which is 0 by default.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.pointerEvents([value]) <>
Sets the pointer-events to the specified accessor function or static string.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the rotate percentage for each box to the specified accessor function or static string.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the anchor point around which to rotate the text box.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.select([selector]) <>
Sets the SVG container element to the specified d3 selector or DOM element. If not explicitly specified, an SVG element will be added to the page for use.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the word split behavior to the specified function, which when passed a string is expected to return that string split into an array of words.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the text for each box to the specified accessor function or static string.
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(d) {
return d.text;
}
# TextBox.textAnchor([value]) <>
Sets the horizontal text anchor to the specified accessor function or static string, whose values are analagous to the SVG text-anchor property.
This is a static method of TextBox
, and is chainable with other methods of this Class.
# TextBox.verticalAlign([value]) <>
Sets the vertical alignment to the specified accessor function or static string. Accepts "top"
, "middle"
, and "bottom"
.
This is a static method of TextBox
, and is chainable with other methods of this Class.
Sets the width for each box to the specified accessor function or static number.
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(d) {
return d.width || 200;
}
Sets the x position for each box to the specified accessor function or static number. The number given should correspond to the left side of the textBox.
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(d) {
return d.x || 0;
}
Sets the y position for each box to the specified accessor function or static number. The number given should correspond to the top side of the textBox.
This is a static method of TextBox
, and is chainable with other methods of this Class.
default accessor
function(d) {
return d.y || 0;
}
Given either a single font-family or a list of fonts, returns the name of the first font that can be rendered, or false
if none are installed on the user's machine.
This is a global function.
Returns true
if the HTML or body element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl".
This is a global function.
Coerces value into a String.
This is a global function.
Removes all non ASCII characters from a string.
This is a global function.
Splits a given sentence into an array of words.
This is a global function.
Strips HTML and "un-escapes" escape characters.
This is a global function.
Given a text string, returns the predicted pixel width of the string when placed into DOM.
This is a global function.
Param | Type | Description |
---|---|---|
text | String | Array | Can be either a single string or an array of strings to analyze. |
[style] | Object | An object of CSS font styles to apply. Accepts any of the valid CSS font property values. |
Based on the defined styles and dimensions, breaks a string into an array of strings for each line of text.
This is a global function.
# d3plus..fontFamily([value]) <>
If value is specified, sets the font family accessor to the specified function or string and returns this generator. If value is not specified, returns the current font family.
This is a static method of textWrap
.
# d3plus..fontSize([value]) <>
If value is specified, sets the font size accessor to the specified function or number and returns this generator. If value is not specified, returns the current font size.
This is a static method of textWrap
.
# d3plus..fontWeight([value]) <>
If value is specified, sets the font weight accessor to the specified function or number and returns this generator. If value is not specified, returns the current font weight.
This is a static method of textWrap
.
If value is specified, sets height limit to the specified value and returns this generator. If value is not specified, returns the current value.
This is a static method of textWrap
.
# d3plus..lineHeight([value]) <>
If value is specified, sets the line height accessor to the specified function or number and returns this generator. If value is not specified, returns the current line height accessor, which is 1.1 times the font size by default.
This is a static method of textWrap
.
# d3plus..maxLines([value]) <>
If value is specified, sets the maximum number of lines allowed when wrapping.
This is a static method of textWrap
.
# d3plus..overflow([value]) <>
If value is specified, sets the overflow to the specified boolean and returns this generator. If value is not specified, returns the current overflow value.
This is a static method of textWrap
.
If value is specified, sets the word split function to the specified function and returns this generator. If value is not specified, returns the current word split function.
This is a static method of textWrap
.
If value is specified, sets width limit to the specified value and returns this generator. If value is not specified, returns the current value.
This is a static method of textWrap
.
Capitalizes the first letter of each word in a phrase/sentence.
This is a global function.
Cross-browser implementation of trim.
This is a global function.
Cross-browser implementation of trimLeft.
This is a global function.
Cross-browser implementation of trimRight.
This is a global function.
FAQs
A smart SVG text box with line wrapping and automatic font size scaling.
We found that d3plus-text demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.