X-axis Y-Axis labels

Is there any provision available for displaying data labels or a single label on x-axis and y-axis? That can indicate what it is actually, for example: year, age etc. I’ve created a chart and i’m unable to set the labels. Attaching pictures of the actual and desired chart.
graph.PNG (8.2 KB)
Graph-With-Labels-Example.png (9.2 KB)

@wajiha,
Unfortunately, Aspose.Words does not support setting axis title of chart at the moment. This missing feature is already logged in our issue tracking system as WORDSNET - 13933. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

However, you can use the Aspose.Words.Layout classes to insert a TextBox with desired axis label into your document separately from a chart. Please check the following code example:

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

Shape chart = builder.InsertChart(ChartType.ColumnStacked, 400, 400);
chart.Chart.Legend.Position = LegendPosition.None;

var collector = new LayoutCollector(doc);
var enumerator = new LayoutEnumerator(doc);
enumerator.Current = collector.GetEntity(chart);

var tb = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.LeftMargin, enumerator.Rectangle.X + 150, RelativeVerticalPosition.TopMargin, enumerator.Rectangle.Y + 385, 100, 10, WrapType.None);
tb.TextBox.FitShapeToText = true;
tb.Stroked = false;

tb.RemoveAllChildren();
Paragraph p = new Paragraph(doc);
tb.AppendChild(p);
builder.MoveTo(p);
builder.Write("X axis title"); 

doc.Save("graph.docx");

@sergey.lobanov thank you for your suggestion. I’ll try this. Can i do same thing for y axis?

I tried your example but its not working, if i save as pdf. Please have a look.
Axis title.PNG (7.6 KB)

@wajiha,
To fix this issue, please save your document as docx document first, and then convert it to pdf:

doc.Save("graph.docx");
doc = new Document("graph.docx");
doc.Save("graph.pdf");

You can do the same with Y axis by changing the vertical and horizontal position of the inserted TextBox.

Please check the full code example:

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

Shape chart = builder.InsertChart(ChartType.ColumnStacked, 400, 400);
chart.Chart.Legend.Position = LegendPosition.None;

var collector = new LayoutCollector(doc);
var enumerator = new LayoutEnumerator(doc);
enumerator.Current = collector.GetEntity(chart);

var tb_x = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.LeftMargin, enumerator.Rectangle.X + 175, RelativeVerticalPosition.TopMargin, enumerator.Rectangle.Y + 400, 100, 10, WrapType.None);
tb_x.TextBox.FitShapeToText = true;
tb_x.Stroked = false;
tb_x.RemoveAllChildren();
Paragraph p_x = new Paragraph(doc);
tb_x.AppendChild(p_x);
builder.MoveTo(p_x);
builder.Write("X axis title");

builder.MoveToDocumentStart();
            
var tb_y = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.LeftMargin, enumerator.Rectangle.X - 65, RelativeVerticalPosition.TopMargin, enumerator.Rectangle.Y + 200, 100, 10, WrapType.None);
tb_y.TextBox.FitShapeToText = true;
tb_y.Stroked = false;
tb_y.RemoveAllChildren();
Paragraph p_y = new Paragraph(doc);
tb_y.AppendChild(p_y);
builder.MoveTo(p_y);
builder.Write("Y axis title");
            
doc.Save("graph.docx");
doc = new Document("graph.docx");
doc.Save("graph.pdf");

@sergey.lobanov thank you so much. It works! :slight_smile:

@sergey.lobanov is it possible to calculate relative to chart? When i’m applying above mentioned logic on my actual document y-axis textbox is appearing somewhere on first page. I want to calculate enumerator.Rectangle.Y relatively from the chart that I just inserted. Because my document is dynamic, chart can appear anywhere on any page.

@wajiha
To get the desired result with charts, appearing on different pages, please use the DocumentBuilder.MoveTo(chart) method. Please check the following code example:

var collector = new LayoutCollector(doc);
var enumerator = new LayoutEnumerator(doc);
foreach (Node chartNode in doc.GetChildNodes(NodeType.Shape, true))
{
    Shape chart = (Shape)chartNode;
    if (!chart.HasChart)
        continue;
    enumerator.Current = collector.GetEntity(chart);

    builder.MoveTo(chart);

    var tb_x = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.LeftMargin, enumerator.Rectangle.X + chart.Width/2 - 25, RelativeVerticalPosition.TopMargin, enumerator.Rectangle.Y + chart.Height, 65, 10, WrapType.None);
    tb_x.TextBox.FitShapeToText = true;
    tb_x.Stroked = false;
    tb_x.RemoveAllChildren();
    Paragraph p_x = new Paragraph(doc);
    tb_x.AppendChild(p_x);
    builder.MoveTo(p_x);
    builder.Write("X axis title");

    builder.MoveTo(chart);

    var tb_y = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.LeftMargin, enumerator.Rectangle.X - 65, RelativeVerticalPosition.TopMargin, enumerator.Rectangle.Y + chart.Height/2, 65, 10, WrapType.None);
    tb_y.TextBox.FitShapeToText = true;
    tb_y.Stroked = false;
    tb_y.RemoveAllChildren();
    Paragraph p_y = new Paragraph(doc);
    tb_y.AppendChild(p_y);
    builder.MoveTo(p_y);
    builder.Write("Y axis title");
}

@sergey.lobanov thanks

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