Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/d3plus/d3plus-text

A smart SVG text box with line wrapping and automatic font size scaling.
https://github.com/d3plus/d3plus-text

Last synced: 21 days ago
JSON representation

A smart SVG text box with line wrapping and automatic font size scaling.

Awesome Lists containing this project

README

        

# d3plus-text

A smart SVG text box with line wrapping and automatic font size scaling.

## Installing

If using npm, `npm install d3plus-text`. Otherwise, you can download the [latest release from GitHub](https://github.com/d3plus/d3plus-text/releases/latest) or load from a [CDN](https://cdn.jsdelivr.net/npm/d3plus-text@1).

```js
import modules from "d3plus-text";
```

d3plus-text can be loaded as a standalone library or bundled as part of [D3plus](https://github.com/d3plus/d3plus). ES modules, AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3plus` global is exported:

```html

console.log(d3plus);

```

## Examples

Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using [d3plus-react](https://github.com/d3plus/d3plus-react/). These examples are powered by the [d3plus-storybook](https://github.com/d3plus/d3plus-storybook/) repo, and PRs are always welcome. :beers:

## API Reference

#####
* [TextBox](#TextBox)

#####
* [fontExists](#fontExists) - 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.
* [rtl](#rtl) - Returns `true` if the or element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl". Accepts an optional DOM element as an argument, whose own inherited state will be evaluated rather than the default html/body logic.
* [stringify](#stringify) - Coerces value into a String.
* [strip](#strip) - Removes all non ASCII characters from a string.
* [textSplit](#textSplit) - Splits a given sentence into an array of words.
* [htmlDecode](#htmlDecode) - Strips HTML and "un-escapes" escape characters.
* [textWidth](#textWidth) - Given a text string, returns the predicted pixel width of the string when placed into DOM.
* [textWrap](#textWrap) - Based on the defined styles and dimensions, breaks a string into an array of strings for each line of text.
* [titleCase](#titleCase) - Capitalizes the first letter of each word in a phrase/sentence.
* [trim](#trim) - Cross-browser implementation of [trim](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim).
* [trimLeft](#trimLeft) - Cross-browser implementation of [trimLeft](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimLeft).
* [trimRight](#trimRight) - Cross-browser implementation of [trimRight](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimRight).

---


#### **TextBox** [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L28)

This is a global class, and extends all of the methods and functionality of [BaseClass](https://github.com/d3plus/d3plus-common#BaseClass).

* [TextBox](#TextBox) ⇐ [BaseClass](https://github.com/d3plus/d3plus-common#BaseClass)
* [new TextBox()](#new_TextBox_new)
* [.render([*callback*])](#TextBox.render)
* [.ariaHidden(*value*)](#TextBox.ariaHidden) ↩︎
* [.data([*data*])](#TextBox.data) ↩︎
* [.delay([*value*])](#TextBox.delay) ↩︎
* [.duration([*value*])](#TextBox.duration) ↩︎
* [.ellipsis([*value*])](#TextBox.ellipsis) ↩︎
* [.fontColor([*value*])](#TextBox.fontColor) ↩︎
* [.fontFamily([*value*])](#TextBox.fontFamily) ↩︎
* [.fontMax([*value*])](#TextBox.fontMax) ↩︎
* [.fontMin([*value*])](#TextBox.fontMin) ↩︎
* [.fontOpacity([*value*])](#TextBox.fontOpacity) ↩︎
* [.fontResize([*value*])](#TextBox.fontResize) ↩︎
* [.fontSize([*value*])](#TextBox.fontSize) ↩︎
* [.fontStroke([*value*])](#TextBox.fontStroke) ↩︎
* [.fontStrokeWidth([*value*])](#TextBox.fontStrokeWidth) ↩︎
* [.fontWeight([*value*])](#TextBox.fontWeight) ↩︎
* [.height([*value*])](#TextBox.height) ↩︎
* [.html([*value* = {
i: 'font-style: italic;',
em: 'font-style: italic;',
b: 'font-weight: bold;',
strong: 'font-weight: bold;'
}])](#TextBox.html) ↩︎
* [.id([*value*])](#TextBox.id) ↩︎
* [.lineHeight([*value*])](#TextBox.lineHeight) ↩︎
* [.maxLines([*value*])](#TextBox.maxLines) ↩︎
* [.overflow([*value*])](#TextBox.overflow) ↩︎
* [.padding([*value*])](#TextBox.padding) ↩︎
* [.pointerEvents([*value*])](#TextBox.pointerEvents) ↩︎
* [.rotate([*value*])](#TextBox.rotate) ↩︎
* [.rotateAnchor(_)](#TextBox.rotateAnchor) ↩︎
* [.select([*selector*])](#TextBox.select) ↩︎
* [.split([*value*])](#TextBox.split) ↩︎
* [.text([*value*])](#TextBox.text) ↩︎
* [.textAnchor([*value*])](#TextBox.textAnchor) ↩︎
* [.verticalAlign([*value*])](#TextBox.verticalAlign) ↩︎
* [.width([*value*])](#TextBox.width) ↩︎
* [.x([*value*])](#TextBox.x) ↩︎
* [.y([*value*])](#TextBox.y) ↩︎

# new **TextBox**()

Creates a wrapped text box for each point in an array of data. See [this example](https://d3plus.org/examples/d3plus-text/getting-started/) for help getting started using the TextBox class.

# TextBox.**render**([*callback*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L83)

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).

# TextBox.**ariaHidden**(*value*) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L412)

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](#TextBox), and is chainable with other methods of this Class.

# TextBox.**data**([*data*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L424)

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](#TextBox), and is chainable with other methods of this Class.

# TextBox.**delay**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L434)

Sets the animation delay to the specified number in milliseconds.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**duration**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L444)

Sets the animation duration to the specified number in milliseconds.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**ellipsis**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L458)

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](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(text, line) {
return line ? text.replace(/\.|,$/g, "") + "..." : "";
}
```

# TextBox.**fontColor**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L468)

Sets the font color to the specified accessor function or static string, which is inferred from the [DOM selection](#textBox.select) by default.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontFamily**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L478)

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](#fontExists) function will be used to determine the first available font on the client's machine.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontMax**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L488)

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](#textBox.fontResize).

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontMin**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L498)

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](#textBox.fontResize).

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontOpacity**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L508)

Sets the font opacity to the specified accessor function or static number between 0 and 1.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontResize**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L518)

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](http://d3plus.org/examples/d3plus-text/resizing-text/) for a side-by-side comparison.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontSize**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L528)

Sets the font size to the specified accessor function or static number (which corresponds to pixel units), which is inferred from the [DOM selection](#textBox.select) by default.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontStroke**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L538)

Sets the font stroke color for the rendered text.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontStrokeWidth**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L548)

Sets the font stroke width for the rendered text.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**fontWeight**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L558)

Sets the font weight to the specified accessor function or static number, which is inferred from the [DOM selection](#textBox.select) by default.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**height**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L572)

Sets the height for each box to the specified accessor function or static number.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(d) {
return d.height || 200;
}
```

# TextBox.**html**([*value* = {
i: 'font-style: italic;',
em: 'font-style: italic;',
b: 'font-weight: bold;',
strong: 'font-weight: bold;'
}]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L587)

Configures the ability to render simple HTML tags. Defaults to supporting ``, ``, ``, and ``, set to false to disable or provide a mapping of tags to svg styles

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**id**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L602)

Defines the unique id for each box to the specified accessor function or static number.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(d, i) {
return d.id || i + "";
}
```

# TextBox.**lineHeight**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L612)

Sets the line height to the specified accessor function or static number, which is 1.2 times the [font size](#textBox.fontSize) by default.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**maxLines**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L622)

Restricts the maximum number of lines to wrap onto, which is null (unlimited) by default.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**overflow**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L632)

Sets the text overflow to the specified accessor function or static boolean.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**padding**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L642)

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](#TextBox), and is chainable with other methods of this Class.

# TextBox.**pointerEvents**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L652)

Sets the pointer-events to the specified accessor function or static string.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**rotate**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L662)

Sets the rotate percentage for each box to the specified accessor function or static string.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**rotateAnchor**(_) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L672)

Sets the anchor point around which to rotate the text box.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**select**([*selector*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L682)

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](#TextBox), and is chainable with other methods of this Class.

# TextBox.**split**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L692)

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](#TextBox), and is chainable with other methods of this Class.

# TextBox.**text**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L706)

Sets the text for each box to the specified accessor function or static string.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(d) {
return d.text;
}
```

# TextBox.**textAnchor**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L716)

Sets the horizontal text anchor to the specified accessor function or static string, whose values are analagous to the SVG [text-anchor](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor) property.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**verticalAlign**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L726)

Sets the vertical alignment to the specified accessor function or static string. Accepts `"top"`, `"middle"`, and `"bottom"`.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.

# TextBox.**width**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L740)

Sets the width for each box to the specified accessor function or static number.

This is a static method of [TextBox](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(d) {
return d.width || 200;
}
```

# TextBox.**x**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L754)

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](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(d) {
return d.x || 0;
}
```

# TextBox.**y**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/TextBox.js#L768)

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](#TextBox), and is chainable with other methods of this Class.
default accessor

```js
function(d) {
return d.y || 0;
}
```

---


#### d3plus.**fontExists**(font) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/fontExists.js#L10)

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.

---


#### d3plus.**rtl**([elem]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/rtl.js#L5)

Returns `true` if the or element has either the "dir" HTML attribute or the "direction" CSS property set to "rtl". Accepts an optional DOM element as an argument, whose own inherited state will be evaluated rather than the default html/body logic.

This is a global function.

---


#### d3plus.**stringify**(value) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/stringify.js#L1)

Coerces value into a String.

This is a global function.

---


#### d3plus.**strip**(value, [spacer]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/strip.js#L18)

Removes all non ASCII characters from a string.

This is a global function.

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | String | | |
| [spacer] | String | "-" | The character(s) to be used in place of spaces. |

---


#### d3plus.**textSplit**(sentence) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textSplit.js#L50)

Splits a given sentence into an array of words.

This is a global function.

---


#### d3plus.**htmlDecode**(input) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWidth.js#L5)

Strips HTML and "un-escapes" escape characters.

This is a global function.

---


#### d3plus.**textWidth**(text, [style]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWidth.js#L12)

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](http://www.w3schools.com/cssref/pr_font_font.asp) values. |

---


#### d3plus.**textWrap**() [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L6)

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.

* [textWrap()](#textWrap)
* [.fontFamily([*value*])](#textWrap.fontFamily)
* [.fontSize([*value*])](#textWrap.fontSize)
* [.fontWeight([*value*])](#textWrap.fontWeight)
* [.height([*value*])](#textWrap.height)
* [.lineHeight([*value*])](#textWrap.lineHeight)
* [.maxLines([*value*])](#textWrap.maxLines)
* [.overflow([*value*])](#textWrap.overflow)
* [.split([*value*])](#textWrap.split)
* [.width([*value*])](#textWrap.width)

# d3plus..**fontFamily**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L89)

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](#textWrap).

# d3plus..**fontSize**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L98)

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](#textWrap).

# d3plus..**fontWeight**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L107)

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](#textWrap).

# d3plus..**height**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L116)

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](#textWrap).

# d3plus..**lineHeight**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L125)

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](#textWrap.fontSize) by default.

This is a static method of [textWrap](#textWrap).

# d3plus..**maxLines**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L134)

If *value* is specified, sets the maximum number of lines allowed when wrapping.

This is a static method of [textWrap](#textWrap).

# d3plus..**overflow**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L143)

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](#textWrap).

# d3plus..**split**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L152)

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](#textWrap).

# d3plus..**width**([*value*]) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/textWrap.js#L161)

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](#textWrap).

---


#### d3plus.**titleCase**(str) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/titleCase.js#L6)

Capitalizes the first letter of each word in a phrase/sentence.

This is a global function.

---


#### d3plus.**trim**(str) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/trim.js#L1)

Cross-browser implementation of [trim](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim).

This is a global function.

---


#### d3plus.**trimLeft**(str) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/trim.js#L10)

Cross-browser implementation of [trimLeft](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimLeft).

This is a global function.

---


#### d3plus.**trimRight**(str) [<>](https://github.com/d3plus/d3plus-text/blob/master/src/trim.js#L19)

Cross-browser implementation of [trimRight](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimRight).

This is a global function.

---

###### Documentation generated on Wed, 06 Mar 2024 16:14:10 GMT