Java: Create Column Charts in Word Documents

2024-08-23 06:42:05 Written by  support iceblue
Rate this item
(0 votes)

Column charts, also known as bar charts, provide a visual comparison of data points across different categories. Whether you're summarizing sales figures, tracking project milestones, or visualizing survey results, column charts in Word provide a powerful way to translate complex data into an accessible, engaging format within your written materials.

In this article, you will learn how to create a clustered column chart and a stacked column chart in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.9.0</version>
    </dependency>
</dependencies>
    

Create a Clustered Column Chart in Word in Java

To insert a chart into a Microsoft Word document, you can use the Paragraph.appendChart(ChartType chartType, float width, float height) method. The ChartType enumeration provides various pre-defined chart types available in MS Word. To create a clustered column chart, you would specify the chart type as Column.

The steps to add a clustered column chart to a Word document using Java are as follows:

  • Create a Document object.
  • Add a section and a paragraph to the document.
  • Add a clustered column chart to the paragraph using Paragraph.appendChart() method.
  • Add series to the chart using Chart.getSeries().add() method.
  • Set the chart title using Chart.getTilte().setText() method.
  • Set other attributes of the chart using the methods available in the Chart object.
  • Save the document to a different Word file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.shapes.charts.*;

public class CreateClusteredColumnChart {

    public static void main(String[] args) {

        // Create a Document object
        Document document = new Document();

        // Add a section
        Section section = document.addSection();

        // Add a paragraph
        Paragraph paragraph = section.addParagraph();

        // Add a column chart
        ShapeObject shape = paragraph.appendChart(ChartType.Column, 490, 250);

        // Get the chart
        Chart chart = shape.getChart();

        // Clear the default data
        chart.getSeries().clear();

        // Add a series including series name, category names, and series values to chart
        chart.getSeries().add("June",
                new String[] { "Cuba", "Mexico", "France"},
                new double[] { 5000, 8000, 9000 });

        // Add two more series
        chart.getSeries().add("July",
                new String[] { "Cuba", "Mexico", "France"},
                new double[] { 4000, 5000, 7000 });
        chart.getSeries().add("August",
                new String[] { "Cuba", "Mexico", "France"},
                new double[] { 3500, 7000, 5000 });

        // Set the chart title
        chart.getTitle().setText("Sales by Country");

        // Set the number format of the Y-axis
        chart.getAxisY().getNumberFormat().setFormatCode("#,##0");

        // Set the legend position
        chart.getLegend().setPosition(LegendPosition.Bottom);

        // Save to file
        document.saveToFile("ClusteredColumnChart.docx", FileFormat.Docx_2019);

        // Dispose resources
        document.dispose();
    }
}

Java: Create Column Charts in Word Documents

Create a Stacked Column Chart in Word in Java

Creating a stacked column chart in a Word document follows a similar process to the clustered column chart. The only difference is specifying the chart type as Column_Stacked instead of Column.

The detailed steps to add a stacked column chart are:

  • Create a Document object.
  • Add a section and a paragraph to the document.
  • Add a stacked column chart to the paragraph using Paragraph.appendChart() method.
  • Add series to the chart using Chart.getSeries().add() method.
  • Set the chart title using Chart.getTilte().setText() method.
  • Set other attributes of the chart using the methods available in the Chart object.
  • Save the document to a different Word file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.shapes.charts.Chart;
import com.spire.doc.fields.shapes.charts.ChartType;
import com.spire.doc.fields.shapes.charts.LegendPosition;

public class CreateStackedColumnChart {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a paragraph
        Paragraph paragraph = section.addParagraph();

        //Add a stacked column chart
        ShapeObject shape = paragraph.appendChart(ChartType.Column_Stacked, 490, 250);

        //Get the chart
        Chart chart = shape.getChart();

        //Clear the default data
        chart.getSeries().clear();

        //Add a series including series name, category names, and series values to chart
        chart.getSeries().add("Store A",
                new String[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
                new double[] { 2500, 4600, 2800, 5100 });

        //Add another series
        chart.getSeries().add("Store B",
                new String[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
                new double[] { 4100, 3200, 3800, 4000 });

        //Set the chart title
        chart.getTitle().setText("Store Wise Soda Soft Drink Sales");

        //Set the number format of the Y-axis
        chart.getAxisY().getNumberFormat().setFormatCode("#,##0");

        //Set the legend position
        chart.getLegend().setPosition(LegendPosition.Bottom);

        //Save to file
        document.saveToFile("StackedColumnChart.docx", FileFormat.Docx_2019);

        // Dispose resources
        document.dispose();
    }
}

Java: Create Column Charts in Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.