Results 1 to 6 of 6

Thread: Type Expected error when attempting to use Chart

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Type Expected error when attempting to use Chart

    Hi Guys,

    I am getting a Type Expected error when I am using the following simple line of code

    Dim chart as new Chart()

    I have searched extensively but am unable to find out why the error occurs or what to do about it.
    I am trying to create a simple XY Column Graph with Positive(Blue) and negative(Red) values on the Y axis and Time on the X axis

    Here is the code so far.
    Code:
    Imports System.Security.Cryptography.X509Certificates
    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1 'ColumnChartExample
        ' summary
        ' Windows Forms example for a column chart with time (seconds) on the X-axis
        ' and positive/negative values on the Y-axis.
        ' Positive values are displayed in blue; negative values are displayed in red.
    
    
        Inherits Form
    
        Dim chart As New Chart()
        Private chartArea As ChartArea
        Private series As Series
    
        Public AnalysisValues(10) As Integer
        Private Sub Form_Load()
    
        End Sub
        Private Sub Data()
            AnalysisValues = {1, 3, 4, 3, 2, 1, 0, -1, -3, -4}
        End Sub
    
        Public Sub New()
            ' Initialize the form
            Me.Text = "Column Chart Example"
            Me.Size = New Drawing.Size(900, 600)
    
            ' Create the chart control
            chart = New Chart()
            chart.Dock = DockStyle.Fill
    
            ' Create and configure the chart area
            chartArea = New ChartArea("MainArea")
            chartArea.AxisX.Title = "Time (seconds)"
            chartArea.AxisY.Title = "Value"
    
            ' Add a zero-crossing line on the Y-axis for clarity
            chartArea.AxisX.Crossing = 0
            chartArea.AxisY.Crossing = 0
            chartArea.AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount
    
            ' Optional: customize grid lines
            chartArea.AxisX.MajorGrid.LineColor = Drawing.Color.LightGray
            chartArea.AxisY.MajorGrid.LineColor = Drawing.Color.LightGray
    
            chart.ChartAreas.Add(chartArea)
    
            ' Create and configure the data series
            series = New Series("DataSeries")
            series.ChartType = SeriesChartType.Column
            series.ChartArea = "MainArea"
            series.IsValueShownAsLabel = True
            series.LabelForeColor = Drawing.Color.Black
    
            ' Sample data: time in seconds and corresponding positive/negative values
            Dim data As New List(Of Tuple(Of Double, Double)) From {
                Tuple.Create(1.0, 15.0),
                Tuple.Create(2.0, -8.0),
                Tuple.Create(3.0, 22.0),
                Tuple.Create(4.0, -5.0),
                Tuple.Create(5.0, -18.0),
                Tuple.Create(6.0, 7.0),
                Tuple.Create(7.0, 0.0),
                Tuple.Create(8.0, -12.0),
                Tuple.Create(9.0, 30.0),
                Tuple.Create(10.0, -3.0)
            }
    
            ' Populate the series and color each column based on positive/negative value
            For Each point In data
                Dim dp As New DataPoint()
                dp.SetValueXY(point.Item1, point.Item2)
                dp.AxisLabel = point.Item1.ToString() ' X-axis label shows time in seconds
    
                ' Color logic: blue for positive, red for negative
                If point.Item2 >= 0 Then
                    dp.Color = Drawing.Color.Blue
                Else
                    dp.Color = Drawing.Color.Red
                End If
    
                series.Points.Add(dp)
            Next
    
            chart.Series.Add(series)
    
            ' Add a legend for clarity
            Dim legend As New Legend("MainLegend")
            legend.Docking = Docking.Top
            chart.Legends.Add(legend)
    
            ' Add the chart to the form
            Me.Controls.Add(chart)
        End Sub
    
        ' summary
        ' Entry point to run the form.
    
            Public Shared Sub Main()
                Application.EnableVisualStyles()
                Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())   '(New ColumnChartExample())
        End Sub
    
        End Class
    Hope you are able to shed some light on my mistake?

    Regards,
    Antony

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Type Expected error when attempting to use Chart

    What happens if you highlight the word Chart in your code window? Are you offered an intellisense drop-down? You’ll often find the drop-down offers the solution to your problem

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Re: Type Expected error when attempting to use Chart

    Hi Paul,

    Intellisense just changes Dim to Private but does not clear the error.

    I can use Dim chart as new Label , for example.
    or if I switch back to VB6 or VS2012, it works. I am referencing .Net 4.8
    I am using VS2026 and I cannot find any workable solution.
    The Microsoft references are of no use and refer to System.Windows.Forms.DataVisualization.Charting
    When I search Keywords, there is no reference to Chart yet I can use the Chart object from the toolbox
    If I have to add a NuGet package to add this functionality, is there one that you might recommend?
    I am only trying to create a simple XY graph with X as time and Y with Blue positive bar going above Zero and Red negative bar values going below Zero

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Type Expected error when attempting to use Chart

    You need a reference to System.Windows.Forms.DataVisualization. Check you have that first...

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Type Expected error when attempting to use Chart

    Quote Originally Posted by AntonyL View Post
    Hi Paul,

    Intellisense just changes Dim to Private but does not clear the error.

    I can use Dim chart as new Label , for example.
    or if I switch back to VB6 or VS2012, it works. I am referencing .Net 4.8
    I am using VS2026 and I cannot find any workable solution.
    The Microsoft references are of no use and refer to System.Windows.Forms.DataVisualization.Charting
    When I search Keywords, there is no reference to Chart yet I can use the Chart object from the toolbox
    If I have to add a NuGet package to add this functionality, is there one that you might recommend?
    I am only trying to create a simple XY graph with X as time and Y with Blue positive bar going above Zero and Red negative bar values going below Zero
    You appear to be selecting the variable name ‘chart’. I meant to select the type name ‘Chart’. You should be offered options such as qualifying the type name ‘Chart’ as ‘Charting.Chart’ among other solutions…

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Re: Type Expected error when attempting to use Chart

    Hi Paul,

    After much fiddling around, I finally came up with a workable solution to my problem

    It seems that I needed to provide the fully resolved object name as in the line below.
    Dim chart As New DataVisualization.Charting.Chart

    I had already included
    Imports System.Windows.Forms.DataVisualization.Charting
    but this had no impact on being able to use Chart in
    Dim chart As New Chart

    So the working code is

    Code:
    Imports System.Security.Cryptography.X509Certificates
    Imports System.Windows.Forms.DataVisualization.Charting
    
    
    Public Class Form1 'ColumnChartExample
        ' <summary>
        ' Windows Forms example for a column chart with time (seconds) on the X-axis
        ' and positive/negative values on the Y-axis.
        ' Positive values are displayed in blue; negative values are displayed in red.
        ' </summary>
    
        Inherits Form
    
        Dim chart As New DataVisualization.Charting.Chart
        Private chartArea As ChartArea
        Private series As Series
    
        Public info(10) As Integer
        Private Sub Form_Load()
    
        End Sub
        Private Sub Data()
            info = {1, 3, 4, 3, 2, 1, 0, -1, -3, -4}
        End Sub
    
        Public Sub New()
            ' Initialize the form
            Me.Text = "Column Chart Example"
            Me.Size = New Drawing.Size(900, 600)
    
            ' Create the chart control
            chart = New DataVisualization.Charting.Chart
            chart.Dock = DockStyle.Fill
    
            ' Create and configure the chart area
            chartArea = New ChartArea("MainArea")
            chartArea.AxisX.Title = "Time (seconds)"
            chartArea.AxisY.Title = "Value"
    
            ' Add a zero-crossing line on the Y-axis for clarity
            chartArea.AxisX.Crossing = 0
            chartArea.AxisY.Crossing = 0
            chartArea.AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount
    
            ' Optional: customize grid lines
            chartArea.AxisX.MajorGrid.LineColor = Drawing.Color.LightGray
            chartArea.AxisY.MajorGrid.LineColor = Drawing.Color.LightGray
    
            chart.ChartAreas.Add(chartArea)
    
            ' Create and configure the data series
            series = New Series("DataSeries")
            series.ChartType = SeriesChartType.Column
            series.ChartArea = "MainArea"
            series.IsValueShownAsLabel = True
            series.LabelForeColor = Drawing.Color.Black
    
            ' Sample data: time in seconds and corresponding positive/negative values
            Dim data As New List(Of Tuple(Of Double, Double)) From {
                Tuple.Create(1.0, 15.0),
                Tuple.Create(2.0, -8.0),
                Tuple.Create(3.0, 22.0),
                Tuple.Create(4.0, -5.0),
                Tuple.Create(5.0, -18.0),
                Tuple.Create(6.0, 7.0),
                Tuple.Create(7.0, 0.0),
                Tuple.Create(8.0, -12.0),
                Tuple.Create(9.0, 30.0),
                Tuple.Create(10.0, -3.0)
            }
    
            ' Populate the series and color each column based on positive/negative value
            For Each point In data
                Dim dp As New DataPoint()
                dp.SetValueXY(point.Item1, point.Item2)
                dp.AxisLabel = point.Item1.ToString() ' X-axis label shows time in seconds
    
                ' Color logic: blue for positive, red for negative
                If point.Item2 >= 0 Then
                    dp.Color = Drawing.Color.Blue
                Else
                    dp.Color = Drawing.Color.Red
                End If
    
                series.Points.Add(dp)
            Next
    
            chart.Series.Add(series)
    
            ' Add a legend for clarity
            Dim legend As New Legend("MainLegend")
            legend.Docking = Docking.Top
            chart.Legends.Add(legend)
    
            ' Add the chart to the form
            Me.Controls.Add(chart)
        End Sub
    
        ''' <summary>
        ''' Entry point to run the form.
        ''' </summary>
        <STAThread()>
            Public Shared Sub Main()
                Application.EnableVisualStyles()
                Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())   '(New ColumnChartExample())
        End Sub
    
        End Class
    Regards,
    Antony

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width