Hi,
Hi Shruti,
I have observed your requirements and like to share that you can add the Bar chart using Aspose.Slides for .NET. I suggest you to please try using the following sample code on your end to serve the purpose. I hope the shared information will be helpful. Please share, if I may help you further in this regard.
public static void AddBarChart()
{
//Instantiate Presentation class that represents PPTX file//Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();
//Access first slide
ISlide sld = pres.Slides[0];
// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredBar, 0, 0, 500, 500);
//Setting chart Title
// chart.ChartTitle.TextFrameForOverriding.Text = “Sample Title”;
chart.ChartTitle.AddTextFrameForOverriding(“Sample Title”);
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
//Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
//Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
//Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;
//Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, “Series 1”), chart.Type);
//Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, “XHtml /CSS 1”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, “WordPress”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, “Illustrator”));
//Take first chart series
IChartSeries series = chart.ChartData.Series[0];
//Now populating series data
IChartDataPoint point=series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = Color.Red;
point=series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = Color.Pink;
series.Labels.IsVisible = true;
point=series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = Color.Green;
series.ParentSeriesGroup.IsColorVaried = true;
series.ParentSeriesGroup.GapWidth = 0;
pres.Save(“D:\Aspose Data\AsposeChart.pptx”, SaveFormat.Pptx);
}
Many Thanks,
Hi Mudassir,
Hi Shruti,
series.Labels.DefaultDataLabelFormat.ShowCategoryName=true;series.Labels.DefaultDataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
Hi Adnan,
Hi,
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(true);
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
series.getLabels().getDefaultDataLabelFormat().setPosition(LegendDataLabelPosition.InsideBase);
Thanks,
Shruti
Hi Shruti,
I have observed your requirements and request you to please share the requirements in the form of sample presentation and snapshot highlighting requirement. I will observe the requirement on my end to help you further in this regard.
Many Thanks,
Hi Mudassir,
currentSlide = pres.getSlides().get_Item(4);
// Add bar chart
IChart brkOutLocsChart = (IChart) findShape("breakout_locations_chart");
int categories = brkOutLocsChart.getChartData().getCategories().size();
float height=brkOutLocsChart.getHeight()/(categories+2);
for(int i=0;i<categories;i++){
IAutoShape ashp = currentSlide.getShapes().addAutoShape(ShapeType.Rectangle, brkOutLocsChart.getX()+70, (float) (brkOutLocsChart.getY()+15+ 1.1 *height *i), brkOutLocsChart.getWidth(), height-1);
ashp.addTextFrame("").setText("");
ITextFrame txtFrame;
IPortionFormat pf;
FontData fdArial = new FontData("Arial");
IParagraph para;
IPortion portion;
ashp.addTextFrame(" ");
txtFrame = ashp.getTextFrame();
para = txtFrame.getParagraphs().get_Item(0);
para.getParagraphFormat().setAlignment(TextAlignment.Left);
portion = para.getPortions().get_Item(0);
portion.setText("Category "+i);
pf = portion.getPortionFormat();
pf.getFillFormat().setFillType(FillType.Solid);
pf.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
pf.setLatinFont(fdArial);
pf.setFontHeight(12);
ashp.getFillFormat().setFillType(FillType.NoFill);
ashp.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
}
//End
Please help me with this use case. Thanks in advance.
-Shruti
Hi Shruti,
I have observed your requirements and like to share that chart area is rendered dynamically based on chart series and categories data. You can have know access till chart top left corner and bottom right corner. But what is inside chart area is dynamic. You need to figure out your own strategy in this regard by setting different values for autoshape positions and their respective mappings on chart bars. I hope the shared information will be helpful.
Many Thanks,
Hi,
Hi Shruti,
I have tried creating the working sample fulfilling your needs. Please try using following sample code on your end to serve the purpose. I hope this will be helpful. For your kind reference, I have attached the generated presentation as well.
public static void AddBarChart()
{
//Instantiate Presentation class that represents PPTX file//Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();
//Access first slide
ISlide sld = pres.Slides[0];
// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredBar, 0, 0, 500, 500);
//Setting chart Title
// chart.ChartTitle.TextFrameForOverriding.Text = “Sample Title”;
chart.ChartTitle.AddTextFrameForOverriding(“Sample Title”);
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
//Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
//Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
//Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;
//Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, “Horse”), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, “Camel”), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, “Cat”), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 4, “Dog”), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 5, “Lion”), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 6, “Tiger”), chart.Type);
//Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, “59.1 GB”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, “56.3 GB”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, “51.6 GB”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 4, 0, “49.0 GB”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 5, 0, “48.9 GB”));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 6, 0, “41.2 GB”));
//Take first chart series
IChartSeries series = chart.ChartData.Series[0];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(185, 205, 229);
//Now populating series data
IChartDataPoint point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 59.1));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 3, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 4, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 5, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 6, String.Empty));
series.DataPoints[0].Label.DataLabelFormat.ShowSeriesName = true;
series.DataPoints[0].Label.DataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
series = chart.ChartData.Series[1];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(185, 205, 229);
//Now populating series data
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 56.3));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 4, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 5, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 6, String.Empty));
series.Labels.DefaultDataLabelFormat.ShowSeriesName = false;
series.DataPoints[1].Label.DataLabelFormat.ShowSeriesName = true;
series.DataPoints[1].Label.DataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
series = chart.ChartData.Series[2];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(185, 205, 229);
//Now populating series data
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 51.6));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 4, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 5, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 6, String.Empty));
series.Labels.DefaultDataLabelFormat.ShowSeriesName = false;
series.DataPoints[2].Label.DataLabelFormat.ShowSeriesName = true;
series.DataPoints[2].Label.DataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
series = chart.ChartData.Series[3];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(185, 205, 229);
//Now populating series data
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 1, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 2, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 4, 49.0));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 5, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 6, String.Empty));
series.Labels.DefaultDataLabelFormat.ShowSeriesName = false;
series.DataPoints[3].Label.DataLabelFormat.ShowSeriesName = true;
series.DataPoints[3].Label.DataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
series = chart.ChartData.Series[4];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(185, 205, 229);
//Now populating series data
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 1, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 2, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 4, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 5, 48.9));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 6, String.Empty));
series.Labels.DefaultDataLabelFormat.ShowSeriesName = false;
series.DataPoints[4].Label.DataLabelFormat.ShowSeriesName = true;
series.DataPoints[4].Label.DataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
series = chart.ChartData.Series[5];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(185, 205, 229);
//Now populating series data
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 1, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 2, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 3, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 4, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 5, String.Empty));
point = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 6, 41.2));
series.Labels.DefaultDataLabelFormat.ShowSeriesName = false;
series.DataPoints[5].Label.DataLabelFormat.ShowSeriesName = true;
series.DataPoints[5].Label.DataLabelFormat.Position = LegendDataLabelPosition.InsideBase;
chart.DisplayBlanksAs = DisplayBlanksAsType.Span;
chart.ChartData.SeriesGroups[0].GapDepth = 10;
chart.ChartData.SeriesGroups[0].GapWidth = 100;
chart.ChartData.SeriesGroups[0].Overlap = 80;
chart.Axes.HorizontalAxis.IsVisible=false;
chart.Axes.HorizontalAxis.AxisBetweenCategories = false;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
chart.Axes.VerticalAxis.MajorTickMark = TickMarkType.None;
chart.HasLegend = false;
pres.Save(“D:\Aspose Data\AsposeChart.pptx”, SaveFormat.Pptx);
}
Many Thanks,
Hi Mudassir,
Hi Shruti,
You are always welcome. Please share, if I may help you further in this regard.
Many Thanks,
Hi Mudassir,
series.getDataPoints().get_Item(counter).getLabel().getDataLabelFormat().setShowSeriesName(true);
series.getDataPoints().get_Item(counter).getLabel().getDataLabelFormat().setPosition(LegendDataLabelPosition.InsideBase);
series.getDataPoints().get_Item(counter).getLabel().getTextFormat().getTextBlockFormat().setWrapText((byte) 10000000);
series.getDataPoints().get_Item(counter).getLabel().getTextFormat().getPortionFormat().setFontHeight(10);
Hi Shruti,
Can you please provide the desired output presentation with specified labels positioning. I will assess the requirements on my end to help you further in this regard.
Many Thanks,
Hi Mudassir,
Hi Shruti,
I have observed the presentation file shared by you and tried opening that in PowerPoint 2010. I have not been able to observe any issue in opened presentation on my end and labels seem intact. For your kind reference, I suggest you to please visit this snapshot that that I have taken on my end and has no issue in labels. Can you please provide the exact complete PowerPoint version that you are using on your end to reproduce the issue of chart labels.
Many Thanks,
Hi,
Windows 10 - Version 1607, OS BUILD - 14393.187
Office 365 Version - 16.0.7301.1019
Thanks,
Shruti
Hi Adnan,