GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

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.).

Sample also demonstrates how to save just single chart (instead of entire workbook) to supported output formats (PDF, XPS and image formats).

For more information about chart components and export functionality, see chart components section from help documentation.

Screenshot
Chart Components Screenshot

See the full code below, use Run Example to execute.

using System;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        int numberOfEmployees = 4;
        int numberOfYears = 4;

        var ef = new ExcelFile();
        var ws = ef.Worksheets.Add("Chart");

        // Create chart and select data for it.
        var chart = (ColumnChart)ws.Charts.Add(ChartType.Column, "B7", "O27");
        chart.SelectData(ws.Cells.GetSubrangeAbsolute(0, 0, numberOfEmployees, numberOfYears));

        // Set chart title.
        chart.Title.Text = "Clustered Column Chart";

        // Set axis titles.
        chart.Axes.Horizontal.Title.Text = "Years";
        chart.Axes.Vertical.Title.Text = "Salaries";

        // For all charts (except Pie and Bar) value axis is vertical.
        var valueAxis = chart.Axes.VerticalValue;

        // Set value axis scaling, units, gridlines and tick marks.
        valueAxis.Minimum = 0;
        valueAxis.Maximum = 6000;
        valueAxis.MajorUnit = 1000;
        valueAxis.MinorUnit = 500;
        valueAxis.MajorGridlines.IsVisible = true;
        valueAxis.MinorGridlines.IsVisible = true;
        valueAxis.MajorTickMarkType = TickMarkType.Outside;
        valueAxis.MinorTickMarkType = TickMarkType.Cross;

        // Add data which is used by the chart.
        var names = new string[] { "John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat" };
        var random = new Random();
        for (int i = 0; i < numberOfEmployees; ++i)
        {
            ws.Cells[i + 1, 0].Value = names[i % names.Length] + (i < names.Length ? string.Empty : ' ' + (i / names.Length + 1).ToString());

            for (int j = 0; j < numberOfYears; ++j)
                ws.Cells[i + 1, j + 1].SetValue(random.Next(1000, 5000));
        }

        // Set header row and formatting.
        ws.Cells[0, 0].Value = "Name";
        ws.Cells[0, 0].Style.Font.Weight = ExcelFont.BoldWeight;
        ws.Columns[0].Width = (int)LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart);
        for (int i = 0, startYear = DateTime.Now.Year - numberOfYears; i < numberOfYears; ++i)
        {
            ws.Cells[0, i + 1].SetValue(startYear + i);
            ws.Cells[0, i + 1].Style.Font.Weight = ExcelFont.BoldWeight;
            ws.Cells[0, i + 1].Style.NumberFormat = "General";
            ws.Columns[i + 1].Style.NumberFormat = "\"$\"#,##0";
        }

        // Make entire sheet print horizontally centered on a single page with headings and gridlines.
        var printOptions = ws.PrintOptions;
        printOptions.HorizontalCentered = printOptions.PrintHeadings = printOptions.PrintGridlines = true;
        printOptions.FitWorksheetWidthToPages = printOptions.FitWorksheetHeightToPages = 1;

        ef.Save("Chart Components.xlsx");
    }
}
Imports System
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Charts

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim numberOfEmployees = 4
        Dim numberOfYears = 4

        Dim ef = new ExcelFile()
        Dim ws = ef.Worksheets.Add("Chart")

        ' Create chart and select data for it.
        Dim chart = DirectCast(ws.Charts.Add(ChartType.Column, "B7", "O27"), ColumnChart)
        chart.SelectData(ws.Cells.GetSubrangeAbsolute(0, 0, numberOfEmployees, numberOfYears))

        ' Set chart title.
        chart.Title.Text = "Clustered Column Chart"

        ' Set axis titles.
        chart.Axes.Horizontal.Title.Text = "Years"
        chart.Axes.Vertical.Title.Text = "Salaries"

        ' For all charts (except Pie and Bar) value axis is vertical.
        Dim valueAxis = chart.Axes.VerticalValue

        ' Set value axis scaling, units, gridlines and tick marks.
        valueAxis.Minimum = 0
        valueAxis.Maximum = 6000
        valueAxis.MajorUnit = 1000
        valueAxis.MinorUnit = 500
        valueAxis.MajorGridlines.IsVisible = true
        valueAxis.MinorGridlines.IsVisible = true
        valueAxis.MajorTickMarkType = TickMarkType.Outside
        valueAxis.MinorTickMarkType = TickMarkType.Cross

        ' Add data which is used by the chart.
        Dim names = New String() {"John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat"}
        Dim random = New Random()
        For i As Integer = 0 To numberOfEmployees - 1
            ws.Cells(i + 1, 0).Value = names(i Mod names.Length) & (If(i < names.Length, String.Empty, " "c & (i \ names.Length + 1).ToString()))

            For j As Integer = 0 To numberOfYears - 1
                ws.Cells(i + 1, j + 1).SetValue(random.Next(1000, 5000))
            Next
        Next

        ' Set header row and formatting.
        ws.Cells(0, 0).Value = "Name"
        ws.Cells(0, 0).Style.Font.Weight = ExcelFont.BoldWeight
        ws.Columns(0).Width = CInt(LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart))
        Dim startYear As Integer = DateTime.Now.Year - numberOfYears
        For i As Integer = 0 To numberOfYears - 1
            ws.Cells(0, i + 1).SetValue(startYear + i)
            ws.Cells(0, i + 1).Style.Font.Weight = ExcelFont.BoldWeight
            ws.Cells(0, i + 1).Style.NumberFormat = "General"
            ws.Columns(i + 1).Style.NumberFormat = """$""#,##0"
        Next

        ' Make entire sheet print horizontally centered on a single page with headings and gridlines.
        Dim printOptions = ws.PrintOptions
        printOptions.HorizontalCentered = True
        printOptions.PrintHeadings = True
        printOptions.PrintGridlines = True
        printOptions.FitWorksheetWidthToPages = 1
        printOptions.FitWorksheetHeightToPages = 1

        ef.Save("Chart Components.xlsx")

    End Sub

End Module

Check next sample.