
#from . import Chart
def make_chart(df,**kwargs):
    
    c = Chart(**kwargs)
    
    elem_series = {
            'name': '',
            'type': kwargs['type'],
            'data': []}
    
    x = kwargs['x']
    y = kwargs.get('y')
    category = kwargs['category']
    
    
    if kwargs['type'] in ['bar','line']:
        c._option['xAxis']['data'] = df[x].unique().tolist()
        
        def insert_series(df,cat=None):
            """Return data series based on dataframe"""
            
            opt_data = (df[x].value_counts()
                        if y is None else
                        df.groupby(x)[y].aggregate(kwargs['how']))

            series = deepcopy(elem_series)
            series['data'] = opt_data.values.round(3).tolist()
            series['type'] = kwargs['type']
            
            if kwargs['stacked'] == True:
                series['stack'] = category
                    
            series['name'] = cat if cat else x
            c._option['series'].append(series)
        
        
        if category:
            #Iterate and append Data for every category
            for cat, subset in df.groupby(category):
                insert_series(subset,cat=cat)
                c._option['legend']['data'].append(cat)
        else:
            insert_series(df)
            
        if (category and
            kwargs['type'] == 'line' and 
            kwargs['area'] == True and 
            kwargs['stacked']== True):
            for e in c._option['series']:
                e['areaStyle']= {'normal': {}}
            
    elif kwargs['type'] == 'hist':
        kwargs['type'] = 'bar'
        
        
        def insert_series(df,cat=None):
            
            y_val,x_val = np.histogram(df[x],
                                       bins=kwargs['bins'],
                                       normed=kwargs['normed'])
            
            bins = x_val.astype(int).tolist()
            c._option['xAxis']['data'] = bins
            
            series = deepcopy(elem_series)
            series['data'] = y_val.round(3).tolist()
            series['type'] = kwargs['type']
            series['name'] = cat if cat else x
                
            if kwargs['stacked'] == True:
                series['stack'] = category
            
            c._option['series'].append(series)
            
            
        if category:
            for cat,subset in df.groupby(category):
                insert_series(subset,cat=cat)
                c._option['legend']['data'].append(cat)
        else:
            insert_series(df)
#             c._option['xAxis']['data'] =  get_hist_series(df,x,**kwargs)
            
    elif kwargs['type'] == 'scatter':
        
        c._option['xAxis'] = {'type': 'value',
                              'name': x,
                              'max': int(df[x].max())}
        
        c._option['yAxis'] = {'type': 'value',
                              'name': y,
                              'max': int(df[y].max())}
        
        
        cols = [x,y]
     
        c._option['visualMap'] = []
        visual_map_template = {'show': False,
                               'min': 0,
                               'max': 999,
                               'inRange': {}}
        
        size = kwargs['size']
        if size is not None:
            vmap_size = deepcopy(visual_map_template)
            vmap_size['min'] = df[size].min()
            vmap_size['max'] = df[size].max()
            vmap_size['inRange']['symbolSize'] = [6,60]
            c._option['visualMap'].append(vmap_size)
            cols.append(size)
            
        #TODO: Fix Saturate
#          saturate = kwargs['saturate']
#         if saturate is not None:
#             vmap_saturate = deepcopy(visual_map_template)
#             vmap_saturate['min'] = float(df[saturate].min())
#             vmap_saturate['max'] = float(df[saturate].max())
#             vmap_saturate['inRange']['colorLightness'] = [1,0.5]
#             c._option['visualMap'].append(vmap_saturate)
#             cols.append(saturate)

        def insert_series(df,cat=None):
        
            data = df[cols].values.round(3).tolist()        
            series = deepcopy(elem_series)
            series['data'] = data
            series['type'] = kwargs['type']
            series['name'] = cat if cat else x
            c._option['series'].append(series)
            
            
        if category:
            for cat,subset in df.groupby(category):
                insert_series(subset,cat=cat)
                c._option['legend']['data'].append(cat)
        else:
            insert_series(df)
    
            
    return c


def bar(df,x,y=None,category=None,how='count',stacked=False,**kwargs):
    
    kwargs['x'] = x
    kwargs['y'] = y
    kwargs['category'] = category
    kwargs['how'] = how
    kwargs['type'] = 'bar'
    kwargs['stacked'] = stacked
    
    return make_chart(df,**kwargs)

def line(df,x,y=None,category=None,how='count',stacked=False,area=False,**kwargs):
    
    kwargs['x'] = x
    kwargs['y'] = y
    kwargs['category'] = category
    kwargs['how'] = how
    kwargs['type'] = 'line'
    kwargs['stacked'] = stacked
    kwargs['area'] = area
    
    return make_chart(df,**kwargs)

def hist(df,x,category=None,bins=10,normed=False,stacked=False,**kwargs):
    
    kwargs['x'] = x
    kwargs['category'] = category
    kwargs['bins'] = bins
    kwargs['type'] = 'hist'
    kwargs['normed'] = normed
    kwargs['stacked'] = stacked
    
    return make_chart(df,**kwargs)

def scatter(df,x,y,size=None,category=None,**kwargs):
    
    kwargs['x'] = x
    kwargs['y'] = y
    kwargs['category'] = category
    kwargs['size'] = size
    #kwargs['saturate'] = saturate #TODO: Fix saturate
    kwargs['type'] = 'scatter'
    
    return make_chart(df,**kwargs)
@@ -278,110 +278,216 @@
     
-%0A    if kwargs%5B'type'%5D in %5B'bar','line'%5D:%0A        c._option%5B'xAxis'%5D%5B'data'%5D = df%5Bx%5D.unique().tolist()
+def round_list(arr):%0A        try:%0A            return arr.values.round(3).tolist()%0A        except TypeError:%0A            return arr.unique().tolist()%0A    %0A    %0A    if kwargs%5B'type'%5D in %5B'bar','line'%5D:%0A        
 %0A   
@@ -592,32 +592,109 @@
 %22%22%22%0A            
+c._option%5B'xAxis'%5D%5B'data'%5D = round_list(df%5Bx%5D.drop_duplicates())%0A            
 %0A            opt
@@ -690,20 +690,16 @@
         
-opt_
 data = (
@@ -828,16 +828,41 @@
 how'%5D))%0A
+            %0A            
 %0A       
@@ -930,40 +930,23 @@
 %5D = 
-opt_data.values.round(3).to
+round_
 list(
+data
 )%0A  
@@ -2066,32 +2066,55 @@
 '%5D)%0A            
+data = pd.Series(y_val)
 %0A            bin
@@ -2279,30 +2279,23 @@
 %5D = 
-y_val.round(3).to
+round_
 list(
+data
 )%0A  
@@ -4376,41 +4376,8 @@
 ols%5D
-.values.round(3).tolist()        
 %0A   
@@ -4445,20 +4445,32 @@
 ata'%5D = 
+round_list(
 data
+)
 %0A       

Add background and palette method