|
8 | 8 |
|
9 | 9 | [](https://circleci.com/gh/plotly/angular-plotly.js)
|
10 | 10 | [](https://coveralls.io/github/plotly/angular-plotly.js?branch=master)
|
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Content |
| 15 | + |
| 16 | +* [Installation](#installation) |
| 17 | +* [Quick start](#quick-start) |
| 18 | +* [API](#api-reference) |
| 19 | + * [Basic props](#basic-props) |
| 20 | +* [Development](#development) |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +```bash |
| 25 | +$ npm install angular-plotly.js plotly.js |
| 26 | +``` |
| 27 | + |
| 28 | +Using the [angular CLI](https://cli.angular.io/) to start a new project |
| 29 | +```bash |
| 30 | +$ ng new my-project |
| 31 | +$ cd my-project |
| 32 | +$ npm install angular-plotly.js plotly.js --save |
| 33 | +``` |
| 34 | + |
| 35 | +## Quick start |
| 36 | + |
| 37 | +Add the `PlotlyModule` into the main app module of your project |
| 38 | +```typescript |
| 39 | +import { NgModule } from '@angular/core'; |
| 40 | +import { CommonModule } from '@angular/common'; |
| 41 | + |
| 42 | +import { PlotlyModule } from 'angular-plotly.js'; |
| 43 | + |
| 44 | +@NgModule({ |
| 45 | + imports: [CommonModule, PlotlyModule], |
| 46 | + ... |
| 47 | +}) |
| 48 | +export class AppModule { } |
| 49 | +``` |
| 50 | + |
| 51 | +Then use the `<plotly-plot>` component to display the graph |
| 52 | +```typescript |
| 53 | +import { Component } from '@angular/core'; |
| 54 | + |
| 55 | +@Component({ |
| 56 | + selector: 'plotly-example', |
| 57 | + template: '<plotly-plot [data]="graph.data" [layout]="graph.layout"></plotly-plot>', |
| 58 | +}) |
| 59 | +export class PlotlyExampleComponent { |
| 60 | + public graph = { |
| 61 | + data: [ |
| 62 | + { x: [1, 2, 3], y: [2, 6, 3], type: 'scatter', mode: 'lines+points', marker: {color: 'red'} }, |
| 63 | + { x: [1, 2, 3], y: [2, 5, 3], type: 'bar' }, |
| 64 | + ], |
| 65 | + layout: {width: 320, height: 240, title: 'A Fancy Plot'} |
| 66 | + }; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +You should see a plot like this: |
| 71 | + |
| 72 | +<p align="center"> |
| 73 | + <img src="example.png" alt="Example plot" width="320" height="240"> |
| 74 | +</p> |
| 75 | + |
| 76 | + |
| 77 | +For a full description of Plotly chart types and attributes see the following resources: |
| 78 | + |
| 79 | +* [Plotly JavaScript API documentation](https://plot.ly/javascript/) |
| 80 | +* [Full plotly.js attribute listing](https://plot.ly/javascript/reference/) |
| 81 | + |
| 82 | +## API Reference |
| 83 | + |
| 84 | +### Basic Props |
| 85 | + |
| 86 | +**Warning**: for the time being, this component may _mutate_ its `layout` and `data` props in response to user input, going against React rules. This behaviour will change in the near future once https://github.com/plotly/plotly.js/issues/2389 is completed. |
| 87 | + |
| 88 | +| Prop | Type | Default | Description | |
| 89 | +| ------------------ | ---------------------------- | ------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 90 | +| `data` | `Array` | `[]` | list of trace objects (see https://plot.ly/javascript/reference/) | |
| 91 | +| `layout` | `Object` | `undefined` | layout object (see https://plot.ly/javascript/reference/#layout) | |
| 92 | +| `frames` | `Array` | `undefined` | list of frame objects (see https://plot.ly/javascript/reference/) | |
| 93 | +| `config` | `Object` | `undefined` | config object (see https://plot.ly/javascript/configuration-options/) | |
| 94 | +| `revision` | `Number` | `undefined` | When provided, causes the plot to update _only_ when the revision is incremented. | |
| 95 | +| `onInitialized` | `Function(figure, graphDiv)` | `undefined` | Callback executed after plot is initialized. See below for parameter information. | |
| 96 | +| `onUpdate` | `Function(figure, graphDiv)` | `undefined` | Callback executed when when a plot is updated due to new data or layout, or when user interacts with a plot. See below for parameter information. | |
| 97 | +| `onPurge` | `Function(figure, graphDiv)` | `undefined` | Callback executed when component unmounts, before `Plotly.purge` strips the `graphDiv` of all private attributes. See below for parameter information. | |
| 98 | +| `onError` | `Function(err)` | `undefined` | Callback executed when a plotly.js API method rejects | |
| 99 | +| `divId` | `string` | `undefined` | id assigned to the `<div>` into which the plot is rendered. | |
| 100 | +| `className` | `string` | `undefined` | applied to the `<div>` into which the plot is rendered | |
| 101 | +| `style` | `Object` | `{position: 'relative', display: 'inline-block'}` | used to style the `<div>` into which the plot is rendered | |
| 102 | +| `debug` | `Boolean` | `false` | Assign the graph div to `window.gd` for debugging | |
| 103 | +| `useResizeHandler` | `Boolean` | `false` | When true, adds a call to `Plotly.Plot.resize()` as a `window.resize` event handler | |
| 104 | + |
| 105 | +**Note**: To make a plot responsive, i.e. to fill its containing element and resize when the window is resized, use `style` or `className` to set the dimensions of the element (i.e. using `width: 100%; height: 100%` or some similar values) and set `useResizeHandler` to `true` while setting `layout.autosize` to `true` and leaving `layout.height` and `layout.width` undefined. This will implement the behaviour documented here: https://plot.ly/javascript/responsive-fluid-layout/ |
| 106 | + |
| 107 | + |
| 108 | +## Development |
| 109 | + |
| 110 | +To get started: |
| 111 | + |
| 112 | +```bash |
| 113 | +$ npm install |
| 114 | +``` |
| 115 | + |
| 116 | +To see the demo app, run: |
| 117 | + |
| 118 | +```bash |
| 119 | +$ npm start |
| 120 | +``` |
| 121 | + |
| 122 | +To run the tests: |
| 123 | + |
| 124 | +```bash |
| 125 | +$ npm run test |
| 126 | +``` |
| 127 | + |
| 128 | +## License |
| 129 | + |
| 130 | +© 2018 Plotly, Inc. MIT License. |
0 commit comments