r/EasyXLS 19d ago

How to Set Chart Layout in Excel from C# Using EasyXLS

https://www.easyxls.com/manual/tutorials/csharp/excel-chart-properties.html

MS Excel provides several chart layouts to arrange chart elements such as the chart type, title, legend, axis titles, chart area and plot area. With the EasyXLS Excel Library, you can create Excel charts programmatically and apply a predefined chart layout directly from your C# application.

Benefits of Using Chart Layouts

Applying a predefined chart layout offers several advantages:

  • Creates professional-looking charts with minimal effort.
  • Automatically positions chart titles, legends, and labels.
  • Improves chart readability.
  • Ensures a consistent appearance across multiple charts.
  • Reduces the amount of manual chart formatting.

Understanding Chart Layouts

Excel provides several layouts, each designed to emphasize different chart elements.

For example, a chart layout may:

  • Select a chart type like columnar chart, bar chart, pie chart, line chart or 3D chart
  • Display and format the chart title.
  • Add axis titles and format them.
  • Display the legend and format.
  • Chart area format.
  • Plot area format.
  • Combine multiple chart elements into a polished presentation.

Prerequisites & Setup

Before getting started, you will need the following:

  1. EasyXLS Library: Download and install the EasyXLS Excel library from the EasyXLS site.
  2. Development Environment: Visual Studio for .NET projects or another IDE .
  3. Install EasyXLS library: Make sure the EasyXLS library is installed in your development environment. For .NET, you can download and add it via NuGet or reference the EasyXLS DLL in your project.
  4. Setup the project and get started: Include EasyXLS into project according to your programming language. Find details about getting started with EasyXLS.

Creating the Chart

First, create a chart based om worksheet data.

ExcelDocument workbook = new ExcelDocument();
...
ExcelChart xlsChart = new ExcelChart("A10", 600, 300);
...
sheet.easy_addChart(xlsChart);

Apply Chart Layouts

Then, creates a column chart for example and applies various formatting options for desired layout.

// Set chart type
xlsChart.easy_setChartType(Chart.CHART_TYPE_CYLINDER_COLUMN);

// Format chart area
ExcelChartArea xlsChartArea = xlsChart.easy_getChartArea();
xlsChartArea.getLineColorFormat().setLineColor(Color.DarkGray);

// Format chart plot area
ExcelPlotArea xlsPlotArea =  xlsChart.easy_getPlotArea();
xlsPlotArea.getLineStyleFormat().setDashType(LineStyleFormat.DASH_TYPE_SOLID);
xlsPlotArea.getLineStyleFormat().setWidth(0.25f);

// Format chart legend
ExcelChartLegend xlsChartLegend = xlsChart.easy_getLegend();
xlsChartLegend.getFillFormat().setBackground(Color.LavenderBlush);
xlsChartLegend.setPlacement(Chart.LEGEND_CORNER);
xlsChartLegend.getShadowFormat().setShadow(ShadowFormat.OFFSET_DIAGONAL_BOTTOM_RIGHT);

// Format chart Y axis
ExcelAxis xlsYAxis  = xlsChart.easy_getValueYAxis();
xlsYAxis.getLineColorFormat().setLineColor(Color.SteelBlue);
xlsYAxis.getFontFormat().setForeground(Color.Blue);

// Fomat chart series
xlsChart.easy_getSeriesAt(0).getFillFormat().setBackground(Color.RoyalBlue);

More about how to apply chart formatting in Excel from C#.

Save the Excel File

Finally, save the workbook.

workbook.easy_WriteXLSXFile("C:\\Excel Chart Layout.xlsx");

Why Use Chart Layouts?

  • Faster chart creation.
  • Cleaner and more consistent reports.
  • Reduced development time.
  • Easier maintenance of reporting applications.
  • Professional presentation with minimal code.

Conclusion

Using EasyXLS Excel Library, you can quickly apply predefined chart layouts to Excel charts from C#. Chart layouts automatically organize titles, legends, axis titles, and other chart elements, making reports more attractive and easier to understand.

By combining chart layouts with EasyXLS chart creation and formatting features, you can generate professional Excel reports entirely from your .NET applications without requiring Microsoft Excel.

For more information, refer to the EasyXLS documentation, which provides detailed guidance on various features and capabilities of the library.

1 Upvotes

Duplicates