6 Replies - 325 Views - Last Post: 24 July 2012 - 07:26 PM Rate Topic: -----

#1 ryansandberg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 12

Stacked Chart Issue in VB.NET

Posted 17 July 2012 - 07:49 AM

I don't understand why when ListBox4 index (n) becomes 8 (string="joe") that the corresponding string "truck" doesn't stack onto string "bike" and show 2 color coded values of 90 and 20. It just overwrites the 20 and shows 90. All n's less than 8 do this fine for multiple people and/or multiple vehicles

Can anyone help??

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'X AXIS RAW DATA, NOT UNIQUE
        With ListBox4
            .Items.Add("joe")
            .Items.Add("greg")
            .Items.Add("bob")
            .Items.Add("ryan")
            .Items.Add("ryan")
            .Items.Add("fred")
            .Items.Add("bob")
            .Items.Add("fred")
            .Items.Add("joe")
            .Items.Add("fred")
            .Items.Add("greg")
        End With
        ' SERIES RAW DATA, NOT UNIQUE
        With ListBox5
            .Items.Add("bike")
            .Items.Add("sled")
            .Items.Add("plane")
            .Items.Add("truck")
            .Items.Add("car")
            .Items.Add("sled")
            .Items.Add("boat")
            .Items.Add("car")
            .Items.Add("truck")
            .Items.Add("motorcycle")
            .Items.Add("motorcycle")
        End With
        ' Y AXIS RAW DATA, NOT UNIQUE
        With ListBox6
            '  .Items.Add(40)
            .Items.Add(20)
            .Items.Add(10)
            .Items.Add(30)
            .Items.Add(40)
            .Items.Add(50)
            .Items.Add(60)
            .Items.Add(70)
            .Items.Add(5)
            .Items.Add(90)
            .Items.Add(40)
            .Items.Add(100)
        End With
        'X AXIS DATA, UNIQUE
        With ListBox1
            .Items.Add("ryan")
            .Items.Add("joe")
            .Items.Add("bob")
            .Items.Add("fred")
            .Items.Add("greg")
        End With
        'SERIES DATA, UNIQUE
        With ListBox2
            .Items.Add("truck")
            .Items.Add("bike")
            .Items.Add("car")
            .Items.Add("motorcycle")
            .Items.Add("plane")
            .Items.Add("boat")
            .Items.Add("sled")
        End With

        Me.Show()


        'TRAP FOR UNEQUAL LIST LENGTHS
        Do While ListBox4.Items.Count <> ListBox5.Items.Count Or ListBox4.Items.Count <> ListBox6.Items.Count Or ListBox5.Items.Count <> ListBox6.Items.Count
            MsgBox("Fix List Lengths")
        Loop



        'CLEAR EXISTING SERIES'
        With Chart1
            .Series.Clear()
            ' Dim n As String
            n = 0
            'ADD ALL THE SERIES' BASED UPON COUNT FROM LISTBOX2
            Do While n <= ListBox2.Items.Count - 1
                .Series.Add(n)
                .Series(n).Name = ListBox2.Items.Item(n)
                Me.Update()
                n = n + 1
            Loop


            n = 0
            For Me.n = 0 To ListBox4.Items.Count - 1
                g = 0
                'DETERMINE LISTBOX1 INDEX, R1 FROM CURRENT POSITION (N) IN LISTBOX 4 (NAMES)
                Do While ListBox4.Items.Item(n) <> ListBox1.Items.Item(g)
                    g = g + 1
                Loop
                r1 = g
                ' ListBox3.Items.Item(r1) = ListBox3.Items.Item(r1) + 1
                Me.Update()
                g = 0
                'DETERMINE LISTBOX2 INDEX, R2 FROM CURRENT POSITION (N) IN LISTBOX 5 (VEHICLES)
                Do While ListBox5.Items.Item(n) <> ListBox2.Items.Item(g)
                    g = g + 1
                Loop
                r2 = g
                g = 0

                'Z WILL BE THE INDEX TO DETERMINE IF VALID DATA OR ZERO
                For z = 0 To ListBox1.Items.Count - 1
                    If z = r1 Then
                        'REAL DATA ENTRY TO KEEP DIMENSIONING CORRECT
                        .Series(r2).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
                        .Series(r2).Points.AddXY(r1, ListBox6.Items.Item(n))
                        .Series(r2).Points(z).AxisLabel = ListBox1.Items.Item(z)
                        Me.Update()
                    Else
                        'FILLER DATA ENTRY TO KEEP DIMENSIONING CORRECT
                        .Series(r2).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
                        .Series(r2).Points.AddXY(z, 0)
                        Me.Update()
                    End If

                Next
            Next
        End With
    End Sub
End Class


This post has been edited by Martyr2: 17 July 2012 - 12:21 PM
Reason for edit:: Please use code tags in the future, thanks! :)


Is This A Good Question/Topic? 0
  • +

Replies To: Stacked Chart Issue in VB.NET

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

Re: Stacked Chart Issue in VB.NET

Posted 17 July 2012 - 03:45 PM

Quote

I don't understand why when ListBox4 index (n) becomes 8 (string="joe") that the corresponding string "truck" doesn't stack onto string "bike" and show 2 color coded values of 90 and 20. It just overwrites the 20 and shows 90. All n's less than 8 do this fine for multiple people and/or multiple vehicles
From this description it's hard to understand what is your problem. Cane you tell what is the purpose of your code? what are you trying to achieve?
Was This Post Helpful? 0
  • +
  • -

#3 ryansandberg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 12

Re: Stacked Chart Issue in VB.NET

Posted 18 July 2012 - 11:59 AM

View Postsela007, on 17 July 2012 - 03:45 PM, said:

Quote

I don't understand why when ListBox4 index (n) becomes 8 (string="joe") that the corresponding string "truck" doesn't stack onto string "bike" and show 2 color coded values of 90 and 20. It just overwrites the 20 and shows 90. All n's less than 8 do this fine for multiple people and/or multiple vehicles
From this description it's hard to understand what is your problem. Cane you tell what is the purpose of your code? what are you trying to achieve?

Trying to create a stacked column graph using people names for the X axis categories, vehicle names for the series name and numbers for the Y axis category. Does that help? Seems to be working for most of the way iterating through the listbox then stops working
Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

Re: Stacked Chart Issue in VB.NET

Posted 18 July 2012 - 07:47 PM

I've figured if you put 90 before 20 for "joe" in ListBox6 everything will be ok, even if you change ChartType to "column" you will se all series(colors). I don't know why is that happening (maybe because your code is nightmare :)). I suggest you, stop use listboxes as your data container, use listboxes just for displaying data, be wise and learn how to use lists and classes.

Try this way:
First create class called "Person"
Public Class Person
    Public Name As String

    Public truck As Integer
    Public bike As Integer
    Public car As Integer
    Public motorcycle As Integer
    Public plane As Integer
    Public boat As Integer
    Public sled As Integer

    Public Sub New(ByVal PersonName As String)
        Name = PersonName
    End Sub
End Class

After that you will create list for persons (in the scope of the form1) so you can easy manipulate with data.
Public Persons As New List(Of Person)


Next: add persons to list of persons and set data for each person:
        Persons.Add(New Person("ryan"))
        Persons.Add(New Person("joe"))
        Persons.Add(New Person("bob"))
        Persons.Add(New Person("fred"))
        Persons.Add(New Person("greg"))

        Persons(0).truck = 90
        Persons(0).bike = 90
        Persons(1).motorcycle = 20
        Persons(1).sled = 30
        Persons(2).boat = 30
        Persons(2).car = 100
        Persons(2).plane = 80
        Persons(3).truck = 30
        Persons(3).motorcycle = 50
        Persons(4).boat = 30
        Persons(4).bike = 70


Next: Create new series and draw your chart.
       Chart1.Series.Clear()
        Chart1.Series.Add("truck")
        Chart1.Series.Add("bike")
        Chart1.Series.Add("car")
        Chart1.Series.Add("motorcycle")
        Chart1.Series.Add("plane")
        Chart1.Series.Add("boat")
        Chart1.Series.Add("sled")

        'setup ChartType for each series in Chart1
        For Each ser As DataVisualization.Charting.Series In Chart1.Series
            ser.ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
        Next
        
        'draw chart
        With Chart1
            For i As Integer = 0 To Persons.Count - 1
                .Series("truck").Points.AddXY(i, Persons(i).truck)
                .Series("truck").Points(.Series("truck").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("bike").Points.AddXY(i, Persons(i).bike)
                .Series("bike").Points(.Series("bike").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("car").Points.AddXY(i, Persons(i).car)
                .Series("car").Points(.Series("car").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("motorcycle").Points.AddXY(i, Persons(i).motorcycle)
                .Series("motorcycle").Points(.Series("motorcycle").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("plane").Points.AddXY(i, Persons(i).plane)
                .Series("plane").Points(.Series("plane").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("boat").Points.AddXY(i, Persons(i).boat)
                .Series("boat").Points(.Series("boat").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("sled").Points.AddXY(i, Persons(i).sled)
                .Series("sled").Points(.Series("sled").Points.Count - 1).AxisLabel = Persons(i).Name

            Next
        End With


Now everything is clear and you don't need a hundred loops and variables with meaningless names (r1,r2,n,g,z,??)
Was This Post Helpful? 0
  • +
  • -

#5 ryansandberg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 12

Re: Stacked Chart Issue in VB.NET

Posted 20 July 2012 - 04:40 AM

View Postsela007, on 18 July 2012 - 07:47 PM, said:

I've figured if you put 90 before 20 for "joe" in ListBox6 everything will be ok, even if you change ChartType to "column" you will se all series(colors). I don't know why is that happening (maybe because your code is nightmare :)). I suggest you, stop use listboxes as your data container, use listboxes just for displaying data, be wise and learn how to use lists and classes.

Try this way:
First create class called "Person"
Public Class Person
    Public Name As String

    Public truck As Integer
    Public bike As Integer
    Public car As Integer
    Public motorcycle As Integer
    Public plane As Integer
    Public boat As Integer
    Public sled As Integer

    Public Sub New(ByVal PersonName As String)
        Name = PersonName
    End Sub
End Class

After that you will create list for persons (in the scope of the form1) so you can easy manipulate with data.
Public Persons As New List(Of Person)


Next: add persons to list of persons and set data for each person:
        Persons.Add(New Person("ryan"))
        Persons.Add(New Person("joe"))
        Persons.Add(New Person("bob"))
        Persons.Add(New Person("fred"))
        Persons.Add(New Person("greg"))

        Persons(0).truck = 90
        Persons(0).bike = 90
        Persons(1).motorcycle = 20
        Persons(1).sled = 30
        Persons(2).boat = 30
        Persons(2).car = 100
        Persons(2).plane = 80
        Persons(3).truck = 30
        Persons(3).motorcycle = 50
        Persons(4).boat = 30
        Persons(4).bike = 70


Next: Create new series and draw your chart.
       Chart1.Series.Clear()
        Chart1.Series.Add("truck")
        Chart1.Series.Add("bike")
        Chart1.Series.Add("car")
        Chart1.Series.Add("motorcycle")
        Chart1.Series.Add("plane")
        Chart1.Series.Add("boat")
        Chart1.Series.Add("sled")

        'setup ChartType for each series in Chart1
        For Each ser As DataVisualization.Charting.Series In Chart1.Series
            ser.ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
        Next
        
        'draw chart
        With Chart1
            For i As Integer = 0 To Persons.Count - 1
                .Series("truck").Points.AddXY(i, Persons(i).truck)
                .Series("truck").Points(.Series("truck").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("bike").Points.AddXY(i, Persons(i).bike)
                .Series("bike").Points(.Series("bike").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("car").Points.AddXY(i, Persons(i).car)
                .Series("car").Points(.Series("car").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("motorcycle").Points.AddXY(i, Persons(i).motorcycle)
                .Series("motorcycle").Points(.Series("motorcycle").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("plane").Points.AddXY(i, Persons(i).plane)
                .Series("plane").Points(.Series("plane").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("boat").Points.AddXY(i, Persons(i).boat)
                .Series("boat").Points(.Series("boat").Points.Count - 1).AxisLabel = Persons(i).Name

                .Series("sled").Points.AddXY(i, Persons(i).sled)
                .Series("sled").Points(.Series("sled").Points.Count - 1).AxisLabel = Persons(i).Name

            Next
        End With


Now everything is clear and you don't need a hundred loops and variables with meaningless names (r1,r2,n,g,z,??)

Thanks for the help. One more question though. I think I still have a syntax error as the plotting won't advance past the first person in the person list
Here is the code:

Public Class form1
Public Persons As New List(Of Person)
Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Persons.Add(New Person("ryan"))
Persons.Add(New Person("joe"))
Persons.Add(New Person("bob"))
Persons.Add(New Person("fred"))
Persons.Add(New Person("greg"))

Persons(0).truck = 90
Persons(0).bike = 90
Persons(1).motorcycle = 20
Persons(1).sled = 30
Persons(2).boat = 30
Persons(2).car = 100
Persons(2).plane = 80
Persons(3).truck = 30
Persons(3).motorcycle = 50
Persons(4).boat = 30
Persons(4).bike = 70

Chart1.Series.Clear()
Chart1.Series.Add("truck")
Chart1.Series.Add("bike")
Chart1.Series.Add("car")
Chart1.Series.Add("motorcycle")
Chart1.Series.Add("plane")
Chart1.Series.Add("boat")

'setup ChartType for each series in Chart1
For Each ser As DataVisualization.Charting.Series In Chart1.Series
ser.ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
Next

'draw chart
With Chart1
For i As Integer = 0 To Persons.Count - 1
.Series("truck").Points.AddXY(i, Persons(i).truck)
.Series("truck").Points(.Series("truck").Points.Count - 1).AxisLabel = Persons(i).Name
.Series("bike").Points.AddXY(i, Persons(i).bike)
.Series("bike").Points(.Series("bike").Points.Count - 1).AxisLabel = Persons(i).Name
.Series("car").Points.AddXY(i, Persons(i).car)
.Series("car").Points(.Series("car").Points.Count - 1).AxisLabel = Persons(i).Name
.Series("motorcycle").Points.AddXY(i, Persons(i).motorcycle)
.Series("motorcycle").Points(.Series("motorcycle").Points.Count - 1).AxisLabel = Persons(i).Name
.Series("plane").Points.AddXY(i, Persons(i).plane)
.Series("plane").Points(.Series("plane").Points.Count - 1).AxisLabel = Persons(i).Name
.Series("boat").Points.AddXY(i, Persons(i).boat)
.Series("boat").Points(.Series("boat").Points.Count - 1).AxisLabel = Persons(i).Name
.Series("sled").Points.AddXY(i, Persons(i).sled)
.Series("sled").Points(.Series("sled").Points.Count - 1).AxisLabel = Persons(i).Name
Next
End With
End Sub
End Class

Public Class Person
Public Name As String
Public truck As Integer
Public bike As Integer
Public car As Integer
Public motorcycle As Integer
Public plane As Integer
Public boat As Integer
Public sled As Integer

Public Sub New(ByVal PersonName As String)
Name = PersonName
End Sub
End Class
Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

Re: Stacked Chart Issue in VB.NET

Posted 20 July 2012 - 09:50 AM

Quote

I think I still have a syntax error as the plotting won't advance past the first person in the person list
Sorry I don't understand very well, can you explain better? do you get any error?

Just for the future, when you post the code put your code between tags (select your code and press CODE button in the text editor).
Was This Post Helpful? 0
  • +
  • -

#7 ryansandberg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 12

Re: Stacked Chart Issue in VB.NET

Posted 24 July 2012 - 07:26 PM

View Postsela007, on 20 July 2012 - 09:50 AM, said:

Quote

I think I still have a syntax error as the plotting won't advance past the first person in the person list
Sorry I don't understand very well, can you explain better? do you get any error?

Just for the future, when you post the code put your code between tags (select your code and press CODE button in the text editor).


I was able to get the graphing to work correctly by changing Persons as New List(Of Person) to Dim from Public. I wasn't entirely clear before but what I would really like to be able to do is to create the Person item/property section by collecting all the items that are in a Listbox 1 on the form (instead of hard coding them. Currently truck, bike, car, etc.,etc....The Person Class section). Then I would also like to create the Person list by collecting all the items of another Listbox 2 (again instead of hard coding them. Currently ryan, joe, bob etc., etc....The first few lines in Form Load) All of the indexes in the 2 Listboxes are designed to map to the same index (First index of Listbox 1 would map to first index of Listbox 2)
I hope I'm explaining that properly, probably not though :)
By the way, thanks very much for your help so far
Here is the code posted again below
Ryan
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Persons As New List(Of Person)
        Persons.Add(New Person("ryan"))
        Persons.Add(New Person("joe"))
        Persons.Add(New Person("bob"))
        Persons.Add(New Person("fred"))
        Persons.Add(New Person("greg"))
        Persons(0).truck = 90
        Persons(0).bike = 90
        Persons(1).motorcycle = 20
        Persons(1).sled = 30
        Persons(2).boat = 30
        Persons(2).car = 100
        Persons(2).plane = 80
        Persons(3).truck = 30
        Persons(3).motorcycle = 50
        Persons(4).boat = 30
        Persons(4).bike = 70

        Chart1.Series.Clear()
        Chart1.Series.Add("truck")
        Chart1.Series.Add("bike")
        Chart1.Series.Add("car")
        Chart1.Series.Add("motorcycle")
        Chart1.Series.Add("plane")
        Chart1.Series.Add("boat")
        Chart1.Series.Add("sled")

        'setup ChartType for each series in Chart1
        For Each ser As DataVisualization.Charting.Series In Chart1.Series
            ser.ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
        Next

        'draw chart
        With Chart1
            For i As Integer = 0 To Persons.Count - 1
                .Series("truck").Points.AddXY(i, Persons(i).truck)
                .Series("truck").Points(.Series("truck").Points.Count - 1).AxisLabel = Persons(i).Name
                .Series("bike").Points.AddXY(i, Persons(i).bike)
                .Series("bike").Points(.Series("bike").Points.Count - 1).AxisLabel = Persons(i).Name
                .Series("car").Points.AddXY(i, Persons(i).car)
                .Series("car").Points(.Series("car").Points.Count - 1).AxisLabel = Persons(i).Name
                .Series("motorcycle").Points.AddXY(i, Persons(i).motorcycle)
                .Series("motorcycle").Points(.Series("motorcycle").Points.Count - 1).AxisLabel = Persons(i).Name
                .Series("plane").Points.AddXY(i, Persons(i).plane)
                .Series("plane").Points(.Series("plane").Points.Count - 1).AxisLabel = Persons(i).Name
                .Series("boat").Points.AddXY(i, Persons(i).boat)
                .Series("boat").Points(.Series("boat").Points.Count - 1).AxisLabel = Persons(i).Name
                .Series("sled").Points.AddXY(i, Persons(i).sled)
                .Series("sled").Points(.Series("sled").Points.Count - 1).AxisLabel = Persons(i).Name:
            Next
        End With

    End Sub
End Class
Public Class Person
    Public Name As String
    Public truck As Integer
    Public bike As Integer
    Public car As Integer
    Public motorcycle As Integer
    Public plane As Integer
    Public boat As Integer
    Public sled As Integer
    Public Sub New(ByVal PersonName As String)
        Name = PersonName
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1