How to Set Chart Axis Properties using Java

I’m rendering the attached chart from Java code. I want to change the rendering of the axis labels (which are now: 0, 0.5, 1 e.t.c.).

I want to change it to whole integers, so my axis becomes: 0,1,2,3,4. Note: I also want to show the 4.

How can I do this? I look to the functions of Shape, Chart, ShapeRenderer, ChartSeriesCollection, ChartSeries, DataLabelCollection, DataLabel but I cant find anything to to this.

1 Like

Hi Herbert,
Thanks for your inquiry. We suggest you please recreate new series as shown below. Hope this helps you.
If you still face problem, please share your input word document here for our reference. We will then provide you more information about your query.

Document doc = new Document("input.docx");

for (Shape shape : (Iterable)doc.getChildNodes(NodeType.SHAPE, true))
{
    if (shape.hasChart())
    {
        // Word Chart
        // Chart property of Shape contains all chart related options.
        Chart chart = shape.getChart();

        // Get chart series collection.
        ChartSeriesCollection seriesColl = chart.getSeries();

        // Delete all old series, or just the one you would like to update seriesColl.RemoveAt(1);.
        seriesColl.clear();

        // Create category names array, in this example we have two categories.
        String[] categories = new String[] { "AW Category 1", "AW Category 2" };

        // Adding new series. Please note, data arrays must not be empty and arrays must be the same size.
        seriesColl.add("AW Series 1", categories, new double[] { 1, 2 });
        seriesColl.add("AW Series 2", categories, new double[] { 3, 4 });
        seriesColl.add("AW Series 3", categories, new double[] { 5, 6 });
        seriesColl.add("AW Series 4", categories, new double[] { 7, 8 });
        seriesColl.add("AW Series 5", categories, new double[] { 9, 10 });
    }
}

doc.save("UpdateChart.docx");

Thank you. That’s how I’m using it. I’m replacing a merge field in my IFieldMergingCallback extended class and I’m inserting a Chart.

builder.moveToMergeField(e.getDocumentFieldName());
Shape shape = builder.insertChart(type, chartMx.getWidth(), chartMx.getHeight());
// Chart property of Shape contains all chart related options.
com.aspose.words.Chart chart = shape.getChart();
chart.getTitle().setText(chartMx.getCaption());
// Get chart series collection.
ChartSeriesCollection seriesColl = chart.getSeries();

So I’m not using a preconfigured chart in Word. From your code I understand that that’s also possible? Insert chart already in template and then add data?
See my template attached. I’m merging a Radar Chart to «RadarChart»

Hi Herbert,

Thanks for sharing the detail. Yes, you can update the chart as shared in my previous post. In your case, we suggest you please create the chart from scratch.

Unfortunately, Aspose.Words does not provide API to format chart axis. Please create a simple Java application (source code without compilation errors) that creates the chart showing undesired behavior. We will then provide you more information about your query along with code. Thanks for your cooperation.

@Herbertv123

We added support to format the chart axis in Aspose.Words for Java 17.12.0. Now, you can format chart axis X/Y/Z. Please read the following article for more detail.
How to Set Chart Axis Properties

The API changes detail is mentioned in the release notes of Aspose.Words for Java 17.12.