com.aspose.words

  • java.lang.Object
    • com.aspose.words.Chart
public class Chart 
extends java.lang.Object

Provides access to the chart shape properties.

Example:

Shows how to insert a chart and change its title.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Use a document builder to insert a bar chart
Shape chartShape = builder.insertChart(ChartType.BAR, 400.0, 300.0);

Assert.assertEquals(chartShape.getShapeType(), ShapeType.NON_PRIMITIVE);
Assert.assertTrue(chartShape.hasChart());

// Get the chart object from the containing shape
Chart chart = chartShape.getChart();

// Set the title text, which appears at the top center of the chart and modify its appearance
ChartTitle title = chart.getTitle();
title.setText("MyChart");
title.setOverlay(true);
title.setShow(true);

doc.save(getArtifactsDir() + "Charts.ChartTitle.docx");

Property Getters/Setters Summary
ChartAxisgetAxisX()
Provides access to properties of the X axis of the chart.
ChartAxisgetAxisY()
Provides access to properties of the Y axis of the chart.
ChartAxisgetAxisZ()
Provides access to properties of the Z axis of the chart.
ChartLegendgetLegend()
Provides access to the chart legend properties.
ChartSeriesCollectiongetSeries()
Provides access to series collection.
ChartTitlegetTitle()
Provides access to the chart title properties.
 

    • Property Getters/Setters Detail

      • getAxisX

        public ChartAxis getAxisX()
        
        Provides access to properties of the X axis of the chart.

        Example:

        Shows how to insert chart using the axis options for detailed configuration.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert chart.
        Shape shape = builder.insertChart(ChartType.COLUMN, 432.0, 252.0);
        Chart chart = shape.getChart();
        
        // Clear demo data.
        chart.getSeries().clear();
        chart.getSeries().add("Aspose Test Series",
                new String[]{"Word", "PDF", "Excel", "GoogleDocs", "Note"},
                new double[]{640.0, 320.0, 280.0, 120.0, 150.0});
        
        // Get chart axes
        ChartAxis xAxis = chart.getAxisX();
        ChartAxis yAxis = chart.getAxisY();
        
        // For 2D charts like the one we made, the Z axis is null
        Assert.assertNull(chart.getAxisZ());
        
        // Set X-axis options
        xAxis.setCategoryType(AxisCategoryType.CATEGORY);
        xAxis.setCrosses(AxisCrosses.MINIMUM);
        xAxis.setReverseOrder(false);
        xAxis.setMajorTickMark(AxisTickMark.INSIDE);
        xAxis.setMinorTickMark(AxisTickMark.CROSS);
        xAxis.setMajorUnit(10.0);
        xAxis.setMinorUnit(15.0);
        xAxis.setTickLabelOffset(50);
        xAxis.setTickLabelPosition(AxisTickLabelPosition.LOW);
        xAxis.setTickLabelSpacingIsAuto(false);
        xAxis.setTickMarkSpacing(1);
        
        // Set Y-axis options
        yAxis.setCategoryType(AxisCategoryType.AUTOMATIC);
        yAxis.setCrosses(AxisCrosses.MAXIMUM);
        yAxis.setReverseOrder(true);
        yAxis.setMajorTickMark(AxisTickMark.INSIDE);
        yAxis.setMinorTickMark(AxisTickMark.CROSS);
        yAxis.setMajorUnit(100.0);
        yAxis.setMinorUnit(20.0);
        yAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO_AXIS);
      • getAxisY

        public ChartAxis getAxisY()
        
        Provides access to properties of the Y axis of the chart.

        Example:

        Shows how to insert chart using the axis options for detailed configuration.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert chart.
        Shape shape = builder.insertChart(ChartType.COLUMN, 432.0, 252.0);
        Chart chart = shape.getChart();
        
        // Clear demo data.
        chart.getSeries().clear();
        chart.getSeries().add("Aspose Test Series",
                new String[]{"Word", "PDF", "Excel", "GoogleDocs", "Note"},
                new double[]{640.0, 320.0, 280.0, 120.0, 150.0});
        
        // Get chart axes
        ChartAxis xAxis = chart.getAxisX();
        ChartAxis yAxis = chart.getAxisY();
        
        // For 2D charts like the one we made, the Z axis is null
        Assert.assertNull(chart.getAxisZ());
        
        // Set X-axis options
        xAxis.setCategoryType(AxisCategoryType.CATEGORY);
        xAxis.setCrosses(AxisCrosses.MINIMUM);
        xAxis.setReverseOrder(false);
        xAxis.setMajorTickMark(AxisTickMark.INSIDE);
        xAxis.setMinorTickMark(AxisTickMark.CROSS);
        xAxis.setMajorUnit(10.0);
        xAxis.setMinorUnit(15.0);
        xAxis.setTickLabelOffset(50);
        xAxis.setTickLabelPosition(AxisTickLabelPosition.LOW);
        xAxis.setTickLabelSpacingIsAuto(false);
        xAxis.setTickMarkSpacing(1);
        
        // Set Y-axis options
        yAxis.setCategoryType(AxisCategoryType.AUTOMATIC);
        yAxis.setCrosses(AxisCrosses.MAXIMUM);
        yAxis.setReverseOrder(true);
        yAxis.setMajorTickMark(AxisTickMark.INSIDE);
        yAxis.setMinorTickMark(AxisTickMark.CROSS);
        yAxis.setMajorUnit(100.0);
        yAxis.setMinorUnit(20.0);
        yAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO_AXIS);
      • getAxisZ

        public ChartAxis getAxisZ()
        
        Provides access to properties of the Z axis of the chart.

        Example:

        Shows how to insert chart using the axis options for detailed configuration.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert chart.
        Shape shape = builder.insertChart(ChartType.COLUMN, 432.0, 252.0);
        Chart chart = shape.getChart();
        
        // Clear demo data.
        chart.getSeries().clear();
        chart.getSeries().add("Aspose Test Series",
                new String[]{"Word", "PDF", "Excel", "GoogleDocs", "Note"},
                new double[]{640.0, 320.0, 280.0, 120.0, 150.0});
        
        // Get chart axes
        ChartAxis xAxis = chart.getAxisX();
        ChartAxis yAxis = chart.getAxisY();
        
        // For 2D charts like the one we made, the Z axis is null
        Assert.assertNull(chart.getAxisZ());
        
        // Set X-axis options
        xAxis.setCategoryType(AxisCategoryType.CATEGORY);
        xAxis.setCrosses(AxisCrosses.MINIMUM);
        xAxis.setReverseOrder(false);
        xAxis.setMajorTickMark(AxisTickMark.INSIDE);
        xAxis.setMinorTickMark(AxisTickMark.CROSS);
        xAxis.setMajorUnit(10.0);
        xAxis.setMinorUnit(15.0);
        xAxis.setTickLabelOffset(50);
        xAxis.setTickLabelPosition(AxisTickLabelPosition.LOW);
        xAxis.setTickLabelSpacingIsAuto(false);
        xAxis.setTickMarkSpacing(1);
        
        // Set Y-axis options
        yAxis.setCategoryType(AxisCategoryType.AUTOMATIC);
        yAxis.setCrosses(AxisCrosses.MAXIMUM);
        yAxis.setReverseOrder(true);
        yAxis.setMajorTickMark(AxisTickMark.INSIDE);
        yAxis.setMinorTickMark(AxisTickMark.CROSS);
        yAxis.setMajorUnit(100.0);
        yAxis.setMinorUnit(20.0);
        yAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO_AXIS);
      • getLegend

        public ChartLegend getLegend()
        
        Provides access to the chart legend properties.

        Example:

        Shows how to edit the appearance of a chart's legend.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert a line graph
        Shape chartShape = builder.insertChart(ChartType.LINE, 450.0, 300.0);
        Chart chart = chartShape.getChart();
        
        // Get its legend
        ChartLegend legend = chart.getLegend();
        
        // By default, other elements of a chart will not overlap with its legend
        Assert.assertFalse(legend.getOverlay());
        
        // We can move its position by setting this attribute
        legend.setPosition(LegendPosition.TOP_RIGHT);
        
        doc.save(getArtifactsDir() + "Charts.ChartLegend.docx");
      • getSeries

        public ChartSeriesCollection getSeries()
        
        Provides access to series collection.

        Example:

        Shows an appropriate graph type for each chart series.
        public void chartSeriesCollection() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // There are 4 ways of populating a chart's series collection
            // 1: Each series has a string array of categories, each with a corresponding data value
            // Some of the other possible applications are bar, column, line and surface charts
            Chart chart = appendChart(builder, ChartType.COLUMN, 300.0, 300.0);
        
            // Create and name 3 categories with a string array
            String[] categories = {"Category 1", "Category 2", "Category 3"};
        
            // Create 2 series of data, each with one point for every category
            // This will generate a column graph with 3 clusters of 2 bars
            chart.getSeries().add("Series 1", categories, new double[]{76.6, 82.1, 91.6});
            chart.getSeries().add("Series 2", categories, new double[]{64.2, 79.5, 94.0});
        
            // Categories are distributed along the X-axis while values are distributed along the Y-axis
            Assert.assertEquals(chart.getAxisX().getType(), ChartAxisType.CATEGORY);
            Assert.assertEquals(chart.getAxisY().getType(), ChartAxisType.VALUE);
        
            // 2: Each series will have a collection of dates with a corresponding value for each date
            // Area, radar and stock charts are some of the appropriate chart types for this
            chart = appendChart(builder, ChartType.AREA, 300.0, 300.0);
        
            // Create a collection of dates to serve as categories
            Date[] dates = {DocumentHelper.createDate(2014, 3, 31),
                    DocumentHelper.createDate(2017, 1, 23),
                    DocumentHelper.createDate(2017, 6, 18),
                    DocumentHelper.createDate(2019, 11, 22),
                    DocumentHelper.createDate(2020, 9, 7)
            };
        
            // Add one series with one point for each date
            // Our sporadic dates will be distributed along the X-axis in a linear fashion 
            chart.getSeries().add("Series 1", dates, new double[]{15.8, 21.5, 22.9, 28.7, 33.1});
        
            // 3: Each series will take two data arrays
            // Appropriate for scatter plots
            chart = appendChart(builder, ChartType.SCATTER, 300.0, 300.0);
        
            // In each series, the first array contains the X-coordinates and the second contains respective Y-coordinates of points
            chart.getSeries().add("Series 1", new double[]{3.1, 3.5, 6.3, 4.1, 2.2, 8.3, 1.2, 3.6}, new double[]{3.1, 6.3, 4.6, 0.9, 8.5, 4.2, 2.3, 9.9});
            chart.getSeries().add("Series 2", new double[]{2.6, 7.3, 4.5, 6.6, 2.1, 9.3, 0.7, 3.3}, new double[]{7.1, 6.6, 3.5, 7.8, 7.7, 9.5, 1.3, 4.6});
        
            // Both axes are value axes in this case
            Assert.assertEquals(chart.getAxisX().getType(), ChartAxisType.VALUE);
            Assert.assertEquals(chart.getAxisY().getType(), ChartAxisType.VALUE);
        
            // 4: Each series will be built from three data arrays, used for bubble charts
            chart = appendChart(builder, ChartType.BUBBLE, 300.0, 300.0);
        
            // The first two arrays contain X/Y coordinates like above and the third determines the thickness of each point
            chart.getSeries().add("Series 1", new double[]{1.1, 5.0, 9.8}, new double[]{1.2, 4.9, 9.9}, new double[]{2.0, 4.0, 8.0});
        
            doc.save(getArtifactsDir() + "Charts.ChartSeriesCollection.docx");
        }
        
        /// <summary>
        /// Get the DocumentBuilder to insert a chart of a specified ChartType, width and height and clean out its default data
        /// </summary>
        private Chart appendChart(DocumentBuilder builder, /*ChartType*/int chartType, double width, double height) throws Exception {
            Shape chartShape = builder.insertChart(chartType, width, height);
            Chart chart = chartShape.getChart();
            chart.getSeries().clear();
        
            Assert.assertEquals(chart.getSeries().getCount(), 0);
        
            return chart;
        }
      • getTitle

        public ChartTitle getTitle()
        
        Provides access to the chart title properties.

        Example:

        Shows how to insert a chart and change its title.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Use a document builder to insert a bar chart
        Shape chartShape = builder.insertChart(ChartType.BAR, 400.0, 300.0);
        
        Assert.assertEquals(chartShape.getShapeType(), ShapeType.NON_PRIMITIVE);
        Assert.assertTrue(chartShape.hasChart());
        
        // Get the chart object from the containing shape
        Chart chart = chartShape.getChart();
        
        // Set the title text, which appears at the top center of the chart and modify its appearance
        ChartTitle title = chart.getTitle();
        title.setText("MyChart");
        title.setOverlay(true);
        title.setShow(true);
        
        doc.save(getArtifactsDir() + "Charts.ChartTitle.docx");