Bengt Berg
Bengt Berg

Reputation: 41

python-pptx color legend not showing same colors as graph

This drives me nuts.

    chart_AllData = slideref.shapes.add_chart(XL_CHART_TYPE.BAR_STACKED_100, x, y, cx, cy, chart_data)

    chart = chart_AllData.chart        
    plot = chart.plots[0]
    pointCount = 0
    for answer in consolidatedAnswersList: # Looping over the answer codes
        print('answer: ', answer)
        catBar = plot.series[pointCount].points
        for cat in catBar: # Looping over the categories (= the declarations)
            fill = cat.format.fill
            fill.solid()
            colorString = ccpStatistics.gAnswerCodeColorDict.get(answer, 'FFFFFF')
            r = int(colorString[0]+colorString[1], 16)
            g = int(colorString[2]+colorString[3], 16)
            b = int(colorString[4]+colorString[5], 16)
            fill.fore_color.rgb = RGBColor(r , g , b)
            print('setting ', answer, ' to ', colorString)
        pointCount += 1

    print(ccpStatistics.gAnswerCodeColorDict)

    category_axis = chart.category_axis
    category_axis.minor_tick_mark = XL_TICK_MARK.OUTSIDE
    category_axis.tick_labels.font.italic = True
    category_axis.tick_labels.font.size = Pt(12)

    value_axis = chart.value_axis       
    value_axis.format.line.color.rgb = RGBColor(0 , 0 , 0)
    value_axis.tick_labels.font.size = Pt(int(thisPageDict.get('SCALEFONTSIZE', ['18'])[0]))
    value_axis.major_tick_mark = XL_TICK_MARK.OUTSIDE
    value_axis.has_major_gridlines = True
    value_axis.tick_labels.font.color.rgb = RGBColor(0,0,0)
    value_axis.major_tick_mark.number_format = '0.0"%"'

    chart.has_legend = True
    chart.legend.position = XL_LEGEND_POSITION.RIGHT
    chart.legend.include_in_layout = False # set to true
    chart.legend.font.color.rgb = RGBColor(0,0,0)
    chart.legend.font.size = Pt(int(thisPageDict.get('LEGENDSIZE', ['12'])[0]))

    plot.has_data_labels = True
    data_labels = plot.data_labels

    data_labels.position = XL_LABEL_POSITION.CENTER
    data_labels.font.size = Pt(int(thisPageDict.get('LEGENDSIZE', ['10'])[0]))
    data_labels.font.color.rgb = RGBColor(0x00, 0x00, 0x00)

The content of the ccpStatistics.gAnswerCodeColorDict is as follows:

{'oaq_NoAnswer': 'DCDCDC', 'maq_NoAnswer': 'DCDCDC', 'Answer code': 'Answer code color', '1': 'FF0000', '2': 'FF6600', '3': 'FFC301', '4': '00B050', 'OK': '00B050', 'NOK': 'FF0000', 'NA': '333333', 'NEU': 'FF00FF', 'maq': '3A87AD', 'dq': '660000', 'txta': '660000', 'nq': '660000'}

Now, the graph gets properly colored according to the settings in series... And the names of the series in the legend is OK too... But the colors of the legend does not correspond to the colors in the bar.

NOTE, similar code seems to work well in other methods. I'm at wit's end now.

See the resulting ppt chart

Upvotes: 3

Views: 977

Answers (1)

scanny
scanny

Reputation: 29021

Try setting the color at the series level rather than at the individual point level.

fill = series.format.fill
fill.solid()
fill.fore_color.rgb = RGBColor(r, g, b)

Upvotes: 5

Related Questions