Charts

  • Demo
  • C# source
  • VB.Net source
This demo shows you how to create chart in an excel workbook.
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the demo you want, please contact support@e-iceblue.com for the free customized demo just for you.
using System.Data;
using Spire.Xls;

namespace DemoOnlineCode
{
    class Charts
    {
        public void demoChart(DataTable table, ExcelChartType chartFormat, string resultFileName)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets[0];
            sheet.InsertDataTable(table, true, 1, 1);
            SetChart(sheet, chartFormat);
            sheetStyle(book, sheet);
            book.SaveToFile(resultFileName,ExcelVersion.Version2010);
        
        }
        private void SetChart(Worksheet sheet, ExcelChartType chartFormat)
        {
            sheet.Name = "Chart data";
            sheet.GridLinesVisible = false;

            //Writes chart data
            //CreateChartData(sheet);
            //Add a new  chart worsheet to workbook
            Chart chart = sheet.Charts.Add();

            //Set region of chart data

            chart.DataRange = sheet.Range["A1:C7"];
            chart.SeriesDataFromRange = false;

            //Set position of chart
            chart.LeftColumn = 1;
            chart.TopRow = 8;
            chart.RightColumn = 11;
            chart.BottomRow = 29;
            chart.ChartType = chartFormat;


            //Chart title
            chart.ChartTitle = "Sales market by country";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;

            chart.PrimaryCategoryAxis.Title = "Country";
            chart.PrimaryCategoryAxis.Font.IsBold = true;
            chart.PrimaryCategoryAxis.TitleArea.IsBold = true;

            chart.PrimaryValueAxis.Title = "Sales(in Dollars)";
            chart.PrimaryValueAxis.HasMajorGridLines = false;
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
            chart.PrimaryValueAxis.MinValue = 1000;
            chart.PrimaryValueAxis.TitleArea.IsBold = true;


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

            chart.Legend.Position = LegendPositionType.Top;

        }
        public static void sheetStyle(Workbook book, Worksheet sheet)
        {
            CellStyle oddStyle = book.Styles.Add("oddStyle");
            oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            oddStyle.KnownColor = ExcelColors.LightGreen1;

            CellStyle evenStyle = book.Styles.Add("evenStyle");
            evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            evenStyle.KnownColor = ExcelColors.LightTurquoise;


            foreach (CellRange range in sheet.AllocatedRange.Rows)
            {
                if (range.Row % 2 == 0)
                    range.CellStyleName = evenStyle.Name;
                else
                    range.CellStyleName = oddStyle.Name;
            }

            //Sets header style
            CellStyle styleHeader = sheet.Rows[0].Style;
            styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            styleHeader.VerticalAlignment = VerticalAlignType.Center;
            styleHeader.KnownColor = ExcelColors.Green;
            styleHeader.Font.KnownColor = ExcelColors.White;
            styleHeader.Font.IsBold = true;

            sheet.Columns[sheet.AllocatedRange.LastColumn - 1].Style.NumberFormat = "\"$\"#,##0";
            sheet.Columns[sheet.AllocatedRange.LastColumn - 2].Style.NumberFormat = "\"$\"#,##0";

            sheet.AllocatedRange.AutoFitColumns();
            sheet.AllocatedRange.AutoFitRows();

            sheet.Rows[0].RowHeight = 20;
        }
    }
}

Imports System.Data
Imports Spire.XLS

Namespace DemoOnlineCode
    Class Charts
        Public Sub demoChart(table As DataTable, chartFormat As ExcelChartType, resultFileName As String)
            Dim book As New Workbook()
            Dim sheet As Worksheet = book.Worksheets(0)
            sheet.InsertDataTable(table, True, 1, 1)
            SetChart(sheet, chartFormat)
            sheetStyle(book, sheet)
            book.SaveToFile(resultFileName, ExcelVersion.Version2010)

        End Sub
        Private Sub SetChart(sheet As Worksheet, chartFormat As ExcelChartType)
            sheet.Name = "Chart data"
            sheet.GridLinesVisible = False

            'Writes chart data
            'CreateChartData(sheet);
            'Add a new  chart worsheet to workbook
            Dim chart As Chart = sheet.Charts.Add()

            'Set region of chart data

            chart.DataRange = sheet.Range("A1:C7")
            chart.SeriesDataFromRange = False

            'Set position of chart
            chart.LeftColumn = 1
            chart.TopRow = 8
            chart.RightColumn = 11
            chart.BottomRow = 29
            chart.ChartType = chartFormat


            'Chart title
            chart.ChartTitle = "Sales market by country"
            chart.ChartTitleArea.IsBold = True
            chart.ChartTitleArea.Size = 12

            chart.PrimaryCategoryAxis.Title = "Country"
            chart.PrimaryCategoryAxis.Font.IsBold = True
            chart.PrimaryCategoryAxis.TitleArea.IsBold = True

            chart.PrimaryValueAxis.Title = "Sales(in Dollars)"
            chart.PrimaryValueAxis.HasMajorGridLines = False
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90
            chart.PrimaryValueAxis.MinValue = 1000
            chart.PrimaryValueAxis.TitleArea.IsBold = True


            For Each cs As Spire.Xls.Charts.ChartSerie In chart.Series
                cs.Format.Options.IsVaryColor = True
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
            Next

            chart.Legend.Position = LegendPositionType.Top

        End Sub
        Public Shared Sub sheetStyle(book As Workbook, sheet As Worksheet)
            Dim oddStyle As CellStyle = book.Styles.Add("oddStyle")
            oddStyle.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
            oddStyle.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
            oddStyle.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
            oddStyle.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
            oddStyle.KnownColor = ExcelColors.LightGreen1

            Dim evenStyle As CellStyle = book.Styles.Add("evenStyle")
            evenStyle.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
            evenStyle.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
            evenStyle.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
            evenStyle.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
            evenStyle.KnownColor = ExcelColors.LightTurquoise


            For Each range As CellRange In sheet.AllocatedRange.Rows
                If range.Row Mod 2 = 0 Then
                    range.CellStyleName = evenStyle.Name
                Else
                    range.CellStyleName = oddStyle.Name
                End If
            Next

            'Sets header style
            Dim styleHeader As CellStyle = sheet.Rows(0).Style
            styleHeader.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
            styleHeader.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
            styleHeader.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
            styleHeader.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
            styleHeader.VerticalAlignment = VerticalAlignType.Center
            styleHeader.KnownColor = ExcelColors.Green
            styleHeader.Font.KnownColor = ExcelColors.White
            styleHeader.Font.IsBold = True

            sheet.Columns(sheet.AllocatedRange.LastColumn - 1).Style.NumberFormat = """$""#,##0"
            sheet.Columns(sheet.AllocatedRange.LastColumn - 2).Style.NumberFormat = """$""#,##0"

            sheet.AllocatedRange.AutoFitColumns()
            sheet.AllocatedRange.AutoFitRows()

            sheet.Rows(0).RowHeight = 20
        End Sub
    End Class
End Namespace