def transform(self, data_frame, slicer, dimensions, references): import matplotlib.pyplot as plt data_frame = data_frame.copy() n_axes = len(self.items) figsize = (14, 5 * n_axes) fig, plt_axes = plt.subplots(n_axes, sharex='row', figsize=figsize) fig.suptitle(self.title) if not hasattr(plt_axes, '__iter__'): plt_axes = (plt_axes, ) colors = itertools.cycle('bgrcmyk') for axis, plt_axis in zip(self.items, plt_axes): for series in axis: series_color = next(colors) linestyles = itertools.cycle(['-', '--', '-.', ':']) for reference in [None] + references: metric = series.metric f_metric_key = utils.alias_selector( reference_alias(metric, reference)) f_metric_label = reference_label(metric, reference) plot = self.get_plot_func_for_series_type( data_frame[f_metric_key], f_metric_label, series) plot(ax=plt_axis, label=axis.label, color=series_color, stacked=series.stacking is not None, linestyle=next(linestyles)) \ .legend(loc='center left', bbox_to_anchor=(1, 0.5)) return plt_axes
def __init__(self, item, reference): assert isinstance(reference, Reference) self.data_type = item.data_type self.alias = reference_alias(item, reference) self.label = reference_label(item, reference) self.prefix = reference_prefix(item, reference) self.suffix = reference_suffix(item, reference) self.thousands = item.thousands self.precision = item.precision
def _render_pie_series(self, metric: Field, reference: Reference, data_frame: pd.DataFrame, dimension_fields: List[Field]) -> dict: metric_alias = utils.alias_selector(metric.alias) if self.split_dimension: dimension_fields = [ dimension for dimension in dimension_fields if dimension != self.split_dimension ] data_frame = data_frame.reset_index(alias_selector( self.split_dimension.alias), drop=True) data = [] for dimension_values, y in data_frame[metric_alias].iteritems(): dimension_values = utils.wrap_list(dimension_values) name = self._format_dimension_values(dimension_fields, dimension_values) data.append({ "name": name or metric.label, "y": formats.raw_value(y, metric) }) return { "name": reference_label(metric, reference), "type": "pie", "data": data, "tooltip": { "pointFormat": '<span style="color:{point.color}">\u25CF</span> {series.name}: ' "<b>{point.y} ({point.percentage:.1f}%)</b><br/>", "valueDecimals": metric.precision, "valuePrefix": reference_prefix(metric, reference), "valueSuffix": reference_suffix(metric, reference), }, }
def _render_pie_series(self, metric, reference, data_frame, dimension_fields): metric_alias = utils.alias_selector(metric.alias) data = [] for dimension_values, y in data_frame[metric_alias].iteritems(): dimension_values = utils.wrap_list(dimension_values) name = self._format_dimension_values(dimension_fields, dimension_values) data.append( {"name": name or metric.label, "y": formats.raw_value(y, metric),} ) return { "name": reference_label(metric, reference), "type": "pie", "data": data, "tooltip": { "pointFormat": '<span style="color:{point.color}">\u25CF</span> {series.name}: ' "<b>{point.y} ({point.percentage:.1f}%)</b><br/>", "valueDecimals": metric.precision, "valuePrefix": reference_prefix(metric, reference), "valueSuffix": reference_suffix(metric, reference), }, }
def _render_highcharts_series( self, series, series_df: pd.DataFrame, references: List[Reference], dimension_label: str, is_timeseries: bool, symbol: str, axis_idx: int, axis_color: str, series_color: str, ): """ Note on colors: - With a single axis, use different colors for each series - With multiple axes, use the same color for the entire axis and only change the dash style :param series: :param series_df: :param references: :param dimension_label: :param is_timeseries: :param symbol: :param axis_idx: :param axis_color: :param series_color: :return: """ if is_timeseries: series_df = series_df.sort_index(level=0) results = [] for reference, dash_style in zip([None] + references, itertools.cycle(DASH_STYLES)): field_alias = utils.alias_selector( reference_alias(series.metric, reference)) metric_label = reference_label(series.metric, reference) if field_alias not in series_df: continue hc_series = { "type": series.type, "name": "{} ({})".format(metric_label, dimension_label) if dimension_label else metric_label, "data": (self._render_timeseries_data(series_df, field_alias, series.metric) if is_timeseries else self._render_category_data( series_df, field_alias, series.metric)), "tooltip": self._render_tooltip(series.metric, reference), "yAxis": ("{}_{}".format(axis_idx, reference.alias) if reference is not None and reference.delta else str(axis_idx)), "marker": ({ "symbol": symbol, "fillColor": axis_color or series_color } if isinstance(series, SERIES_NEEDING_MARKER) else {}), "stacking": series.stacking, } if isinstance(series, ContinuousAxisSeries): # Set each series in a continuous series to a specific color hc_series["color"] = series_color hc_series["dashStyle"] = dash_style results.append(hc_series) return results