Skip to content

Commit

Permalink
[line] growth vs factor option for 'Period Ratio' (apache#970)
Browse files Browse the repository at this point in the history
* [line] growth vs factor option for 'Period Ratio'

* i18n
  • Loading branch information
mistercrunch authored Aug 17, 2016
1 parent 379cf6c commit 84213ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions caravel/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,18 @@ def __init__(self, viz):
"[integer] Number of period to compare against, "
"this is relative to the granularity selected")
}),
'period_ratio_type': (SelectField, {
"label": _("Period Ratio Type"),
"default": 'growth',
"choices": (
('factor', _('factor')),
('growth', _('growth')),
('value', _('value')),
),
"description": _(
"`factor` means (new/previous), `growth` is "
"((new/previous) - 1), `value` is (new-previous)")
}),
'time_compare': (TextField, {
"label": _("Time Shift"),
"default": "",
Expand Down
11 changes: 9 additions & 2 deletions caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ class NVD3TimeSeriesViz(NVD3Viz):
'fields': (
('rolling_type', 'rolling_periods'),
'time_compare',
'num_period_compare',
('num_period_compare', 'period_ratio_type'),
None,
('resample_how', 'resample_rule',), 'resample_fillmethod'
),
Expand Down Expand Up @@ -1038,7 +1038,14 @@ def get_df(self, query_obj=None):
num_period_compare = form_data.get("num_period_compare")
if num_period_compare:
num_period_compare = int(num_period_compare)
df = (df / df.shift(num_period_compare)) - 1
prt = form_data.get('period_ratio_type')
if prt and prt == 'growth':
df = (df / df.shift(num_period_compare)) - 1
elif prt and prt == 'value':
df = df - df.shift(num_period_compare)
else:
df = df / df.shift(num_period_compare)

df = df[num_period_compare:]

rolling_periods = form_data.get("rolling_periods")
Expand Down

0 comments on commit 84213ab

Please sign in to comment.