You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2020/01/28 13:18:43 UTC

[GitHub] [incubator-superset] VinodLouis commented on a change in pull request #5144: Adding a new viz line-bar chart

VinodLouis commented on a change in pull request #5144: Adding a new viz line-bar chart
URL: https://github.com/apache/incubator-superset/pull/5144#discussion_r371795669
 
 

 ##########
 File path: superset/viz.py
 ##########
 @@ -2623,6 +2623,80 @@ def get_data(self, df):
         return self.nest_values(levels)
 
 
+class LineBarViz(NVD3Viz):
+
+    """A simple line bar chart with dual axis"""
+
+    viz_type = 'line_bar'
+    verbose_name = _('Time Series - Line Bar Chart')
+    sort_series = False
+    is_timeseries = True
+
+    def query_obj(self):
+        d = super(LineBarViz, self).query_obj()
+        m1 = self.form_data.get('metric')
+        m2 = self.form_data.get('metric_2')
+        d['metrics'] = [m1, m2]
+        if not m1:
+            raise Exception(_('Pick a metric for left(bar) axis!'))
+        if not m2:
+            raise Exception(_('Pick a metric for right(line) axis!'))
+        if m1 == m2:
+            raise Exception(_('Please choose different metrics'
+                            ' on left and right axis'))
+        return d
+
+    def to_series(self, df, classed=''):
+        cols = []
+        for col in df.columns:
+            if col == '':
+                cols.append('N/A')
+            elif col is None:
+                cols.append('NULL')
+            else:
+                cols.append(col)
+        df.columns = cols
+        series = df.to_dict('series')
+        chart_data = []
+        metrics = [
+            self.form_data.get('metric'),
+            self.form_data.get('metric_2'),
+        ]
+        for i, m in enumerate(metrics):
+            ys = series[m]
 
 Review comment:
   ```
   class LineBarViz(DistributionPieViz):
       """A rich line chart with dual axis"""
       viz_type = 'line_bar'
       verbose_name = _('Time Series - Line Bar Chart')
       sort_series = False
       is_timeseries = True
       def query_obj(self):
           d = super(LineBarViz, self).query_obj()
           m1 = self.form_data.get('metric')
           m2 = self.form_data.get('metric_2i')
           d['metrics'] = [m1, m2]
           if not m1:
               raise Exception(_('Pick a metric for left(bar) axis!'))
           if not m2:
               raise Exception(_('Pick a metric for right(line) axis!'))
           if m1['label'] == m2['label']:
               raise Exception(_('Please choose different metrics'
                               ' on left and right axis'))
           return d
       def to_series(self, df, classed=''):
           cols = []
           for col in df.columns:
               if col == '':
                   cols.append('N/A')
               elif col is None:
                   cols.append('NULL')
               else:
                   cols.append(col)
           df.columns = cols
           series = df.to_dict('series')
           chart_data = []
           metrics = [
               self.form_data.get('metric')['label'],
               self.form_data.get('metric_2i')['label'],
           ]
           for i, m in enumerate(metrics):
               ys = series[m]
               if df[m].dtype.kind not in 'biufc':
                   continue
               series_title = m
               d = {
                   'key': series_title,
                   'classed': classed,
                   'values': [
                       {'x': ds, 'y': ys[ds] if ds in ys else None}
                       for ds in df.index
                   ],
                   'yAxis': i + 1,
                   'type': 'bar' if i==0 else 'line',
               }
               chart_data.append(d)
           return chart_data
       def get_data(self, df):
           fd = self.form_data
           df = df.fillna(0)
           if self.form_data.get('granularity') == 'all':
               raise Exception(_('Pick a time granularity for your time series'))
           metric = fd.get('metric')
           metric_2i = fd.get('metric_2i')
           print(metric)
           df = df.pivot_table(
               index=DTTM_ALIAS,
               values=[metric['label'], metric_2i['label']])
           chart_data = self.to_series(df)
           return chart_data
   ```
   
   This May help you

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org