def build_chart(data_id=None, **inputs): """ Factory method that forks off into the different chart building methods (heatmaps are handled separately) - line - bar - scatter - pie - wordcloud - 3D scatter - surface :param data_id: identifier of data to build axis configurations against :type data_id: str :param inputs: Optional keyword arguments containing the following information: - x: column to be used as x-axis of chart - y: column to be used as y-axis of chart - z: column to use for the Z-Axis - agg: points to a specific function that can be applied to :func: pandas.core.groupby.DataFrameGroupBy :return: plotly chart object(s) :rtype: type of (:dash:`dash_core_components.Graph <dash-core-components/graph>`, dict) """ try: if inputs.get('chart_type') == 'heatmap': data = make_timeout_request(threaded_heatmap_builder, kwargs=dict_merge( dict(data_id=data_id), inputs)) data = data.pop() return data, None data = make_timeout_request(threaded_build_figure_data, kwargs=dict_merge(dict(data_id=data_id), inputs)) data = data.pop() if data is None: return None, None if 'error' in data: return build_error(data['error'], data['traceback']), None range_data = dict(min=data['min'], max=data['max']) axis_inputs = inputs.get('yaxis', {}) chart_builder = chart_wrapper(data_id, data, inputs) chart_type, x, y, z, agg = ( inputs.get(p) for p in ['chart_type', 'x', 'y', 'z', 'agg']) z = z if chart_type in ZAXIS_CHARTS else None chart_inputs = { k: v for k, v in inputs.items() if k not in ['chart_type', 'x', 'y', 'z', 'group'] } if chart_type == 'wordcloud': return (chart_builder( dash_components.Wordcloud(id='wc', data=data, y=y, group=inputs.get('group'))), range_data) axes_builder = build_axes(data_id, x, axis_inputs, data['min'], data['max'], z=z, agg=agg) if chart_type == 'scatter': if inputs['cpg']: scatter_charts = flatten_lists([ scatter_builder(data, x, y, axes_builder, chart_builder, group=group, agg=agg) for group in data['data'] ]) else: scatter_charts = scatter_builder(data, x, y, axes_builder, chart_builder, agg=agg) return cpg_chunker(scatter_charts), range_data if chart_type == '3d_scatter': return scatter_builder(data, x, y, axes_builder, chart_builder, z=z, agg=agg), range_data if chart_type == 'surface': return surface_builder(data, x, y, z, axes_builder, chart_builder, agg=agg), range_data if chart_type == 'bar': return bar_builder(data, x, y, axes_builder, chart_builder, **chart_inputs), range_data if chart_type == 'line': return line_builder(data, x, y, axes_builder, chart_builder, **chart_inputs), range_data if chart_type == 'pie': return pie_builder(data, x, y, chart_builder, **chart_inputs), range_data raise NotImplementedError('chart type: {}'.format(chart_type)) except BaseException as e: return build_error(str(e), str(traceback.format_exc())), None
def heatmap_builder(data_id, **inputs): """ Builder function for :plotly:`plotly.graph_objects.Heatmap <plotly.graph_objects.Heatmap>` :param data_id: integer string identifier for a D-Tale process's data :type data_id: str :param inputs: Optional keyword arguments containing the following information: - x: column to be used as x-axis of chart - y: column to be used as y-axis of chart - z: column to use for the Z-Axis - agg: points to a specific function that can be applied to :func: pandas.core.groupby.DataFrameGroupBy :type inputs: dict :return: heatmap :rtype: :plotly:`plotly.graph_objects.Heatmap <plotly.graph_objects.Heatmap>` """ try: if not valid_chart(**inputs): return None raw_data = global_state.get_data(data_id) wrapper = chart_wrapper(data_id, raw_data, inputs) hm_kwargs = dict(hoverongaps=False, colorscale='Greens', showscale=True, hoverinfo='x+y+z') x, y, z, agg = (inputs.get(p) for p in ['x', 'y', 'z', 'agg']) y = y[0] data = retrieve_chart_data(raw_data, x, y, z) x_title = update_label_for_freq(x) y_title = update_label_for_freq(y) z_title = z data = data.sort_values([x, y]) check_all_nan(data) dupe_cols = [x, y] if agg is not None: z_title = '{} ({})'.format(z_title, AGGS[agg]) if agg == 'corr': data = data.dropna() data = data.set_index([x, y]).unstack().corr() data = data.stack().reset_index(0, drop=True) y_title = x_title dupe_cols = [ '{}{}'.format(col, i) for i, col in enumerate(data.index.names) ] [x, y] = dupe_cols data.index.names = dupe_cols data = data.reset_index() data.loc[data[x] == data[y], z] = np.nan hm_kwargs = dict_merge( hm_kwargs, dict(colorscale=[[0, 'red'], [0.5, 'yellow'], [1.0, 'green']], zmin=-1, zmax=1)) else: data = build_agg_data(data, x, y, inputs, agg, z=z) if not len(data): raise Exception('No data returned for this computation!') check_exceptions( data[dupe_cols], agg != 'corr', data_limit=40000, limit_msg= 'Heatmap exceeds {} cells, cannot render. Please apply filter...') dtypes = { c: classify_type(dtype) for c, dtype in get_dtypes(data).items() } data_f, _ = chart_formatters(data) data = data_f.format_df(data) data = data.sort_values([x, y]) data = data.set_index([x, y]) data = data.unstack(0)[z] x_data = weekday_tick_handler(data.columns, x) y_data = weekday_tick_handler(data.index.values, y) heat_data = data.values x_axis = dict_merge({ 'title': x_title, 'tickangle': -20 }, build_spaced_ticks(x_data)) if dtypes.get(x) == 'I': x_axis['tickformat'] = '.0f' y_axis = dict_merge({ 'title': y_title, 'tickangle': -20 }, build_spaced_ticks(y_data)) if dtypes.get(y) == 'I': y_axis['tickformat'] = '.0f' hovertemplate = ''.join([ x_title, ': %{customdata[0]}<br>', y_title, ': %{customdata[1]}<br>', z_title, ': %{z}<extra></extra>' ]) hm_kwargs = dict_merge( hm_kwargs, dict(z=heat_data, colorbar={'title': z_title}, hoverinfo='x+y+z', hovertemplate=hovertemplate, customdata=[[[xd, yd] for xd in x_data] for yd in y_data])) return wrapper( dcc.Graph(id='heatmap-graph-{}'.format(y), style={ 'margin-right': 'auto', 'margin-left': 'auto', 'height': 600 }, figure=dict(data=[go.Heatmap(**hm_kwargs)], layout=build_layout( dict_merge( dict(xaxis=x_axis, yaxis=y_axis, xaxis_zeroline=False, yaxis_zeroline=False), build_title(x, y, z=z, agg=agg)))))) except BaseException as e: return build_error(str(e), str(traceback.format_exc()))