How to Draw Line Chart in Excel Based on Existed Data Information with C#

Line Chart is used to present changes of data during a specified period. It is called line chart because the changes are drawn as lines. For example, if an Excel file saves each product sales information in one month, we can draw a line chart to show data visually. In line chart, horizontal axis presents data categories and vertical axis presents value.

In this post, I will share my method about how to draw line chart in Excel file by using C#. I have prepared an Excel file which has saved some data information. Then, I will draw line chart based on the data in the same worksheet. 

Note: a component, Spire.XLS is used in this method. So, I add its dll file as reference in my project at the beginning. 

STEPS:

  1. Declare a new workbook and load Excel file by using workbook.LoadFromFile() method.
  2. Initialize worksheet. Change worksheet name.
  3. Insert Excel chart. Set chart type as line chart and then confirm data range.
  4. Define chart position, including top/bottom row, left/right column.
  5. Set chart title, category axis and value axis titles and format them.
  6. Save and launch new Excel file.

CODE:

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

namespace ExcelLineChart
{
    class LineChart
    {
        static void Main(string[] args)
        {

            //Load File
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@”D:\work\My Documents\SalesReport.xls”);

            //Initailize worksheet
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Name = “Sales Report”;
            sheet.GridLinesVisible = false;

            //Define Chart Type adn Data Range
            Chart chart = sheet.Charts.Add();
            chart.ChartType = ExcelChartType.Line;
            chart.DataRange = sheet.Range[“A1:G8”];

            //Chart Position
            chart.LeftColumn = 1;
            chart.TopRow = 10;
            chart.RightColumn = 10;
            chart.BottomRow = 28;

            //Chart Title
            chart.ChartTitle = “Sales Report”;
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Font.FontName = “Calibri”;
            chart.ChartTitleArea.Size = 14;

            //Chart Axis
            chart.PrimaryCategoryAxis.Title = “Month”;
            chart.PrimaryCategoryAxis.Font.FontName = “Calibri”;
            chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
            chart.PrimaryCategoryAxis.TitleArea.Size = 12;

            chart.PrimaryValueAxis.Title = “Total Sales”;
            chart.PrimaryValueAxis.HasMajorGridLines = false;
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
            chart.PrimaryValueAxis.MinValue = 4500;
            chart.PrimaryValueAxis.TitleArea.Font.FontName = “Calibri”;
            chart.PrimaryValueAxis.TitleArea.IsBold = true;
            chart.PrimaryValueAxis.TitleArea.Size = 12;

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

            chart.PlotArea.Fill.Visible = false;
            chart.Legend.Position = LegendPositionType.Top;

            //Save and Luanch
            workbook.SaveToFile(“LineChart.xls”);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

RESULT:

______________________________________________________________________________

Click Here to LEARN MORE about Spire.XLS

Click Here to DOWNLOAD Spire.XLS

Advertisements

2 thoughts on “How to Draw Line Chart in Excel Based on Existed Data Information with C#

    • janewdaisy says:

      In my example, I add Spire.XLS.dll file. If you want to use it, please download Spire.XLS and install it at first. Also, the dll version must match with your .NET Framework version. For example, I use Spire.XLS.dll file version 6.0.8.4 with .NET Framework 4.0.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s