Skip to content

Commit

Permalink
Support normalized full stacked chart for line and bar, fix #28
Browse files Browse the repository at this point in the history
* add full area for stacked bar and line chart
* rename points to scatter_geo
* Upgrade coverage testing
  • Loading branch information
napjon authored Sep 2, 2016
1 parent c92fc16 commit b7489f4
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 16 deletions.
18 changes: 12 additions & 6 deletions krisk/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def bar(df,
c=None,
how='count',
stacked=False,
annotate=None):
annotate=None,
full=False):
"""
Parameters
----------
Expand All @@ -26,7 +27,9 @@ def bar(df,
Whether to stacked category on top of the other categories.
annotate: string, {'all',True} default to None
if True, annotate value on top of the plot element. If stacked is also True, annotate the last
category. if 'all' and stacked, annotate all category
category. if 'all' and stacked, annotate all category
full: boolean, default to False.
If true, set to full area stacked chart. Only work if stacked is True.
Returns
-------
Expand All @@ -35,7 +38,7 @@ def bar(df,

# TODO: add optional argument trendline

return make_chart(df,type='bar',x=x,y=y,c=c,how=how,stacked=stacked,
return make_chart(df,type='bar',x=x,y=y,c=c,how=how,stacked=stacked,full=full,
annotate='top' if annotate == True else annotate)


Expand All @@ -46,7 +49,8 @@ def line(df,
how=None,
stacked=False,
area=False,
annotate=None):
annotate=None,
full=False):
"""
Parameters
----------
Expand All @@ -65,13 +69,15 @@ def line(df,
Whether to stacked category on top of the other categories.
annotate: string, {'all',True} default to None
if True, annotate value on top of the plot element. If stacked is also True, annotate the last
category. if 'all' and stacked, annotate all category
category. if 'all' and stacked, annotate all category
full: boolean, default to False.
If true, set to full area stacked chart. Only work if stacked is True.
Returns
-------
Chart Object
"""
return make_chart(df,type='line',x=x,y=y,c=c,how=how,stacked=stacked,area=area,
return make_chart(df,type='line',x=x,y=y,c=c,how=how,stacked=stacked,area=area,full=full,
annotate='top' if annotate == True else annotate)


Expand Down
16 changes: 11 additions & 5 deletions krisk/plot/bar_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ def set_bar_line_chart(chart, df, x, c, **kwargs):
s['areaStyle'] = {'normal': {}}

if kwargs['annotate'] == 'all':
s['label'] = deepcopy(d_annotate)

if chart_type == 'bar':
d_ant = deepcopy(d_annotate)
d_ant['normal']['position'] = 'inside'
s['label'] = deepcopy(d_ant)
else:
s['label'] = deepcopy(d_annotate)
s['label']['normal']['position'] = 'inside'

if kwargs['type'] in ['line','bar'] and kwargs['full']:
chart.option['yAxis']['max'] = 1

if kwargs['annotate'] == 'top':
series[-1]['label'] = d_annotate


# TODO: make annotate receive all kinds supported in echarts.


Expand All @@ -69,6 +71,10 @@ def get_bar_line_data(df, x, c, y, **kwargs):
data = df.groupby(x)[y].agg(kwargs['how'])
else:
data = df[x].value_counts()


if c and kwargs['stacked'] and kwargs['full']:
data = data.div(data.sum(1),axis=0)

return data

Expand Down
2 changes: 1 addition & 1 deletion krisk/plot/make_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def insert_series_data(data, x, chart_type, chart, cat=None):
def make_chart(df, **kwargs):

from krisk.plot.bar_line import set_bar_line_chart
from krisk.plot.points import set_scatter_chart
from krisk.plot.scatter_geo import set_scatter_chart

chart = Chart(**kwargs)
chart._kwargs_chart_['data_columns'] = df.columns
Expand Down
File renamed without changes.
133 changes: 133 additions & 0 deletions krisk/tests/data/bar_x_c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"xAxis": {
"data": [
1952,
1957,
1962,
1967,
1972,
1977,
1982,
1987,
1992,
1997,
2002,
2007
]
},
"tooltip": {
"axisPointer": {
"type": ""
}
},
"title": {
"text": ""
},
"legend": {
"data": [
"Africa",
"Americas",
"Asia",
"Europe",
"Oceania"
]
},
"yAxis": {},
"series": [
{
"type": "bar",
"name": "Africa",
"stack": "continent",
"data": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
},
{
"type": "bar",
"name": "Americas",
"stack": "continent",
"data": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
},
{
"type": "bar",
"name": "Asia",
"stack": "continent",
"data": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
},
{
"type": "bar",
"name": "Europe",
"stack": "continent",
"data": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
},
{
"type": "bar",
"name": "Oceania",
"stack": "continent",
"data": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
}
]
}
130 changes: 130 additions & 0 deletions krisk/tests/data/full_bar_line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"xAxis": {
"data": [
1952,
1957,
1962,
1967,
1972,
1977,
1982,
1987,
1992,
1997,
2002,
2007
]
},
"tooltip": {
"axisPointer": {
"type": ""
}
},
"title": {
"text": ""
},
"legend": {
"data": [
"Africa",
"Americas",
"Asia",
"Europe",
"Oceania"
]
},
"yAxis": {
"max": 1
},
"series": [
{
"name": "Africa",
"stack": "continent",
"data": [
0.204,
0.204,
0.2,
0.209,
0.217,
0.227,
0.25,
0.264,
0.27,
0.266,
0.265,
0.257
]
},
{
"name": "Americas",
"stack": "continent",
"data": [
0.392,
0.39,
0.386,
0.375,
0.364,
0.357,
0.366,
0.359,
0.349,
0.331,
0.325,
0.311
]
},
{
"name": "Asia",
"stack": "continent",
"data": [
0.185,
0.184,
0.186,
0.189,
0.192,
0.197,
0.161,
0.157,
0.168,
0.203,
0.214,
0.246
]
},
{
"name": "Europe",
"stack": "continent",
"data": [
0.028,
0.029,
0.031,
0.032,
0.033,
0.033,
0.035,
0.035,
0.034,
0.031,
0.03,
0.028
]
},
{
"name": "Oceania",
"stack": "continent",
"data": [
0.191,
0.193,
0.196,
0.194,
0.194,
0.186,
0.189,
0.185,
0.18,
0.17,
0.166,
0.158
]
}
]
}
Loading

0 comments on commit b7489f4

Please sign in to comment.