-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCharts.py
More file actions
338 lines (290 loc) · 13.5 KB
/
Charts.py
File metadata and controls
338 lines (290 loc) · 13.5 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from JavaScriptD3.CodeSnippets import CodeSnippets
from JavaScriptD3.CodeSnippets import process_margins
from JavaScriptD3.CodeSnippets import process_grid_lines
from JavaScriptD3.CodeSnippets import wrap_it
from JavaScriptD3.Plots import is_list_of_dicts
from JavaScriptD3.Plots import is_list_of_numeric_arrays
import json
import numpy
import pandas
from IPython.display import clear_output, display, HTML, Javascript
# ============================================================
# BarChart
# ============================================================
def js_d3_bar_chart(data,
background="white",
color="steelblue",
width=600,
height=400,
title='',
x_axis_label='',
y_axis_label='',
grid_lines=False,
margins=None,
legends=None,
fmt="jupyter"):
"""
Bar chart
-------------------------------
:param data: Data to charted.
:param background: Background color.
:param color: Color of the points.
:param width: Width of the plot.
:param height: Height of the plot.
:param title: Plot title.
:param x_axis_label: X-axis label.
:param y_axis_label: Y-axis label.
:param grid_lines: Grid lines spec. If True automatic grid lines spec made.
:param margins: Margins spec: an integer or dictionary with keys "top", "bottom", "left", "right".
:param legends: Should legends be placed or not?
:param fmt: Format, one of "html", "jupyter", or "script".
:return: JavaScript code or HTML code.
"""
# Type checking and coercion
dataLocal = data
if isinstance(dataLocal, numpy.ndarray):
if len(dataLocal.shape) == 1:
dataLocal = [{"variable": k, "value": dataLocal[k]} for k in range(0, dataLocal.shape[0])]
elif len(dataLocal.shape) == 2:
dataLocal = [{"variable": dataLocal[k, 0], "value": dataLocal[k, 1]} for k in range(0, dataLocal.shape[0])]
if not is_list_of_dicts(dataLocal):
raise TypeError("The first argument is expected to be coercible to numpy.ndarray or a list of dictionaries.")
# Convert to JSON data
jsData = json.dumps(dataLocal, default=str)
# Grid lines
gridLinesLocal = process_grid_lines(grid_lines)
# Process margins
marginsLocal = process_margins(margins)
# Groups
hasGroups = True
for d in dataLocal:
if "group" not in d.keys():
hasGroups = False
break
# Code snippets object
cs = CodeSnippets()
# Select code fragment to splice in
if hasGroups:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_multi_bar_chart_part())
else:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_bar_chart_part())
# Chose to add legend code fragment or not
maxGroupChars = len("all")
if hasGroups:
maxGroupChars = max([len(x) for x in [r["group"] for r in dataLocal]])
if isinstance(legends, bool) and legends or legends is None and hasGroups:
marginsLocal["right"] = max(marginsLocal["right"], (maxGroupChars + 4) * 12)
jsChartMiddle = jsChartMiddle + "\n" + cs.get_legend_code().replace('return o.group;', "return o.variable;")
# Stencil
jsChart = cs.get_plot_margins_and_labels_code(fmt) + "\n" + jsChartMiddle
# Concrete parameters
res = (jsChart
.replace('$DATA', jsData)
.replace('$BACKGROUND_COLOR', '"' + background + '"')
.replace('$FILL_COLOR', '"' + color + '"')
.replace('$WIDTH', str(width))
.replace('$HEIGHT', str(height))
.replace('$TITLE', '"' + title + '"')
.replace('$X_AXIS_LABEL', '"' + x_axis_label + '"')
.replace('$Y_AXIS_LABEL', '"' + y_axis_label + '"')
.replace('$MARGINS', json.dumps(marginsLocal))
.replace('$LEGEND_X_POS', 'width + 3*12')
.replace('$LEGEND_Y_POS', '0')
.replace('$LEGEND_Y_GAP', '25'))
return wrap_it(code=res, fmt=fmt)
# ============================================================
# Histogram
# ============================================================
# The Histogram and BarChart codes have to be refactored.
# (After Histogram for multi-datasets is implemented.)
def js_d3_histogram(data,
background="white",
color="steelblue",
width=600,
height=400,
title='',
x_axis_label='',
y_axis_label='',
grid_lines=False,
margins=None,
legends=None,
fmt="jupyter"):
"""
Histogram
-------------------------------
:param data: Data to charted.
:param background: Background color.
:param color: Color of the points.
:param width: Width of the plot.
:param height: Height of the plot.
:param title: Plot title.
:param x_axis_label: X-axis label.
:param y_axis_label: Y-axis label.
:param grid_lines: Grid lines spec. If True automatic grid lines spec made.
:param margins: Margins spec: an integer or dictionary with keys "top", "bottom", "left", "right".
:param legends: Should legends be placed or not?
:param fmt: Format, one of "html", "jupyter", or "script".
:return: JavaScript code or HTML code.
"""
# Type checking and coercion
dataLocal = data
if isinstance(dataLocal, numpy.ndarray):
if len(dataLocal.shape) == 1:
dataLocal = list(dataLocal.data)
elif len(dataLocal.shape) > 1:
dataLocal = None
if not isinstance(dataLocal, list):
raise TypeError("The first argument is expected to be coercible to 1D numpy.ndarray.")
# Convert to JSON data
jsData = json.dumps(dataLocal, default=str)
# Grid lines
gridLinesLocal = process_grid_lines(grid_lines)
# Process margins
marginsLocal = process_margins(margins)
# Groups
hasGroups = False
# Code snippets object
cs = CodeSnippets()
# Select code fragment to splice in
if hasGroups:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_histogram_part())
else:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_histogram_part())
# Chose to add legend code fragment or not
maxGroupChars = len("all")
if hasGroups:
maxGroupChars = max([len(x) for x in [r["group"] for r in dataLocal]])
if isinstance(legends, bool) and legends or legends is None and hasGroups:
marginsLocal["right"] = max(marginsLocal["right"], (maxGroupChars + 4) * 12)
jsChartMiddle = jsChartMiddle + "\n" + cs.get_legend_code().replace('return o.group;', "return o.variable;")
# Stencil
jsChart = cs.get_plot_margins_and_labels_code(fmt) + "\n" + jsChartMiddle
# Concrete parameters
res = (jsChart
.replace('$DATA', jsData)
.replace('$BACKGROUND_COLOR', '"' + background + '"')
.replace('$FILL_COLOR', '"' + color + '"')
.replace('$WIDTH', str(width))
.replace('$HEIGHT', str(height))
.replace('$TITLE', '"' + title + '"')
.replace('$X_AXIS_LABEL', '"' + x_axis_label + '"')
.replace('$Y_AXIS_LABEL', '"' + y_axis_label + '"')
.replace('$MARGINS', json.dumps(marginsLocal))
.replace('$LEGEND_X_POS', 'width + 3*12')
.replace('$LEGEND_Y_POS', '0')
.replace('$LEGEND_Y_GAP', '25'))
return wrap_it(code=res, fmt=fmt)
# ============================================================
# BubbleChart
# ============================================================
def js_d3_bubble_chart(data,
background="white",
color="steelblue",
opacity=0.7,
width=600,
height=400,
title='',
x_axis_label='',
y_axis_label='',
grid_lines=False,
margins=None,
legends=None,
tooltip=None,
fmt="jupyter"):
"""
Bubble chart
-------------------------------
:param data: Data to charted.
:param background: Background color.
:param color: Color of the bubbles. (Single dataset.)
:param opacity: Opacity of the bubbles.
:param width: Width of the plot.
:param height: Height of the plot.
:param title: Plot title.
:param x_axis_label: X-axis label.
:param y_axis_label: Y-axis label.
:param grid_lines: Grid lines spec. If True automatic grid lines spec made.
:param margins: Margins spec: an integer or dictionary with keys "top", "bottom", "left", "right".
:param legends: Should legends be placed or not?
:param tooltip: Should tooltips be placed or not?
:param fmt: Format, one of "html", "jupyter", or "script".
:return: JavaScript code or HTML code.
"""
# Type checking and coercion
dataLocal = data
if is_list_of_numeric_arrays(obj=dataLocal, length=3) or is_list_of_numeric_arrays(obj=dataLocal, length=2):
dataLocal = numpy.asarray(dataLocal)
if isinstance(dataLocal, pandas.core.frame.DataFrame):
dataLocal = list(dataLocal.transpose().to_dict().values())
if isinstance(dataLocal, numpy.ndarray):
if len(dataLocal.shape) == 1:
dataLocal = [{"x": k, "y": dataLocal[k], "z": 1} for k in range(0, dataLocal.shape[0])]
elif len(dataLocal.shape) == 2 and dataLocal.shape[1] == 2:
dataLocal = [{"x": dataLocal[k, 0], "y": dataLocal[k, 1], "z": 1} for k in range(0, dataLocal.shape[0])]
elif len(dataLocal.shape) == 2 and dataLocal.shape[1] == 3:
dataLocal = [{"x": dataLocal[k, 0], "y": dataLocal[k, 1], "z": dataLocal[k, 2]} for k in
range(0, dataLocal.shape[0])]
if not is_list_of_dicts(dataLocal):
raise TypeError("The first argument is expected to be coercible to numpy.ndarray or a list of dictionaries.")
# Convert to JSON data
jsData = json.dumps(dataLocal, default=str)
# Grid lines
gridLinesLocal = process_grid_lines(grid_lines)
# Process margins
marginsLocal = process_margins(margins)
# Groups
hasGroups = True
for d in dataLocal:
if "group" not in d.keys():
hasGroups = False
break
# Code snippets object
cs = CodeSnippets()
# Tooltip
tooltipLocal = hasGroups and tooltip is None or isinstance(tooltip, bool) and tooltip
# Select code fragment to splice in
if hasGroups and not tooltipLocal:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_multi_bubble_chart_part())
elif tooltipLocal:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_tooltip_multi_bubble_chart_part())
else:
jsChartMiddle = cs.get_plot_data_scales_and_axes_code(n_x_ticks=gridLinesLocal[0],
n_y_ticks=gridLinesLocal[1],
code_fragment=cs.get_bubble_chart_part())
# Chose to add legend code fragment or not
maxGroupChars = len("all")
if hasGroups:
maxGroupChars = max([len(str(x)) for x in [r["group"] for r in dataLocal]])
if isinstance(legends, bool) and legends or legends is None and hasGroups:
marginsLocal["right"] = max(marginsLocal["right"], (maxGroupChars + 4) * 12)
jsChartMiddle = jsChartMiddle + "\n" + cs.get_legend_code()
# Stencil
jsChart = cs.get_plot_preparation_code(fmt, gridLinesLocal[0], gridLinesLocal[1]) + "\n" + jsChartMiddle
# Concrete parameters
res = (jsChart
.replace('$DATA', jsData)
.replace('$BACKGROUND_COLOR', '"' + background + '"')
.replace('$FILL_COLOR', '"' + color + '"')
.replace('$OPACITY', '"' + str(opacity) + '"')
.replace('$WIDTH', str(width))
.replace('$HEIGHT', str(height))
.replace('$TITLE', '"' + title + '"')
.replace('$X_AXIS_LABEL', '"' + x_axis_label + '"')
.replace('$Y_AXIS_LABEL', '"' + y_axis_label + '"')
.replace('$MARGINS', json.dumps(marginsLocal))
.replace('$LEGEND_X_POS', 'width + 3*12')
.replace('$LEGEND_Y_POS', '0')
.replace('$LEGEND_Y_GAP', '25'))
return wrap_it(code=res, fmt=fmt)