Search Results for

    Show / Hide Table of Contents

    Chart API (VB.Net / netframework)

    Note

    This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\vb\VS2022\netframework\10.API\16.Chart API and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​NET-​demos/​tree/​master/​vb/​VS2022/​netframework/​Modules/​10.​API/​16.​Chart API

    Overview

    Here we will show how to create a simple chart only with code. Note that as charts are complex and have many configuration options, the best way to find out how to create a particular chart is to create it in Excel, and then open it with APIMate. APIMate has full support for showing the chart API.

    Concepts

    • How you can use ExcelFile.AddChart, ExcelChart.AddSeries and other commands to create a simple chart.

    • The chart API works only in xlsx files, so this demo wouldn't work with xls files.

    Files

    AssemblyInfo.vb

    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    
    '
    ' General Information about an assembly is controlled through the following 
    ' set of attributes. Change these attribute values to modify the information
    ' associated with an assembly.
    '
    <Assembly: AssemblyTitle("")>
    <Assembly: AssemblyDescription("")>
    <Assembly: AssemblyConfiguration("")>
    <Assembly: AssemblyCompany("")>
    <Assembly: AssemblyProduct("")>
    <Assembly: AssemblyCopyright("(c) 2002 - 2014 TMS Software")>
    <Assembly: AssemblyTrademark("")>
    <Assembly: AssemblyCulture("")>
    
    '
    ' Version information for an assembly consists of the following four values:
    '
    '      Major Version
    '      Minor Version 
    '      Build Number
    '      Revision
    '
    ' You can specify all the values or you can default the Revision and Build Numbers 
    ' by using the '*' as shown below:
    
    <Assembly: AssemblyVersion("6.2.1.0")>
    
    '
    ' In order to sign your assembly you must specify a key to use. Refer to the 
    ' Microsoft .NET Framework documentation for more information on assembly signing.
    '
    ' Use the attributes below to control which key is used for signing. 
    '
    ' Notes: 
    '   (*) If no key is specified, the assembly is not signed.
    '   (*) KeyName refers to a key that has been installed in the Crypto Service
    '       Provider (CSP) on your machine. KeyFile refers to a file which contains
    '       a key.
    '   (*) If the KeyFile and the KeyName values are both specified, the 
    '       following processing occurs:
    '       (1) If the KeyName can be found in the CSP, that key is used.
    '       (2) If the KeyName does not exist and the KeyFile does exist, the key 
    '           in the KeyFile is installed into the CSP and used.
    '   (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
    '       When specifying the KeyFile, the location of the KeyFile should be
    '       relative to the project output directory which is
    '       %Project Directory%\obj\<configuration>. For example, if your KeyFile is
    '       located in the project directory, you would specify the AssemblyKeyFile 
    '       attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
    '   (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
    '       documentation for more information on this.
    '
    <Assembly: AssemblyDelaySign(False)>
    <Assembly: AssemblyKeyFile("")>
    <Assembly: AssemblyKeyName("")>
    

    Form1.Designer.vb

    Imports System.Collections
    Imports FlexCel.Core
    Imports FlexCel.XlsAdapter
    Imports System.IO
    Imports System.Reflection
    Imports System.Text
    Namespace ChartAPI
        Partial Public Class mainForm
            Inherits System.Windows.Forms.Form
    
            Private WithEvents button1 As System.Windows.Forms.Button
            Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
            Private label1 As System.Windows.Forms.Label
            ''' <summary>
            ''' Required designer variable.
            ''' </summary>
            Private components As System.ComponentModel.Container = Nothing
    
            ''' <summary>
            ''' Clean up any resources being used.
            ''' </summary>
            Protected Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing Then
                    If components IsNot Nothing Then
                        components.Dispose()
                    End If
                End If
                MyBase.Dispose(disposing)
            End Sub
    
            #Region "Windows Form Designer generated code"
            ''' <summary>
            ''' Required method for Designer support - do not modify
            ''' the contents of this method with the code editor.
            ''' </summary>
            Private Sub InitializeComponent()
                Me.button1 = New System.Windows.Forms.Button()
                Me.saveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
                Me.label1 = New System.Windows.Forms.Label()
                Me.SuspendLayout()
                ' 
                ' button1
                ' 
                Me.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom
                Me.button1.Location = New System.Drawing.Point(136, 160)
                Me.button1.Name = "button1"
                Me.button1.Size = New System.Drawing.Size(75, 23)
                Me.button1.TabIndex = 0
                Me.button1.Text = "GO!"
    '           Me.button1.Click += New System.EventHandler(Me.button1_Click)
                ' 
                ' saveFileDialog1
                ' 
                Me.saveFileDialog1.DefaultExt = "xlsx"
                Me.saveFileDialog1.Filter = "Excel Files (2007 or newer)|*.xlsx"
                Me.saveFileDialog1.RestoreDirectory = True
                ' 
                ' label1
                ' 
                Me.label1.Anchor = (CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles))
                Me.label1.BackColor = System.Drawing.Color.FromArgb((CInt((CByte(255)))), (CInt((CByte(255)))), (CInt((CByte(192)))))
                Me.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
                Me.label1.Location = New System.Drawing.Point(16, 16)
                Me.label1.Name = "label1"
                Me.label1.Size = New System.Drawing.Size(320, 90)
                Me.label1.TabIndex = 1
                Me.label1.Text = "A demo showing how to create a simple chart with code. Use APIMate for more compl" & "ex charts"
                ' 
                ' mainForm
                ' 
                Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
                Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
                Me.ClientSize = New System.Drawing.Size(344, 197)
                Me.Controls.Add(Me.label1)
                Me.Controls.Add(Me.button1)
                Me.Name = "mainForm"
                Me.Text = "Chart API"
                Me.ResumeLayout(False)
    
            End Sub
            #End Region
        End Class
    End Namespace
    

    Form1.vb

    Imports System.Collections
    Imports FlexCel.Core
    Imports FlexCel.XlsAdapter
    Imports System.IO
    Imports System.Reflection
    Imports System.Text
    
    Namespace ChartAPI
        ''' <summary>
        ''' A demo on creating a chart with code.
        ''' </summary>
        Partial Public Class mainForm
            Inherits System.Windows.Forms.Form
    
            Public Sub New()
                InitializeComponent()
            End Sub
    
            Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
                AutoRun()
            End Sub
    
            Private ReadOnly Property PathToExe() As String
                Get
                    Return Path.Combine(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), ".."), "..") & Path.DirectorySeparatorChar
                End Get
            End Property
    
            Private Sub AutoRun()
                'We will use data already stored in a file. For a real case, you would
                'probably fill this data from some database.
                Dim fileName As String = Path.Combine(PathToExe, "git-stats.xlsx")
                Dim Xls As ExcelFile = New XlsFile(fileName, True)
    
                'Add a new empty sheet for adding the chart.
                Xls.InsertAndCopySheets(0, 1, 1)
                Xls.ActiveSheet = 1
                Xls.SheetName = "Chart"
                Xls.PrintToFit = True
                Xls.PrintScale = 70
                Xls.PrintXResolution = 600
                Xls.PrintYResolution = 600
                Xls.PrintOptions = TPrintOptions.None
                Xls.PrintPaperSize = TPaperSize.Letter
                Xls.PrintLandscape = True
    
                AddChart(Xls)
                NormalOpen(Xls)
            End Sub
    
    
    
            Private Sub AddChart(ByVal xls As ExcelFile)
                'This code is adapted from APIMate.
                'Objects
                Dim ChartOptions1 As New TShapeProperties()
                ChartOptions1.Anchor = New TClientAnchor(TFlxAnchorType.MoveAndResize, 1, 215, 1, 608, 30, 228, 17, 736)
                ChartOptions1.ShapeName = "Lines of code"
                ChartOptions1.Print = True
                ChartOptions1.Visible = True
                ChartOptions1.ShapeOptions.SetValue(TShapeOption.fLockText, True)
                ChartOptions1.ShapeOptions.SetValue(TShapeOption.LockRotation, True)
                ChartOptions1.ShapeOptions.SetValue(TShapeOption.fAutoTextMargin, True)
                ChartOptions1.ShapeOptions.SetValue(TShapeOption.fillColor, 134217806)
                ChartOptions1.ShapeOptions.SetValue(TShapeOption.wzName, "Lines of code")
                Dim Chart1 As ExcelChart = xls.AddChart(ChartOptions1, TChartType.Area, New ChartStyle(102), False)
    
                Dim Title As New TDataLabel()
                Title.PositionZeroBased = Nothing
                Dim TextFillOptions As New ChartFillOptions(New TShapeFill(New TSolidFill(TDrawingColor.FromRgb(&H80, &H80, &H80)), True, TFormattingType.Subtle, TDrawingColor.FromRgb(&H0, &H0, &H0, New TColorTransform(TColorTransformType.Alpha, 0)), False))
                Dim LabelTextOptions As New TChartTextOptions(New TFlxChartFont("Calibri Light", 320, TExcelColor.FromArgb(&H80, &H80, &H80), TFlxFontStyles.Bold, TFlxUnderline.None, TFontScheme.Major), THFlxAlignment.center, TVFlxAlignment.center, TBackgroundMode.Transparent, TextFillOptions)
                Title.TextOptions = LabelTextOptions
                Dim LabelOptions As New TDataLabelOptions()
                Title.LabelOptions = LabelOptions
                Dim ChartLineOptions As New ChartLineOptions(New TShapeLine(True, New TLineStyle(New TNoFill(), Nothing), Nothing, TFormattingType.Subtle))
                Dim ChartFillOptions As New ChartFillOptions(New TShapeFill(New TNoFill(), False, TFormattingType.Subtle, Nothing, False))
                Title.Frame = New TChartFrameOptions(ChartLineOptions, ChartFillOptions, False)
    
                Dim Runs() As TRTFRun
                Runs = New TRTFRun(0){}
                Runs(0).FirstChar = 0
                Dim fnt As TFlxFont
                fnt = xls.GetDefaultFont
                fnt.Name = "Calibri Light"
                fnt.Size20 = 320
                fnt.Color = TExcelColor.FromArgb(&H80, &H80, &H80)
                fnt.Style = TFlxFontStyles.Bold
                fnt.Family = 0
                fnt.CharSet = 1
                fnt.Scheme = TFontScheme.Major
                Runs(0).FontIndex = xls.AddFont(fnt)
                Dim LabelValue1 As New TRichString("FlexCel: Lines of code over time", Runs, xls)
    
                Title.LabelValues = New Object() { LabelValue1 }
    
                Chart1.SetTitle(Title)
    
                Chart1.Background = New TChartFrameOptions(TDrawingColor.FromTheme(TThemeColor.Dark1, New TColorTransform(TColorTransformType.LumMod, 0.15), New TColorTransform(TColorTransformType.LumOff, 0.85)), 9525, TDrawingColor.FromTheme(TThemeColor.Light1), False)
    
                Dim PlotAreaFrame As TChartFrameOptions
                ChartLineOptions = New ChartLineOptions(New TShapeLine(True, New TLineStyle(New TNoFill(), Nothing), Nothing, TFormattingType.Subtle))
                ChartFillOptions = New ChartFillOptions(New TShapeFill(New TPatternFill(TDrawingColor.FromTheme(TThemeColor.Dark1, New TColorTransform(TColorTransformType.LumMod, 0.15), New TColorTransform(TColorTransformType.LumOff, 0.85)), TDrawingColor.FromTheme(TThemeColor.Light1), TDrawingPattern.ltDnDiag), True, TFormattingType.Subtle, Nothing, False))
                PlotAreaFrame = New TChartFrameOptions(ChartLineOptions, ChartFillOptions, False)
                Dim PlotAreaPos As New TChartPlotAreaPosition(True, TChartRelativeRectangle.Automatic, TChartLayoutTarget.Inner, True)
                Chart1.PlotArea = New TChartPlotArea(PlotAreaFrame, PlotAreaPos, False)
    
                Chart1.SetChartOptions(1, New TAreaChartOptions(False, TStackedMode.Stacked, Nothing))
    
                Dim LastYear As Integer = 0
                Dim shade As Double = 1
                For i As Integer = 2 To 189
                    Dim Series As New ChartSeries("=" & (New TCellAddress("Data", 1, i, True, True)).CellRef, "=" & (New TCellAddress("Data", 2, i, True, True)).CellRef & ":" & (New TCellAddress("Data", 189, i, True, True)).CellRef, "=Data!$A$2:$A$189")
    
                    'We will display every year in a single color. Each month gets its own shade.
                    Dim xf As Integer = -1
                    Dim Year As Integer = FlxDateTime.FromOADate((CDbl(xls.GetCellValue(2, 1, i, xf))), False).Year
                    If LastYear <> Year Then
                        shade = 1
                    ElseIf shade > 0.3 Then
                        shade -= 0.05
                    End If
                        LastYear = Year
                    Dim SeriesColor As TDrawingColor = TDrawingColor.FromTheme(CType(TThemeColor.Accent1 + (Year Mod 6), TThemeColor), New TColorTransform(TColorTransformType.Shade, shade))
    
    
                    Dim SeriesFill As New ChartSeriesFillOptions(New TShapeFill(New TSolidFill(SeriesColor), True, TFormattingType.Subtle, Nothing, False), Nothing, False, False)
                    Dim SeriesLine As New ChartSeriesLineOptions(New TShapeLine(True, New TLineStyle(New TNoFill(), Nothing), Nothing, TFormattingType.Subtle), False)
                    Series.Options.Add(New ChartSeriesOptions(-1, SeriesFill, SeriesLine, Nothing, Nothing, Nothing, True))
    
                    Chart1.AddSeries(Series)
                Next i
    
                Chart1.PlotEmptyCells = TPlotEmptyCells.Zero
                Chart1.ShowDataInHiddenRowsAndCols = False
    
                Dim AxisFont As New TFlxChartFont("Calibri", 180, TExcelColor.FromArgb(&H59, &H59, &H59), TFlxFontStyles.None, TFlxUnderline.None, TFontScheme.Minor)
                Dim AxisLine As New TAxisLineOptions()
                AxisLine.MainAxis = New ChartLineOptions(New TShapeLine(True, New TLineStyle(New TSolidFill(TDrawingColor.FromTheme(TThemeColor.Dark1, New TColorTransform(TColorTransformType.LumMod, 0.15), New TColorTransform(TColorTransformType.LumOff, 0.85))), 9525, TPenAlignment.Center, TLineCap.Flat, TCompoundLineType.Single, Nothing, TLineJoin.Round, Nothing, Nothing, Nothing), Nothing, TFormattingType.Subtle))
                AxisLine.DoNotDrawLabelsIfNotDrawingAxis = False
                Dim AxisTicks As New TAxisTickOptions(TTickType.Outside, TTickType.None, TAxisLabelPosition.NextToAxis, TBackgroundMode.Transparent, TDrawingColor.FromRgb(&H59, &H59, &H59), 0)
                Dim AxisRangeOptions As New TAxisRangeOptions(12, 1, False, False, False)
                Dim CatAxis As TBaseAxis = New TCategoryAxis(0, 0, 12, TDateUnits.Days, 12, TDateUnits.Days, TDateUnits.Months, 0, TCategoryAxisOptions.AutoMin Or TCategoryAxisOptions.AutoMax Or TCategoryAxisOptions.DateAxis Or TCategoryAxisOptions.AutoCrossDate Or TCategoryAxisOptions.AutoDate, AxisFont, "yyyy\-mm\-dd;@", True, AxisLine, AxisTicks, AxisRangeOptions, Nothing, TChartAxisPos.Bottom, 1)
                AxisFont = New TFlxChartFont("Calibri", 180, TExcelColor.FromArgb(&H59, &H59, &H59), TFlxFontStyles.None, TFlxUnderline.None, TFontScheme.Minor)
                AxisLine = New TAxisLineOptions()
                AxisLine.MainAxis = New ChartLineOptions(New TShapeLine(True, New TLineStyle(New TSolidFill(TDrawingColor.FromTheme(TThemeColor.Dark1, New TColorTransform(TColorTransformType.LumMod, 0.15), New TColorTransform(TColorTransformType.LumOff, 0.85))), 9525, TPenAlignment.Center, TLineCap.Flat, TCompoundLineType.Single, Nothing, TLineJoin.Round, Nothing, Nothing, Nothing), Nothing, TFormattingType.Subtle))
                AxisLine.MajorGridLines = New ChartLineOptions(New TShapeLine(True, New TLineStyle(New TSolidFill(TDrawingColor.FromTheme(TThemeColor.Dark1, New TColorTransform(TColorTransformType.LumMod, 0.15), New TColorTransform(TColorTransformType.LumOff, 0.85))), 9525, TPenAlignment.Center, TLineCap.Flat, TCompoundLineType.Single, Nothing, TLineJoin.Round, Nothing, Nothing, Nothing), Nothing, TFormattingType.Subtle))
                AxisLine.DoNotDrawLabelsIfNotDrawingAxis = False
                AxisTicks = New TAxisTickOptions(TTickType.None, TTickType.None, TAxisLabelPosition.NextToAxis, TBackgroundMode.Transparent, TDrawingColor.FromRgb(&H59, &H59, &H59), 0)
                CatAxis.NumberFormat = "yyyy-mm"
                CatAxis.NumberFormatLinkedToSource = False
    
                Dim ValAxis As TBaseAxis = New TValueAxis(0, 0, 0, 0, 0, TValueAxisOptions.AutoMin Or TValueAxisOptions.AutoMax Or TValueAxisOptions.AutoMajor Or TValueAxisOptions.AutoMinor Or TValueAxisOptions.AutoCross, AxisFont, "General", True, AxisLine, AxisTicks, Nothing, TChartAxisPos.Left)
                Chart1.SetChartAxis(New TChartAxis(0, CatAxis, ValAxis))
    
            End Sub
    
            Private Sub NormalOpen(ByVal Xls As ExcelFile)
                If saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                    Xls.Save(saveFileDialog1.FileName)
    
                    If MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
                        Process.Start(saveFileDialog1.FileName)
                    End If
                End If
            End Sub
        End Class
    End Namespace
    

    Program.vb

    Namespace ChartAPI
        Friend NotInheritable Class Program
    
            Private Sub New()
            End Sub
    
            ''' <summary>
            ''' The main entry point for the application.
            ''' </summary>
           <STAThread> _
            Shared Sub Main()
                Application.EnableVisualStyles()
                Application.SetCompatibleTextRenderingDefault(False)
                Application.Run(New mainForm())
            End Sub
        End Class
    End Namespace
    
    In This Article
    Back to top FlexCel Studio for the .NET Framework v7.24.0.0
    © 2002 - 2025 tmssoftware.com