JuiCe
JuiCe

Reputation: 4201

How can I make my Line Graph appear?

I am trying to programatically get my Chart object to appear. I have been able to get the Chart to appear using the Design file, but for what I am doing, i need to understand how the Chart Control works programatically.

This is my code:

    Dim wavesGraph As New Chart()

    Dim chartarea As New ChartArea

    Dim vn1Series As New Series("VN1", SeriesChartType.Line)
    Dim vn2Series As New Series("VN2", SeriesChartType.Line)
    Dim vn3Series As New Series("VN3", SeriesChartType.Line)
    Dim vdi1Series As New Series("VDI1", SeriesChartType.Line)
    Dim vdi2Series As New Series("VDI2", SeriesChartType.Line)
    Dim vdi3Series As New Series("VDI3", SeriesChartType.Line)


    wavesGraph.ChartAreas.Add(chartarea)

    wavesGraph.Series.Add(vn1Series)
    wavesGraph.Series.Add(vn2Series)
    wavesGraph.Series.Add(vn3Series)
    wavesGraph.Series.Add(vdi1Series)
    wavesGraph.Series.Add(vdi2Series)
    wavesGraph.Series.Add(vdi3Series)

    Console.WriteLine("Here1")
    wavesGraph.ChartAreas(0).Visible = True


    wavesGraph.ChartAreas(0).AxisY2.Enabled = AxisEnabled.True
    wavesGraph.ChartAreas(0).AxisY.Title = "Network Voltage"
    If (WavesIsI) Then

        wavesGraph.ChartAreas(0).AxisY2.Title = "Network Current"
    Else
        wavesGraph.ChartAreas(0).AxisY2.Title = "Differential Volts"
    End If
    wavesGraph.Series("VDI1").YAxisType = AxisType.Secondary
    wavesGraph.Series("VDI2").YAxisType = AxisType.Secondary
    wavesGraph.Series("VDI3").YAxisType = AxisType.Secondary


    Dim rand As New Random
    Console.WriteLine("Here2")

    For i As Integer = 0 To 166

        wavesGraph.Series(0).Points.AddXY(i, rand.Next(-255, 255))
        'wavesGraph.Series(1).Points.AddXY(i, rand.Next(-255, 255))
        'wavesGraph.Series(2).Points.AddXY(i, rand.Next(-255, 255))
        wavesGraph.Series(3).Points.AddXY(i, rand.Next(-255, 255))
        'wavesGraph.Series(4).Points.AddXY(i, rand.Next(-255, 255))
        'wavesGraph.Series(5).Points.AddXY(i, rand.Next(-255, 255))
    Next
    wavesGraph.Enabled = True
    wavesGraph.Visible = True
    Console.WriteLine("Here3")

This is the resulting Line Graph that I get: enter image description here

Upvotes: 0

Views: 417

Answers (1)

Jens
Jens

Reputation: 6375

I copied your code 1:1 and just added Me.Controls.Add(wavesGraph) at the end to add the graph to the form.

You did not add the control to the form in any way so the newly created graph never shows up.

Upvotes: 1

Related Questions