|
| 1 | +# SysPlot |
| 2 | + |
| 3 | +A library that systematically generates a plane for plotting shapes, with a variety of algorithms to choose from. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +``` |
| 8 | +$ yarn add sysplot |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +SysPlot is used for generating XY coordinates for placing shapes. It is agnostic about the approach used for the visualisation, be it HTML, SVG, Canvas, WebGL.. etc. |
| 14 | + |
| 15 | +Positions will be given starting from the center and will attempt to fill as much |
| 16 | +space as possible. |
| 17 | + |
| 18 | +```js |
| 19 | +import SysPlot, { VogelSpiral /* any provided algorithm */ } from 'sysplot'; |
| 20 | + |
| 21 | +const sysPlot = new SysPlot(); |
| 22 | + |
| 23 | +sysPlot.setConfig({ |
| 24 | + algorithm: VogelSpiral, |
| 25 | + padding: 10, |
| 26 | + proportional: true, |
| 27 | + spread: 0.25, |
| 28 | +}); |
| 29 | + |
| 30 | +sysPlot.setBounds(1000, 500); |
| 31 | + |
| 32 | +const shapes = [{ |
| 33 | + radius: 20, |
| 34 | +}, { |
| 35 | + width: 30, |
| 36 | + height: 50, |
| 37 | +}] |
| 38 | + |
| 39 | +/** Give it the shapes to calculate the position for. */ |
| 40 | +sysPlot.setShape(shapes); |
| 41 | + |
| 42 | +/** Get the positions for those shapes */ |
| 43 | +const positions = sysPlot.getPositions(); |
| 44 | + |
| 45 | +/** Shapes that were unable to be placed will have no position */ |
| 46 | +const shapesWithValidPositions = zip(positions, shapes).filter(([, p]) => p); |
| 47 | +``` |
| 48 | + |
| 49 | +## Algorithms |
| 50 | + |
| 51 | +#### `ArchimedesSpiral` |
| 52 | + |
| 53 | +> The Archimedean spiral (also known as the arithmetic spiral) is a spiral named after the 3rd century BC Greek mathematician Archimedes. It is the locus of points corresponding to the locations over time of a point moving away from a fixed point with a constant speed along a line which rotates with constant angular velocity. |
| 54 | +> |
| 55 | +> [Wikipedia - Archimedes Spiral](https://en.wikipedia.org/wiki/Archimedean_spiral) |
| 56 | +
|
| 57 | +#### `ConcentricCircles` |
| 58 | + |
| 59 | +> In geometry, two or more objects are said to be concentric, coaxal, or coaxial when they share the same center or axis. |
| 60 | +> |
| 61 | +> [Wikipedia - Concentric Circles](https://en.wikipedia.org/wiki/Concentric_objects) |
| 62 | +
|
| 63 | +#### `FermatSpiral` |
| 64 | + |
| 65 | +> Fermat's spiral (also known as a [parabolic spiral](https://en.wikipedia.org/wiki/Parabola)) was first discovered by Pierre de Fermat. It is a type of Archimedean spiral |
| 66 | +> |
| 67 | +> [Wikipedia - Fermat Spiral](https://en.wikipedia.org/wiki/Fermat%27s_spiral) |
| 68 | +
|
| 69 | +#### `UlamSpiral` |
| 70 | + |
| 71 | +> The Ulam spiral or prime spiral (in other languages also called the Ulam cloth) is a graphical depiction of the set of [prime numbers](https://en.wikipedia.org/wiki/Prime_number)... It is constructed by writing the positive integers in a square spiral and specially marking the prime numbers. |
| 72 | +> |
| 73 | +> [Wikipedia - Ulam Spiral](https://en.wikipedia.org/wiki/Ulam_spiral) |
| 74 | +
|
| 75 | +#### `VogelSpiral` |
| 76 | + |
| 77 | +> In disc [phyllotaxis](https://en.wikipedia.org/wiki/Phyllotaxis), as in the sunflower and daisy, the mesh of spirals occurs in [Fibonacci numbers](https://en.wikipedia.org/wiki/Fibonacci_number) because divergence (angle of succession in a single spiral arrangement) approaches the [golden ratio](https://en.wikipedia.org/wiki/Golden_ratio). |
| 78 | +> |
| 79 | +> [Wikipedia - Vogel Spiral](https://en.wikipedia.org/wiki/Fermat%27s_spiral) |
| 80 | +
|
| 81 | +## API |
| 82 | + |
| 83 | +#### Config Object |
| 84 | + |
| 85 | +```js |
| 86 | +const config = { |
| 87 | + /** |
| 88 | + * One of the exported algorithm functions mentioned above. |
| 89 | + */ |
| 90 | + algorithm: Function, |
| 91 | + |
| 92 | + /** |
| 93 | + * The amount of padding to be used around the shapes when |
| 94 | + * positioning. |
| 95 | + */ |
| 96 | + padding: Number, |
| 97 | + |
| 98 | + /** |
| 99 | + * Retains the aspect ratio for plotting the vector points. |
| 100 | + */ |
| 101 | + proportional: Boolean, |
| 102 | + |
| 103 | + /** |
| 104 | + * A number between 0.1 and 1 that affects the density of the |
| 105 | + * vector points. 0.1 being very dense and 1 being very spread |
| 106 | + * apart. |
| 107 | + */ |
| 108 | + spread: Number, |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +#### SysPlot() |
| 113 | + |
| 114 | +The main class for storing the environment and generating vectors and positions. |
| 115 | + |
| 116 | +```js |
| 117 | +new SysPlot(); |
| 118 | +``` |
| 119 | + |
| 120 | +#### Sysplot.setConfig(configObject) |
| 121 | + |
| 122 | +Updates the config and sets a flag to regenerate the plotting vectors and/or the positions on next `getPositions()` call. |
| 123 | + |
| 124 | +```js |
| 125 | +sysPlot.setConfig(configObject); |
| 126 | +``` |
| 127 | + |
| 128 | +#### SysPlot.setBounds(width, height) |
| 129 | + |
| 130 | +Sets the bounds to generate the plotting vectors for. |
| 131 | + |
| 132 | +```js |
| 133 | +const width = 1000; |
| 134 | +const height = 500; |
| 135 | + |
| 136 | +sysPlot.setBounds(width, height); |
| 137 | +``` |
| 138 | + |
| 139 | +#### SysPlot.setShapes(shapes) |
| 140 | + |
| 141 | +Sets the shapes and sets a flag to regenerate positions on next `getPositions()` call. |
| 142 | + |
| 143 | +```js |
| 144 | +const shapes = [{ |
| 145 | + radius: 20, |
| 146 | +}, { |
| 147 | + width: 30, |
| 148 | + height: 50, |
| 149 | +}] |
| 150 | + |
| 151 | +sysPlot.setShapes(shapes); |
| 152 | +``` |
| 153 | + |
| 154 | +#### SysPlot.getPositions() |
| 155 | + |
| 156 | +Gets the positions for the set shapes. Returns XY coordinates in order of the shapes that were set. Any shapes they were unable to be positioned will be null. |
| 157 | + |
| 158 | +```js |
| 159 | +const shapes = [...]; |
| 160 | +const positions = sysPlot.getPositions(); |
| 161 | + |
| 162 | +const shapesWithValidPositions = zip(positions, shapes).filter(([, p]) => p); |
| 163 | +``` |
| 164 | + |
| 165 | + |
| 166 | +#### SysPlot.getVectors() |
| 167 | + |
| 168 | +Gets the vector points used for positioning (as XY coordinates) that the algorithm generated, radiating out from the center. |
| 169 | + |
| 170 | +```js |
| 171 | +sysPlot.getVectors(); // [[x, y], [x, y] ...etc] |
| 172 | +``` |
| 173 | + |
| 174 | +## Performance |
| 175 | + |
| 176 | +Generating the vector points and positioning the shapes can be intensive depending on the algorithm used, the number of shapes and the spread option. **SysPlot will only generate new vectors and positions if it needs to**, which is determined when certain parts of the config, bounds or shapes change. |
| 177 | + |
| 178 | +Use the `setBounds`, `setConfig` and `setShapes` methods when the environment changes. |
0 commit comments