|
1 | 1 | # React-PlotlyJS-Typescript [](https://badge.fury.io/js/react-plotlyjs-ts)
|
2 | 2 |
|
3 |
| -Inspired by [React-PlotlyJS](https://github.com/benjeffery/react-plotlyjs), many thanks! |
4 |
| - |
5 | 3 | A react-typescript component for Plotly.JS graphs.
|
6 | 4 |
|
7 | 5 | Self-redraw when props changed.
|
8 | 6 |
|
9 | 7 | ## Usage
|
10 | 8 |
|
| 9 | +```bash |
| 10 | +$ npm install react react-dom typescript plotly.js |
| 11 | +$ npm install react-plotlyjs-ts |
| 12 | +``` |
| 13 | + |
11 | 14 | ```typescript
|
12 | 15 | import PlotlyChart from 'react-plotlyjs-ts';
|
13 | 16 |
|
14 | 17 | ...
|
15 | 18 | render(){
|
16 | 19 | const data = [
|
17 | 20 | {
|
18 |
| - type: 'scatter', // all "scatter" attributes: https://plot.ly/javascript/reference/#scatter |
19 |
| - x: [1, 2, 3], // more about "x": #scatter-x |
20 |
| - y: [6, 2, 3], // #scatter-y |
21 |
| - marker: { // marker is an object, valid marker keys: #scatter-marker |
22 |
| - color: 'rgb(16, 32, 77)' // more about "marker.color": #scatter-marker-color |
| 21 | + type: 'scatter', |
| 22 | + x: [1, 2, 3], |
| 23 | + y: [6, 2, 3], |
| 24 | + marker: { |
| 25 | + color: 'rgb(16, 32, 77)' |
23 | 26 | }
|
24 | 27 | },
|
25 | 28 | {
|
26 |
| - type: 'bar', // all "bar" chart attributes: #bar |
27 |
| - x: [1, 2, 3], // more about "x": #bar-x |
28 |
| - y: [6, 2, 3], // #bar-y |
29 |
| - name: 'bar chart example' // #bar-name |
| 29 | + type: 'bar', |
| 30 | + x: [1, 2, 3], |
| 31 | + y: [6, 2, 3], |
| 32 | + name: 'bar chart example' |
30 | 33 | }
|
31 | 34 | ];
|
32 |
| - const layout = { // all "layout" attributes: #layout |
33 |
| - title: 'simple example', // more about "layout.title": #layout-title |
34 |
| - xaxis: { // all "layout.xaxis" attributes: #layout-xaxis |
35 |
| - title: 'time' // more about "layout.xaxis.title": #layout-xaxis-title |
| 35 | + const layout = { |
| 36 | + title: 'simple example', |
| 37 | + xaxis: { |
| 38 | + title: 'time' |
36 | 39 | },
|
37 |
| - annotations: [ // all "annotation" attributes: #layout-annotations |
| 40 | + annotations: [ |
38 | 41 | {
|
39 |
| - text: 'simple annotation', // #layout-annotations-text |
40 |
| - x: 0, // #layout-annotations-x |
41 |
| - xref: 'paper', // #layout-annotations-xref |
42 |
| - y: 0, // #layout-annotations-y |
43 |
| - yref: 'paper' // #layout-annotations-yref |
| 42 | + text: 'simple annotation', |
| 43 | + x: 0, |
| 44 | + xref: 'paper', |
| 45 | + y: 0, |
| 46 | + yref: 'paper' |
44 | 47 | }
|
45 | 48 | ]
|
46 | 49 | };
|
47 | 50 | return (
|
48 |
| - <PlotlyChart data={data} layout={layout} /> |
49 |
| - ) |
| 51 | + <PlotlyChart data={toJS(this.model_data)} |
| 52 | + layout={layout} |
| 53 | + onClick={({points, event}) => console.log(points, event)}> ) |
50 | 54 | }
|
| 55 | +``` |
| 56 | + |
| 57 | +## Documentation |
| 58 | +Define PlotlyChart props below: |
| 59 | +```typescript |
| 60 | + data: any[]; |
| 61 | + layout?: any; |
| 62 | + config?: any; |
| 63 | + onClick?: (data: { points: any, event: any }) => any; |
| 64 | + onBeforeHover?: (data: { points: any, event: any }) => any; |
| 65 | + onHover?: (data: { points: any, event: any }) => any; |
| 66 | + onUnHover?: (data: { points: any, event: any }) => any; |
| 67 | + onSelected?: (data: { points: any, event: any }) => any; |
| 68 | +``` |
| 69 | +* data, layout, config are the same as in the [plotly.js](https://www.npmjs.com/package/plotly.js). |
| 70 | +* <b>onClick, onBeforeHover, onHover, onUnHover, onSelected</b> are on event functions. |
| 71 | +Use ES6 destructuring to get the points and event. |
| 72 | + |
51 | 73 |
|
| 74 | +## Todo |
| 75 | +Add testing |
52 | 76 |
|
53 |
| -``` |
| 77 | +## Thanks |
| 78 | +Inspired by [React-PlotlyJS](https://github.com/benjeffery/react-plotlyjs), many thanks! |
0 commit comments