Repository: napjon/krisk

Issue:
Add full area (100%) for stacked bar and line chart

Discussion:

Patch:
diff --git a/krisk/plot/__init__.py b/krisk/plot/__init__.py
index 0ed9354..14e24b8 100644
--- a/krisk/plot/__init__.py
+++ b/krisk/plot/__init__.py
@@ -7,7 +7,8 @@ def bar(df,
         c=None,
         how='count',
         stacked=False,
-        annotate=None):
+        annotate=None,
+        full=False):
     """
     Parameters
     ----------
@@ -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
     -------
@@ -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)
 
 
@@ -46,7 +49,8 @@ def line(df,
          how=None,
          stacked=False,
          area=False,
-         annotate=None):
+         annotate=None,
+         full=False):
     """
     Parameters
     ----------
@@ -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)
 
 
diff --git a/krisk/plot/bar_line.py b/krisk/plot/bar_line.py
index 5884aa4..057ed1d 100644
--- a/krisk/plot/bar_line.py
+++ b/krisk/plot/bar_line.py
@@ -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.
 
 
@@ -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
 
diff --git a/krisk/plot/make_chart.py b/krisk/plot/make_chart.py
index ceab454..ce77d49 100644
--- a/krisk/plot/make_chart.py
+++ b/krisk/plot/make_chart.py
@@ -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


Test patch:
diff --git a/krisk/plot/points.py b/krisk/plot/scatter_geo.py
similarity index 100%
rename from krisk/plot/points.py
rename to krisk/plot/scatter_geo.py
diff --git a/krisk/tests/data/bar_x_c.json b/krisk/tests/data/bar_x_c.json
new file mode 100644
index 0000000..db81bc8
--- /dev/null
+++ b/krisk/tests/data/bar_x_c.json
@@ -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
+            ]
+        }
+    ]
+}
\ No newline at end of file
diff --git a/krisk/tests/data/full_bar_line.json b/krisk/tests/data/full_bar_line.json
new file mode 100644
index 0000000..ea4fa05
--- /dev/null
+++ b/krisk/tests/data/full_bar_line.json
@@ -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
+            ]
+        }
+    ]
+}
\ No newline at end of file
diff --git a/krisk/tests/data/hist_x.json b/krisk/tests/data/hist_x.json
new file mode 100644
index 0000000..5bf0e15
--- /dev/null
+++ b/krisk/tests/data/hist_x.json
@@ -0,0 +1,47 @@
+{
+    "xAxis": {
+        "data": [
+            28,
+            34,
+            39,
+            44,
+            49,
+            55,
+            60,
+            65,
+            70,
+            75,
+            81
+        ]
+    },
+    "tooltip": {
+        "axisPointer": {
+            "type": ""
+        }
+    },
+    "title": {
+        "text": ""
+    },
+    "legend": {
+        "data": []
+    },
+    "yAxis": {},
+    "series": [
+        {
+            "type": "bar",
+            "name": "lifeExp",
+            "data": [
+                4,
+                2,
+                7,
+                2,
+                2,
+                3,
+                5,
+                13,
+                16,
+                6
+            ]
+        }
+    ]
+}
\ No newline at end of file
diff --git a/krisk/tests/data/simple_scatter.json b/krisk/tests/data/simple_scatter.json
new file mode 100644
index 0000000..15ef0b6
--- /dev/null
+++ b/krisk/tests/data/simple_scatter.json
@@ -0,0 +1,72 @@
+{
+    "xAxis": {
+        "max": 17876956,
+        "type": "value",
+        "name": "pop"
+    },
+    "tooltip": {
+        "axisPointer": {
+            "type": ""
+        }
+    },
+    "title": {
+        "text": ""
+    },
+    "legend": {
+        "data": []
+    },
+    "visualMap": [],
+    "yAxis": {
+        "max": 69,
+        "type": "value",
+        "name": "lifeExp"
+    },
+    "series": [
+        {
+            "type": "scatter",
+            "name": "pop",
+            "data": [
+                [
+                    9279525.0,
+                    43.077,
+                    "Africa",
+                    "Algeria",
+                    2449.008,
+                    1952
+                ],
+                [
+                    17876956.0,
+                    62.485,
+                    "Americas",
+                    "Argentina",
+                    5911.315,
+                    1952
+                ],
+                [
+                    8425333.0,
+                    28.801,
+                    "Asia",
+                    "Afghanistan",
+                    779.445,
+                    1952
+                ],
+                [
+                    1282697.0,
+                    55.23,
+                    "Europe",
+                    "Albania",
+                    1601.056,
+                    1952
+                ],
+                [
+                    8691212.0,
+                    69.12,
+                    "Oceania",
+                    "Australia",
+                    10039.596,
+                    1952
+                ]
+            ]
+        }
+    ]
+}
\ No newline at end of file
diff --git a/krisk/tests/test_plot.py b/krisk/tests/test_plot.py
index 2fecdff..41aed67 100644
--- a/krisk/tests/test_plot.py
+++ b/krisk/tests/test_plot.py
@@ -3,11 +3,12 @@ import pytest
 import krisk.plot as kk
 DATA_DIR = 'krisk/tests/data'
 
+read_option_tests = lambda f: json.load(open(DATA_DIR + '/' + f, 'r'))
 
 def test_bar(gapminder):
 
     #Bar
-    true_option = json.load(open(DATA_DIR + '/bar.json', 'r'))
+    true_option = read_option_tests('bar.json')
     p = kk.bar(gapminder,
                'year',
                y='pop',
@@ -17,8 +18,14 @@ def test_bar(gapminder):
                annotate=True)
     assert p.get_option() == true_option
 
+    #Bar with x-axis and category
+    true_option = read_option_tests('bar_x_c.json')
+    p = kk.bar(gapminder,'year',c='continent',stacked=True)
+    assert p.get_option() == true_option
+
+
     # Bar Annotate All
-    true_option = json.load(open(DATA_DIR + '/bar_ann_all.json', 'r'))
+    true_option = read_option_tests('/bar_ann_all.json')
     p = kk.bar(gapminder,
                'year',
                y='pop',
@@ -42,7 +49,7 @@ def test_bar(gapminder):
 
 def test_line(gapminder):
 
-    true_option = json.load(open(DATA_DIR + '/line.json', 'r'))
+    true_option = read_option_tests('line.json')
     p = kk.line(
         gapminder,
         'year',
@@ -55,9 +62,28 @@ def test_line(gapminder):
 
     assert p.get_option() == true_option
 
+def test_full_bar_line(gapminder):
+    bar = kk.bar(gapminder,'year',c='continent',y='pop',how='mean',stacked=True,full=True,annotate='all')
+    line = kk.line(gapminder,'year',c='continent',y='pop',how='mean',stacked=True,full=True,annotate='all')
+   
+    for i in range(len(bar.option['series'])):
+            bar.option['series'][i].pop('type')
+            line.option['series'][i].pop('type')
+
+            bar.option['series'][i].pop('label')
+            line.option['series'][i].pop('label')
+
+    true_option = read_option_tests('full_bar_line.json')
+
+    assert bar.option == line.option == true_option
 
 def test_hist(gapminder):
 
+    true_option  = read_option_tests('hist_x.json')
+    p = kk.hist(gapminder,'lifeExp',bins=10)
+    assert p.get_option() == true_option
+
+
     true_option = json.load(open(DATA_DIR + '/hist.json', 'r'))
     p = kk.hist(
         gapminder,
@@ -71,6 +97,10 @@ def test_hist(gapminder):
 
 
 def test_scatter(gapminder):
+    # Simple Scatter
+    p = kk.scatter(gapminder[gapminder.year == 1952],'pop','lifeExp')
+    true_option = read_option_tests('simple_scatter.json')
+    assert p.get_option() == true_option
 
     # Grouped Scatter
     true_option = json.load(open(DATA_DIR + '/scatter.json', 'r'))
@@ -83,7 +113,7 @@ def test_scatter(gapminder):
     assert p.get_option() == true_option
 
     # Scatter
-    true_option = json.load(open(DATA_DIR + '/scatter_single.json', 'r'))
+    true_option = read_option_tests('scatter_single.json')
     p = kk.scatter(
         gapminder[gapminder.year == 1952], 'lifeExp', 'gdpPercap', s='pop')
     assert p.get_option() == true_option