Implementing my own highcharts wrapper
This commit is contained in:
parent
9ce4f68473
commit
d438520660
|
|
@ -12,7 +12,7 @@ class Highchart(object):
|
||||||
width=None,
|
width=None,
|
||||||
height=None,
|
height=None,
|
||||||
show_legend=True,
|
show_legend=True,
|
||||||
stockchart=True,
|
stockchart=False,
|
||||||
title=None,
|
title=None,
|
||||||
tooltip=None,
|
tooltip=None,
|
||||||
sort_columns=False,
|
sort_columns=False,
|
||||||
|
|
@ -41,6 +41,7 @@ class Highchart(object):
|
||||||
self.zoom = zoom
|
self.zoom = zoom
|
||||||
self.polar = polar
|
self.polar = polar
|
||||||
self.grid = grid
|
self.grid = grid
|
||||||
|
self.stacked = stacked
|
||||||
|
|
||||||
chart['chart'] = {}
|
chart['chart'] = {}
|
||||||
chart['chart']["type"] = chart_type
|
chart['chart']["type"] = chart_type
|
||||||
|
|
@ -56,8 +57,8 @@ class Highchart(object):
|
||||||
chart["legend"] = {
|
chart["legend"] = {
|
||||||
"enabled": show_legend
|
"enabled": show_legend
|
||||||
}
|
}
|
||||||
if title:
|
chart["title"] = {"text": title}
|
||||||
chart["title"] = {"text": title}
|
|
||||||
if tooltip:
|
if tooltip:
|
||||||
chart['tooltip'] = tooltip
|
chart['tooltip'] = tooltip
|
||||||
if self.zoom:
|
if self.zoom:
|
||||||
|
|
@ -89,7 +90,7 @@ class Highchart(object):
|
||||||
d['data'] = [v for k, v in d['data']]
|
d['data'] = [v for k, v in d['data']]
|
||||||
if self.compare:
|
if self.compare:
|
||||||
d['compare'] = self.compare # either `value` or `percent`
|
d['compare'] = self.compare # either `value` or `percent`
|
||||||
if self.chart_type in ("area", "bar") and self.stacked:
|
if self.chart_type in ("area", "column", "bar") and self.stacked:
|
||||||
d["stacking"] = 'normal'
|
d["stacking"] = 'normal'
|
||||||
#if kwargs.get("style"):
|
#if kwargs.get("style"):
|
||||||
# d["dashStyle"] = pd2hc_linestyle(kwargs["style"].get(name, "-"))
|
# d["dashStyle"] = pd2hc_linestyle(kwargs["style"].get(name, "-"))
|
||||||
|
|
@ -103,7 +104,8 @@ class Highchart(object):
|
||||||
if df.index.dtype.kind in "M":
|
if df.index.dtype.kind in "M":
|
||||||
x_axis["type"] = "datetime"
|
x_axis["type"] = "datetime"
|
||||||
if df.index.dtype.kind == 'O':
|
if df.index.dtype.kind == 'O':
|
||||||
chart['xAxis']['categories'] = sorted(list(df.index)) if self.sort_columns else list(df.index)
|
x_axis['categories'] = sorted(list(df.index)) if self.sort_columns else list(df.index)
|
||||||
|
print list(df.index)
|
||||||
if self.grid:
|
if self.grid:
|
||||||
x_axis["gridLineWidth"] = 1
|
x_axis["gridLineWidth"] = 1
|
||||||
x_axis["gridLineDashStyle"] = "Dot"
|
x_axis["gridLineDashStyle"] = "Dot"
|
||||||
|
|
@ -120,7 +122,7 @@ class Highchart(object):
|
||||||
if "xticks" in kwargs:
|
if "xticks" in kwargs:
|
||||||
x_axis["tickPositions"] = kwargs["xticks"]
|
x_axis["tickPositions"] = kwargs["xticks"]
|
||||||
'''
|
'''
|
||||||
self.x_axis = x_axis
|
self.chart['xAxis'] = x_axis
|
||||||
|
|
||||||
def serialize_yaxis(self):
|
def serialize_yaxis(self):
|
||||||
yAxis = {}
|
yAxis = {}
|
||||||
|
|
@ -152,5 +154,5 @@ class Highchart(object):
|
||||||
def javascript_cmd(self):
|
def javascript_cmd(self):
|
||||||
js = dumps(self.chart)
|
js = dumps(self.chart)
|
||||||
if self.stockchart:
|
if self.stockchart:
|
||||||
return "new Highcharts.StockChart({});".format(js)
|
return "new Highcharts.StockChart(%s);" % js
|
||||||
return "new Highcharts.Chart({});".format(js)
|
return "new Highcharts.Chart(%s);" %js
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
{% block tail %}
|
{% block tail %}
|
||||||
{{ super() }}
|
{{ super() }}
|
||||||
{% if viz.chart_type == "stock" %}
|
{% if viz.stockchart %}
|
||||||
<script src="{{ url_for("static", filename="highstock.js") }}"></script>
|
<script src="{{ url_for("static", filename="highstock.js") }}"></script>
|
||||||
{% else %}
|
{% else %}
|
||||||
<script src="{{ url_for("static", filename="highcharts.js") }}"></script>
|
<script src="{{ url_for("static", filename="highcharts.js") }}"></script>
|
||||||
|
|
|
||||||
25
app/viz.py
25
app/viz.py
|
|
@ -220,7 +220,7 @@ class HighchartsViz(BaseViz):
|
||||||
class TimeSeriesViz(HighchartsViz):
|
class TimeSeriesViz(HighchartsViz):
|
||||||
verbose_name = "Time Series - Line Chart"
|
verbose_name = "Time Series - Line Chart"
|
||||||
chart_type = "spline"
|
chart_type = "spline"
|
||||||
highstock = True
|
stockchart = True
|
||||||
|
|
||||||
def render(self):
|
def render(self):
|
||||||
metrics = self.metrics
|
metrics = self.metrics
|
||||||
|
|
@ -241,6 +241,7 @@ class TimeSeriesViz(HighchartsViz):
|
||||||
compare=self.compare,
|
compare=self.compare,
|
||||||
chart_type=self.chart_type,
|
chart_type=self.chart_type,
|
||||||
stacked=self.stacked,
|
stacked=self.stacked,
|
||||||
|
stockchart=self.stockchart,
|
||||||
**CHART_ARGS)
|
**CHART_ARGS)
|
||||||
return super(TimeSeriesViz, self).render(chart_js=chart.javascript_cmd)
|
return super(TimeSeriesViz, self).render(chart_js=chart.javascript_cmd)
|
||||||
|
|
||||||
|
|
@ -302,18 +303,18 @@ class TimeSeriesAreaViz(TimeSeriesViz):
|
||||||
|
|
||||||
class TimeSeriesBarViz(TimeSeriesViz):
|
class TimeSeriesBarViz(TimeSeriesViz):
|
||||||
verbose_name = "Time Series - Bar Chart"
|
verbose_name = "Time Series - Bar Chart"
|
||||||
chart_type = "bar"
|
chart_type = "column"
|
||||||
|
|
||||||
|
|
||||||
class TimeSeriesStackedBarViz(TimeSeriesViz):
|
class TimeSeriesStackedBarViz(TimeSeriesViz):
|
||||||
verbose_name = "Time Series - Stacked Bar Chart"
|
verbose_name = "Time Series - Stacked Bar Chart"
|
||||||
chart_type = "bar"
|
chart_type = "column"
|
||||||
stacked = True
|
stacked = True
|
||||||
|
|
||||||
|
|
||||||
class DistributionBarViz(HighchartsViz):
|
class DistributionBarViz(HighchartsViz):
|
||||||
verbose_name = "Distribution - Bar Chart"
|
verbose_name = "Distribution - Bar Chart"
|
||||||
chart_type = "bar"
|
chart_type = "column"
|
||||||
|
|
||||||
def query_obj(self):
|
def query_obj(self):
|
||||||
d = super(DistributionBarViz, self).query_obj()
|
d = super(DistributionBarViz, self).query_obj()
|
||||||
|
|
@ -326,14 +327,15 @@ class DistributionBarViz(HighchartsViz):
|
||||||
index=self.groupby,
|
index=self.groupby,
|
||||||
values=self.metrics)
|
values=self.metrics)
|
||||||
df = df.sort(self.metrics[0], ascending=False)
|
df = df.sort(self.metrics[0], ascending=False)
|
||||||
chart_js = serialize(
|
chart = Highchart(
|
||||||
df, kind=self.chart_kind, **CHART_ARGS)
|
df, chart_type=self.chart_type, **CHART_ARGS)
|
||||||
return super(DistributionBarViz, self).render(chart_js=chart_js)
|
return super(DistributionBarViz, self).render(
|
||||||
|
chart_js=chart.javascript_cmd)
|
||||||
|
|
||||||
|
|
||||||
class DistributionPieViz(HighchartsViz):
|
class DistributionPieViz(HighchartsViz):
|
||||||
verbose_name = "Distribution - Pie Chart"
|
verbose_name = "Distribution - Pie Chart"
|
||||||
chart_kind = "pie"
|
chart_type = "pie"
|
||||||
|
|
||||||
def query_obj(self):
|
def query_obj(self):
|
||||||
d = super(DistributionPieViz, self).query_obj()
|
d = super(DistributionPieViz, self).query_obj()
|
||||||
|
|
@ -346,9 +348,10 @@ class DistributionPieViz(HighchartsViz):
|
||||||
index=self.groupby,
|
index=self.groupby,
|
||||||
values=[self.metrics[0]])
|
values=[self.metrics[0]])
|
||||||
df = df.sort(self.metrics[0], ascending=False)
|
df = df.sort(self.metrics[0], ascending=False)
|
||||||
chart_js = serialize(
|
chart = Highchart(
|
||||||
df, kind=self.chart_kind, **CHART_ARGS)
|
df, chart_type=self.chart_type, **CHART_ARGS)
|
||||||
return super(DistributionPieViz, self).render(chart_js=chart_js)
|
return super(DistributionPieViz, self).render(
|
||||||
|
chart_js=chart.javascript_cmd)
|
||||||
|
|
||||||
viz_types = OrderedDict([
|
viz_types = OrderedDict([
|
||||||
['table', TableViz],
|
['table', TableViz],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue