Example #1
0
    def it_can_generate_xml_for_multi_level_cat_charts(self):
        chart_data = CategoryChartData()
        WEST = chart_data.add_category("WEST")
        WEST.add_sub_category("SF")
        WEST.add_sub_category("LA")
        EAST = chart_data.add_category("EAST")
        EAST.add_sub_category("NY")
        EAST.add_sub_category("NJ")
        chart_data.add_series("Series 1", (1, 2, None, 4))
        chart_data.add_series("Series 2", (5, None, 7, 8))
        xml_writer = _BarChartXmlWriter(XL_CHART_TYPE.BAR_CLUSTERED, chart_data)

        assert xml_writer.xml == snippet_text("4x2-multi-cat-bar")
Example #2
0
def when_I_add_a_clustered_bar_chart_with_multi_level_categories(context):
    chart_type = XL_CHART_TYPE.BAR_CLUSTERED
    chart_data = CategoryChartData()

    WEST = chart_data.add_category('WEST')
    WEST.add_sub_category('SF')
    WEST.add_sub_category('LA')
    EAST = chart_data.add_category('EAST')
    EAST.add_sub_category('NY')
    EAST.add_sub_category('NJ')

    chart_data.add_series('Series 1', (1, 2, None, 4))
    chart_data.add_series('Series 2', (5, None, 7, 8))

    context.chart = context.slide.shapes.add_chart(chart_type, Inches(1),
                                                   Inches(1), Inches(8),
                                                   Inches(5), chart_data).chart
    def multi_fixture(self):
        chart_data = CategoryChartData()

        WEST = chart_data.add_category('WEST')
        WEST.add_sub_category('SF')
        WEST.add_sub_category('LA')
        EAST = chart_data.add_category('EAST')
        EAST.add_sub_category('NY')
        EAST.add_sub_category('NJ')

        chart_data.add_series('Series 1', (1, 2, None, 4))
        chart_data.add_series('Series 2', (5, None, 7, 8))

        xml_writer = _BarChartXmlWriter(XL_CHART_TYPE.BAR_CLUSTERED,
                                        chart_data)
        expected_xml = snippet_text('4x2-multi-cat-bar')
        return xml_writer, expected_xml
Example #4
0
    def multi_fixture(self):
        chart_data = CategoryChartData()

        WEST = chart_data.add_category("WEST")
        WEST.add_sub_category("SF")
        WEST.add_sub_category("LA")
        EAST = chart_data.add_category("EAST")
        EAST.add_sub_category("NY")
        EAST.add_sub_category("NJ")

        chart_data.add_series("Series 1", (1, 2, None, 4))
        chart_data.add_series("Series 2", (5, None, 7, 8))

        xml_writer = _BarChartXmlWriter(XL_CHART_TYPE.BAR_CLUSTERED,
                                        chart_data)
        expected_xml = snippet_text("4x2-multi-cat-bar")
        return xml_writer, expected_xml
def when_I_add_a_clustered_bar_chart_with_multi_level_categories(context):
    chart_type = XL_CHART_TYPE.BAR_CLUSTERED
    chart_data = CategoryChartData()

    WEST = chart_data.add_category("WEST")
    WEST.add_sub_category("SF")
    WEST.add_sub_category("LA")
    EAST = chart_data.add_category("EAST")
    EAST.add_sub_category("NY")
    EAST.add_sub_category("NJ")

    chart_data.add_series("Series 1", (1, 2, None, 4))
    chart_data.add_series("Series 2", (5, None, 7, 8))

    context.chart = context.slide.shapes.add_chart(
        chart_type, Inches(1), Inches(1), Inches(8), Inches(5), chart_data
    ).chart
Example #6
0
def when_I_add_a_clustered_bar_chart_with_multi_level_categories(context):
    chart_type = XL_CHART_TYPE.BAR_CLUSTERED
    chart_data = CategoryChartData()

    WEST = chart_data.add_category('WEST')
    WEST.add_sub_category('SF')
    WEST.add_sub_category('LA')
    EAST = chart_data.add_category('EAST')
    EAST.add_sub_category('NY')
    EAST.add_sub_category('NJ')

    chart_data.add_series('Series 1', (1, 2, None, 4))
    chart_data.add_series('Series 2', (5, None, 7, 8))

    context.chart = context.slide.shapes.add_chart(
        chart_type, Inches(1), Inches(1), Inches(8), Inches(5), chart_data
    ).chart
Example #7
0
    def multi_fixture(self):
        chart_data = CategoryChartData()

        WEST = chart_data.add_category('WEST')
        WEST.add_sub_category('SF')
        WEST.add_sub_category('LA')
        EAST = chart_data.add_category('EAST')
        EAST.add_sub_category('NY')
        EAST.add_sub_category('NJ')

        chart_data.add_series('Series 1', (1, 2, None, 4))
        chart_data.add_series('Series 2', (5, None, 7, 8))

        xml_writer = _BarChartXmlWriter(
            XL_CHART_TYPE.BAR_CLUSTERED, chart_data
        )
        expected_xml = snippet_text('4x2-multi-cat-bar')
        return xml_writer, expected_xml
Example #8
0
def ChartData_from_DataFrame(df, number_format="0%", xl_number_format='0.00%'):
    """
    Return a CategoryChartData instance from the given Pandas DataFrame.
    """
    def get_parent(sub_categories, line, pos):
        """
        Return the sub_category's parent given its lineage position.
        """

        for subcat in sub_categories:
            if subcat.label == line[pos]:
                return subcat

    cd = CategoryChartData(number_format=number_format)

    if isinstance(df.index, pd.MultiIndex):
        cats = []
        for line in df.index.unique().tolist():
            for l, lvl in enumerate(line):
                if l == 0:
                    if not any([lvl == cat.label for cat in cats]):
                        cats.append(cd.add_category(lvl))
                else:
                    parent = get_parent(cats, line, 0)
                    if l > 1:
                        for i in range(1, l):
                            parent = get_parent(parent.sub_categories, line, i)
                    sub_categories = parent.sub_categories
                    seen = [lvl == subcat.label for subcat in sub_categories]
                    if not any(seen):
                        parent.add_sub_category(lvl)
    else:
        categories = tuple(df.index.values.tolist())
        cd.categories = categories

    for col in df.columns:
        values = [
            value if value == value else None
            for value in df[col].values.tolist()
        ]
        if isinstance(col, tuple):
            series = (" - ".join(col), tuple(values))
        else:
            series = (col, tuple(values))

        cd.add_series(*series, number_format=xl_number_format)

    return cd