Skip to content

Commit 6456782

Browse files
othalanksolan
authored andcommitted
JSONP multisource support (#42)
* Add multiple JSONP data sources, assuming all data comes from JSONP * Add array version of data definition for improving the jinja2 template * Fix bug in Highchart add jsonp url * Apply multi-source JSONP code to highcharts * Update inline comments for clarity and accuracy * Update variable name to 'data_list' as per request for style consistency
1 parent d59f9f8 commit 6456782

File tree

6 files changed

+99
-60
lines changed

6 files changed

+99
-60
lines changed

highcharts/highcharts/highchart_types.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -623,22 +623,23 @@ def __init__(self, data, series_type="line", supress_errors=False, **kwargs):
623623

624624
# List of dictionaries. Each dict contains data and properties,
625625
# which need to handle before construct the object for series
626-
for item in data:
627-
if isinstance(item, dict):
628-
for k, v in item.items():
629-
if k in DATA_SERIES_ALLOWED_OPTIONS:
630-
if SeriesOptions.__validate_options__(k,v,DATA_SERIES_ALLOWED_OPTIONS[k]):
631-
if isinstance(DATA_SERIES_ALLOWED_OPTIONS[k], tuple):
632-
if isinstance(v, dict):
633-
item.update({k:DATA_SERIES_ALLOWED_OPTIONS[k][0](**v)})
634-
elif isinstance(v, CommonObject) or isinstance(v, ArrayObject) or \
635-
isinstance(v, CSSObject) or isinstance(v, SVGObject) or isinstance(v, ColorObject) or \
636-
isinstance(v, JSfunction) or isinstance(v, Formatter) or isinstance(v, datetime.datetime):
637-
item.update({k:v})
626+
if isinstance(data, list):
627+
for item in data:
628+
if isinstance(item, dict):
629+
for k, v in item.items():
630+
if k in DATA_SERIES_ALLOWED_OPTIONS:
631+
if SeriesOptions.__validate_options__(k,v,DATA_SERIES_ALLOWED_OPTIONS[k]):
632+
if isinstance(DATA_SERIES_ALLOWED_OPTIONS[k], tuple):
633+
if isinstance(v, dict):
634+
item.update({k:DATA_SERIES_ALLOWED_OPTIONS[k][0](**v)})
635+
elif isinstance(v, CommonObject) or isinstance(v, ArrayObject) or \
636+
isinstance(v, CSSObject) or isinstance(v, SVGObject) or isinstance(v, ColorObject) or \
637+
isinstance(v, JSfunction) or isinstance(v, Formatter) or isinstance(v, datetime.datetime):
638+
item.update({k:v})
639+
else:
640+
item.update({k:DATA_SERIES_ALLOWED_OPTIONS[k][0](v)})
638641
else:
639-
item.update({k:DATA_SERIES_ALLOWED_OPTIONS[k][0](v)})
640-
else:
641-
item.update({k:v})
642+
item.update({k:v})
642643

643644
self.__dict__.update({
644645
"data": data,

highcharts/highcharts/highcharts.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ def __init__(self, **kwargs):
8787
self.data_temp = []
8888
# Data from jsonp
8989
self.jsonp_data_flag = False
90+
self.jsonp_data_url_list = [] # DEM 2017/07/27: List of JSON data sources
9091

9192
# set drilldown data
9293
self.drilldown_data = []
9394
self.drilldown_data_temp = []
94-
95+
9596
# javascript
9697
self.jscript_head_flag = False
9798
self.jscript_head = kwargs.get('jscript_head', None)
@@ -220,10 +221,10 @@ def add_drilldown_data_set(self, data, series_type, id, **kwargs):
220221
self.drilldown_data_set_count += 1
221222
if self.drilldown_flag == False:
222223
self.drilldown_flag = True
223-
224+
224225
kwargs.update({'id':id})
225226
series_data = Series(data, series_type=series_type, **kwargs)
226-
227+
227228
series_data.__options__().update(SeriesOptions(series_type=series_type, **kwargs).__options__())
228229
self.drilldown_data_temp.append(series_data)
229230

@@ -233,12 +234,17 @@ def add_data_from_jsonp(self, data_src, data_name='json_data', series_type="line
233234
the data_src is the https link for data
234235
and it must be in jsonp format
235236
"""
236-
self.jsonp_data_flag = True
237-
self.jsonp_data_url = json.dumps(data_src)
238-
if data_name == 'data':
239-
data_name = 'json_'+ data_name
240-
self.jsonp_data = data_name
237+
if not self.jsonp_data_flag:
238+
self.jsonp_data_flag = True
239+
240+
if data_name == 'data':
241+
data_name = 'json_'+ data_name
242+
243+
self.jsonp_data = data_name
241244
self.add_data_set(RawJavaScriptText(data_name), series_type, name=name, **kwargs)
245+
# DEM 2017/07/27: Append new JSON data source to a list instead of
246+
# replacing whatever already exists
247+
self.jsonp_data_url_list.append(json.dumps(data_src))
242248

243249

244250
def add_JSscript(self, js_script, js_loc):
@@ -305,9 +311,14 @@ def buildcontent(self):
305311

306312
self.buildcontainer()
307313
self.option = json.dumps(self.options, cls = HighchartsEncoder)
308-
self.setoption = json.dumps(self.setOptions, cls = HighchartsEncoder)
314+
self.setoption = json.dumps(self.setOptions, cls = HighchartsEncoder)
309315
self.data = json.dumps(self.data_temp, cls = HighchartsEncoder)
310-
316+
317+
# DEM 2017/04/25: Make 'data' available as an array
318+
# ... this permits jinja2 array access to each data definition
319+
# ... which is useful for looping over multiple data sources
320+
self.data_list = [json.dumps(x, cls = HighchartsEncoder) for x in self.data_temp]
321+
311322
if self.drilldown_flag:
312323
self.drilldown_data = json.dumps(self.drilldown_data_temp, cls = HighchartsEncoder)
313324
self._htmlcontent = self.template_content_highcharts.render(chart=self).encode('utf-8')

highcharts/highcharts/templates/content.html

+24-16
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,48 @@
55

66
{% block body_head %}
77

8-
{% if chart.jsonp_data_flag %}
9-
$.getJSON({{chart.jsonp_data_url}}, function ({{chart.jsonp_data}})
10-
{
11-
{% endif %}
8+
{% if chart.jscript_head_flag %}
9+
{{chart.jscript_head}}
10+
{% endif %}
1211

1312
{% endblock body_head %}
1413

1514
{% block body_content %}
1615

17-
{% if chart.jscript_head_flag %}
18-
{{chart.jscript_head}}
19-
{% endif %}
20-
2116
Highcharts.setOptions({{chart.setoption}});
2217
var option = {{chart.option}};
2318

2419
{% if chart.mapdata_flag %}
2520
var geojson = {{chart.mapdata}}
2621
{% endif %}
2722

28-
var data = {{chart.data}};
29-
option.series = data;
30-
3123
{% if chart.drilldown_flag %}
3224
var drilldowndata = {{chart.drilldown_data}};
3325
option.drilldown.series = drilldowndata;
3426
{% endif %}
3527

3628
var chart = new Highcharts.Chart(option);
3729

30+
{# DEM 2017/07/27: Use a list of JSONP data sources
31+
{# DEM 2017/07/27: This implementation is limited and could easily be improved! #}
32+
{% if chart.jsonp_data_flag %}
33+
{% for data_url in chart.jsonp_data_url_list %}
34+
35+
$.getJSON({{data_url}}, function ({{chart.jsonp_data}})
36+
{
37+
chart.addSeries({{chart.data_list[loop.index0]}});
38+
});
39+
40+
{% endfor %}
41+
{% else %}
42+
var data = {{chart.data}};
43+
var dataLen = data.length;
44+
for (var ix = 0; ix < dataLen; ix++) {
45+
chart.addSeries(data[ix]);
46+
}
47+
{% endif %}
48+
49+
3850
{% if chart.jscript_end_flag %}
3951
{{chart.jscript_end}}
4052
{% endif %}
@@ -73,8 +85,4 @@
7385

7486
{% block body_end %}
7587

76-
{% if chart.jsonp_data_flag %}
77-
});
78-
{% endif %}
79-
80-
{% endblock body_end %}
88+
{% endblock body_end %}

highcharts/highstock/highstock.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__(self, **kwargs):
8383

8484
# Data from jsonp
8585
self.jsonp_data_flag = False
86+
self.jsonp_data_url_list = [] # DEM 2017/04/25: List of JSON data sources
8687

8788
# javascript
8889
self.jscript_head_flag = False
@@ -206,13 +207,15 @@ def add_data_from_jsonp(self, data_src, data_name='json_data', series_type="line
206207
"""
207208
if not self.jsonp_data_flag:
208209
self.jsonp_data_flag = True
209-
self.jsonp_data_url = json.dumps(data_src)
210210

211211
if data_name == 'data':
212212
data_name = 'json_'+ data_name
213213

214214
self.jsonp_data = data_name
215215
self.add_data_set(RawJavaScriptText(self.jsonp_data), series_type, name=name, **kwargs)
216+
# DEM 2017/04/25: Append new JSON data source to a list instead of
217+
# replacing whatever already exists
218+
self.jsonp_data_url_list.append(json.dumps(data_src))
216219

217220

218221
def add_navi_series(self, data, series_type="line", **kwargs):
@@ -294,6 +297,11 @@ def buildcontent(self):
294297
self.option = json.dumps(self.options, cls = HighchartsEncoder)
295298
self.setoption = json.dumps(self.setOptions, cls = HighchartsEncoder)
296299
self.data = json.dumps(self.data_temp, cls = HighchartsEncoder)
300+
301+
# DEM 2017/04/25: Make 'data' available as an array
302+
# ... this permits jinja2 array access to each data definition
303+
# ... which is useful for looping over multiple data sources
304+
self.data_list = [json.dumps(x, cls = HighchartsEncoder) for x in self.data_temp]
297305

298306
if self.navi_seri_flag:
299307
self.navi_seri = json.dumps(self.navi_seri_temp, cls = HighchartsEncoder)

highcharts/highstock/templates/content.html

+28-18
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,52 @@
55

66
{% block body_head %}
77

8-
{% if chart.jsonp_data_flag %}
9-
$.getJSON({{chart.jsonp_data_url}}, function ({{chart.jsonp_data}})
10-
{
11-
{% endif %}
8+
{% if chart.jscript_head_flag %}
9+
{{chart.jscript_head}}
10+
{% endif %}
1211

1312
{% endblock body_head %}
1413

1514
{% block body_content %}
1615

17-
{% if chart.jscript_head_flag %}
18-
{{chart.jscript_head}}
19-
{% endif %}
20-
2116
Highcharts.setOptions({{chart.setoption}});
2217
var option = {{chart.option}};
2318

24-
var data = {{chart.data}};
25-
option.series = data;
26-
2719
{% if chart.navi_seri_flag %}
2820
var navi_data = {{chart.navi_seri}}
2921
option.navigator.series = navi_data;
3022
{% endif %}
3123

3224
var chart = new Highcharts.StockChart(option);
3325

34-
{% if chart.jscript_end_flag %}
35-
{{chart.jscript_end}}
36-
{% endif %}
26+
27+
{# DEM 2017/04/25: Use a list of JSONP data sources
28+
{# DEM 2017/07/27: This implementation is limited and could easily be improved! #}
29+
{% if chart.jsonp_data_flag %}
30+
{% for data_url in chart.jsonp_data_url_list %}
31+
32+
$.getJSON({{data_url}}, function ({{chart.jsonp_data}})
33+
{
34+
chart.addSeries({{chart.data_list[loop.index0]}});
35+
});
36+
37+
{% endfor %}
38+
{% else %}
39+
40+
var data = {{chart.data}};
41+
var dataLen = data.length;
42+
for (var ix = 0; ix < dataLen; ix++) {
43+
chart.addSeries(data[ix]);
44+
}
45+
46+
{% endif %}
3747

3848
{% endblock body_content %}
3949

4050
{% block body_end %}
4151

42-
{% if chart.jsonp_data_flag %}
43-
});
44-
{% endif %}
52+
{% if chart.jscript_end_flag %}
53+
{{chart.jscript_end}}
54+
{% endif %}
4555

46-
{% endblock body_end %}
56+
{% endblock body_end %}

highcharts/highstock/templates/page.html

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
</head>
99
<body style="margin:0;padding:0">
1010
{{ chart.content }}
11+
{{ chart.other }}
1112
</body>
1213
</html>

0 commit comments

Comments
 (0)