UNPKG

3.49 kBMarkdownView Raw
1## Plotly.js
2
3The current integration of [plotly.js](https://plot.ly/javascript/) is fairly raw, and you may encounter problems with autolayout/sizing as well as other features I haven't tested or fixed.
4
5### Hello World
6
7Here is the [Hello World](https://plot.ly/javascript/getting-started/#hello-world-example) example.
8
9This example enables the user to enter an alternate Title, which is associated with the variable `PLOT_TITLE`. Smartdown's Plotly integration is still in its initial stages, with the plot title being adjustable by changing the `PLOT_TITLE` variable. Eventually, it will be possible to have plots respect arbitrary smartdown variable data, instead of just the special `PLOT_TITLE` variables.
10
11---
12
13[Title](:?PLOT_TITLE)
14
15---
16
17```plotly/autoplay/playable
18var layout = {
19 title: 'Default Title',
20 autosize: true,
21 width: 500,
22 height: 300,
23 margin: {
24 t: 100, b: 0, l: 0, r: 0
25 }
26};
27
28Plotly.newPlot( this.div, [{
29 x: [1, 2, 3, 4, 5],
30 y: [1, 2, 4, 8, 16] }], layout,
31 {displayModeBar: false} );
32
33```
34
35
36### 3D Surface Plots
37
38From [3D Surface Plots](https://plot.ly/javascript/3d-surface-plots/)
39
40
41```plotly/playable
42
43var myDiv = this.div;
44Plotly.d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv', function(err, rows){
45 function unpack(rows, key) {
46 return rows.map(function(row) { return row[key]; });
47 }
48
49 var z_data=[ ]
50 for(i=0;i<24;i++)
51 {
52 z_data.push(unpack(rows,i));
53 }
54
55 var data = [{
56 z: z_data,
57 type: 'surface'
58 }];
59
60 var layout = {
61 title: 'Mt Bruno Elevation',
62 autosize: true,
63 width: 500,
64 height: 300,
65 margin: {
66 t: 100, b: 0, l: 0, r: 0
67 }
68 };
69 Plotly.newPlot(myDiv, data, layout, {displayModeBar: false});
70});
71
72
73```
74
75
76
77
78### Simple Contour Plot
79
80```plotly/playable
81var myDiv = this.div;
82
83var size = 100, x = new Array(size), y = new Array(size), z = new Array(size), i, j;
84
85for(var i = 0; i < size; i++) {
86 x[i] = y[i] = -2 * Math.PI + 4 * Math.PI * i / size;
87 z[i] = new Array(size);
88}
89
90for(var i = 0; i < size; i++) {
91 for(j = 0; j < size; j++) {
92 var r2 = x[i]*x[i] + y[j]*y[j];
93 z[i][j] = Math.sin(x[i]) * Math.cos(y[j]) * Math.sin(r2) / Math.log(r2+1);
94 }
95}
96
97var data = [ {
98 z: z,
99 x: x,
100 y: y,
101 type: 'contour'
102 }
103];
104
105 var layout = {
106 title: 'Simple Contour Plot',
107 autosize: true,
108 width: 500,
109 height: 300,
110 margin: {
111 t: 100, b: 0, l: 0, r: 0
112 }
113 };
114
115Plotly.newPlot(myDiv, data, layout, {displayModeBar: false});
116```
117
118
119
120### Maps
121
122From [Chloropleth Map](https://plot.ly/javascript/choropleth-maps)
123
124
125```plotly/autoplay
126
127var myDiv = this.div;
128Plotly.d3.csv(
129 'https://raw.githubusercontent.com/plotly/datasets/master/2010_alcohol_consumption_by_country.csv',
130 function(err, rows) {
131 function unpack(rows, key) {
132 return rows.map(function(row) { return row[key]; });
133 }
134
135 var data = [{
136 type: 'choropleth',
137 locationmode: 'country names',
138 locations: unpack(rows, 'location'),
139 z: unpack(rows, 'alcohol'),
140 text: unpack(rows, 'location'),
141 autocolorscale: true
142 }];
143
144 var layout = {
145 autosize: true,
146 title: 'Pure alcohol consumption among adults (age 15+) in 2010',
147 geo: {
148 projection: {
149 type: 'robinson'
150 }
151 }
152 };
153
154 Plotly.newPlot(myDiv, data, layout, {showLink: false, displayModeBar: false});
155 });
156
157```
158
159---
160
161[Back to Home](:@Home)
162