A donut chart is a variant of the pie chart, with a blank center allowing for additional information about the data as a whole to be included. In this article, you will learn how to create a doughnut chart using Spire.XLS in C#.

Step 1: Initialize a new instance of Workbook class and set the Excel version as 2013.

Workbook wb = new Workbook();
wb.Version = ExcelVersion.Version2013;

Step 2: Get the first sheet from workbook.

Worksheet sheet = wb.Worksheets[0];

Step 3: Insert some data in the sheet.

sheet.Range["A1"].Value = "Country";
sheet.Range["A1"].Style.Font.IsBold = true;
sheet.Range["A2"].Value = "Cuba";
sheet.Range["A3"].Value = "Mexico";
sheet.Range["A4"].Value = "France";
sheet.Range["A5"].Value = "German";
sheet.Range["B1"].Value = "Sales";
sheet.Range["B1"].Style.Font.IsBold = true;
sheet.Range["B2"].NumberValue = 6000;
sheet.Range["B3"].NumberValue = 8000;
sheet.Range["B4"].NumberValue = 9000;
sheet.Range["B5"].NumberValue = 8500;

Step 4: Create a Doughnut Chart based on the data from range A1:B5.

Chart chart = sheet.Charts.Add();
chart.ChartType = ExcelChartType.Doughnut;
chart.DataRange = sheet.Range["A1:B5"];
chart.SeriesDataFromRange = false;

Step 5: Set the chart position.

chart.LeftColumn = 4;
chart.TopRow = 2;
chart.RightColumn = 12;
chart.BottomRow = 22;

Step 6: Display percentage value in data labels.

foreach (ChartSerie cs in chart.Series)
{
    cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = true;
}

Step 7: Save the file.

wb.SaveToFile("DoughnutChart.xlsx",ExcelVersion.Version2010);

Output:

How to Create a Doughnut Chart in Excel in C#

Full Code:

using Spire.Xls;
using Spire.Xls.Charts;

namespace DoughnutChart
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.Version = ExcelVersion.Version2013;
            Worksheet sheet = wb.Worksheets[0];

            //insert data
            sheet.Range["A1"].Value = "Country";
            sheet.Range["A1"].Style.Font.IsBold = true;
            sheet.Range["A2"].Value = "Cuba";
            sheet.Range["A3"].Value = "Mexico";
            sheet.Range["A4"].Value = "France";
            sheet.Range["A5"].Value = "German";
            sheet.Range["B1"].Value = "Sales";
            sheet.Range["B1"].Style.Font.IsBold = true;
            sheet.Range["B2"].NumberValue = 6000;
            sheet.Range["B3"].NumberValue = 8000;
            sheet.Range["B4"].NumberValue = 9000;
            sheet.Range["B5"].NumberValue = 8500;

            //add a new chart, set chart type as doughnut
            Chart chart = sheet.Charts.Add();
            chart.ChartType = ExcelChartType.Doughnut;
            chart.DataRange = sheet.Range["A1:B5"];
            chart.SeriesDataFromRange = false;

            //set position of chart
            chart.LeftColumn = 4;
            chart.TopRow = 2;
            chart.RightColumn = 12;
            chart.BottomRow = 22;
           
            //chart title
            chart.ChartTitle = "Market share by country";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;

            foreach (ChartSerie cs in chart.Series)
            {
                cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = true;
            }

            chart.Legend.Position = LegendPositionType.Top;
            wb.SaveToFile("DoughnutChart.xlsx",ExcelVersion.Version2010);
        }
    }
}
Published in Chart

Spire.XLS offers multiple functions to enable developers to set the font for the text for Excel chart. We have already demonstrated how to set the font for the text on legend and datalable in Excel chart by using the SetFont() in C#. This article will focus on showing how to set font for the text on Chart title and Chart Axis.

Firstly, please view the Excel worksheet with chart which the font will be changed later:

Set font for the text on Chart title and Chart Axis in C#

Note: Before Start, please download the latest version of Spire.XLS and add Spire.Xls.dll in the bin folder as the reference of Visual Studio.

Step 1: Create a new Excel workbook and load from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from workbook.

Worksheet worksheet = workbook.Worksheets[0];
Spire.Xls.Chart chart = worksheet.Charts[0];

Step 3: Format the font for the chart title.

chart.ChartTitleArea.Font.Color = Color.Blue;
chart.ChartTitleArea.Font.Size = 20.0;

Step 4: Format the font for the chart Axis.

chart.PrimaryValueAxis.Font.Color = Color.Gold;
chart.PrimaryValueAxis.Font.Size = 10.0;

chart.PrimaryCategoryAxis.Font.Color = Color.Red;
chart.PrimaryCategoryAxis.Font.Size = 20.0;

Step 5: Save the document to file.

workbook.SaveToFile("result.xlsx", FileFormat.Version2010);

Effective screenshot after formatting the font for the chart title and chart axis.

Set font for the text on Chart title and Chart Axis in C#

Full codes:

using Spire.Xls;
using System.Drawing;
namespace SetFont
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet worksheet = workbook.Worksheets[0];
            Spire.Xls.Chart chart = worksheet.Charts[0];

            chart.ChartTitleArea.Font.Color = Color.Blue;
            chart.ChartTitleArea.Font.Size = 20.0;

            chart.PrimaryValueAxis.Font.Color = Color.Gold;
            chart.PrimaryValueAxis.Font.Size = 10.0;

            chart.PrimaryCategoryAxis.Font.Color = Color.Red;
            chart.PrimaryCategoryAxis.Font.Size = 20.0;

            workbook.SaveToFile("result.xlsx", FileFormat.Version2010);

        }

    }
}
Published in Chart

Excel provides an option to display the trendline equation when we add a trendline on a chart. Sometimes, we may have the requirement of extracting the trendline equation from the chart. This article introduces a simple method to implement this aim by using Spire.XLS.

For demonstration, we used a sample chart which contains a trendline equation: y=2x – 1.

How to extract the trendline equation from an Excel chart

Code snippets:

Step 1: Instantiate a Workbook object and load the Excel document.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the chart from the first worksheet.

Chart chart = workbook.Worksheets[0].Charts[0];

Step 3: Get the trendline of the chart and then extract the equation of the trendline.

IChartTrendLine trendLine = chart.Series[0].TrendLines[0];
string formula = trendLine.Formula;

Effective screenshot:

How to extract the trendline equation from an Excel chart

Full code:

[C#]
using System;
using Spire.Xls;
using Spire.Xls.Core;

namespace Extract_the_equation
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Chart chart = workbook.Worksheets[0].Charts[0];
            IChartTrendLine trendLine = chart.Series[0].TrendLines[0];
            string formula = trendLine.Formula;
            Console.WriteLine("The equation is:\n" +formula);
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Core

Namespace Extract_the_equation
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("Sample.xlsx")
			Dim chart As Chart = workbook.Worksheets(0).Charts(0)
			Dim trendLine As IChartTrendLine = chart.Series(0).TrendLines(0)
			Dim formula As String = trendLine.Formula
			Console.WriteLine(Convert.ToString("The equation is:" & vbLf) & formula)
			Console.ReadKey()
		End Sub
	End Class
End Namespace
Published in Chart

Excel 2013 has provided some new charting features, for example, it enables users to set data callout labels which makes it easier to show the details about the data series or its individual data points in a clear and easy-to-read format. This article is going to introduce how to add data callout labels to a chart using Spire.XLS.

Step 1: Initialize a new instance of Workbook class and set the Excel version as 2013.

Workbook wb = new Workbook();
wb.Version = ExcelVersion.Version2013;

Step 2: Get the first sheet from workbook.

Worksheet ws = wb.Worksheets[0];

Step 3: Insert some data.

ws.Range["A2"].Text = "Product 1";
ws.Range["A3"].Text = "Product 2";
ws.Range["A4"].Text = "Product 3";
ws.Range["A5"].Text = "Product 4";
ws.Range["B1"].Text = "Sales";
ws.Range["B1"].Style.Font.IsBold = true;
ws.Range["B2"].NumberValue = 251;
ws.Range["B3"].NumberValue = 515;
ws.Range["B4"].NumberValue = 454;
ws.Range["B5"].NumberValue = 874;

Step 4: Create a Clustered Column Chart based on the data from range A1:B5.

Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
chart.DataRange = ws.Range["A1:B5"];
chart.SeriesDataFromRange = false;
chart.PrimaryValueAxis.HasMajorGridLines = false;

Step 5: Set the chart position.

chart.LeftColumn = 4;
chart.TopRow = 2;
chart.RightColumn = 12;
chart.BottomRow = 22;

Step 6: Set the HasWedgeCallout property as true to display callout labels in a chart.

foreach (ChartSerie cs in chart.Series)
{
    cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
    cs.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true;
}

Step 7: Save the file.

wb.SaveToFile("result.xlsx", FileFormat.Version2013);

Output:

How to Add Data Callout Labels to Charts in Excel in C#

Full Code:

using Spire.Xls;
using Spire.Xls.Charts;

namespace AddCalloutLabels
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.Version = ExcelVersion.Version2013;
            Worksheet ws = wb.Worksheets[0];

            ws.Range["A2"].Text = "Product 1";
            ws.Range["A3"].Text = "Product 2";
            ws.Range["A4"].Text = "Product 3";
            ws.Range["A5"].Text = "Product 4";
            ws.Range["B1"].Text = "Sales";
            ws.Range["B1"].Style.Font.IsBold = true;
            ws.Range["B2"].NumberValue = 251;
            ws.Range["B3"].NumberValue = 515;
            ws.Range["B4"].NumberValue = 454;
            ws.Range["B5"].NumberValue = 874;

            Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
            chart.DataRange = ws.Range["A1:B5"];
            chart.SeriesDataFromRange = false;
            chart.PrimaryValueAxis.HasMajorGridLines = false;

            chart.LeftColumn = 4;
            chart.TopRow = 2;
            chart.RightColumn = 12;
            chart.BottomRow = 22;

            foreach (ChartSerie cs in chart.Series)
            {
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
                cs.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true;
            }
            wb.SaveToFile("result.xlsx", FileFormat.Version2013);
        }
    }
}
Published in Chart

Spire.XLS enables developers to change the color of data series in an Excel chart with just a few lines of code. Once changed, the legend color will also turn into the same as the color you set to the series.

The following part demonstrates the steps of how to accomplish this task. Below picture shows the original colors of the data series in an Excel chart:

How to change the color of data series in an Excel chart

Code snippets:

Step 1: Instantiate a Workbook object and load the Excel workbook.

Workbook book = new Workbook();
book.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = book.Worksheets[0];

Step 3: Get the second series of the chart.

ChartSerie cs = sheet.Charts[0].Series[1];

Step 4: Change the color of the second series to Purple.

cs.Format.Fill.FillType = ShapeFillType.SolidColor;
cs.Format.Fill.ForeColor = Color.FromKnownColor(KnownColor.Purple);

Step 5: Save the Excel workbook to file.

book.SaveToFile("ChangeSeriesColor.xlsx", ExcelVersion.Version2010);

After running the code, the color of the second series has been changed into Purple, screenshot as shown below.

How to change the color of data series in an Excel chart

Full code:

[C#]
using System.Drawing;
using Spire.Xls;
using Spire.Xls.Charts;

namespace Change_Series_Color
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook book = new Workbook();
            book.LoadFromFile("Sample.xlsx");
            Worksheet sheet = book.Worksheets[0];
            ChartSerie cs = sheet.Charts[0].Series[1];
            cs.Format.Fill.FillType = ShapeFillType.SolidColor;
            cs.Format.Fill.ForeColor = Color.FromKnownColor(KnownColor.Purple);
            book.SaveToFile("ChangeSeriesColor.xlsx", ExcelVersion.Version2010);
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Xls
Imports Spire.Xls.Charts

Namespace Change_Series_Color
	Class Program
		Private Shared Sub Main(args As String())
			Dim book As New Workbook()
			book.LoadFromFile("Sample.xlsx")
			Dim sheet As Worksheet = book.Worksheets(0)
			Dim cs As ChartSerie = sheet.Charts(0).Series(1)
			cs.Format.Fill.FillType = ShapeFillType.SolidColor
			cs.Format.Fill.ForeColor = Color.FromKnownColor(KnownColor.Purple)
			book.SaveToFile("ChangeSeriesColor.xlsx", ExcelVersion.Version2010)
		End Sub
	End Class
End Namespace
Published in Chart
Tuesday, 01 November 2016 08:03

Add Oval shape to Excel Chart in C#

We have demonstrated how to insert textbox to Excel worksheet in C#. Starts from Spire.XLS v7.11.1, we have add a new method of chart.Shapes.AddOval(left,top,right,bottom); to enable developers to add oval shape to excel chart directly. Developers can also add the text contents to the oval shape and format the style for the oval. This article will describe clearly how to insert oval shape to Excel chart in C#.

Step 1: Create a workbook and get the first worksheet from the workbook.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Step 2: Add a chart to the worksheet.

Chart chart = sheet.Charts.Add();

Step 3: Add oval shape to Excel chart.

var shape = chart.Shapes.AddOval(20, 60, 500,400);

Step 4: Add the text to the oval shape and set the text alignment on the shape.

shape.Text = "Oval Shape added by Spire.XLS";
shape.HAlignment = CommentHAlignType.Center;
shape.VAlignment = CommentVAlignType.Center;

Step 5: Format the color for the oval shape.

((XlsOvalShape)shape).Line.ForeColor = Color.Blue;
((XlsOvalShape)shape).Fill.ForeColor = Color.Green;

Step 6: Save the document to file.

workbook.SaveToFile("Result.xlsx",ExcelVersion.Version2010);

Effective screenshot of Oval shape added to the Excel chart:

Add Oval shape to Excel Chart in C#

Full codes:

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
using System.Drawing;
namespace AddOvalShape
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                Workbook workbook = new Workbook();

                Worksheet sheet = workbook.Worksheets[0];

                Chart chart = sheet.Charts.Add();

                var shape = chart.Shapes.AddOval(20, 60, 500, 400);

                shape.Text = "Oval Shape added by Spire.XLS";

                shape.HAlignment = CommentHAlignType.Center;
                shape.VAlignment = CommentVAlignType.Center;

                ((XlsOvalShape)shape).Line.ForeColor = Color.Blue;
                ((XlsOvalShape)shape).Fill.ForeColor = Color.Green;

                workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

            }
        }
    }
}
Published in Chart

Apart from creating chart with continuous data range, Spire.XLS also supports to create chart with discontinuous data range by calling the XlsRange.AddCombinedRange(CellRange cr) method. This example explains a quick solution of how to achieve this task in C# with the help of Spire.XLS.

For demonstration, here we used a template excel document, in which you can see there are some blank rows among the data, in other words, the data range is discontinuous.

Use Discontinuous Data Range to Create Chart in Excel

Here comes to the detail steps:

Step 1: Instantiate a Wordbook object, load the excel document and get its first worksheet.

Workbook book = new Workbook();
book.LoadFromFile("Sample.xlsx");
Worksheet sheet = book.Worksheets[0];

Step 2: Add a column chart to the first worksheet and set the position of the chart.

Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
chart.SeriesDataFromRange = false;
//Set chart position
chart.LeftColumn = 5;
chart.TopRow = 1;
chart.RightColumn = 13;
chart.BottomRow = 10;

Step 3: Add two series to the chart, set data source for category labels and values of the series with discontinuous data range.

//Add the first series
var cs1 = (ChartSerie)chart.Series.Add();
//Set name of the serie
cs1.Name = sheet.Range["B1"].Value;
//Set data source for Category Labels and Values of the serie with discontinuous data range
cs1.CategoryLabels = sheet.Range["A2:A3"].AddCombinedRange(sheet.Range["A5:A6"]).AddCombinedRange(sheet.Range["A8:A9"]);
cs1.Values = sheet.Range["B2:B3"].AddCombinedRange(sheet.Range["B5:B6"]).AddCombinedRange(sheet.Range["B8:B9"]);
//Specify the serie type
cs1.SerieType = ExcelChartType.ColumnClustered;

//Add the second series
var cs2 = (ChartSerie)chart.Series.Add();
cs2.Name = sheet.Range["C1"].Value;
cs2.CategoryLabels = cs2.CategoryLabels = sheet.Range["A2:A3"].AddCombinedRange(sheet.Range["A5:A6"]).AddCombinedRange(sheet.Range["A8:A9"]);
cs2.Values = sheet.Range["C2:C3"].AddCombinedRange(sheet.Range["C5:C6"]).AddCombinedRange(sheet.Range["C8:C9"]);
cs2.SerieType = ExcelChartType.ColumnClustered;

Step 4: Save the excel document.

book.SaveToFile("Result.xlsx", FileFormat.Version2010);

After executing the above example code, a column chart with discontinuous data range was added to the worksheet as shown below.

Use Discontinuous Data Range to Create Chart in Excel

Full code:

using Spire.Xls;
using Spire.Xls.Charts;

namespace Assign_discontinuous_range_for_chart
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook book = new Workbook();
            book.LoadFromFile("Sample.xlsx");
            Worksheet sheet = book.Worksheets[0];

            Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
            chart.SeriesDataFromRange = false;
            chart.LeftColumn = 5;
            chart.TopRow = 1;
            chart.RightColumn = 13;
            chart.BottomRow = 10;

            var cs1 = (ChartSerie)chart.Series.Add();
            cs1.Name = sheet.Range["B1"].Value;
            cs1.CategoryLabels = sheet.Range["A2:A3"].AddCombinedRange(sheet.Range["A5:A6"]).AddCombinedRange(sheet.Range["A8:A9"]);
            cs1.Values = sheet.Range["B2:B3"].AddCombinedRange(sheet.Range["B5:B6"]).AddCombinedRange(sheet.Range["B8:B9"]);
            cs1.SerieType = ExcelChartType.ColumnClustered;

            var cs2 = (ChartSerie)chart.Series.Add();
            cs2.Name = sheet.Range["C1"].Value;
            cs2.CategoryLabels = sheet.Range["A2:A3"].AddCombinedRange(sheet.Range["A5:A6"]).AddCombinedRange(sheet.Range["A8:A9"]);
            cs2.Values = sheet.Range["C2:C3"].AddCombinedRange(sheet.Range["C5:C6"]).AddCombinedRange(sheet.Range["C8:C9"]);
            cs2.SerieType = ExcelChartType.ColumnClustered;
        
            chart.ChartTitle = string.Empty;

            book.SaveToFile("Result.xlsx", FileFormat.Version2010);
            System.Diagnostics.Process.Start("Result.xlsx");
        }
    }
}
Published in Chart

Excel version 2013 added a fantastic feature in Chart Data Label option that you can custom data labels from a column/row of data. The chart below uses labels from the data in cells C2: C5 next to the plotted values. This article will present how to add labels to data points using the values from cells in C#.

Custom data labels using values from cells in C#

Code Snippets:

Step 1: Initialize a new instance of Workbook class and set the Excel version as 2013.

Workbook wb = new Workbook();
wb.Version = ExcelVersion.Version2013;

Step 2: Get the first sheet from workbook.

Worksheet ws = wb.Worksheets[0];

Step 3: Insert data.

ws.Range["A2"].Text = "Product 1";
ws.Range["A3"].Text = "Product 2";
ws.Range["A4"].Text = "Product 3";
ws.Range["A5"].Text = "Product 4";
ws.Range["B1"].Text = "Sales";
ws.Range["B1"].Style.Font.IsBold = true;
ws.Range["B2"].NumberValue = 251;
ws.Range["B3"].NumberValue = 515;
ws.Range["B4"].NumberValue = 454;
ws.Range["B5"].NumberValue = 874;
ws.Range["C1"].Text = "+/-\nPrevious\nPeriod";
ws.Range["C1"].Style.Font.IsBold = true;
ws.Range["C2"].NumberValue = -120;
ws.Range["C3"].NumberValue = 31;
ws.Range["C4"].NumberValue = -76;
ws.Range["C5"].NumberValue = 201;
ws.SetRowHeight(1, 40);

Step 4: Insert a Clustered Column Chart in Excel based on the data range from A1:B5.

Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
chart.DataRange = ws.Range["A1:B5"];
chart.SeriesDataFromRange = false;
chart.PrimaryValueAxis.HasMajorGridLines = false;

Step 5: Set chart position.

chart.LeftColumn = 5;
chart.TopRow = 2;
chart.RightColumn = 13;
chart.BottomRow = 22;

Step 6: Add labels to data points using the values from cell range C2:C5.

chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.ValueFromCell = ws.Range["C2:C5"];

Step 7: Save and launch the file.

wb.SaveToFile("result.xlsx",ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.xlsx");

Full Code:

using Spire.Xls;

namespace CustomLabels
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.Version = ExcelVersion.Version2013;
            Worksheet ws = wb.Worksheets[0];

            ws.Range["A2"].Text = "Product 1";
            ws.Range["A3"].Text = "Product 2";
            ws.Range["A4"].Text = "Product 3";
            ws.Range["A5"].Text = "Product 4";
            ws.Range["B1"].Text = "Sales";
            ws.Range["B1"].Style.Font.IsBold = true;
            ws.Range["B2"].NumberValue = 251;
            ws.Range["B3"].NumberValue = 515;
            ws.Range["B4"].NumberValue = 454;
            ws.Range["B5"].NumberValue = 874;
            ws.Range["C1"].Text = "+/-\nPrevious\nPeriod";
            ws.Range["C1"].Style.Font.IsBold = true;
            ws.Range["C2"].NumberValue = -120;
            ws.Range["C3"].NumberValue = 31;
            ws.Range["C4"].NumberValue = -76;
            ws.Range["C5"].NumberValue = 201;
            ws.SetRowHeight(1, 40);

            Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
            chart.DataRange = ws.Range["A1:B5"];
            chart.SeriesDataFromRange = false;
            chart.PrimaryValueAxis.HasMajorGridLines = false;

            chart.LeftColumn = 5;
            chart.TopRow = 2;
            chart.RightColumn = 13;
            chart.BottomRow = 22;

            chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.ValueFromCell = ws.Range["C2:C5"];

            wb.SaveToFile("result.xlsx",ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");
        }
    }
}
Published in Chart

With the help of Spire.XLS, developers can easily set the font for the text for Excel chart. We have already demonstrate how to set the font for TextBox in Excel Chart, this article will focus on demonstrating how to set the font for legend and datalable in Excel chart by using the SetFont() method to change the font for the legend and datalable easily in C#.

Firstly, please view the Excel worksheet with chart which the font will be changed later:

How to set the font for legend and datalable in Excel Chart

Note: Before Start, please download the latest version of Spire.XLS and add Spire.Xls.dll in the bin folder as the reference of Visual Studio.

Step 1: Create a new Excel workbook and load from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from workbook.

Worksheet ws = workbook.Worksheets[0];
Spire.Xls.Chart chart = ws.Charts[0];

Step 3: Create a font with specified size and color.

ExcelFont font = workbook.CreateFont();
font.Size =12.0;
font.Color = Color.Red;

Step 4: Apply the font to chart Legend.

chart.Legend.TextArea.SetFont(font);

Step 5: Apply the font to chart DataLabel.

foreach (ChartSerie cs in chart.Series)
   {
     cs.DataPoints.DefaultDataPoint.DataLabels.TextArea.SetFont(font);
   }

Step 6: Save the document to file.

workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);

Effective screenshot after changing the text font.

How to set the font for legend and datalable in Excel Chart

Full codes:

using Spire.Xls;
using Spire.Xls.Charts;
using System.Drawing;
namespace SetFont
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("Sample.xlsx");

                Worksheet ws = workbook.Worksheets[0];
                Spire.Xls.Chart chart = ws.Charts[0];

                ExcelFont font = workbook.CreateFont();
                font.Size = 12.0;
                font.Color = Color.Red;

                chart.Legend.TextArea.SetFont(font);

                foreach (ChartSerie cs in chart.Series)
                {
                    cs.DataPoints.DefaultDataPoint.DataLabels.TextArea.SetFont(font);
                }

                workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);


            }
        }
    }
}
Published in Chart
Tuesday, 05 January 2016 08:43

How to explode a pie chart sections in C#

When we work with Excel pie chart, we may need to separate each part of pie chart to make them stand out. Spire.XLS offers a property of Series.DataFormat.Percent to enable developers to pull the whole pie apart. It also offers a property of Series.DataPoints.DataFormat.Percent to pull apart a single slice from the whole pie chart.

This article is going to introduce the method of how to set the separation width between slices in pie chart in C# by using Spire.XLS.

On MS Excel, We can adjust the percentage of "Pie Explosion" on the Series Options at the "format data series" area to control the width between each section in the chart.

How to explode a pie chart sections in C#

Code Snippet of how to set the separation width between slices in pie chart.

using Spire.Xls;
namespace ExplodePieChart
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet ws = workbook.Worksheets[0];
            Chart chart = ws.Charts[0];

            // Set the separation width between slices in pie chart.
            for (int i = 0; i < chart.Series.Count; i++)
            {
                chart.Series[i].DataFormat.Percent = 20;
            }

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);

        }
    }
}

Effective screenshot after pull the whole pie apart.

How to explode a pie chart sections in C#

Code Snippet of how to split a single slice from the whole pie chart.

using Spire.Xls;
namespace ExplodePieChart
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("Sample.xlsx");

                Worksheet ws = workbook.Worksheets[0];
                Chart chart = ws.Charts[0];

                chart.Series[0].DataPoints[0].DataFormat.Percent = 20;

                workbook.SaveToFile("ExplodePieChart.xlsx", ExcelVersion.Version2013);
            }


        }
    }
}

Effective screenshot after pull a single part from the pie chart apart.

How to explode a pie chart sections in C#

Published in Chart
Page 2 of 5