Skip to content

Commit

Permalink
Added a test case for single dimension column charts with totals enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
twheys committed Dec 18, 2018
1 parent 8dfb184 commit 144035e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fireant/slicer/widgets/highcharts.py
Expand Up @@ -189,8 +189,10 @@ def _render_x_axis(self, data_frame, dimensions, dimension_display_values):
if not isinstance(data_frame.index, pd.MultiIndex) and data_frame.index.name is None \
else [utils.getdeepattr(dimension_display_values,
(first_level.name, dimension_value),
dimension_value or 'Totals')
dimension_value)
for dimension_value in first_level]
categories = [formats.dimension_value(category)
for category in categories]

return {
"type": "category",
Expand Down
9 changes: 9 additions & 0 deletions fireant/tests/slicer/mocks.py
Expand Up @@ -354,6 +354,14 @@ def totals(data_frame, dimensions, columns):
def get_totals_marker_for_dtype(dtype):
return totals_markers.get(dtype, MAX_STRING)

if not isinstance(data_frame.index, pd.MultiIndex):
totals_marker = get_totals_marker_for_dtype(data_frame.index.dtype)
totals_df = pd.DataFrame([data_frame.sum()],
index=pd.Index([totals_marker],
name=data_frame.index.name))

return data_frame.append(totals_df)

def _totals(df):
if isinstance(df, pd.Series):
return df.sum()
Expand Down Expand Up @@ -401,6 +409,7 @@ def _totals(df):
elif not isinstance(l.index, (pd.DatetimeIndex, pd.RangeIndex)):
l.index = l.index.astype('str')

cat_dim_totals_df = totals(cat_dim_df, [fd('political_party')], _columns)
cont_cat_dim_totals_df = totals(cont_cat_dim_df, [fd('political_party')], _columns)
cont_uni_dim_totals_df = totals(cont_uni_dim_df, [fd('state')], _columns)
cont_uni_dim_all_totals_df = totals(cont_uni_dim_df, [fd('timestamp'), fd('state')], _columns)
Expand Down
38 changes: 38 additions & 0 deletions fireant/tests/slicer/widgets/test_highcharts.py
Expand Up @@ -12,6 +12,7 @@
from fireant.tests.slicer.mocks import (
ElectionOverElection,
cat_dim_df,
cat_dim_totals_df,
cont_dim_df,
cont_dim_operation_df,
cont_uni_dim_all_totals_df,
Expand Down Expand Up @@ -1590,6 +1591,43 @@ def test_cont_uni_dims_multi_metric_multi_axis_bar_chart(self):
"colors": DEFAULT_COLORS,
}, result)

def test_cat_dim_with_totals_chart(self):
result = HighCharts(title="Categorical Dimension with Totals") \
.axis(self.chart_class(slicer.metrics.votes)) \
.transform(cat_dim_totals_df, slicer, [slicer.dimensions.political_party.rollup()], [])

self.assertEqual({
'title': {'text': 'Categorical Dimension with Totals'},
'xAxis': {
'categories': ['Democrat', 'Independent', 'Republican', 'Totals'],
'type': 'category',
'visible': True
},
'yAxis': [{
'id': '0',
'labels': {'style': {'color': None}},
'title': {'text': None},
'visible': True
}],
'legend': {'useHTML': True},
'series': [{
'name': 'Votes',
'yAxis': '0',
'data': [54551568, 1076384, 56046384, 111674336],
'marker': {},
'tooltip': {
'valueDecimals': None,
'valuePrefix': None,
'valueSuffix': None
},
'type': self.chart_type,
'stacking': self.stacking,
}],
'tooltip': {'enabled': True, 'shared': True, 'useHTML': True},
"colors": DEFAULT_COLORS,
}, result)


def test_invisible_y_axis(self):
result = HighCharts(title="All Votes") \
.axis(self.chart_class(slicer.metrics.votes),
Expand Down

0 comments on commit 144035e

Please sign in to comment.