How to change font size of labels in a pie chart

Hi,
I would like to change the font size of the lables in a pie chart.
Is it possible? How do I do it?
Regards, Moshe

PresentationEx pres = new PresentationEx();

SlideEx sld = pres.Slides[0];

Aspose.Slides.Pptx.ChartEx chart = sld.Shapes.AddChart(ChartTypeEx.Pie3D, 100, 100, 400, 400);

ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;

chart.ChartData.Series.Clear();

chart.ChartData.Categories.Clear();

chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, "First Qtr"));

chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, "2nd Qtr"));

chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, "3rd Qtr"));

chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);

ChartSeriesEx series = chart.ChartData.Series[0];

series.Values.Add(fact.GetCell(0, 1, 1, 20000));

series.Values.Add(fact.GetCell(0, 2, 1, 50000));

series.Values.Add(fact.GetCell(0, 3, 1, 30000));

series.Labels.ShowValue = true;

series.Labels.ShowPercentage = true;

series.Labels.ShowLegendKey = false;

pres.Write(@"D:\\PieChart.pptx");

Hi Moshe,


I have worked over the requirements shared by you and like to share that the LabelCollection does not involve the default labels displaying values or percentage value for the respective series data. It seems to be an inconsistency in Aspose.Slides and I have created an issue with ID SLIDESNET-33620 in our issue tracking system to further investigate and resolve the issue. However, in the mean while you may use the following approach shared by me to display the series values in user defined setting. In this approach you need to calculate the values and then show them accordingly your self. The following code will help in this regard.


PresentationEx pres = new PresentationEx();

SlideEx sld = pres.Slides[0];

Aspose.Slides.Pptx.ChartEx chart = sld.Shapes.AddChart(ChartTypeEx.Pie3D, 100, 100, 400, 400);

ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;

chart.ChartData.Series.Clear();

chart.ChartData.Categories.Clear();

chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, “First Qtr”));

chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, “2nd Qtr”));

chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, “3rd Qtr”));

chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, “Series 1”), chart.Type);

ChartSeriesEx series = chart.ChartData.Series[0];

series.Values.Add(fact.GetCell(0, 1, 1, 20000));

series.Values.Add(fact.GetCell(0, 2, 1, 50000));

series.Values.Add(fact.GetCell(0, 3, 1, 30000));

series.Labels.ShowValue = true;

series.Labels.ShowPercentage = true;

series.Labels.ShowLegendKey = false;

double sum = 0;
foreach (ChartDataCell cell in series.Values)
{
sum += Convert.ToDouble(cell.Value);
}
double percent = (Convert.ToDouble(series.Values[0].Value) / sum) * 100;


DataLabelEx lab=new DataLabelEx(series);
int id=series.Labels.Add(lab);
lab = series.Labels[id];
int sd = series.Labels.Count;
lab.ShowPercentage = true;
lab.ShowValue = true;
lab.ShowLegendKey = true;
lab.TextFrame.Text = series.Values[0].Value.ToString();

lab.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillTypeEx.Solid;
lab.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color=Color.Blue;
lab.TextFrame.Paragraphs[0].Portions[0].Text = series.Values[0].Value.ToString();
if(lab.ShowPercentage==true)
{
PortionEx percentage = new PortionEx();
percentage.PortionFormat.FillFormat.FillType = FillTypeEx.Solid;
percentage.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
percentage.Text = “\v” + percent.ToString() + " %";
lab.TextFrame.Paragraphs[0].Portions.Add(percentage);
}

pres.Write(@“D:\Aspose Data\PieChart.pptx”);


Many Thanks,

Thanks for the quick response.
Your example changes only one label. I’ve changed the code to set all 3 labels but it doesn’t work. Can you please review and see what I’m doing wrong

double sum = 0;

foreach (ChartDataCell cell in series.Values)

{

sum += Convert.ToDouble(cell.Value);

}

for (int j = 0; j < series.Values.Count; j++)

{

double percent = (Convert.ToDouble(series.Values[j].Value) / sum) * 100;

DataLabelEx lab = new DataLabelEx(series);

int id = series.Labels.Add(lab);

lab = series.Labels[id];

int sd = series.Labels.Count;

lab.ShowPercentage = true;

lab.ShowValue = true;

lab.ShowLegendKey = true;

lab.TextFrame.Text = series.Values[j].Value.ToString();

lab.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillTypeEx.Solid;

lab.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;

lab.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 8;

lab.TextFrame.Paragraphs[0].Portions[0].Text = series.Values[j].Value.ToString();

if (lab.ShowPercentage == true)

{

PortionEx percentage = new PortionEx();

percentage.PortionFormat.FillFormat.FillType = FillTypeEx.Solid;

percentage.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;

percentage.PortionFormat.FontHeight = 8;

percentage.Text = "\v" + percent.ToString() + " %";

lab.TextFrame.Paragraphs[0].Portions.Add(percentage);

}

}

pres.Write(@"F:\\src\\btca\\econ\\PieChartLbl.pptx");

Hi Moshe,


I have observed the requirements shared by you and have observed that in code snippet the association of labels with chart sector has not been done. Please assoicate the label with chart sector using following code.


double percent = (Convert.ToDouble(series.Values[j].Value) / sum) * 100;

DataLabelEx lab = new DataLabelEx(series);

int id = series.Labels.Add(lab);

//Associating label with pie chart sector
lab.Id = id;

lab = series.Labels[id];


Many Thanks,

Hi Moshe,


Please try using the following code snippet for setting the default pie chart series labels text.

public static void SetPieDefaultLabels()
{
PresentationEx pres = new PresentationEx();

SlideEx sld = pres.Slides[0];

Aspose.Slides.Pptx.ChartEx chart = sld.Shapes.AddChart(ChartTypeEx.Pie3D, 100, 100, 400, 400);

ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;

chart.ChartData.Series.Clear();

chart.ChartData.Categories.Clear();

chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, “First Qtr”));

chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, “2nd Qtr”));

chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, “3rd Qtr”));

chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, “Series 1”), chart.Type);

ChartSeriesEx series = chart.ChartData.Series[0];

series.Values.Add(fact.GetCell(0, 1, 1, 20000));

series.Values.Add(fact.GetCell(0, 2, 1, 50000));

series.Values.Add(fact.GetCell(0, 3, 1, 30000));

series.Labels.ShowValue = true;

series.Labels.ShowPercentage = true;

series.Labels.ShowLegendKey = false;

// chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, “Series 1”), chart.Type);
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillTypeEx.Solid;
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;


pres.Write(@“D:\Aspose Data\PieChart.pptx”);
}

Many Thanks,

The issues you have found earlier (filed as SLIDESNET-33620) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(5)

Hi,
I’ve changed the code in order to format the values with comma separator , but it doesn’t work

static void SetPieDefaultLabels()
{
pres = new PresentationEx();
SlideEx sld = pres.Slides[0];
Aspose.Slides.Pptx.ChartEx chart = sld.Shapes.AddChart(ChartTypeEx.Pie3D, 100, 100, 400, 400);
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, "First Qtr"));
chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, "2nd Qtr"));
chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, "3rd Qtr"));
chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);
ChartSeriesEx series = chart.ChartData.Series[0];
series.Values.Add(fact.GetCell(0, 1, 1, 20000));
series.Values.Add(fact.GetCell(0, 2, 1, 50000));
series.Values.Add(fact.GetCell(0, 3, 1, 30000));
series.Labels.ShowValue = true;
//series.Labels.ShowPercentage = true;
series.Labels.ShowLegendKey = false;
//chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillTypeEx.Solid;
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
series.Labels.NumberFormat = "#,##0";
pres.Write(@"D:\\Aspose Data\\PieChart.pptx");
}

Results should display 30,000 not 30000
See attachment

Regards,
Moshe

Hi Moshe,


Please use the following code snippet to serve the purpose. Please share, if I may help you further in this regard.

public static void SetPieDefaultLabels()
{
PresentationEx pres = new PresentationEx();

SlideEx sld = pres.Slides[0];

Aspose.Slides.Pptx.ChartEx chart = sld.Shapes.AddChart(ChartTypeEx.Pie3D, 100, 100, 400, 400);

ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;

chart.ChartData.Series.Clear();

chart.ChartData.Categories.Clear();

chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, “First Qtr”));

chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, “2nd Qtr”));

chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, “3rd Qtr”));

chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, “Series 1”), chart.Type);

ChartSeriesEx series = chart.ChartData.Series[0];

series.Values.Add(fact.GetCell(0, 1, 1, 20000));

series.Values.Add(fact.GetCell(0, 2, 1, 50000));

series.Values.Add(fact.GetCell(0, 3, 1, 30000));

series.Labels.ShowValue = true;

// series.Labels.ShowPercentage = true;

series.Labels.ShowLegendKey = false;

series.Labels.LinkedSource = false;
series.Labels.NumberFormat = “#,##0”;

series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillTypeEx.Solid;
series.Labels.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;


pres.Write(@“D:\Aspose Data\PieChart.pptx”);

}

Many Thanks,