Hi, I am trying to determine the height of the plot area of Horizontal Bar Chart in C#.
I am using the following code:
chart.ValidateChartLayout();
float plotAreaHeight = foundChart.PlotArea.ActualHeight;
but it is not giving me the value I am expecting.
If I vary the plot height, for example by using:
chart.PlotArea.Height = .5F;
it returns the same value whatever the value for height I use.
Can you give me some guidance on how to correctly determine the plot area height?
I have observed the issue shared by you and request you to please share the source presentation along with working sample code reproducing the issue on your end. Please also share the expected output as well that you want Aspose.Slides to offer. We will investigate the issue further on our end to help you out.
Hi, here is some example code that reproduces the issue. I am using Aspose.Slides for .NET 4.0 17.8. I have tried two ways to try and achieve what I want but both give different values. I have attached the output plus what I am trying to achieve. Aspose Example.zip (34.2 KB)
Thanks.
//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, 100, 100, 350, 400);
// Clear the current example data from the chart
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
chart.ChartData.ChartDataWorkbook.Clear(0);
// Create dummy data
string[] prods = new string[] {"Product 1", "Product 2", "Product 3", "Product 4", "Product 5"};
double[] values = new double[] { 5.4, 6.5, 5.2, 6.4, 5.2 };
int[] colours = new int[] {6740479, 9359529, 11892015, 10498160, 8696052};
// Set the default chart worksheet index
int defaultWorksheetIndex = 0;
// Get the chart data worksheet
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Add a series (Mean)
IChartSeries series = chart.ChartData.Series.Add(workbook.GetCell(defaultWorksheetIndex, 0, 1, "Mean"), chart.Type);
IChartDataPoint point;
// Add the data to the workbook
for (int i = 0; i < prods.Length; i++)
{
chart.ChartData.Categories.Add(workbook.GetCell(defaultWorksheetIndex, i + 1, 0, prods[i]));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, i + 1, 1, values[i]));
// Colour the bars
Color color = From0BGR(colours[i]);
point = series.DataPoints[i];
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = color;
}
// Add data labels
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// Tidy up the chart
// Remove legend
chart.HasLegend = false;
// Remove vertical axis labels
chart.Axes.VerticalAxis.TickLabelPosition = TickLabelPositionType.None;
// Remove gridlines
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
// Set bar widths
series.ParentSeriesGroup.GapWidth = 20;
// Set plot area to top of chart
chart.PlotArea.Height = 1;
// Get plot area height
chart.ValidateChartLayout();
float xAxisLocation = chart.PlotArea.ActualY;
// Calculate required height
float height = chart.Height - xAxisLocation;
// Add box next to axis
IShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 5, 100, 95, height);
// Try .ActualHeight instead
chart.ValidateChartLayout();
height = chart.PlotArea.ActualHeight;
// Add another shape
IShape shape2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 5, 100, 95, height);
// Save presentation.
string filePath = System.IO.Path.GetTempPath() + "/horizontal_bar.pptx";
presentation.Save(filePath, SaveFormat.Pptx);
// Open the file
System.Diagnostics.Process.Start(filePath);
Doing a bit of testing it seems that if you adjust the plot area this isn’t picked up when you call chart.ValidateChartLayout(), you always get the original values.
I have worked with the sample code shared by you and it seems to be an issue. An issue with ID SLIDESNET-39297 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed.
I like to share that the concerned issue has been resolved in Aspose.Slides for .NET 17.10. I request you to please try using following sample code with specified version on your end.
public static void TestPresIssue()
{
//create PPT document.
Presentation presentation = new 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, 100, 100, 350, 400);
// Clear the current example data from the chart
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
chart.ChartData.ChartDataWorkbook.Clear(0);
// Create dummy data
string[] prods = new string[] { "Product 1", "Product 2", "Product 3", "Product 4", "Product 5" };
double[] values = new double[] { 5.4, 6.5, 5.2, 6.4, 5.2 };
int[] colours = new int[] { 6740479, 9359529, 11892015, 10498160, 8696052 };
// Set the default chart worksheet index
int defaultWorksheetIndex = 0;
// Get the chart data worksheet
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Add a series (Mean)
IChartSeries series = chart.ChartData.Series.Add(workbook.GetCell(defaultWorksheetIndex, 0, 1, "Mean"), chart.Type);
IChartDataPoint point;
// Add the data to the workbook
for (int i = 0; i < prods.Length; i++)
{
chart.ChartData.Categories.Add(workbook.GetCell(defaultWorksheetIndex, i + 1, 0, prods[i]));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, i + 1, 1, values[i]));
// Colour the bars
Color color = From0BGR(colours[i]);
// Color color =colours[i];
point = series.DataPoints[i];
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = color;
}
// Add data labels
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// Tidy up the chart
// Remove legend
chart.HasLegend = false;
// Remove vertical axis labels
chart.Axes.VerticalAxis.TickLabelPosition = TickLabelPositionType.None;
// Remove gridlines
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
// Set bar widths
series.ParentSeriesGroup.GapWidth = 20;
// Set plot area to top of chart
chart.PlotArea.Height = 1;
chart.PlotArea.Width = 1;
chart.PlotArea.X = 0;
chart.PlotArea.Y = 0;
chart.ValidateChartLayout();
float height = chart.PlotArea.ActualHeight;
slide.Shapes.AddAutoShape(ShapeType.Rectangle, 5, 100, 95, height);
// Save presentation.
string filePath ="C:\\Aspose Data\\" + "horizontal_bar_17.10.pptx";
presentation.Save(filePath, Aspose.Slides.Export.SaveFormat.Pptx);
// Open the file
System.Diagnostics.Process.Start(filePath);
}
public static Color From0BGR(int bgrColor)
{
// Get the color bytes
var bytes = BitConverter.GetBytes(bgrColor);
// Return the color from the byte array
return Color.FromArgb(bytes[0], bytes[1], bytes[2]);
}