Area Charts

This example demonstrates how to create an Area Chart.

A chart is a representational data statictics. They allow the user to get a visual image of the observations, which simplifies interpretation and drawing conclusions.

They can be used to track changes over time for one or more groups.

A good graph or chart can show as much as several paragraphs of words.

Sometime we need to represent data into charts so that it can be self explanatory.

Here firstly, We can see a drop down of cantaining types of chart

  • Area Chart
  • Stacked Area
  • Percent Stacked Area

Next we have a checkbox to opt 3D chart. By clicking checkbox it will have 3D chart

Lastly, we have versions of this representation in excel ,using dropdown you can make a choice within the following

  • Excel 97/2003
  • Excel 2007
  • Excel 2010
  • Excel 2013
  • Excel 2016

After clicking a generate button will launch a file with above choosen version of excel with used defined charts.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Excel;
using OfficeComponent.Excel.Charts;

namespace OfficeComponent.Samples
{
    class AreaChartsExample : ExcelExampleBase
    {
        public bool ThreeDChart;
        public int Type;

        public AreaChartsExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public AreaChartsExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
            // Create a new instance of WorkbookManager class.
            WorkbookManager manager = new WorkbookManager();

            // Add a workbook.
            Workbook workbook = manager.Workbooks.Add();
            workbook.Version = SaveAsFormat;

            IWorksheet sheet = workbook.Worksheets[0];

            CreateChartData(sheet);

            // Hide gridlines.
            sheet.IsGridLinesVisible = false;

            //goto Save;

            #region Chart
            // Create Chart

            IChart chart = workbook.Charts.Add();
            chart.Name = "Area Chart";
            chart.ChartTitle = "Area Chart - Sales by Region";

            if (ThreeDChart)
                switch (Type)
                {
                    case 0:
                        chart.ChartType = ChartType.Area_3D;
                        break;

                    case 1:
                        chart.ChartType = ChartType.Area_Stacked_3D;
                        break;

                    case 2:
                        chart.ChartType = ChartType.Area_Stacked_100_3D;
                        break;
                }
            else
                switch (Type)
                {
                    case 0:
                        chart.ChartType = ChartType.Area;
                        break;

                    case 1:
                        chart.ChartType = ChartType.Area_Stacked;
                        break;

                    case 2:
                        chart.ChartType = ChartType.Area_Stacked_100;
                        break;
                }

            chart.DataRange = sheet.Range["A1:F4"];

            chart.Legend.Position = ExcelLegendPosition.Bottom;
            chart.Legend.IsVerticalLegend = false;

            chart.Activate();

            #endregion

            string fileName = OutputDir + "\\" + this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat);
            // Save the document.
            workbook.SaveAs(fileName);

            // Close the document.
            workbook.Close();

            // return the output file name.
            return fileName;
        }

        void CreateChartData(IWorksheet sheet)
        {
            //Put string into a cells of Column A
            sheet["A1"].Value = "Region";
            sheet["A2"].Value = "France";
            sheet["A3"].Value = "Germany";
            sheet["A4"].Value = "England";

            //Put a value into a Row 1
            sheet["B1"].Text = "2002";
            sheet["C1"].Text = "2003";
            sheet["D1"].Text = "2004";
            sheet["E1"].Text = "2005";
            sheet["F1"].Text = "2006";

            //Put a value into a Row 2
            sheet["B2"].Number = 40000;
            sheet["C2"].Number = 45000;
            sheet["D2"].Number = 50000;
            sheet["E2"].Number = 55000;
            sheet["F2"].Number = 70000;

            //Put a value into a Row 3
            sheet["B3"].Number = 10000;
            sheet["C3"].Number = 25000;
            sheet["D3"].Number = 40000;
            sheet["E3"].Number = 52000;
            sheet["F3"].Number = 60000;

            //Put a value into a Row 4
            sheet["B4"].Number = 5000;
            sheet["C4"].Number = 15000;
            sheet["D4"].Number = 35000;
            sheet["E4"].Number = 30000;
            sheet["F4"].Number = 20000;

            sheet.UsedRange.AutofitColumns();
        }

    }

}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Excel
Imports OfficeComponent.Excel.Charts

Namespace OfficeComponent.Samples
	Friend Class AreaChartsExample
		Inherits ExcelExampleBase
		Public ThreeDChart As Boolean
		Public Type As Integer

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of WorkbookManager class.
			Dim manager As New WorkbookManager()

			' Add a workbook.
			Dim workbook As Workbook = manager.Workbooks.Add()
			workbook.Version = SaveAsFormat

			Dim sheet As IWorksheet = workbook.Worksheets(0)

			CreateChartData(sheet)

			' Hide gridlines.
			sheet.IsGridLinesVisible = False

			'goto Save;

'			#Region "Chart"
			' Create Chart

			Dim chart As IChart = workbook.Charts.Add()
			chart.Name = "Area Chart"
			chart.ChartTitle = "Area Chart - Sales by Region"

			If ThreeDChart Then
				Select Case Type
					Case 0
						chart.ChartType = ChartType.Area_3D

					Case 1
						chart.ChartType = ChartType.Area_Stacked_3D

					Case 2
						chart.ChartType = ChartType.Area_Stacked_100_3D
				End Select
			Else
				Select Case Type
					Case 0
						chart.ChartType = ChartType.Area

					Case 1
						chart.ChartType = ChartType.Area_Stacked

					Case 2
						chart.ChartType = ChartType.Area_Stacked_100
				End Select
			End If

			chart.DataRange = sheet.Range("A1:F4")

			chart.Legend.Position = ExcelLegendPosition.Bottom
			chart.Legend.IsVerticalLegend = False

			chart.Activate()

'			#End Region

			Dim fileName As String = OutputDir & "\" & Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat)
			' Save the document.
			workbook.SaveAs(fileName)

			' Close the document.
			workbook.Close()

			' return the output file name.
			Return fileName
		End Function

		Private Sub CreateChartData(ByVal sheet As IWorksheet)
			'Put string into a cells of Column A
			sheet("A1").Value = "Region"
			sheet("A2").Value = "France"
			sheet("A3").Value = "Germany"
			sheet("A4").Value = "England"

			'Put a value into a Row 1
			sheet("B1").Text = "2002"
			sheet("C1").Text = "2003"
			sheet("D1").Text = "2004"
			sheet("E1").Text = "2005"
			sheet("F1").Text = "2006"

			'Put a value into a Row 2
			sheet("B2").Number = 40000
			sheet("C2").Number = 45000
			sheet("D2").Number = 50000
			sheet("E2").Number = 55000
			sheet("F2").Number = 70000

			'Put a value into a Row 3
			sheet("B3").Number = 10000
			sheet("C3").Number = 25000
			sheet("D3").Number = 40000
			sheet("E3").Number = 52000
			sheet("F3").Number = 60000

			'Put a value into a Row 4
			sheet("B4").Number = 5000
			sheet("C4").Number = 15000
			sheet("D4").Number = 35000
			sheet("E4").Number = 30000
			sheet("F4").Number = 20000

			sheet.UsedRange.AutofitColumns()
		End Sub

	End Class

End Namespace