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

https://github.com/tylerrick/d3-force-labels


https://github.com/tylerrick/d3-force-labels

Last synced: 19 days ago
JSON representation

Awesome Lists containing this project

README

          

# D3 Force Labels

Demo:

Generates an automatic and dynamic positioning for labels, using [`d3.forceSimulation()`](https://github.com/d3/d3-force/blob/master/README.md#forceSimulation).

## Usage

Pass in a selection of the objects you want to attach labels to.

```js
labelSim = d3.forceLabels(anchors)
```

`forceLabels()` returns a [`d3.forceSimulation()`](https://github.com/d3/d3-force/blob/master/README.md#forceSimulation) object which allows full control over the charge strength, etc.

For example, to change the charge strength, you could do something like this:
```js
labelSim.force('charge').strength(-60);
```

At each tick the following occurs:

- Center of each object (anchor position) is determined by the SVG Bounding Box of that object and stored in object ```anchorPos``` under x,y
- The position of the label element is determined by a force layout where anchors are fixed nodes and labels are floating. New position for each label is stored in object ```labelPos``` under x,y

Both the ```anchorPos``` and ```labelPos``` are inserted in the ```__data__``` variable of the object being labeled. This allows easy access when drawing the labels and connectors.

In the demo the label and link are created as svg objects on the same data selection as the anchors. As the position information is embedded in ```__data__```, the redraw function is simply:

```js
function redrawLabels() {
labelBox
.attr("transform", function(d) {
return "translate(" + d.labelPos.x + " " + d.labelPos.y + ")"
})

links
.attr("x1", function(d) { return d.anchorPos.x })
.attr("y1", function(d) { return d.anchorPos.y })
.attr("x2", function(d) { return d.labelPos.x })
.attr("y2", function(d) { return d.labelPos.y })
}
```

which is attached like this:

```js
labelSim = d3.forceLabels(anchors)
.on("tick", redrawLabels)
```

## Authors

-
-