|
1 |
| -# nodeplotlib |
| 1 | +# NodePlotLib |
2 | 2 |
|
3 |
| -This library was generated with [Nx](https://nx.dev). |
| 3 | +[](https://github.com/ngfelixl/nodeplotlib/actions?query=workflow%3A%22Node.js+CI%22) |
| 4 | +[](https://coveralls.io/github/ngfelixl/nodeplotlib?branch=master) |
| 5 | +[](https://npmjs.com/package/nodeplotlib) |
| 6 | +[](https://npmjs.com/package/nodeplotlib) |
| 7 | +[](https://github.com/prettier/prettier) |
| 8 | +[](https://gitter.im/nodeplotlib/) |
4 | 9 |
|
5 |
| -## Running unit tests |
| 10 | +Library to create top-notch plots directly within NodeJS on top of [plotly.js](https://plot.ly/javascript/) |
| 11 | +without any front-end preparations. Inspired by matplotlib. |
6 | 12 |
|
7 |
| -Run `nx test nodeplotlib` to execute the unit tests via [Jest](https://jestjs.io). |
| 13 | +[](https://raw.githubusercontent.com/ngfelixl/nodeplotlib/master/img/animation.gif) |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```sh |
| 18 | +npm install nodeplotlib |
| 19 | +# or |
| 20 | +yarn add nodeplotlib |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Overview |
| 26 | + |
| 27 | +Use with TypeScript/JavaScript: |
| 28 | + |
| 29 | +```typescript |
| 30 | +import { plot, Plot } from 'nodeplotlib'; |
| 31 | +const data: Plot[] = [{x: [1, 3, 4, 5], y: [3, 12, 1, 4], type: 'line'}]; |
| 32 | +plot(data); |
| 33 | +``` |
| 34 | + |
| 35 | +If ES5 use `require()` instead of `import`. Here is a short animation about howto and the results. |
| 36 | + |
| 37 | +### Detailed usage |
| 38 | + |
| 39 | +Since Python provides with matplotlib a library for spawning plot windows, NodeJS isn't by default. |
| 40 | +But there are awesome plotting libraries for usage in front-end. So this lib targets people like |
| 41 | +scientists who easily want to create beautiful plots in a time-saving way. |
| 42 | + |
| 43 | +The library provides a simple interface with (for now) just three functions. A `plot`, `stack` and a |
| 44 | +`clear` function. The `plot()` functions spawns a plot to the browser, if a plotdata is given as an |
| 45 | +argument. Otherwise it plots all the `stack()`ed plotdata. The arguments are of type `Plot`, which is a |
| 46 | +partial of plotly's `PlotData` type. With the `clear()` function the stack container can be cleared. |
| 47 | + |
| 48 | +With the stack function the user is able to print multiple charts on one page. |
| 49 | + |
| 50 | +```typescript |
| 51 | +import { plot, stack, clear, Plot } from 'nodeplotlib'; |
| 52 | + |
| 53 | +const data: Plot[] = [{ |
| 54 | + x: [ 1, 3, 4, 6, 7], |
| 55 | + y: [ 2, 4, 6, 8, 9], |
| 56 | + type: 'scatter' |
| 57 | +}]; |
| 58 | + |
| 59 | +stack(data); |
| 60 | +stack(data); |
| 61 | +stack(data); |
| 62 | +plot(); |
| 63 | +``` |
| 64 | + |
| 65 | +The plot function plots all stacked plots and the plot given by parameter (if there is one). |
| 66 | +Afterwards the temporary container gets cleared and you can call `stack()` and `plot()` again |
| 67 | +without any predefined plots. |
| 68 | + |
| 69 | +The functions are of the form: |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { plot, stack, clear, Plot, Layout } from 'nodeplotlib'; |
| 73 | + |
| 74 | +plot(data?: Plot[], layout?: Layout): void; |
| 75 | +stack(data: Plot[], layout?: Layout): void; |
| 76 | +clear(): void; |
| 77 | +``` |
| 78 | + |
| 79 | +## Quick start |
| 80 | + |
| 81 | +In this section there are some examples to getting started. See the full plotly |
| 82 | +[cheatsheet](https://images.plot.ly/plotly-documentation/images/plotly_js_cheat_sheet.pdf?_ga=2.2676214.711017137.1550402185-1513144731.1549064935). |
| 83 | + |
| 84 | +#### Line Plots |
| 85 | + |
| 86 | +```typescript |
| 87 | +const trace1: Plot = {x: [1, 2], y: [1, 2], type: 'scatter'}; |
| 88 | +const trace2: Plot = {x: [3, 4], y: [9, 16], type: 'scatter'}; |
| 89 | +plot([trace1, trace2]); |
| 90 | +``` |
| 91 | + |
| 92 | +#### Bar Charts |
| 93 | + |
| 94 | +```typescript |
| 95 | +const trace: Plot = {x: [1, 2], y: [1, 2], type: 'bar'}; |
| 96 | +plot([trace]); |
| 97 | +``` |
| 98 | + |
| 99 | +#### 3D Line Plots |
| 100 | + |
| 101 | +```typescript |
| 102 | +const trace: Plot = {x: [9, 8, 5, 1], y: [1, 2, 4, 8], z: [11, 8, 15, 3], type: 'scatter3d'}; |
| 103 | +plot([trace]); |
| 104 | +``` |
| 105 | + |
| 106 | +#### 3D Surface Plots |
| 107 | + |
| 108 | +```typescript |
| 109 | +const trace: Plot = {colorscale: 'Viridis', z: [[3, 5, 7, 9], [21, 13, 8, 5]]}; |
| 110 | +plot([trace]); |
| 111 | +``` |
| 112 | + |
| 113 | +#### Radial Plots |
| 114 | + |
| 115 | +In order to style the plot, one is able to pass in the `layout` parameter, which internally |
| 116 | +is typeof `Partial<Layout>` from plotly's `Layout`. See the full layout documentation |
| 117 | +[here](https://plot.ly/javascript/#layout-options). |
| 118 | + |
| 119 | +With this parameter one is able to define styles like *title*, *axis labels*, |
| 120 | +*subplots* and many more. |
| 121 | + |
| 122 | +```typescript |
| 123 | +const data: Plot[] = [{ |
| 124 | + type: 'scatterpolar', |
| 125 | + r: [1.5, 10, 39, 31, 15, 1.5], |
| 126 | + theta: ['A','B','C', 'D', 'E', 'A'], |
| 127 | + fill: 'toself', |
| 128 | + name: 'Group B' |
| 129 | +}]; |
| 130 | + |
| 131 | +const layout: Layout = { |
| 132 | + polar: { |
| 133 | + radialaxis: { |
| 134 | + visible: true, |
| 135 | + range: [0, 50] |
| 136 | + } |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +plot(data, layout); |
| 141 | +``` |
| 142 | + |
| 143 | +## Plot types |
| 144 | + |
| 145 | +| Simple charts | Advanced charts | 3D Plots | |
| 146 | +| -------------------------- | --------------------------- | ------------------ | |
| 147 | +| Scatter | 2d density plots | Scatter | |
| 148 | +| Line | Histograms | Surface | |
| 149 | +| Bar | Box-plots | Lines | |
| 150 | +| Pie charts | Contour plots | | |
| 151 | +| Sankey diagrams | Heatmaps | | |
| 152 | +| Tables | Radar charts | | |
| 153 | + |
| 154 | +## Behind the scenes |
| 155 | + |
| 156 | +The lib launches a webserver and opens new tabs for every plot located at |
| 157 | +`http://localhost:8080/plots/:id`. At this address a temporary html template |
| 158 | +file, the nodeplotlib script and plotly.min.js are available. The client side |
| 159 | +js requests the plot data at `http://localhost:8080/data/:id`. After all |
| 160 | +pending plots are opened in a unique tab and all the data is requested, the |
| 161 | +server shuts down. If you fire another plot the server starts again provides |
| 162 | +your plot and shuts down automatically. |
| 163 | + |
| 164 | +Another port can be provided via PORT environment variable. |
| 165 | + |
| 166 | +## Contributing |
| 167 | + |
| 168 | +Contributions in all forms are welcome. |
| 169 | + |
| 170 | +## Developers guide |
| 171 | + |
| 172 | +Fork the [Github repository](https://github.com/ngfelixl/nodeplotlib) and clone |
| 173 | +it to your PC. Install the npm dependencies using the `install` command. It installs |
| 174 | +the dependencies and copies plotly types to project source. These won't affect |
| 175 | +the git tree. |
| 176 | + |
| 177 | +## Roadmap |
| 178 | + |
| 179 | +It would be nice to make the library compatible with Observable-streams and update |
| 180 | +the plots in real-time. |
| 181 | + |
| 182 | +## Contributors |
| 183 | + |
| 184 | +<a href="https://github.com/ngfelixl"><img src="https://avatars2.githubusercontent.com/u/24190530" title="ngfelixl" width="100" height="100"></a> |
| 185 | +<a href="https://github.com/mitsos1os"><img src="https://avatars3.githubusercontent.com/u/8208733" title="mitsos1os" width="100" height="100"></a> |
| 186 | +<a href="https://github.com/medved-nsk"><img src="https://avatars1.githubusercontent.com/u/6310906" title="medved-nsk" width="100" height="100"></a> |
| 187 | +<a href="https://github.com/Moumouls"><img src="https://avatars.githubusercontent.com/u/27959372" title="Moumouls" width="100" height="100"></a> |
| 188 | +<a href="https://github.com/grgr-dkrk"><img src="https://avatars.githubusercontent.com/u/40130327" title="guruguru-dekiruko" width="100" height="100"></a> |
0 commit comments