Repository: napjon/krisk

Issue:
Add trendline parameters for bar chart
Add trendline that shows changes for bar chart

Comments:

Patch:
diff --git a/krisk/plot/__init__.py b/krisk/plot/__init__.py
index 14e24b8..e702c4a 100644
--- a/krisk/plot/__init__.py
+++ b/krisk/plot/__init__.py
@@ -8,7 +8,8 @@ def bar(df,
         how='count',
         stacked=False,
         annotate=None,
-        full=False):
+        full=False,
+        trendline=False):
     """
     Parameters
     ----------
@@ -26,10 +27,13 @@ def bar(df,
     stacked: Boolean, default to False.
         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
+        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
     full: boolean, default to False.
         If true, set to full area stacked chart. Only work if stacked is True.
+    trendline: boolean, default to False.
+        If true, add line that connected the bars. Only work if not category, category but stacked,
+        or not full.  
     
     Returns
     -------
@@ -39,6 +43,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,full=full,
+                      trendline=trendline,
                       annotate='top' if annotate == True else annotate)
 
 
diff --git a/krisk/plot/bar_line.py b/krisk/plot/bar_line.py
index 057ed1d..1101415 100644
--- a/krisk/plot/bar_line.py
+++ b/krisk/plot/bar_line.py
@@ -51,6 +51,20 @@ def set_bar_line_chart(chart, df, x, c, **kwargs):
     if kwargs['annotate'] == 'top':
         series[-1]['label'] = d_annotate
 
+    if kwargs['type'] == 'bar' and kwargs['trendline']:
+        trendline = {'name':'trendline', 'type': 'line'}
+
+        if c and kwargs['stacked']:
+            trendline['data']  =  [0] * len(series[-1]['data'])
+            trendline['stack'] = c
+        elif c is None:
+            trendline['data'] = series[0]['data']
+        else:
+            raise AssertionError('Trendline must either stacked category, or not category')
+
+        series.append(trendline)
+
+
     
     # TODO: make annotate receive all kinds supported in echarts.
 


Test patch:
diff --git a/krisk/tests/data/bar_year_pop_mean_continent_trendline.json b/krisk/tests/data/bar_year_pop_mean_continent_trendline.json
new file mode 100644
index 0000000..89aa040
--- /dev/null
+++ b/krisk/tests/data/bar_year_pop_mean_continent_trendline.json
@@ -0,0 +1,152 @@
+{
+    "legend": {
+        "data": [
+            "Africa",
+            "Americas",
+            "Asia",
+            "Europe",
+            "Oceania"
+        ]
+    },
+    "title": {
+        "text": ""
+    },
+    "yAxis": {},
+    "series": [
+        {
+            "stack": "continent",
+            "type": "bar",
+            "data": [
+                9279525,
+                10270856,
+                11000948,
+                12760499,
+                14760787,
+                17152804,
+                20033753,
+                23254956,
+                26298373,
+                29072015,
+                31287142,
+                33333216
+            ],
+            "name": "Africa"
+        },
+        {
+            "stack": "continent",
+            "type": "bar",
+            "data": [
+                17876956,
+                19610538,
+                21283783,
+                22934225,
+                24779799,
+                26983828,
+                29341374,
+                31620918,
+                33958947,
+                36203463,
+                38331121,
+                40301927
+            ],
+            "name": "Americas"
+        },
+        {
+            "stack": "continent",
+            "type": "bar",
+            "data": [
+                8425333,
+                9240934,
+                10267083,
+                11537966,
+                13079460,
+                14880372,
+                12881816,
+                13867957,
+                16317921,
+                22227415,
+                25268405,
+                31889923
+            ],
+            "name": "Asia"
+        },
+        {
+            "stack": "continent",
+            "type": "bar",
+            "data": [
+                1282697,
+                1476505,
+                1728137,
+                1984060,
+                2263554,
+                2509048,
+                2780097,
+                3075321,
+                3326498,
+                3428038,
+                3508512,
+                3600523
+            ],
+            "name": "Europe"
+        },
+        {
+            "stack": "continent",
+            "type": "bar",
+            "data": [
+                8691212,
+                9712569,
+                10794968,
+                11872264,
+                13177000,
+                14074100,
+                15184200,
+                16257249,
+                17481977,
+                18565243,
+                19546792,
+                20434176
+            ],
+            "name": "Oceania"
+        },
+        {
+            "stack": "continent",
+            "data": [
+                0,
+                0,
+                0,
+                0,
+                0,
+                0,
+                0,
+                0,
+                0,
+                0,
+                0,
+                0
+            ],
+            "type": "line",
+            "name": "trendline"
+        }
+    ],
+    "tooltip": {
+        "axisPointer": {
+            "type": ""
+        }
+    },
+    "xAxis": {
+        "data": [
+            1952,
+            1957,
+            1962,
+            1967,
+            1972,
+            1977,
+            1982,
+            1987,
+            1992,
+            1997,
+            2002,
+            2007
+        ]
+    }
+}
\ No newline at end of file
diff --git a/krisk/tests/data/bar_year_pop_mean_trendline.json b/krisk/tests/data/bar_year_pop_mean_trendline.json
new file mode 100644
index 0000000..15ef467
--- /dev/null
+++ b/krisk/tests/data/bar_year_pop_mean_trendline.json
@@ -0,0 +1,68 @@
+{
+    "legend": {
+        "data": []
+    },
+    "title": {
+        "text": ""
+    },
+    "yAxis": {},
+    "series": [
+        {
+            "type": "bar",
+            "data": [
+                9111144.6,
+                10062280.4,
+                11014983.8,
+                12217802.8,
+                13612120.0,
+                15120030.4,
+                16044248.0,
+                17615280.2,
+                19476743.2,
+                21899234.8,
+                23588394.4,
+                25911953.0
+            ],
+            "name": "year"
+        },
+        {
+            "data": [
+                9111144.6,
+                10062280.4,
+                11014983.8,
+                12217802.8,
+                13612120.0,
+                15120030.4,
+                16044248.0,
+                17615280.2,
+                19476743.2,
+                21899234.8,
+                23588394.4,
+                25911953.0
+            ],
+            "type": "line",
+            "name": "trendline"
+        }
+    ],
+    "tooltip": {
+        "axisPointer": {
+            "type": ""
+        }
+    },
+    "xAxis": {
+        "data": [
+            1952,
+            1957,
+            1962,
+            1967,
+            1972,
+            1977,
+            1982,
+            1987,
+            1992,
+            1997,
+            2002,
+            2007
+        ]
+    }
+}
\ No newline at end of file
diff --git a/krisk/tests/test_plot.py b/krisk/tests/test_plot.py
index 41aed67..bdce2e5 100644
--- a/krisk/tests/test_plot.py
+++ b/krisk/tests/test_plot.py
@@ -77,6 +77,20 @@ def test_full_bar_line(gapminder):
 
     assert bar.option == line.option == true_option
 
+
+def test_trendline(gapminder):
+
+    p = kk.bar(gapminder,'year',how='mean',y='pop',trendline=True)
+    assert p.get_option() == read_option_tests('bar_year_pop_mean_trendline.json')
+
+    p = kk.bar(gapminder,'year',how='mean',y='pop',trendline=True,c='continent',stacked=True)
+    assert p.get_option() == read_option_tests('bar_year_pop_mean_continent_trendline.json')
+
+    try:
+        kk.bar(gapminder,'year',how='mean',y='pop',trendline=True,c='continent')
+    except AssertionError:
+        pass
+
 def test_hist(gapminder):
 
     true_option  = read_option_tests('hist_x.json')