-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotter.js
179 lines (171 loc) · 5.11 KB
/
plotter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
define(['plotly-js/plotly.min', 'd3'], function(Plotly,d3) {
'use strict';
return {
plotData: function(plotId, data) {
// extent returns array: [min, max]
var maxXs = Object.keys(data).map(function(key) {
return d3.extent(data[key].data, function(xy) { return xy[0]; })[1];
});
var maxYs = Object.keys(data).map(function(key) {
return d3.extent(data[key].data, function(xy) { return xy[1]; })[1];
});
var xdomain = d3.max(maxXs);
var ydomain = d3.max(maxYs);
var pdata = [];
var annotations = [];
var tzOffset = new Date().getTimezoneOffset();
// convert from minutes to milliseconds
tzOffset = tzOffset * 60000;
console.log(tzOffset);
var findAnnotations = function(x, y, floorAnn) {
var foundAnnotations = annotations.filter(function(ann) {
//console.log('comparing x,y point ('+x+', '+y+')');
//console.log(' to ann ('+ann.x+', '+ann.y+')');
if (floorAnn === undefined)
floorAnn = false;
if (floorAnn)
return Math.floor(ann.x) == x && ann.y == y;
return ann.x == x && ann.y == y;
});
return foundAnnotations;
};
Object.keys(data).map(function(key) {
if (data[key].annotations.length) {
data[key].annotations.map(function(ann) {
annotations.push({
x: ann.x,
y: ann.y,
xref: 'x',
yref: 'y',
text: ann.text,
showarrow: true,
arrowhead: 7,
ax: 0,
ay: -40
});
});
}
pdata.push({
x : data[key].data.map(function(xy) { return new Date(xy[0]); }),
y : data[key].data.map(function(xy) { return xy[1]; }),
mode: !data[key].annotations.length ? 'lines' : 'markers+lines',
type: 'scatter',
name: key,
marker: {
maxdisplayed: 1000,
size: !data[key].annotations.length ? [] : data[key].data.map(function(xy) {
if (findAnnotations(xy[0], xy[1]).length > 0)
return 15;
else
return 1;
}),
/*
color: "rgb(164, 194, 244)",
line: {
color: "white",
width: 0.5
}
*/
}
});
});
var layout = {
xaxis: {
title: 'Time (s)'
},
legend: {
xanchor: 'right'
},
//annotations: annotations,
margin: {
pad: 0,
l: 50,
r: 0,
b: 50,
t: 0
},
hovermode: 'closest',
autosize: true,
showlegend: true
};
var id = '#'+plotId;
Plotly.plot(plotId, pdata, layout, {
modeBarButtons: [[{
'name': 'toImage',
'title': 'Download plot as png',
'icon': Plotly.Icons.camera,
'click': function(gd) {
var format = 'png';
Plotly.downloadImage(gd, {
'format': format,
'width': $(id).width(),
'height': $(id).height(),
})
.then(function(filename) {
})
.catch(function() {
});
}
}],[
'zoom2d',
'pan2d',
'select2d',
'lasso2d',
'zoomIn2d',
'zoomOut2d',
'autoScale2d',
'resetScale2d',
'hoverClosestCartesian',
'hoverCompareCartesian'
]],
});
var myPlot = document.getElementById(plotId);
myPlot.on('plotly_click', function(data){
var point = data.points[0];
var foundAnnotations = findAnnotations(point.xaxis.d2l(point.x) + tzOffset,
point.yaxis.d2l(point.y),
true);
if (foundAnnotations.length) {
var yOffset = 0;
var yIncrement = 20;
foundAnnotations.map((foundAnn) => {
var newAnnotation = {
x: foundAnn.x,
y: foundAnn.y,
arrowhead: 6,
ax: 0,
ay: -80 - yOffset,
bgcolor: 'rgba(255, 255, 255, 0.9)',
//arrowcolor: point.fullData.marker.color,
font: {size:12},
//bordercolor: point.fullData.marker.color,
borderwidth: 3,
borderpad: 4,
text: foundAnn.text
},
divId = document.getElementById(plotId),
newIndex = (divId.layout.annotations || []).length;
// delete instead if clicked twice
if(newIndex) {
var foundCopy = false;
divId.layout.annotations.forEach(function(ann, sameIndex) {
if(ann.text === newAnnotation.text &&
ann.x == newAnnotation.x &&
ann.y == newAnnotation.y) {
Plotly.relayout(plotId, 'annotations[' + sameIndex + ']', 'remove');
foundCopy = true;
}
});
if(foundCopy) return;
}
yOffset += yIncrement;
Plotly.relayout(plotId, 'annotations[' + newIndex + ']', newAnnotation);
});
}
})
.on('plotly_clickannotation', function(event, data) {
Plotly.relayout(plotId, 'annotations[' + data.index + ']', 'remove');
});
}
};
});