def test_get_figure_filter_zero_length(self): data = self.get_data() filt = dict( column='Amount', comparator='==', value='donotmatch', ) sdt.get_figure(data, filters=[filt, filt, filt])
def test_get_figure_filter_error(self): data = self.get_data() good = dict(column='Description', comparator='~', value='amex|visa') sdt.get_figure(data, filters=[good, good]) bad = dict(comparator='~', value='amex|visa') expected = 'Invalid filter.*column.*This field is required' with self.assertRaisesRegexp(DataError, expected): sdt.get_figure(data, filters=[good, bad])
def test_get_figure_filter_group(self): data = self.get_data() filt = dict( column='Amount', comparator='==', value='donotmatch', ) grp = dict( columns=['Description', 'Amount'], metric='count', datetime_column='date', ) sdt.get_figure(data, filters=[filt], group=grp)
def test_get_figure_pivot_error(self): data = self.get_data() good = dict( columns=['Description', 'Category'], values=['Amount'], index='Date', ) sdt.get_figure(data, pivot=good) bad = dict( values=['Amount'], index='Date', ) expected = 'Invalid pivot.*columns.*This field is required' with self.assertRaisesRegexp(DataError, expected): sdt.get_figure(data, pivot=bad)
def test_get_figure_group_error(self): data = self.get_data() good = dict( columns=['Description', 'Amount'], metric='count', datetime_column='date', ) sdt.get_figure(data, group=good) bad = dict( metric='mean', datetime_column='date', ) expected = 'Invalid group.*columns.*This field is required' with self.assertRaisesRegexp(DataError, expected): sdt.get_figure(data, group=bad)
def test_get_figure_group_pivot(self): data = self.get_data() data['Description'] = None grp = dict( columns=['Description'], metric='count', datetime_column='date', ) pvt = dict( columns=['Description', 'Category'], values=['Amount'], index='Date', ) expected = 'DataFrame must be at least 1 in length. Given length: 0.' with self.assertRaisesRegexp(EnforceError, expected): sdt.get_figure(data, group=grp, pivot=pvt)
def test_get_figure_params(self): data = self.get_data() pivot = dict( columns=['Description'], values=['Amount'], index='Date', ) fig = sdt.get_figure(data, pivot=pivot, color_scheme={ 'grey1': '#0000FF', 'cyan2': '#FF0000', }, kind='hist', title='Foobar', x_title='foos', y_title='bars', bins=100) # color scheme result = fig['layout']['legend']['bgcolor'] self.assertEqual(result, '#0000FF') result = fig['data'][0]['marker']['color'] self.assertEqual(result, '#FF0000') # kind result = fig['data'][0]['type'] self.assertEqual(result, 'histogram') # bins result = fig['data'][0]['nbinsx'] self.assertEqual(result, 100) # title result = fig['layout']['title']['text'] self.assertEqual(result, 'Foobar') # x_title result = fig['layout']['xaxis']['title']['text'] self.assertEqual(result, 'foos') # y_title result = fig['layout']['yaxis']['title']['text'] self.assertEqual(result, 'bars')
def get_plots(data, plots): # type: (List[dict], List[dict]) -> List[dcc.Graph] ''' Gets a Dash plots using given dicts. Assumes dict element has all columns of table as keys. Args: data (list[dict]): List of dicts defining data. plots (list[dict]): List of dicts defining plots. Raises: EnforceError: If data is not a list of dicts. EnforceError: If plots is not a list of dicts. Returns: list[dcc.Graph]: Plots. ''' msg = 'Data must be a list of dictionaries. Given value: {a}.' Enforce(data, 'instance of', list, message=msg) for item in data: Enforce(item, 'instance of', dict, message=msg) msg = 'Plots must be a list of dictionaries. Given value: {a}.' Enforce(plots, 'instance of', list, message=msg) for item in plots: Enforce(item, 'instance of', dict, message=msg) # -------------------------------------------------------------------------- data_ = DataFrame(data) if 'date' in data_.columns: data_.date = DatetimeIndex(data_.date) elems = [] for i, x in enumerate(plots): plot = cfg.PlotItem(x) plot.validate() plot = plot.to_primitive() min_width = str(plot['min_width']) + '%' try: fig = sdt.get_figure( data_, filters=plot['filters'], group=plot['group'], pivot=plot['pivot'], **plot['figure'], ) fig = dcc.Graph( id=f'plot-{i:02d}', className='plot', figure=fig, style={'min-width': min_width}, ) except (DataError, EnforceError): fig = html.Div( id=f'plot-{i:02d}', className='plot plot-error', style={'min-width': min_width}, children=html.Div( className='plot-error-container', children=html.Div( className='plot-error-message', children='no data found' ) ) ) elems.append(fig) return elems