Area Charts

This example demonstrates how to create an Area Chart.

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 PdfDocument 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();
        }

    }

}
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 PdfDocument 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();
        }

    }

}