Skip to content

Commit

Permalink
Implementing my own highcharts wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jul 23, 2015
1 parent 9ce4f68 commit d438520
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
18 changes: 10 additions & 8 deletions app/highchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(
width=None,
height=None,
show_legend=True,
stockchart=True,
stockchart=False,
title=None,
tooltip=None,
sort_columns=False,
Expand Down Expand Up @@ -41,6 +41,7 @@ def __init__(
self.zoom = zoom
self.polar = polar
self.grid = grid
self.stacked = stacked

chart['chart'] = {}
chart['chart']["type"] = chart_type
Expand All @@ -56,8 +57,8 @@ def __init__(
chart["legend"] = {
"enabled": show_legend
}
if title:
chart["title"] = {"text": title}
chart["title"] = {"text": title}

if tooltip:
chart['tooltip'] = tooltip
if self.zoom:
Expand Down Expand Up @@ -89,7 +90,7 @@ def serialize_series(self):
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, "-"))
Expand All @@ -103,7 +104,8 @@ def serialize_xaxis(self):
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"
Expand All @@ -120,7 +122,7 @@ def serialize_xaxis(self):
if "xticks" in kwargs:
x_axis["tickPositions"] = kwargs["xticks"]
'''
self.x_axis = x_axis
self.chart['xAxis'] = x_axis

def serialize_yaxis(self):
yAxis = {}
Expand Down Expand Up @@ -152,5 +154,5 @@ def serialize_yaxis(self):
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
2 changes: 1 addition & 1 deletion app/templates/panoramix/viz_highcharts.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
25 changes: 14 additions & 11 deletions app/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -241,6 +241,7 @@ def render(self):
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)

Expand Down Expand Up @@ -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()
Expand All @@ -326,14 +327,15 @@ def render(self):
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()
Expand All @@ -346,9 +348,10 @@ def render(self):
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],
Expand Down

0 comments on commit d438520

Please sign in to comment.