{"id":15633031,"url":"https://github.com/jamesscottbrown/respacer","last_synced_at":"2026-01-08T01:34:42.133Z","repository":{"id":48312056,"uuid":"328358990","full_name":"jamesscottbrown/respacer","owner":"jamesscottbrown","description":"Reposition objects (such as labels) so they do not overlap","archived":false,"fork":false,"pushed_at":"2022-02-14T09:45:17.000Z","size":717,"stargazers_count":0,"open_issues_count":8,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-04T20:14:53.638Z","etag":null,"topics":["javascript","library","visualization"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamesscottbrown.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-10T10:43:50.000Z","updated_at":"2023-04-11T15:34:17.000Z","dependencies_parsed_at":"2022-08-24T04:22:11.084Z","dependency_job_id":null,"html_url":"https://github.com/jamesscottbrown/respacer","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesscottbrown%2Frespacer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesscottbrown%2Frespacer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesscottbrown%2Frespacer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesscottbrown%2Frespacer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamesscottbrown","download_url":"https://codeload.github.com/jamesscottbrown/respacer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246231532,"owners_count":20744513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","library","visualization"],"created_at":"2024-10-03T10:46:30.887Z","updated_at":"2026-01-08T01:34:42.087Z","avatar_url":"https://github.com/jamesscottbrown.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/respacer)](https://www.npmjs.com/package/respacer)\n[![npm bundle size](https://img.shields.io/bundlephobia/min/respacer)](https://www.npmjs.com/package/respacer)\n[![npm bundle size](https://img.shields.io/bundlephobia/minzip/respacer)](https://www.npmjs.com/package/respacer)\n\n# Respacer\n\nRespacer is a library for adjusting the position of points or line-segments to avoid overlaps.\n\nIt accepts an array of objects, each of which has a property storing its 1D position, and returns a new array containing\nthe objects with an added property storing the adjusted position.\nThe array is sorted by this position.\n\nIt is similar to [labella.js](https://github.com/twitter/labella.js), but differs both in method used (it solves\na [Quadratic Program](https://en.wikipedia.org/wiki/Quadratic_programming) rather than doing a force based simulation),\nand in API (it accepts an array of arbitrary objects, rather than an array of `labella.Node` objects).\n\n\n## Usage\n\n### For points\n\n[![](./img/points.png)](./examples/points.html)\n\nThe `repositionPoints(data, options)` function is used to reposition objects which **do not** have individual widths.\n\nThe optional argument `options` is an object in which the following properties can be set:\n\n| Name              | Type   | Default | Description                                                                                                          |\n| ----------------- | ------ | ------- | -------------------------------------------------------------------------------------------------------------------- |\n| `width`           | number | 1000    | The maximum position value that can be assigned. Note that if this is too small, the problem may not be satisfiable. |\n| `minSpacing`      | number | 10      | The minimum distance between adjacent points after repositioning.                                                    |\n| `oldPositionName` | string | x       | The name of the property which contains the original position.                                                       |\n| `newPositionName` | string | newX    | The name of the property in which the updated position will be stored.                                               |                                    \n\nFor example, the input array\n\n```javascript\nconst data = [\n    {x: 4, label: \"A\"},\n    {x: 9, label: \"B\"},\n    {x: 2, label: \"C\"},\n    {x: 13, label: \"D\"},\n    {x: 9, label: \"E\"},\n    {x: 9.1, label: \"F\"}\n];\n\nconst newPositions = repositionPoints(data);\n```\n\nreturns:\n\n```javascript\n[\n    {\n        \"x\": 2,\n        \"label\": \"C\",\n        \"newX\": 0\n    },\n    {\n        \"x\": 4,\n        \"label\": \"A\",\n        \"newX\": 10.000000000000005\n    },\n    {\n        \"x\": 9,\n        \"label\": \"B\",\n        \"newX\": 20\n    },\n    {\n        \"x\": 9,\n        \"label\": \"E\",\n        \"newX\": 30\n    },\n    {\n        \"x\": 9.1,\n        \"label\": \"F\",\n        \"newX\": 39.999999999999986\n    },\n    {\n        \"x\": 13,\n        \"label\": \"D\",\n        \"newX\": 49.999999999999986\n    }\n]\n```\n\n### For line segments\n\n[![](./img/rectangles.png)](./examples/rectangles.html)\n\nThe `repositionLineSegments(data, options)` function is used to reposition objects which **do** have individual widths.\n\nThe optional argument `options` is an object in which the following properties can be set:\n\n| Name              | Type   | Default | Description                                                                                                                   |\n| ----------------- | ------ | ------- | ----------------------------------------------------------------------------------------------------------------------------- |\n| `width`           | number | 1000    | The maximum position value that can be assigned. Note that if this is too small, the problem may not be satisfiable.          |\n| `minSpacing`      | number | 10      | The minimum distance between adjacent the end of one object (position + width) and the start of the next after repositioning. |\n| `oldPositionName` | string | x       | The name of the property which contains the original position of each object.                                                 |\n| `widthName`       | string | width   | The name of the property which contains the width of each object.                                                             |\n| `newPositionName` | string | newX    | The name of the property in which the updated position will be stored.                                               |                                    \n\nFor example,\n\n```javascript\nconst data = [\n    {x: 4, width: 3},\n    {x: 5, width: 4},\n    {x: 2, width: 5},\n    {x: 9, width: 6},\n    {x: 5, width: 7}\n];\n\n\nconst repositionedData = repositionLineSegments(data);\n```\n\nreturns:\n\n```javascript\n[\n    {\n        \"x\": 2,\n        \"width\": 5,\n        \"newX\": 0\n    },\n    {\n        \"x\": 4,\n        \"width\": 3,\n        \"newX\": 4.999999999999999\n    },\n    {\n        \"x\": 5,\n        \"width\": 4,\n        \"newX\": 7.999999999999998\n    },\n    {\n        \"x\": 5,\n        \"width\": 7,\n        \"newX\": 12\n    },\n    {\n        \"x\": 9,\n        \"width\": 6,\n        \"newX\": 18.999999999999996\n    }\n]\n```\n\n\n\n## Importing\n\n### Using `script` tag\n\nYou can make `respacer` available in the global scope using a `\u003cscript\u003e` tag:\n\n```html\n\n\u003cscript src=\"../dist/bundle.js\"\u003e\u003c/script\u003e\n```\n\nYou can then use the `respacer.repositionPoints()` and `respacer.repositionLineSegments()` methods from within\nJavaScript on that page.\n\nThe files in [`examples/`](./examples) provides complete working examples.\n\nYou can generate this bundle file from the source code in this repository by running `npm install` and then `npm build`.\n\nAlternatively, you can use a copy of the file served by various CDNs, including:\n\n* [`https://cdn.jsdelivr.net/npm/respacer/dist/bundle.js`](https://cdn.jsdelivr.net/npm/respacer/dist/bundle.js)\n* [`https://unpkg.com/respacer/dist/bundle.js`](https://unpkg.com/respacer/dist/bundle.js)\n\n\n### As an ES6 module\n\nYou can import `respacer` as an ES6 module:\n\n```javascript\n\nimport {repositionPoints, repositionLineSegments} from 'respacer';\n\nconst data = [\n    {x: 4, label: \"A\"},\n    {x: 9, label: \"B\"},\n    {x: 2, label: \"C\"},\n    {x: 13, label: \"D\"},\n    {x: 9, label: \"E\"},\n    {x: 9.1, label: \"F\"}\n];\n\nconst repositionedData = repositionPoints(data);\n```\n\n\n### As a commonjs module\n\nYou can use `respacer` as a [CommonJS module](https://en.wikipedia.org/wiki/CommonJS):\n\n```javascript\n\nconst {repositionPoints, repositionLineSegments} = require('./respacer');\n\nconst data = [\n    {x: 4, label: \"A\"},\n    {x: 9, label: \"B\"},\n    {x: 2, label: \"C\"},\n    {x: 13, label: \"D\"},\n    {x: 9, label: \"E\"},\n    {x: 9.1, label: \"F\"}\n];\n\nconst repositionedData = repositionPoints(data);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesscottbrown%2Frespacer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamesscottbrown%2Frespacer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesscottbrown%2Frespacer/lists"}