Create and Format Chart Using Free Spire.Xls

Spire.Xls

The Spire.XLS Free Edition is a versatile Excel library designed for software developers to do a wide range of Excel processing tasks on the .NET Platform, that doesn't need to install Microsoft Excel or Office.

You can download it from: https://freenetexcel.codeplex.com/

Getting Started

Start Visual Studio and create a new console application and add the following Spire.Xls DLLS.

  • Spire.Common.dll
  • Spire.License.dll
  • Spire.Pdf.dll
  • Spire.XLS.dll

Now let's simulate some data and save them into a DataTable.

  1. private static DataTable CreateTable()  
  2. {  
  3.     DataTable dt = new DataTable();  
  4.     dt.Columns.Add("Month"typeof(string));  
  5.     dt.Columns.Add("Count"typeof(int));  
  6.     dt.Rows.Add("January", 10);  
  7.     dt.Rows.Add("February", 20);  
  8.     dt.Rows.Add("March", 30);  
  9.     dt.Rows.Add("April", 40);  
  10.     dt.Rows.Add("May", 50);  
  11.     return dt;  
  12. }  

 

DataSet Visualizer

Next is to insert the DataTable into Excel and create the chart with the above data using Spire.Xls that supports the creation of various charts, such as column, pie, line and so on. This article uses a column chart as an example.

  1. public static void CreateChart()  
  2.  {  
  3.      Workbook workbook = new Workbook();  
  4.      workbook.Worksheets.Clear();  
  5.      var sheet = workbook.Worksheets.Add("Sheet1");  
  6.      DataTable dt = CreateTable();  
  7.      sheet.InsertDataTable(dt, false, 1, 1);  
  8.      Chart chart = sheet.Charts.Add();  
  9.      chart.ChartType = ExcelChartType.ColumnClustered;  
  10.      chart.DataRange = sheet.AllocatedRange;  
  11.      chart.SeriesDataFromRange = false;  
  12.      chart.Height = 220;  
  13.      chart.Width = 450;  
  14.      chart.Left = 0;  
  15.      chart.Top = 100;  
  16.      workbook.SaveToFile("CreateChart.xlsx", ExcelVersion.Version2007);  
  17.  }  

Press F5 and see the output:

Chart Description

 

The process is very simple and when we create the chart, we can set the layout of the chart, such as chart title, chart area, legend, x-axis, and so on. These might be not important for us, so here I have skipped them, but then you'll love it.

Next, I will guide you through the labeling of the data points and coloring the data points that are a useful way to see precise data about the values of the underlying data alongside the graph itself.

The next segment of code is used to label and color the data points. The process is simple enough.

  1. Color[] arrColors = new Color[]   
  2.  {   
  3.      Color.Red,  
  4.      Color.Green,   
  5.      Color.GreenYellow,  
  6.      Color.Pink,  
  7.          Color.Purple  
  8.  };  
  9.  for (int i = 0; i < chart.Series.Count; i++)  
  10.  {  
  11.      chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.HasValue = true;  
  12.      chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Color = Color.Blue;  
  13.      for (int j = 0; j < chart.Series[i].PointNumber; j++)  
  14.      {  
  15.          chart.Series[0].DataPoints[j].DataFormat.Fill.FillType = ShapeFillType.SolidColor;  
  16.          chart.Series[0].DataPoints[j].DataFormat.Fill.ForeColor = arrColors[j];  
  17.      }  
  18.  }  

Effect screenshot:

 

Chart in Excel

I hope you enjoyed this article. I'll write some features of Spire.Xls in separate articles soon.