Esempio n. 1
0
def stacked_bar_chart_by_name(summary: ExpenseSummaryMatrix) -> Figure:
    categories = list(summary.expenses)
    names = sorted(summary.names())

    amounts = np.array([[0 for name in names]] +
                       [[summary.expenses[category][name] for name in names]
                        for category in categories])
    # Build the cumulative sums over the columns
    bar_heights = np.cumsum(amounts, axis=0)

    fig, ax = plt.subplots()
    for i, category in enumerate(categories):
        ax.bar(names, amounts[i + 1], bottom=bar_heights[i], label=category)
    ax.set_title(i18n.t("title.by_name_by_category"))
    add_value_labels(ax, last=len(names))
    ax.legend()

    return fig
Esempio n. 2
0
def stacked_bar_chart_by_name(summary: ExpenseSummaryMatrix) -> Figure:
    categories = list(summary.expenses)
    names = sorted(summary.names())

    data_source = {"names": names}
    data_source.update(
        {
            category: [summary.expenses[category][name] for name in names]
            for category in categories
        }
    )

    chart = figure(
        title=i18n.t("title.by_name_by_category"),
        toolbar_location=None,
        tools="hover",
        tooltips="$name: @$name{0,0.00}",
        x_range=names,
        width=1000,
        height=600,
    )

    chart.vbar_stack(
        categories,
        x="names",
        width=0.8,
        color=color_palette(len(categories)),
        source=data_source,
    )

    # Increase the font size
    chart.title.text_font_size = TITLE_FONT_SIZE
    chart.axis.major_label_text_font_size = LEGEND_FONT_SIZE

    # Make the graph more beautiful
    chart.y_range.start = 0  # Let the bars start at the bottom
    chart.x_range.range_padding = 0.1  # Add horizontal padding
    chart.xgrid.grid_line_color = None  # Hide the vertical grid lines
    chart.axis.minor_tick_line_color = None  # Hide all minor ticks
    chart.outline_line_color = None  # Hide the outline

    return chart