Articles in this section
Category / Section

How to hide chart legend in C#,VB.NET?

2 mins read

This article shows how to hide a legend in a chart in XlsIO using C#/VB.NET.

How to hide a legend?

To hide a chart legend in XlsIO, we need to set the IChartShape.HasLegend property value to False. By default, this value will be True.


Steps to hide a chart legend

  1. Create a workbook and add chart data.
    // Create a workbook
    IWorkbook workbook = application.Workbooks.Create(1);
    IWorksheet worksheet = workbook.Worksheets[0];
     
    // Add chart data
    worksheet.Range["A1"].Text = "Crescent City, CA";
    worksheet.Range["B3"].Text = "Precipitation,in.";
    worksheet.Range["C3"].Text = "Temperature,deg.F";
    worksheet.Range["D3"].Text = "Whether.rep";
    worksheet.Range["A4"].Text = "Jan";
    worksheet.Range["A5"].Text = "Feb";
    worksheet.Range["B4"].Number = 10.9;
    worksheet.Range["B5"].Number = 8.9;
    worksheet.Range["C4"].Number = 47.5;
    worksheet.Range["C5"].Number = 48.7;
    worksheet.Range["D4"].Number = 47.5;
    worksheet.Range["D5"].Number = 48.7;
    

 

  1. Add a chart into the worksheet.
    // Add a chart to the worksheet
    IChart chart = worksheet.Charts.Add();
     
    // Set dataRange and chartType
    chart.DataRange = worksheet.Range["A3:D5"];
    chart.ChartType = ExcelChartType.Column_Clustered_3D;
    

 

  1. Set IChartShape.HasLegend property value to False to hide chart legend.
    // Hide chart legend
    chart.HasLegend = false;
    

 

  1. Save the workbook.
    // Save and close the workbook
    Stream outStream = File.Create("Output.xlsx");
    workbook.SaveAs(outStream);
    

 

To know more about charts, please refer to the documentation.

Download complete sample

The following C#/VB.NET complete code snippet shows how to hide chart legend in XlsIO.

using Syncfusion.XlsIO;
using Syncfusion.XlsIO.Implementation.Shapes;
using System.Drawing;
using System.IO;
using System.Reflection;
 
namespace XlsIO_Sample
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Instantiate the spreadsheet creation engine
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
 
                // Create a workbook
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
 
                // Add chart data
                worksheet.Range["A1"].Text = "Crescent City, CA";
                worksheet.Range["B3"].Text = "Precipitation,in.";
                worksheet.Range["C3"].Text = "Temperature,deg.F";
                worksheet.Range["D3"].Text = "Whether.rep";
                worksheet.Range["A4"].Text = "Jan";
                worksheet.Range["A5"].Text = "Feb";
                worksheet.Range["B4"].Number = 10.9;
                worksheet.Range["B5"].Number = 8.9;
                worksheet.Range["C4"].Number = 47.5;
                worksheet.Range["C5"].Number = 48.7;
                worksheet.Range["D4"].Number = 47.5;
                worksheet.Range["D5"].Number = 48.7;
 
                // Add a chart to the worksheet
                IChart chart = worksheet.Charts.Add();
 
                // Sets data range and chart type
                chart.DataRange = worksheet.Range["A3:D5"];
                chart.ChartType = ExcelChartType.Column_Clustered_3D;
 
                // Hide chart legend
                chart.HasLegend = false;
 
                // Save and close the workbook
                Stream outStream = File.Create("Output.xlsx");
                workbook.SaveAs(outStream);
            }
        }
    }
}
 

 

 
Imports Syncfusion.XlsIO
Imports Syncfusion.XlsIO.Implementation.Shapes
Imports System.Drawing
Imports System.IO
Imports System.Reflection
 
Namespace XlsIO_Sample
 
    Class Program
        Public Shared Sub Main(ByVal args As String())
            'Instantiate the spreadsheet creation engine
            Using excelEngine As ExcelEngine = New ExcelEngine()
 
                Dim application As IApplication = excelEngine.Excel
 
                'Create a workbook
                Dim workbook As IWorkbook = application.Workbooks.Create(1)
                Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
                worksheet.Range("A1").Text = "Crescent City, CA"
                worksheet.Range("B3").Text = "Precipitation,in."
                worksheet.Range("C3").Text = "Temperature,deg.F"
                worksheet.Range("D3").Text = "Whether.rep"
                worksheet.Range("A4").Text = "Jan"
                worksheet.Range("A5").Text = "Feb"
                worksheet.Range("B4").Number = 10.9
                worksheet.Range("B5").Number = 8.9
                worksheet.Range("C4").Number = 47.5
                worksheet.Range("C5").Number = 48.7
                worksheet.Range("D4").Number = 47.5
                worksheet.Range("D5").Number = 48.7
 
                'Add a chart to the worksheet
                Dim chart As IChart = worksheet.Charts.Add()
 
                'Set data range and chart type
                chart.DataRange = worksheet.Range("A3:D5")
                chart.ChartType = ExcelChartType.Column_Clustered_3D
 
                'Hide chart legend
                chart.HasLegend = False
 
                'Save and close the workbook
                Dim outStream As Stream = File.Create("Output.xlsx")
                workbook.SaveAs(outStream)
            End Using
        End Sub
    End Class
End Namespace

 

The following screenshot shows the output of the Excel document in which the chart legend is hidden.

Chart, bar chart

Description automatically generated


Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

 

Conclusion

I hope you enjoyed learning about how to hide chart legend in C#,VB.NET.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied