@mmr-jef,
Thank you for sharing the desired output presentation. I have generated the following sample code for your convenience to serve the purpose. I have also attached the generated presentation for your kind reference as well.
public static void TestCustomerChart()
{
// Create some data
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("Term", typeof(string));
table.Columns.Add("Value", typeof(float));
table.Rows.Add(@"Term 16", -1.5);
table.Rows.Add(@"Term 15", -0.7);
table.Rows.Add(@"Term 14", -0.7);
table.Rows.Add(@"Term 13", -0.6);
table.Rows.Add(@"Term 12", -0.6);
table.Rows.Add(@"Term 11", -0.3);
table.Rows.Add(@"Term 10", -0.1);
table.Rows.Add(@"Term 9", 0.1);
table.Rows.Add(@"Term 8", 0.3);
table.Rows.Add(@"Term 7", 0.5);
table.Rows.Add(@"Term 6", 0.6);
table.Rows.Add(@"Term 5", 0.7);
table.Rows.Add(@"Term 4", 0.9);
table.Rows.Add(@"Term 3", 1.2);
table.Rows.Add(@"Term 2", 1.4);
table.Rows.Add(@"Term 1", 1.7);
//create PPT document.
Presentation presentation = new Presentation();
// Get slides from presentation.
ISlideCollection slides = presentation.Slides;
// Set slide to first slide.
ISlide slide = presentation.Slides[0];
// Add a clustered bar chart.
IChart chart = slide.Shapes.AddChart(ChartType.ClusteredBar, 208, 100, 290, 400);
// Clear the current example data from the chart
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
chart.ChartData.ChartDataWorkbook.Clear(0);
// Set the default chart worksheet index
int defaultWorksheetIndex = 0;
// Get the chart data worksheet
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Getting the chart data worksheet
IChartDataWorkbook worksheet = chart.ChartData.ChartDataWorkbook;
// Add Categories
for (int i = 0; i < table.Rows.Count; i++)
{
chart.ChartData.Categories.Add(worksheet.GetCell(defaultWorksheetIndex, i + 1, 0, table.Rows[i].ItemArray[0].ToString()));
}
// Add series
chart.ChartData.Series.Add(worksheet.GetCell(defaultWorksheetIndex, 0, 1, table.Columns[1].ColumnName), chart.Type);
IChartSeries series = chart.ChartData.Series[0];
for (int i = 0; i < table.Rows.Count; i++)
{
chart.ChartData.Categories.Add(worksheet.GetCell(defaultWorksheetIndex, i + 1, 0, table.Rows[i].ItemArray[0].ToString()));
series.DataPoints.AddDataPointForBarSeries(worksheet.GetCell(defaultWorksheetIndex, i + 1, 1, Convert.ToDecimal(table.Rows[i].ItemArray[1].ToString())));
}
chart.Axes.VerticalAxis.IsVisible = false;
chart.Axes.HorizontalAxis.IsVisible = false;
// add data labels
series.Labels.DefaultDataLabelFormat.ShowValue = true;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType=FillType.NoFill;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
chart.Axes.VerticalAxis.IsVisible = false;
chart.Axes.HorizontalAxis.IsVisible = false;
chart.BackWall.Format.Line.FillFormat.FillType = FillType.NoFill;
chart.PlotArea.Format.Line.FillFormat.FillType = FillType.NoFill;
chart.LineFormat.FillFormat.FillType = FillType.NoFill;
series.InvertIfNegative = false;
chart.HasLegend = false;
// Put boxes around data labels
chart.ValidateChartLayout();
float labelX;
float labelY;
float labelHeight;
float labelWidth;
IShape myShape;
for (int i = 0; i < chart.ChartData.Series[0].DataPoints.Count; i++)
{
int offsetX = 0;
int offsetY = 0;
if (chart.ChartData.Series[0].DataPoints[i].Value.ToDouble() >= 0)
{
offsetX = 5;
offsetY = 4;
}
else
{
offsetX = -4;
offsetY = 5;
}
chart.ValidateChartLayout();
labelX = chart.ChartData.Series[0].DataPoints[i].Label.ActualX + offsetX;
labelY = chart.ChartData.Series[0].DataPoints[i].Label.ActualY + offsetY;
labelHeight = 10;// chart.ChartData.Series[0].DataPoints[i].Label.ActualHeight;
labelWidth = chart.ChartData.Series[0].DataPoints[i].Label.ActualWidth;
myShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, chart.X + labelX, chart.Y + labelY, labelWidth, labelHeight+7);
myShape.FillFormat.FillType = FillType.NoFill;
myShape.LineFormat.Width = 2;
myShape.LineFormat.FillFormat.FillType=FillType.Solid;
myShape.LineFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(79,129,189);
chart.ChartData.Series[0].DataPoints[i].Label.TextFormat.PortionFormat.FontHeight = 18;
chart.ChartData.Series[0].DataPoints[i].Label.TextFormat.PortionFormat.LatinFont = new FontData("Calibri");
}
chart.PlotArea.Format.Line.FillFormat.FillType = FillType.NoFill;
series.InvertIfNegative = false;
chart.HasLegend = false;
// Save the file.
string filePath = System.IO.Path.GetTempPath() + "/example_chart.pptx";
presentation.Save(filePath, Aspose.Slides.Export.SaveFormat.Pptx);
// Open the file
System.Diagnostics.Process.Start(filePath);
}
Many Thanks,
Mudassir Fayyaz
example_chart.zip (29.2 KB)