Stacked bar chart not respecting a minimum x-axis setting

Hi,

We are trying to create a stacked bar chart, but unfortunately we don’t seem to be able to get the chart to respect the minimum X-axis which always seems to set at zero.

We are trying to create a chart that looks like this:

https://docs.google.com/spreadsheets/d/e/2PACX-1vROzbBfrU5X5xS0vr4Yeho5NIJUkjhL5Qz2sPxjDuOSsCnTzuJ9uQlPgLM8__cFUQ/pubchart?oid=324847637&format=interactive

Here is out code example. We would greatly appreciate if anybody has any feedback as to what we are doing wrong.

Thank you so much.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

string[] categories = new string[]
{
    "Part time work",
    "Private school",
    "Build wealth",
    "Wealth protection",
    "Retirement",
    "Bequeath to estate",
    "Holiday",
    "Car",
    "Income in retirement"
};

double[] startDates = new double[]
{
    45658,
    45292,
    44927,
    44927,
    44927,
    44927,
    44927,
    44927,
    48214
};

double[] duration = new double[]
{
    2556,
    2192,
    3661,
    3661,
    3287,
    10592,
    365,
    451,
    7305
};

double[] totalLength = startDates.Select((x, index) => x + duration[index]).ToArray();

ChartType chartType = ChartType.BarStacked;

Shape shape = builder.InsertChart(chartType, 432, 252);
Chart chart = shape.Chart;

ChartSeriesCollection seriesColl = chart.Series;
seriesColl.Clear();

seriesColl.Add("Start dates", categories, startDates.ToArray());
seriesColl.Add("Duration", categories, duration.ToArray());

//seriesColl.Add("Start dates", startDates, startDates.ToArray());
//seriesColl.Add("Duration", startDates, duration.ToArray());

chart.Series[0].Format.Fill.Visible = false;

chart.Title.Show = false;
chart.Legend.Position = LegendPosition.None;

ChartAxis xAxis = chart.AxisX;

// Set X axis bounds.
xAxis.Scaling.Minimum = new AxisBound(startDates.Min());
xAxis.Scaling.Maximum = new AxisBound(totalLength.Max());

chart.AxisX.NumberFormat.FormatCode = "dd MMM yy";

doc.Save(@"C:\Temp\PrimeSolve\gantt-demo.docx");

@timhall Your code is almost correct, but instead of X-Axis, you should specify scaling of Y-Axis. In Bar Chart, X-Axis is a category axis and is vertical. Please see the followingmodified code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

string[] categories = new string[]
{
    "Part time work",
    "Private school",
    "Build wealth",
    "Wealth protection",
    "Retirement",
    "Bequeath to estate",
    "Holiday",
    "Car",
    "Income in retirement"
};

double[] startDates = new double[]
{
    45658,
    45292,
    44927,
    44927,
    44927,
    44927,
    44927,
    44927,
    48214
};

double[] duration = new double[]
{
    2556,
    2192,
    3661,
    3661,
    3287,
    10592,
    365,
    451,
    7305
};

Color[] dataPointColors = new Color[]
{
    Color.Orange,
    Color.Yellow,
    Color.LightYellow,
    Color.LawnGreen,
    Color.Green,
    Color.LightBlue,
    Color.Blue,
    Color.DarkBlue,
    Color.Violet
};

double[] totalLength = startDates.Select((x, index) => x + duration[index]).ToArray();

ChartType chartType = ChartType.BarStacked;

Shape shape = builder.InsertChart(chartType, 432, 252);
Chart chart = shape.Chart;

ChartSeriesCollection seriesColl = chart.Series;
seriesColl.Clear();

seriesColl.Add("Start dates", categories, startDates.ToArray());
seriesColl.Add("Duration", categories, duration.ToArray());

// Hide the first series
chart.Series[0].Format.Fill.Visible = false;

// Show datalabels of the second series
chart.Series[1].DataLabels.ShowValue = true;

// Change colors of data points in the second series.
for(int i=0; i< dataPointColors.Length; i++)
    chart.Series[1].DataPoints[i].Format.Fill.ForeColor = dataPointColors[i];

chart.Title.Show = false;
chart.Legend.Position = LegendPosition.None;

ChartAxis yAxis = chart.AxisY;

// Set Y axis bounds.
yAxis.Scaling.Minimum = new AxisBound(startDates.Min());
yAxis.Scaling.Maximum = new AxisBound(totalLength.Max());
// Set number format of the Y axis.
yAxis.NumberFormat.FormatCode = "dd MMM yy";

doc.Save(@"C:\Temp\out.docx");

out.docx (8.8 KB)

The issues you have found earlier (filed as WORDSNET-21965) have been fixed in this Aspose.Words for .NET 24.12 update also available on NuGet.