/repel-boxes

NodeJS implementation of repel_boxes algorithm in R ggplot2 extension ggrepel. https://github.com/slowkow/ggrepel

Primary LanguageJavaScript

repel-boxes

NodeJS implementation of repel_boxes algorithm in R ggplot2 extension ggrepel. https://github.com/slowkow/ggrepel

Getting Started

Install dependencies:

npm install

This library is written using the ES6 standard. If it needs to run on systems that have not implemented ES6, then an ES5 compatible version can be generated by running:

grunt build

The output file will be in the dist/ directory.

Examples

The main function in this library repelBoxes is designed to work on a GeoJSON FeatureCollection that is made up of Point geometries. This point will represent the center of the label. Additionally, each feature needs the bbox key which will contain the bounding box of the label text.

The following example uses the ES5 version of the library.

var RepelBoxes = require('./dist/repelBoxes.js');
var repel = new RepelBoxes();
var boxes = {
	"type": "FeatureCollection",
	"features": [{
		"type": "Feature",
		"bbox": [0.0,0.0,2.0,2.0],
		"geometry": {
			"type": "Point",
			"coordinates": [1.0,1.0]
		}
	},
	{
		"type": "Feature",
		"bbox": [1.5,1.5,3.5,3.5],
		"geometry": {
			"type": "Point",
			"coordinates": [2.5,2.5]
		}
	}]
};

// Repel boxes and store results in labelPoints
var labelPoints = repel.repelBoxes(boxes, [-Infinity, Infinity], [-Infinity, Infinity], 0.001, 2000);

// The locations of optimum label placement
labelPoints.features