I am using Aspose.Slides to add a ClusteredBar chart to a slide. I need to set the MaxValue and MinValue of the horizontal axis and I want to get the automatic ActualMajorUnit back. The following is the funciton I use. First I just try to get the ActualMajorUnit after calling ValidateChartLayout(). Then I set the MaxValue and MinValue before I try to get the ActualMajorUnit. But both give me 0. Can you give me a complete sample code of how to set the MaxValue and MinValue of the horizontal axis of a ClusteredBar chart and get the ActualMajorUnit back?
private void AdjustAxisMinMax(IAxis axis, double maxValue, double minValue)
{
axis.IsAutomaticMajorUnit = true;
axis.Chart.ValidateChartLayout();
System.Diagnostics.Trace.Write($"axis.ActualMajorUnit = {axis.ActualMajorUnit}");
// Set the MaxValue and MinValue
axis.IsAutomaticMaxValue = false;
axis.IsAutomaticMinValue = false;
axis.MaxValue = maxValue;
axis.MinValue = minValue;
axis.Chart.ValidateChartLayout();
System.Diagnostics.Trace.Write($"axis.ActualMajorUnit = {axis.ActualMajorUnit}");
}
The version of Aspose.Slides for .NET I used is 21.9.0.
@admangowilson,
Thank you for describing the issue.
Could you please share the sample code generating the chart and code lines calling the AdjustAxisMinMax
method?
Here is the function that generate the chart:
public void AddBarChart_GoogleFormat(JToken chartObj, ISlide slide, float x, float y, float width, float height, bool addOutline = false)
{
var sizeScale = CalculateSizeScale(width, height);
var chart = slide.Shapes.AddChart(ChartType.ClusteredBar, x, y, width, height, true);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var wb = chart.ChartData.ChartDataWorkbook;
wb.Clear(0);
#region add title
chart.ChartTitle.AddTextFrameForOverriding((string)chartObj["ChartOption"]["title"]);
chart.ChartTitle.Overlay = false;
chart.HasTitle = true;
#endregion
#region add series
var colArray = (JArray)chartObj["ChartData"]["cols"];
for (int i = 1; i < colArray.Count; i++)
{
var seriesName = (string)colArray[i]["label"];
chart.ChartData.Series.Add(wb.GetCell(0, 0, i, seriesName), chart.Type);
}
#endregion
#region determine what is shown on the horizontal axis (value or %)
var ShowPercentage = false;
var hAxisFormat = (string)chartObj.SelectToken("ChartOption.hAxis.format");
if (hAxisFormat != null)
{
if (hAxisFormat.EndsWith("%"))
{
ShowPercentage = true;
}
}
#endregion
#region add categories and data
var allDataPositive = true;
var allDataNegative = true;
var rowArray = (JArray)chartObj["ChartData"]["rows"];
for (int i = 0; i < rowArray.Count; i++)
{
var seriesArray = (JArray)rowArray[i]["c"];
var categoryName = (string)seriesArray[0]["v"];
chart.ChartData.Categories.Add(wb.GetCell(0, i + 1, 0, categoryName));
for (int j = 1; j < seriesArray.Count; j++)
{
var series = chart.ChartData.Series[j - 1];
if (ShowPercentage)
{
var value = (double)seriesArray[j]["v"];
series.DataPoints.AddDataPointForBarSeries(wb.GetCell(0, i + 1, j, value));
if (value >= 0)
allDataNegative = false;
else if (value < 0)
allDataPositive = false;
}
else
{
var value = (long)seriesArray[j]["v"];
series.DataPoints.AddDataPointForBarSeries(wb.GetCell(0, i + 1, j, value));
if (value >= 0)
allDataNegative = false;
else if (value < 0)
allDataPositive = false;
}
}
}
#endregion
#region other settings
GoogleFormat_SetFont(chart, chartObj, sizeScale);
GoogleFormat_SetLegendPosition(chart, chartObj);
if (addOutline)
{
AddChartOutline(chart);
}
#region AdjustAxisMinMax
var maxValueToken = chartObj.SelectToken("ChartOption.hAxis.maxValue");
var minValueToken = chartObj.SelectToken("ChartOption.hAxis.minValue");
double maxValue = 0;
double minValue = 0;
if (maxValueToken != null)
maxValue = (double)maxValueToken;
if (minValueToken != null)
minValue = (double)minValueToken;
if (maxValue != 0 || minValue != 0)
{
AdjustAxisMinMax(chart.Axes.HorizontalAxis, maxValue, minValue);
}
#endregion
if (ShowPercentage)
FormatPercentageAxis(chart.Axes.HorizontalAxis);
else
FormatValueAxis(chart.Axes.HorizontalAxis);
chart.Axes.VerticalAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.Axes.VerticalAxis.CrossType = CrossesType.Maximum;
chart.Axes.VerticalAxis.IsPlotOrderReversed = true;
if (allDataPositive && chart.Axes.HorizontalAxis.IsAutomaticMinValue)
{
chart.Axes.HorizontalAxis.IsAutomaticMinValue = false;
chart.Axes.HorizontalAxis.MinValue = 0;
}
else if (allDataNegative && chart.Axes.HorizontalAxis.IsAutomaticMaxValue)
{
chart.Axes.HorizontalAxis.IsAutomaticMaxValue = false;
chart.Axes.HorizontalAxis.MaxValue = 0;
}
var colorArray = (JArray)chartObj["ChartOption"]["colors"];
for (int i = 0; i < chart.ChartData.Series.Count; i++)
{
var colorIndex = i % colorArray.Count; ;
var colorHex = (string)colorArray[colorIndex];
var color = System.Drawing.ColorTranslator.FromHtml(colorHex);
var series = chart.ChartData.Series[i];
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = color;
series.InvertIfNegative = false;
}
#endregion
}
@admangowilson,
Unfortunately, I was unable to use the code example above to reproduce the problem you are experiencing. Please try to isolate the problem and share a standalone code example or the simplest project.
I have created a .net framework console program to reproduce the issue. Here is the code:
internal class Program
{
static void Main(string[] args)
{
Aspose.Slides.License license = new Aspose.Slides.License();
license.SetLicense("Aspose.Slides.NET.lic");
// Create a new presentation
var presentation = new Presentation();
// Add a new slide to the presentation
var slide = presentation.Slides.AddEmptySlide(presentation.LayoutSlides[0]);
// Add a new ClusteredBar chart to the slide
var chart = slide.Shapes.AddChart(ChartType.ClusteredBar, 50, 50, 400, 300);
var axis = chart.Axes.HorizontalAxis;
axis.IsAutomaticMajorUnit = true;
axis.Chart.ValidateChartLayout();
Console.WriteLine($"axis.ActualMajorUnit = {axis.ActualMajorUnit}");
// Set the MaxValue and MinValue
axis.IsAutomaticMaxValue = false;
axis.IsAutomaticMinValue = false;
axis.MaxValue = 7;
axis.MinValue = -2;
axis.Chart.ValidateChartLayout();
Console.WriteLine($"axis.ActualMajorUnit = {axis.ActualMajorUnit}");
// Save the presentation to a file
presentation.Save("output.pptx", SaveFormat.Pptx);
Console.ReadLine();
}
}
These are the versions I used:
.NET framework 4.6.1
Aspose.Slides.NET 22.3.0
Essentailly, I want to get the Major Units in the chart’s axis options (see the attached image).
Screenshot 2023-07-27 122215.png (201.2 KB)
@admangowilson,
Thank you for the code example, I get the same result.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): SLIDESNET-44106
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Thanks for your checking. I have one more question.
I am using the older version 22.3.0 of Aspose.Slides.NET. Is the issue solved in the latest version?
@admangowilson,
Unfortunately, previous versions of Aspose.Slides will no longer receive updates. However, the issue will be addressed in a new release. We apologize for any inconvenience this may cause.
Also, our developers will first look for a workaround for you.