pythonopenpyxl

How to remove a legend name in openpyxl


--------------- CODE SNIPPET---------------

So the code goes like this. There's some code to create a bar chart.

# create barChart

barChart= BarChart()
barChartData= Reference(worksheet, min_col=min_col, max_col=max_col, min_row=min_row, max_row=max_row)
barChart.add_data(bar_chart_data, from_rows=True, titles_from_data=True)

barChart.legend.position = 't'

... Then there's some code to create a line chart.

# create lineChart
lineChart= LineChart()
lineChartData= Reference(worksheet, min_col=min_col + 1, max_col=max_col, min_row=max_row + 1,
                               max_row=max_row + 1)
lineChart.add_data(compare_chart_data, from_rows=True)

... Then there's some code to add the line chart on top of the bar chart.

# combine both charts on top of each other
lineChart.y_axis.crosses = "min"
barChart+= lineChart

---------------END OF SNIPPET---------------

So what happens is, the chart results into having a legend that says

Bananas, Apples and Oranges are the names of the data series of the bar chart. Series 4, is supposed to be the legend for the line chart. I wanna remove it, and I've tried to do below items.

I'm fairly new to python and I have no idea how to remove the legend "Series 4" in the table that's being created by openpyxl.

barChart += lineChart

I've tried setting below code (after the addition), but it removes everything.

barChart.legend = None

I've also tried setting below code (before the addition), but it does nothing.

lineChart.legend = None

Somehow, I feel like it auto - creates the legend name Series 4 during the addition...

For the sake of testing, I tried to set

titles_from_data = True

But what this does is just change the legend name to 0%.

I don't want to set it to another value, I just want to get rid of it.

----- UPDATE -----

I found a link in the comments section recommended by WBM to try and update the record inside the LegendEntry attribute. It looks something like this.

bar_chart.legend.LegendEntry = [(openpyxl.chart.legend.LegendEntry(3, delete=1))]

Still to no avail.

For investigation, I tried to print out the contents of the LegendEntry attribute prior to me adding the above code, and after.

Before adding the code, surprisingly, the result is empty.

[]

After I added my code, it looked like something like this.

Parameters: [idx=3, delete=True, txPr=None]

Despite the LegendEntry being updated, the output still remains the same:


Solution

  • I'm using openpyxl 3.0.9.

    Looked into the source code for an idea and got this to work:

    import openpyxl
    chart.x_axis = openpyxl.chart.axis.TextAxis(delete=True)
    

    Here is more of the code:

    CHART_ROW = 11
    CHART_HEIGHT = 12
    DATA_TABLE_START_ROW = 34
    LENGTH_DIST_START_COL = 1
    SPACER_DIST_START_COL = LENGTH_DIST_START_COL + 10
    SPACER_RANGE_START_COL = SPACER_DIST_START_COL + 6
    

    . . . .

        chart = BarChart()
        chart.style = style  # 1 = black line, 13 = green bold line
        chart.title = "Spacer Distribution Summary"
        chart.height = CHART_HEIGHT  # default is 7.5
        chart.width = 7  # default is 15
        chart.y_axis.title = 'Target Spacer Frequency'
        chart.x_axis = openpyxl.chart.axis.TextAxis(delete=True)
    
        values = Reference(self.sheet,
                           min_col=SPACER_DIST_START_COL + 1,
                           max_col=SPACER_DIST_START_COL + 1,
                           min_row=self.spacer_distribution_first_row,
                           max_row=self.spacer_distribution_last_row)
        series = Series(values, title_from_data=False)
        chart.series.append(series)
        # chart.legend = None
        pos = f"{column_string(SPACER_DIST_START_COL)}{CHART_ROW}"
        self.sheet.add_chart(chart, pos)