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