-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
755b6a8
commit fed23b2
Showing
2 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,80 @@ | ||
# Automatic scatter labelling | ||
## Automatic scatter labelling | ||
|
||
Implementation in d3 of NOLOOKAHEAD algorithm described in: | ||
Compute the positions of labels in a scatter plot. | ||
It implements the NOLOOKAHEAD algorithm described in: | ||
|
||
Theophil S., Schödl A. _An Efficient Algorithm for Scatter Chart Labeling_ [AAAI](https://www.think-cell.com/en/career/talks/pdf/think-cell_article_aaai2006.pdf) | ||
|
||
![example](https://github.com/furstenheim/automatic-scatter-labelling/blob/master/example-v0.0.2.png) | ||
![example](https://raw.githubusercontent.com/furstenheim/automatic-scatter-labelling/master/example.gif) | ||
|
||
|
||
### Install | ||
|
||
`$ npm install --save automatic-scatter-labelling` | ||
|
||
It requires of a webworker so right now it only works for the browser. | ||
|
||
### Example | ||
|
||
Running `npm run gulp` will launch a full working example in the browser | ||
|
||
### Basic usage | ||
|
||
With browserify or webpack | ||
|
||
```js | ||
const algorithm = require('automatic-scatter-labelling') | ||
const pointsToLabel = [ | ||
{id: 1, | ||
position: { | ||
x: 10, | ||
y: -20 | ||
}, | ||
label: { | ||
height: 30, | ||
width: 20 | ||
} | ||
}, | ||
{ | ||
id: 2, | ||
position: { | ||
x: 10, | ||
y: -20 | ||
}, | ||
label: { | ||
height: 30, | ||
width: 20 | ||
} | ||
] | ||
|
||
mainAlgorithm(pointsToLabel) | ||
.then(function (rectangles) { | ||
const rectangle = rectangles[0] | ||
const id = rectangle.id // Corresponding to the id provided in the algorithm | ||
// Coordinates to place the rectangle | ||
const {left, right, top, bottom} = rectangle.rectangle | ||
}) | ||
|
||
|
||
``` | ||
### Advanced usage | ||
Additionally to the points we want to label we can provide extra arguments to tweak the algorithm. `radius` is used to indicate that some space should be left around the points. `bbox` to indicate where the labels should be restricted (so that they don't go offscreen). | ||
Example: | ||
```js | ||
const result = await algorithm(pointsToLabel, { | ||
radius: 5, | ||
bbox: { | ||
top: margin.top, | ||
bottom: -margin.top + height, | ||
left: margin.left, | ||
right: margin.left + width, | ||
width, | ||
height: height | ||
} | ||
}) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters