Skip to content

Commit

Permalink
Adding contribution to total
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 28, 2015
1 parent 4cd7c3d commit 7143972
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ List of TODO items for Panoramix
* in/notin filters autocomplete

## First Class Line Charts
* Contribution to total
* Contribution to total (added to line chart already)
* Time comparison
* Time ratios
* Use legend shapes that match line markers (useful?)
* Hover line highlighting
* More colors! the Airbnb palette is currently pretty limited
* Line types (dash, dotted)

## New Features
Expand Down
3 changes: 3 additions & 0 deletions panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def form_factory(viz):
'donut': BooleanField(
"Donut", default=False,
description="Do you want a donut or a pie?"),
'contribution': BooleanField(
"Contribution", default=False,
description="Compute the contribution to the total"),
}
field_css_classes = {k: ['form-control'] for k in px_form_fields.keys()}
select2 = [
Expand Down
9 changes: 7 additions & 2 deletions panoramix/templates/panoramix/viz_nvd3.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
.tickFormat(function (d) {return tickMultiFormat(new Date(d)); });
chart.showLegend({{ "{}".format(viz.args.show_legend=='y')|lower }});
chart.yAxis.tickFormat(d3.format('.3s'));
{% if viz.args.contribution=='y' %}
chart.yAxis.tickFormat(d3.format('.3p'));
{% endif %}

{% elif viz.chart_type == 'nvd3_bar' %}
var chart = nv.models.multiBarChart()
Expand All @@ -74,7 +77,9 @@
chart.cornerRadius(true);

{% elif viz.chart_type == 'column' %}
var chart = nv.models.multiBarChart();
var chart = nv.models.multiBarChart()
.reduceXTicks(false)
.rotateLabels(45) ;
chart.yAxis.tickFormat(d3.format('.3s'));

{% elif viz.chart_type == 'compare' %}
Expand Down Expand Up @@ -104,7 +109,7 @@

{% endif %}

{% if viz.chart_type == "nvd3_line" and viz.args.rich_tooltip == 'y' %}
{% if viz.chart_type in ("line", "stacked") and viz.args.rich_tooltip == 'y' %}
chart.useInteractiveGuideline(true);
{% endif %}
{% if viz.args.y_axis_zero == 'y' %}
Expand Down
6 changes: 4 additions & 2 deletions panoramix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ def color(s):
'#FF5A5F'
"""
colors = [
"#FF5A5F", "#007A87", "#7B0051", "#00D1C1", "#8CE071", "#FFB400",
"#FFAA91", "#B4A76C", "#9CA299", "#565A5C"
"#FF5A5F", "#007A87", "#7B0051", "#00D1C1", "#8CE071", "#FFB400",
"#FFAA91", "#B4A76C", "#9CA299", "#565A5C",
"#A14D83", "#4FA3AB", "#4EDED2", "#4EDED2", "#FFCA4F", "#FFC4B3",
"#C9BF97", "#C9BF97", "#898C8C",
]
h = hashlib.md5(s)
i = int(h.hexdigest(), 16)
Expand Down
23 changes: 18 additions & 5 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def get_json(self):
class NVD3TimeSeriesViz(NVD3Viz):
verbose_name = "Time Series - Line Chart"
chart_type = "nvd3_line"
sort_series = False
form_fields = [
'viz_type',
'granularity', ('since', 'until'),
Expand All @@ -323,18 +324,28 @@ class NVD3TimeSeriesViz(NVD3Viz):
('rolling_type', 'rolling_periods'),
('show_brush', 'show_legend'),
('rich_tooltip', 'y_axis_zero'),
('y_log_scale', None)
('y_log_scale', 'contribution')
]

def get_df(self):
args = self.args
df = super(NVD3TimeSeriesViz, self).get_df()
df = df.fillna(0)
metrics = self.metrics
df = df.pivot_table(
index="timestamp",
columns=self.groupby,
values=metrics,)

if self.sort_series:
dfs = df.sum()
dfs.sort(ascending=False)
df = df[dfs.index]

if self.args.get("contribution") == "y":
dft = df.T
df = (dft / dft.sum()).T

rolling_periods = args.get("rolling_periods")
rolling_type = args.get("rolling_type")
if rolling_periods and rolling_type:
Expand All @@ -348,10 +359,10 @@ def get_df(self):

def get_json(self):
df = self.get_df()
df = df.fillna(0)
series = df.to_dict('series')
datas = []
for name, ys in series.items():
for name in df.T.index.tolist():
ys = series[name]
if df[name].dtype.kind not in "biufc":
continue
df['timestamp'] = pd.to_datetime(df.index, utc=False)
Expand Down Expand Up @@ -401,12 +412,14 @@ class NVD3CompareTimeSeriesViz(NVD3TimeSeriesViz):
class NVD3TimeSeriesStackedViz(NVD3TimeSeriesViz):
verbose_name = "Time Series - Stacked"
chart_type = "stacked"
sort_series = True
form_fields = [
'viz_type',
'granularity', ('since', 'until'),
'metrics',
'groupby', 'limit',
('rolling_type', 'rolling_periods'),
('rich_tooltip', 'show_legend'),
]


Expand Down Expand Up @@ -447,7 +460,7 @@ class DistributionBarViz(DistributionPieViz):
chart_type = "column"

def get_df(self):
df = super(DistributionBarViz, self).get_df()
df = super(DistributionPieViz, self).get_df()
df = df.pivot_table(
index=self.groupby,
values=self.metrics)
Expand Down Expand Up @@ -482,7 +495,6 @@ def get_json(self):
viz_types = OrderedDict([
['table', TableViz],
['line', NVD3TimeSeriesViz],
['big_number', BigNumberViz],
['compare', NVD3CompareTimeSeriesViz],
['area', NVD3TimeSeriesStackedViz],
['bar', NVD3TimeSeriesBarViz],
Expand All @@ -491,4 +503,5 @@ def get_json(self):
['bubble', BubbleViz],
['markup', MarkupViz],
['word_cloud', WordCloudViz],
['big_number', BigNumberViz],
])

0 comments on commit 7143972

Please sign in to comment.