Excel Chart Components

Chart contains many components, such as legend, title, axes, axes titles, etc.

Following example shows how to create arbitrary chart with specific grouping and set some properties of chart components (title, axis titles, axis properties, etc.).

For more information about chart components, see Chart Components section from help documentation.

Excel chart components created with GemBox.Spreadsheet for Java
Screenshot of Excel chart components created with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;
import com.gembox.spreadsheet.charts.*;

import java.time.LocalDateTime;
import java.util.Optional;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.

        int numberOfEmployees = %EmployeesCount%;
        int numberOfYears = %YearsCount%;

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Chart");

        // Create chart and select data for it.
        ColumnChart chart = (ColumnChart) worksheet.getCharts().add(ChartType.COLUMN, "B7", "O27");
        chart.selectData(worksheet.getCells().getSubrangeAbsolute(0, 0, numberOfEmployees, numberOfYears));

        // Set chart title.
        chart.getTitle().setText("Clustered Column Chart");

        // Set axis titles.

        // For all charts (except Pie and Bar) value axis is vertical.
        ValueAxis valueAxis = chart.getAxes().getVerticalValue();

        // Set value axis scaling, units, gridlines and tick marks.

        // Add data which is used by the chart.
        String[] names = new String[] { "John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat" };
        java.util.Random random = new java.util.Random();
        for (int i = 0; i < numberOfEmployees; ++i) {
            worksheet.getCell(i + 1, 0).setValue(names[i % names.length] + (i < names.length ? "" : " " + (i / names.length + 1)));

            for (int j = 0; j < numberOfYears; ++j)
                worksheet.getCell(i + 1, j + 1).setValue(random.nextInt(4000) + 1000);

        // Set header row and formatting.
        worksheet.getCell(0, 0).setValue("Name");
        worksheet.getCell(0, 0).getStyle().getFont().setWeight(ExcelFont.BOLD_WEIGHT);
        worksheet.getColumn(0).setWidth((int) LengthUnitConverter.convert(3, LengthUnit.CENTIMETER, LengthUnit.ZERO_CHARACTER_WIDTH_256_TH_PART));
        for (int i = 0, startYear = LocalDateTime.now().getYear() - numberOfYears; i < numberOfYears; ++i) {
            worksheet.getCell(0, i + 1).setValue(startYear + i);
            worksheet.getCell(0, i + 1).getStyle().getFont().setWeight(ExcelFont.BOLD_WEIGHT);
            worksheet.getCell(0, i + 1).getStyle().setNumberFormat("General");
            worksheet.getColumn(i + 1).getStyle().setNumberFormat("\"$\"#,##0");

        // Make entire sheet print horizontally centered on a single page with headings and gridlines.
        ExcelPrintOptions printOptions = worksheet.getPrintOptions();

        workbook.save("Chart Components.xlsx");

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.