-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnc_html_scatter.py
More file actions
325 lines (252 loc) · 10.2 KB
/
gnc_html_scatter.py
File metadata and controls
325 lines (252 loc) · 10.2 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import random
import xml.etree.ElementTree as ET
import pdb
import traceback
import gnc_jqplot
from gnc_html_document import HtmlDoc
from gnc_html_document import HtmlDocument
# this is a re-coding in python of the scheme version
# in html-scatter.scm
# this is not a subclass of HtmlDocument
# - we need to allow for multiple scatter plots in one HTML document
class HtmlScatter(object):
def __init__ (self,width=None,height=None,title=None,subtitle=None,x_axis_label=None,y_axis_label=None,data=None,
marker=None,marker_color=None):
self.width = None
self.height = None
self.title = None
self.subtitle = None
self.x_axis_label = None
self.y_axis_label = None
self.data = []
self.marker = None
self.markercolor = None
def add_datapoint (self, newpoint):
self.data.append(newpoint)
def render (self):
pdb.set_trace()
title = self.title
subtitle = self.subtitle
x_label = self.x_axis_label
y_label = self.y_axis_label
data = self.data
marker = self.marker
markercolor = "#" + self.markercolor
# Use a unique chart-id for each chart. This prevents chart
# clashed on multi-column reports
chart_id = "chart-%d"%random.randint(1,999999)
if len(self.data) > 0:
# cant figure the good way of doing this
# ah - looks like we can nest div elements
# we probably need to count each scatter plot
bodydiv = ET.Element('div', attrib={'id' : 'scatter1'})
bodydiv.text = "\n"
bodydiv.append(gnc_jqplot.html_js_include("jqplot/jquery.min.js"))
bodydiv.append(gnc_jqplot.html_js_include("jqplot/jquery.jqplot.js"))
bodydiv.append(gnc_jqplot.html_css_include("jqplot/jquery.jqplot.css"))
stylstr = "width:"+str(self.width)+"px;height:"+str(self.height)+"px;"
divelm = ET.SubElement(bodydiv,'div',attrib={'id' : chart_id, 'style' : stylstr})
divelm.text = "\n"
divelm.tail = "\n"
screlm = ET.Element('script',attrib={'id' : "source"})
bodydiv.append(screlm)
txtlst = ["$(function () {\nvar data = [];var series = [];\n"]
for x_data,y_data in self.data:
txtlst.append(" data.push([")
txtlst.append(str(x_data))
txtlst.append(", ")
txtlst.append(str(y_data))
txtlst.append("]);\n")
txtlst.append("var series = [];\n")
txtlst.append("""var options = {
legend: { show: false, },
seriesDefaults: {
markerOptions: {
style: '""")
txtlst.append(marker)
txtlst.append("""',
color: '""")
txtlst.append(markercolor)
txtlst.append("""', },
},
series: series,
axesDefaults: {
},
axes: {
xaxis: {
},
yaxis: {
autoscale: true,
},
},
};\n""")
if title:
txtlst.append(' options.title = "')
txtlst.append(title)
txtlst.append('";\n')
if subtitle:
txtlst.append(' options.title += " (')
txtlst.append(subtitle)
txtlst.append(')";\n')
if x_label:
txtlst.append(' options.axes.xaxis.label = "')
txtlst.append(x_label)
txtlst.append('";\n')
if y_label:
txtlst.append(' options.axes.yaxis.label = "')
txtlst.append(y_label)
txtlst.append('";\n')
txtlst.append("$.jqplot.config.enablePlugins = true;\n")
txtlst.append("var plot = $.jqplot('"+chart_id+"', [data], options);\n")
txtlst.append("});\n")
screlm.text = "".join(txtlst)
stdelm = ET.Element('p')
bodydiv.append(stdelm)
return bodydiv
else:
#(gnc:warn "Scatter chart has no non-zero data")
# we need to return a non-null ET document
#bodydiv = ET.Element('div', attrib={'id' : 'scatter1'})
#bodydiv.text = "\n"
#stdelm = ET.Element('p')
#bodydiv.append(stdelm)
bodydiv = ET.Element('p')
return bodydiv
# and a new version which will do multiple lines in a plot
class HtmlMultiScatter(object):
def __init__ (self,width=None,height=None,title=None,subtitle=None,x_axis_label=None,y_axis_label=None,data=None,
marker=None,marker_color=None):
self.width = None
self.height = None
self.title = None
self.subtitle = None
self.x_axis_label = None
self.y_axis_label = None
self.data = []
self.marker = []
self.markercolor = []
self.labels = []
# lets not use this - we will prepare the data (a list of lists) and pass it once
# this only works for a single line
#def add_datapoint (self, newpoint):
# self.data.append(newpoint)
# what we will do is use an add function to add each lines data block
def add_data (self, newlist, marker, marker_color, label=None):
self.data.append(newlist)
self.marker.append(marker)
self.markercolor.append(marker_color)
self.labels.append(label)
def render (self):
#pdb.set_trace()
title = self.title
subtitle = self.subtitle
x_label = self.x_axis_label
y_label = self.y_axis_label
# Use a unique chart-id for each chart. This prevents chart
# clashed on multi-column reports
chart_id = "chart-%d"%random.randint(1,999999)
if len(self.data) > 0:
# cant figure the good way of doing this
# ah - looks like we can nest div elements
# we probably need to count each scatter plot
bodydiv = ET.Element('div', attrib={'id' : 'scatter1'})
bodydiv.text = "\n"
bodydiv.append(gnc_jqplot.html_js_include("jqplot/jquery.min.js"))
bodydiv.append(gnc_jqplot.html_js_include("jqplot/jquery.jqplot.js"))
bodydiv.append(gnc_jqplot.html_css_include("jqplot/jquery.jqplot.css"))
stylstr = "width:"+str(self.width)+"px;height:"+str(self.height)+"px;"
divelm = ET.SubElement(bodydiv,'div',attrib={'id' : chart_id, 'style' : stylstr})
divelm.text = "\n"
divelm.tail = "\n"
screlm = ET.Element('script',attrib={'id' : "source"})
bodydiv.append(screlm)
txtlst = ["$(function () {\nvar data = [];\n"]
for idata,data_block in enumerate(self.data):
txtlst.append(" var data_%d = [];\n"%idata)
for x_data,y_data in data_block:
txtlst.append(" data_%d.push(["%idata)
txtlst.append(str(x_data))
txtlst.append(", ")
txtlst.append(str(y_data))
txtlst.append("]);\n")
txtlst.append("\n data.push(data_%d);\n\n"%idata)
txtlst.append("var series = [];")
txtlst.append("""
series = [""")
for idata,marker in enumerate(self.marker):
markercolor = "#" + self.markercolor[idata]
txtlst.append("""
{
markerOptions: {
style: '""")
txtlst.append(marker)
txtlst.append("""',
color: '""")
txtlst.append(markercolor)
txtlst.append("""',
},
label: '""")
if self.labels[idata] != None:
txtlst.append(self.labels[idata])
else:
txtlst.append("line %d"%(idata+1))
txtlst.append("""',
},""")
txtlst.append("""
]\n""")
#txtlst.append("""var options = {
# legend: { show: false, },
# seriesDefaults: {
# markerOptions: {
# style: '""")
#txtlst.append(marker)
#txtlst.append("""',
# color: '""")
#txtlst.append(markercolor)
#txtlst.append("""', },
# },
txtlst.append("""var options = {
legend: {
show: true,
placement: "outsideGrid",
},
seriesDefaults: {
showMarker: false
},
series: series,
axesDefaults: {
},
axes: {
xaxis: {
},
yaxis: {
autoscale: true,
},
},
};\n""")
if title:
txtlst.append(' options.title = "')
txtlst.append(title)
txtlst.append('";\n')
if subtitle:
txtlst.append(' options.title += " (')
txtlst.append(subtitle)
txtlst.append(')";\n')
if x_label:
txtlst.append(' options.axes.xaxis.label = "')
txtlst.append(x_label)
txtlst.append('";\n')
if y_label:
txtlst.append(' options.axes.yaxis.label = "')
txtlst.append(y_label)
txtlst.append('";\n')
txtlst.append("$.jqplot.config.enablePlugins = true;\n")
txtlst.append("var plot = $.jqplot('"+chart_id+"', data, options);\n")
txtlst.append("});\n")
screlm.text = "".join(txtlst)
stdelm = ET.Element('p')
bodydiv.append(stdelm)
return bodydiv
else:
#(gnc:warn "Scatter chart has no non-zero data")
pass