Pergunta

I have decided to use XlsxWriter to produce a common output product for many countries. While I am impressed with the modules flexibility, I can't seem to figure out how to modify labels in charts appropriately. The 'text_wrap' capability does not appear to be contained within 'set_x_axis', which results in very long labels rotated at 45 degrees. As can be seen below, I tried to make it happen anyway (just to see what would happen), and the statement is ignored.

"""Allow for text wrap for x_axis labels (doesn't seem to do anything)"""
chart_list[2].set_x_axis({'text_wrap':True})

The consequence is a chart area that keeps to the specified dimensions at the external boundaries, but the plot area itself is dominated by the label length. Does anyone have an idea of how to do this? Right now, my only (subpar) solution is to decrease the font size and use abbreviations. This brings with it loss of clarity and there is no room for explanatory text.

UPDATE: I am including a code block to demonstrate length of labels as requested. (Sorry for the delay, I ended up getting swamped and had to put it down.) You can see the length of the desired labels. They end up coming out diagonal and squishing the chart area. I have not been able to test your fix yet, but I did feel I needed to get back to you with more specifics on what I meant. I'd like those labels to be wrapped instead of diagonal (which I described generally as label orientation). Thanks for taking a look! (And yes, label was incorrect. I meant category label...)

 """Chart III - Macro-Level Comparisons in Euros >>> 
    (GDP per capita,Average Gross Annual Salary, Government Expenditure per capita) translates to ...
    Index(Per Capita GDP (Euros), Average gross annual salary (Euros), Public Expenditure Per Capita (€))
    DATA: All must be written to new sheet from outdset_build(country)
    """
    #Create list of relevant figures (have to use lev distance because the euro symbol causes match problems)
    chart3_figures=['Per Capita GDP (Euros)', 'Average gross annual salary (Euros)', 'Public Expenditure Per Capita (€)']
    c3_figs=[]
    for i in range(len(chart3_figures)):
        figs=process.extract(chart3_figures[i],outdset_build(country).index.get_level_values('figure'),limit=1)[0]
        c3_figs.append(figs[0])
    #Generate subindex to subset data 
    chart3_index=outdset_build(country).index.get_level_values('figure').isin(c3_figs)
    #Subset data
    chart3_data=outdset_build(country)[chart3_index]

    #Create new sheet
    temp_data=jagx.add_worksheet('misc_data')

    #Write data to new sheet
    temp_data.write_row('A1',['index','EU','EU-10','CROATIA'])
    temp_data.write_column('A2', ['Per Capita GDP','Average Gross Annual Salary', \
                                  'Public Expenditure Per Capita'])
    temp_data.write_column('B2',chart3_data['EU'])
    temp_data.write_column('C2',chart3_data['EU-10'])
    temp_data.write_column('D2',chart3_data[country])

    #Configure series
    chart_list[2].add_series({
          'name':'EU',
          'categories':'misc_data!A2:A4',
          'values':'misc_data!B2:B4'
    })
    chart_list[2].add_series({
          'name':'EU-10',
          'categories':'misc_data!A2:A4',
          'values':'misc_data!C2:C4'
    })
    chart_list[2].add_series({
          'name':'CROATIA',
          'categories':'misc_data!A2:A4',
          'values':'misc_data!D2:D4'
    })
    #Add title 
    chart_list[2].set_title({'name':'Macro-level Comparison in Euros (per capita)','name_font':{'size':9}})
Foi útil?

Solução

Could you clarify a small bit. The title talks about label orientation but the body is talking about text wrap. Also, I'm not quite clear what you mean by label. In Excel charts a label is associated with a data point. From the code it looks like you are trying to wrap the axis title (Excel's name for it).

Sorry for sounding picky, I just want to clarify what is the issue. I'm willing to fix it if required. Maybe you could update your post with a small working example.

There is support for axis number font and axis title font orientation in the GitHub head although it isn't in PyPI yet. However, non-automatic text wrapping of axis titles isn't supported by XlsxWriter (or afaik) by Excel.

Update: Chart axis font orientation is now in XlsxWriter version 0.3.4 on PyPI:

chart.set_x_axis({'num_font':  {'rotation': 45}})
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top