How to Create a Gauge Chart in Excel Using ASP.NET Core?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to create a Gauge Chart in Excel using Syncfusion’s XlsIO library. A gauge chart, also known as a speedometer chart or dial chart, is a visual representation used to display the progress or achievement of a certain value within a defined range.
Steps to create a gauge chart in Excel programmatically:
To create a gauge chart in Excel using XlsIO, follow these steps:
Step 1: Initialize the Chart
Create a chart object by calling the worksheet.Charts.Add() method and specify the chart type as ExcelChartType.Doughnut for the gauge’s background.
//Adding doughnut chart in worksheet
IChartShape chart = sheet.Charts.Add();
chart.ChartType = ExcelChartType.Doughnut;
chart.DataRange = sheet.Range["A1:A5"];
chart.IsSeriesInRows = false;
Step 2: Assign Data for the Gauge Scale
Set a range of values from the worksheet to the chart’s DataRange property. This defines the segments of the doughnut chart that represent the gauge scale.
//Adding values in worksheet
sheet.Range["A1"].Value = "Value";
sheet.Range["A2"].Value = "30";
sheet.Range["A3"].Value = "60";
sheet.Range["A4"].Value = "90";
sheet.Range["A5"].Value = "180";
sheet.Range["C2"].Value = "value";
sheet.Range["C3"].Value = "pointer";
sheet.Range["C4"].Value = "End";
sheet.Range["D2"].Value = "10";
sheet.Range["D3"].Value = "1";
sheet.Range["D4"].Value = "189";
Step 3: Format the Gauge Scale (Doughnut Chart)
Apply formatting to the doughnut chart to define its appearance. The size of the doughnut hole and the angle of the first slice need to be adjusted to resemble a gauge.
//Formatting value series
chart.Series["Value"].SerieFormat.CommonSerieOptions.DoughnutHoleSize = 60;
chart.Series["Value"].SerieFormat.CommonSerieOptions.FirstSliceAngle = 270;
chart.Series["Value"].DataPoints[3].DataFormat.Fill.Visible = false;
Step 4: Add the Pointer Series (Pie Chart)
Next, add a pie chart series to represent the pointer or needle of the gauge. This series shows the current value on the gauge.
//Adding pointer series as Pie chart
chart.Series.Add("Pointer");
chart.Series["Pointer"].SerieType = ExcelChartType.Pie;
chart.Series["Pointer"].Values = sheet.Range["D2:D4"];
chart.Series["Pointer"].UsePrimaryAxis = false;
Step 5: Format the Pointer Series
Customize the pointer series to show only the needle by hiding the unnecessary segments of the pie chart. Set the pointer’s color to distinguish it from the rest of the chart.
//Formatting pointer series
chart.Series["Pointer"].SerieFormat.CommonSerieOptions.FirstSliceAngle = 270;
chart.Series["Pointer"].DataPoints[0].DataFormat.Fill.Visible = false;
chart.Series["Pointer"].DataPoints[1].DataFormat.Fill.ForeColorIndex = ExcelKnownColors.Black;
chart.Series["Pointer"].DataPoints[2].DataFormat.Fill.Visible = false;
Step 6: Save the Workbook
Finally, save the workbook to generate the output Excel file containing the gauge chart.
workbook.SaveAs("Output.xlsx");
The following complete code snippet shows the creation of gauge chart using XlsIO.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
//Adding values in worksheet
sheet.Range["A1"].Value = "Value";
sheet.Range["A2"].Value = "30";
sheet.Range["A3"].Value = "60";
sheet.Range["A4"].Value = "90";
sheet.Range["A5"].Value = "180";
sheet.Range["C2"].Value = "value";
sheet.Range["C3"].Value = "pointer";
sheet.Range["C4"].Value = "End";
sheet.Range["D2"].Value = "10";
sheet.Range["D3"].Value = "1";
sheet.Range["D4"].Value = "189";
//Adding doughnut chart in worksheet
IChartShape chart = sheet.Charts.Add();
chart.ChartType = ExcelChartType.Doughnut;
chart.DataRange = sheet.Range["A1:A5"];
chart.IsSeriesInRows = false;
//Formatting value series
chart.Series["Value"].SerieFormat.CommonSerieOptions.DoughnutHoleSize = 60;
chart.Series["Value"].SerieFormat.CommonSerieOptions.FirstSliceAngle = 270;
chart.Series["Value"].DataPoints[3].DataFormat.Fill.Visible = false;
//Adding pointer series as Pie chart
chart.Series.Add("Pointer");
chart.Series["Pointer"].SerieType = ExcelChartType.Pie;
chart.Series["Pointer"].Values = sheet.Range["D2:D4"];
chart.Series["Pointer"].UsePrimaryAxis = false;
//Formatting pointer series
chart.Series["Pointer"].SerieFormat.CommonSerieOptions.FirstSliceAngle = 270;
chart.Series["Pointer"].DataPoints[0].DataFormat.Fill.Visible = false;
chart.Series["Pointer"].DataPoints[1].DataFormat.Fill.ForeColorIndex = ExcelKnownColors.Black;
chart.Series["Pointer"].DataPoints[2].DataFormat.Fill.Visible = false;
//Saving the workbook as stream
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream);
}
Download a complete working sample demonstrating how to create a gauge chart in Excel using C# from here.
Below is the output file for gauge chart creation using XlsIO.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheets or workbooks, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to a worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to create a gauge chart in Excel using ASP.NET Core.
You can refer to our ASP.NET Core XIsIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core XIsIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!