diff --git a/echarts/__init__.py b/echarts/__init__.py index 4d1683b..4f71669 100644 --- a/echarts/__init__.py +++ b/echarts/__init__.py @@ -11,6 +11,7 @@ """ import os +import sys import json import logging import tempfile @@ -25,7 +26,19 @@ class Echart(Base): - def __init__(self, title, description=None, axis=True, **kwargs): + validOptions = ['title', 'legend', 'grid', 'xAxis', 'yAxis', 'polar', + 'radiusAxis', 'angleAxis', 'radar', 'dataZoom', 'visualMap', 'tooltip', + 'axisPointer', 'toolbox', 'brush', 'geo', 'parallel', 'parallelAxis', + 'singleAxis', 'timeline', 'graphic', 'calendar', 'series', 'color', + 'backgroundColor', 'textStyle', 'animation', 'animationThreshold', + 'animationDuration', 'animationEasing', 'animationDelay', + 'animationDurationUpdate', 'animationEasingUpdate', + 'animationDelayUpdate', 'progressive', 'progressiveThreshold', + 'blendMode', 'hoverLayerThreshold', 'useUTC'] + + def __init__(self, title, js_path=None, description=None, axis=True, + **kwargs): + self.js_path = js_path self.title = { 'text': title, 'subtext': description, @@ -59,6 +72,8 @@ def use(self, obj): self.toolbox = obj elif isinstance(obj, VisualMap): self.visualMap = obj + elif isinstance(obj, dict): + self.others = obj return self @@ -86,14 +101,25 @@ def json(self): json['toolbox'] = self.toolbox.json if hasattr(self, 'visualMap'): json['visualMap'] = self.visualMap.json + if hasattr(self, 'others'): + for key in self.others.keys(): + if key in self.validOptions: + json[key] = self.others[key] + else: + sys.stderr('Invalid Option: %s' % key) + exit(-1) json.update(self.kwargs) return json def _html(self): + echarts_js_path = 'https://cdnjs.cloudflare.com/ajax/libs/echarts/3.5.4/echarts.min.js' + if self.js_path: + echarts_js_path = self.js_path with open(os.path.join(os.path.dirname(__file__), 'plot.j2')) as f: template = f.read() - return template.replace('{{ opt }}', json.dumps(self.json, indent=4)) + return template.replace('{{ opt }}', json.dumps(self.json, indent=4) + ).replace('{{echarts_js_path}}', echarts_js_path) def plot(self, persist=True): """ @@ -101,12 +127,13 @@ def plot(self, persist=True): :param persist: persist output html to disk """ - with tempfile.NamedTemporaryFile(suffix='.html', delete=not persist) as fobj: + with tempfile.NamedTemporaryFile(suffix='.html', + delete=not persist) as fobj: fobj.write(self._html()) fobj.flush() webbrowser.open('file://' + os.path.realpath(fobj.name)) persist or raw_input('Press enter for continue') - + def save(self, path, name): """ Save html file into project dir @@ -115,5 +142,5 @@ def save(self, path, name): """ if not os.path.exists(path): os.makedirs(path) - with open(path+str(name)+".html", "w") as html_file: - html_file.write(self._html()) + with open(path + str(name) + ".html", "w") as html_file: + html_file.write(self._html()) \ No newline at end of file diff --git a/echarts/plot.j2 b/echarts/plot.j2 index b6edc3e..f0a9599 100644 --- a/echarts/plot.j2 +++ b/echarts/plot.j2 @@ -4,7 +4,7 @@ ECharts - +