Setting properties for pie charts in Python

Hi,

I need your help.
I want to create a pie chart and configure its properties.
Getting the default pie is no problem but I haven’t found examples that explains
how to set colors, no labels etc for pie charts in Aspose Words for Python.

What I would like to do is:

  • Set colors, i.e. red and green.
    When I use “series.format.fill.fore_color = pydraw.Color.red”
    the whole pie gets red and I haven’t found how to set colors individually.

  • I only want the percentage values to be shown, 66% & 34%. Not “series1 - Value2 - 234” etc.

  • I want to set the dividing line to be thinner or remove it.

  • Change font size for “Title”.

  • Remove the line surrounding the sqaure, “chart”, so that it blends in better in the doc.

Any help explaining this is much appreciated.

The code I have right now:

shape1 = builder.insert_chart(aw.drawing.charts.ChartType.PIE,200,150)
chart = shape1.chart
chart.title.text = "Title"
chart.series.clear()

series = chart.series.add("series1",
["Value1", "Value2"],
[123, 234])
	
series.format.fill.fore_color = pydraw.Color.red
series.has_data_labels = True
labels = series.data_labels
labels.show_percentage = True
labels.show_value = False
labels.separator = " - "

@SuneVav

  1. You can use ChartDataPoint.format property to configure color and stroke of slices in Pie chart.

  2. You can specify ChartDataLabelCollection.show_percentage to should percentage value on data label.

  3. You can configure stroke of the slice using ChartDataPoint.format property.

  4. ChartTitle.font can be used to configure font of title.

  5. Just set Shape.stroked property to false to remove border around the chart shape.

Please see the following code:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

chartShape = builder.insert_chart(aw.drawing.charts.ChartType.PIE, 200, 150)
# Rremove border about the chart.
chartShape.stroked = False
chart = chartShape.chart
# Configure title.
chart.title.text = "Chart Title"
chart.title.font.size = 20
chart.title.font.bold = True
chart.title.font.color = pydraw.Color.blue

chart.series.clear()
series = chart.series.add("series 1", ["Value1", "Value2"], [123, 234])
# Configure color of slices.
series.data_points[0].format.fill.color = pydraw.Color.red
series.data_points[1].format.fill.color = pydraw.Color.green
# Configure slice dividing lines between slices
series.data_points[0].format.stroke.color = pydraw.Color.black
series.data_points[0].format.stroke.weight = 2
series.data_points[1].format.stroke.color = pydraw.Color.black
series.data_points[1].format.stroke.weight = 2
# Configure data labels.
series.data_labels.show_percentage = True

doc.save("C:\\Temp\\out.docx")

out.docx (8.3 KB)

1 Like

Thanks Alexey!

That was exactly what I was looking for but couldn’t find.

When I use
“series.data_labels.show_percentage = True”
I still get the text “series1 , Value1,” next to the slices.
Is there a way to remove that text?

@SuneVav Sure, you can use ChartDataLabelCollection properties to configure all data labels and ChartDataLabel properties to configure an individual data label. Just set the appropriate show_XXX properties to true or false value.

1 Like

It’s almost perfect now but I have one last, I hope, question.

I get one of the values inside the pie and the other outside using percentage.
Is there a way to force both values to be outside?

@SuneVav I am afraid currently there is no public API to specify data label position. This feature request is logged as WORDSNET-21965. We will keep you updated and let you know once it is implemented.

Ok, I see.That will be a welcome addition at least for me.

Thanks a lot for your help! :slight_smile:

1 Like

@SuneVav As an alternative to building chart form scratch, you can consider building the chart using LINQ Reporting Engine. in this case you can configure the chart up to your requirements and then simply fill it with data using LINQ Reporting Engine. For example you can use JSON or XML as a data source:
https://docs.aspose.com/words/python-net/linq-reporting-engine-api/

Thanks, but that seems complicated for building 1 or 2 charts embedded in a document.
I found, however, that by adjusting chart height I could make both values end up outside.
In my example I reduced height from 150 to 130 (with font size 8).
It now looks exactly like I want it.

@SuneVav It is perfect that you managed to achieve what is required. Please feel free to ask in case of any another issues we will be glad to help you.

1 Like

The issues you have found earlier (filed as WORDSNET-21965) have been fixed in this Aspose.Words for .NET 24.12 update also available on NuGet.