Set Gap Width in Java Slides

Introduction to Setting Gap Width in Aspose.Slides for Java

In this tutorial, we will guide you through the process of setting the Gap Width for a chart in a PowerPoint presentation using Aspose.Slides for Java. Gap Width determines the spacing between the columns or bars in a chart, allowing you to control the visual appearance of the chart.

Prerequisites

Before you begin, make sure you have the Aspose.Slides for Java library installed. You can download it from the Aspose website here.

Step-by-Step Guide

Follow these steps to set the Gap Width in a chart using Aspose.Slides for Java:

1. Create an Empty Presentation

// The path to the documents directory.
String dataDir = "Your Document Directory";

// Creating an empty presentation 
Presentation presentation = new Presentation();

2. Access the First Slide

// Access the first slide
ISlide slide = presentation.getSlides().get_Item(0);

3. Add a Chart with Default Data

// Add a chart with default data
IChart chart = slide.getShapes().addChart(ChartType.StackedColumn, 0, 0, 500, 500);

4. Set the Index of Chart Data Sheet

// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

5. Get the Chart Data Workbook

// Getting the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();

6. Add Series to the Chart

// Add series to the chart
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());

7. Add Categories to the Chart

// Add categories to the chart
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Category 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Category 2"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Category 3"));

8. Populate Series Data

// Populate series data
IChartSeries series = chart.getChartData().getSeries().get_Item(1);

// Populating series data points
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));

9. Set the Gap Width

// Set the Gap Width value
series.getParentSeriesGroup().setGapWidth(50);

10. Save the Presentation

// Save the presentation with the chart
presentation.save(dataDir + "GapWidth_out.pptx", SaveFormat.Pptx);

Complete Source Code For Set Gap Width in Java Slides

// The path to the documents directory.
String dataDir = "Your Document Directory";
// Creating empty presentation 
Presentation presentation = new Presentation();
// Access first slide
ISlide slide = presentation.getSlides().get_Item(0);
// Add chart with default data
IChart chart = slide.getShapes().addChart(ChartType.StackedColumn, 0, 0, 500, 500);
// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
// Getting the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Add series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());
// Add Catrgories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
// Take second chart series
IChartSeries series = chart.getChartData().getSeries().get_Item(1);
// Now populating series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));
// Set GapWidth value
series.getParentSeriesGroup().setGapWidth(50);
// Save presentation with chart
presentation.save(dataDir + "GapWidth_out.pptx", SaveFormat.Pptx);

Conclusion

In this tutorial, you’ve learned how to set the Gap Width for a chart in a PowerPoint presentation using Aspose.Slides for Java. Adjusting the Gap Width allows you to control the spacing between columns or bars in your chart, enhancing the visual representation of your data.

FAQ’s

How do I change the Gap Width value?

To change the Gap Width, use the setGapWidth method on the ParentSeriesGroup of the chart series. In the example provided, we set the Gap Width to 50, but you can adjust this value to your desired spacing.

Can I customize other chart properties?

Yes, Aspose.Slides for Java provides extensive capabilities for chart customization. You can modify various chart properties, such as colors, labels, titles, and more. Check the API Reference for detailed information on chart customization options.

Where can I find more resources and documentation?

You can find comprehensive documentation and additional resources on Aspose.Slides for Java on the Aspose website.