forked from dinkelk/PyDyGraphs
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpydygraphs.py
executable file
·246 lines (201 loc) · 7.88 KB
/
pydygraphs.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/python
from IPython.display import HTML
from IPython.display import display
import string
import pandas as pd
__PYDYGRAPH__FIGURE__NUMBER__ = 0
__PYDYGRAPH__FIGURE__JSON__ = []
__PYDYGRAPH__DYGRAPHS_LIB_STRING__ = "http://dygraphs.com/dygraph-combined.js"
class __figure__:
def __init__(self):
global __PYDYGRAPH__FIGURE__NUMBER__
self._fignum = __PYDYGRAPH__FIGURE__NUMBER__
self._divname = "Figure" + str(self._fignum)
# Set configurable variable to none:
self._color = None
self._title = None
self._xlabel = None
self._ylabel = None
#print "Making figure", self._fignum
__PYDYGRAPH__FIGURE__NUMBER__ += 1
__PYDYGRAPH__FIGURE__JSON__.append("")
def xlabel(self, xlabel):
self._xlabel = xlabel
def ylabel(self, ylabel):
self._ylabel = ylabel
def title(self, title):
self._title = title
def plotDataFrame(self, dataframe, xaxis, color=None, rangeselector=False, logscale=False, showroller=True):
self._jsondata = dataframe.to_json()
self._x_axis = xaxis
self._rangeselector = rangeselector
self._logscale = logscale
self._showroller = showroller
if color:
if isinstance(color, basestring):
self._color = [color]
else:
self._color = color
__PYDYGRAPH__FIGURE__JSON__[self._fignum] = self._jsondata
def plot(self, x, y=[], ylabels=[], color=None, rangeselector=False, logscale=False, showroller=True):
if not isinstance(y,list):
y=[y]
labelizer = lambda a: (map(lambda x: 'Y' + str(x), a))
if not ylabels:
ylabels = labelizer(range(len(y)))
if len(ylabels) != len(y):
ylabels.extend(labelizer(range(len(y) - len(ylabels))))
# Form dataframe:
xlabel = "_x_axis_label_"
table = {}
table[xlabel] = x
for label,data in zip(ylabels,y):
table[label] = data
dataframe = pd.DataFrame(table)
# Return plot:
return self.plotDataFrame(dataframe, xaxis=xlabel, color=color, rangeselector=rangeselector, logscale=logscale, showroller=showroller)
def show(self):
javascript = self.generateJS()
display(HTML(javascript))
def printJS(self):
print self.generateJS()
def generateJS(self):
dygraphs = ""
dygraphs += """
<script type="text/javascript">
function convertToDataTable_""" + self._divname + """(d) {
var columns = _.keys(d);
var x_col = '""" + self._x_axis +"""';
columns.splice(columns.indexOf(x_col), 1); // Get index column. (prob index). Don't need to do this just to plot all
var out = [];
var i = 0;
for (var k in d[x_col]) {
var row = [d[x_col][k]];
columns.forEach(function(col) {
row.push(d[col][k]);
});
out.push(row);
}
return {data:out, labels:[x_col].concat(columns)};
}
function handle_output_""" + self._divname + """(out) {
var json = out.content.data['text/plain'];
var data = JSON.parse(eval(json));
var tabular = convertToDataTable_""" + self._divname + """(data);
"""
dygraphs += """
g = new Dygraph(document.getElementById('""" + self._divname + """'), tabular.data, {
legend: 'always',
labels: tabular.labels,
labelsDivStyles: { 'textAlign': 'right' },
rollPeriod: 1,
showRoller: true,
animatedZooms: true,
"""
if self._showroller:
dygraphs+= """
showRoller: true,
"""
else:
dygraphs+= """
showRoller: false,
"""
if self._color:
dygraphs+= """
colors: ["""+string.join(['"'+c+'"' for c in self._color],',')+"""],
"""
if self._title:
dygraphs+= """
title: '""" + self._title + """',"""
if self._xlabel:
dygraphs+= """
xlabel: '""" + self._xlabel + """',
"""
if self._ylabel:
dygraphs+= """
ylabel: '""" + self._ylabel + """',
"""
if self._rangeselector:
dygraphs += """
showRangeSelector: true,
rangeSelectorHeight: 50,
"""
if self._logscale:
dygraphs+="""
logscale: true,
"""
dygraphs+="""
labelsDiv: '"""+self._divname+"""_legend',
errorBars: false
})
}
var kernel = IPython.notebook.kernel;
var callbacks_""" + self._divname + """ = { 'iopub' : {'output' : handle_output_""" + self._divname + """}};
kernel.execute("pydygraphs.__PYDYGRAPH__FIGURE__JSON__[""" + str(self._fignum) + """]", callbacks_""" + self._divname + """, {silent:false});
</script>
"""
return dygraphs
def __create_table_for_pydygraph_figure__(divname, width, height):
return """
<script src=\"""" + str(__PYDYGRAPH__DYGRAPHS_LIB_STRING__) + """\"></script>
<table style="width: """ + str(width) + """px; border-style: hidden;">
<tr><td style="border-style: hidden;"><div id='"""+str(divname)+"""' style="width: """ + str(width) + """px; height: """ + str(height) + """px;"></div></td></tr>
<tr><td style="border-style: hidden;"><div style="text-align:right; width: """ + str(width) + """px; height: auto;"; id='"""+str(divname)+"""_legend'></div></td></tr>
</table>
"""
def figure(width=1050, height=400):
''' This public function returns a pydygraph figure that holds a single plot '''
# Create figure:
fig = __figure__()
# Create javascript for figure:
javascript = __create_table_for_pydygraph_figure__(fig._divname, width, height)
# Display the figure:
display(HTML(javascript))
# Return the handle:
return fig
def subplot(v=1, h=1, width=1050, height=400, title=None):
''' This public function returns a list of pydygraph figures. Each table cell holds a single plot '''
figureWidth = width/h
figureHeight = height/v
javascript = """<script src=\"""" + str(__PYDYGRAPH__DYGRAPHS_LIB_STRING__) + """\"></script>
<table style="width: """ + str(width) + """px; border-style: hidden;">"""
# Generate optional subplot title:
if title:
javascript += """
<tr>
<th COLSPAN='"""+str(h)+"""'>
<h1 align="center">"""+title+"""</h1>
</th>
<tr>"""
# Generate subplot table:
figs = []
for i in xrange(v):
javascript += "<tr>"
temp = []
for j in xrange(h):
fig = __figure__()
javascript += """<td style="border-style: hidden;">"""
javascript += __create_table_for_pydygraph_figure__(fig._divname, figureWidth, figureHeight)
javascript += "</td>"
temp.append(fig)
javascript += "</tr>"
figs.append(temp)
javascript += "</table>"
# Display the subplot table:
display(HTML(javascript))
# Return all the figure handles:
return figs
# Set a different remote or local dygraphs library than the default
def useDygraphsLib(dygraphslib):
global __PYDYGRAPH__DYGRAPHS_LIB_STRING__
__PYDYGRAPH__DYGRAPHS_LIB_STRING__ = dygraphslib
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='This utility does all of the awesome.')
# parser.add_argument('config', metavar='configfile', type=str, help='a SBPP packet definition file to ingest')
# parser.add_argument('fname', metavar='binaryfile', type=str, help='a SBPP binary file to ingest')
# args = parser.parse_args()
a = figure()
b = figure()
c = figure()
d = subfigure(2,2)