无法获得正常运行的条形图

时间:2015-07-16 17:13:36

标签: vb.net

我需要一些帮助来完成我的任务。我已经在这个工作了大约一个星期,试图让酒吧正确建立而没有运气。 根据任务,我必须通过.CreateGraphics创建条形来跟踪流感的传播。流感的进展如下: 一个由10,000人组成的社区暴露于流感,其中生病了两天的人对第三个人免疫。在第0天,200人生病了1天,100人生病了两天。每次单击“提前一天”按钮,数据就会移动,这样就可以让100个人免疫,200个人生病了两天,新生病的一天生病的人数由.0001735 *生病的人产生。前一天*前一天易受影响的人数。

然而,当我点击按钮时,它似乎只是将现有数据加倍,图像永远不会重绘...

我编辑了这篇文章,以反映我在Tony Hinkle的指导下取得的进展。谢谢托尼,还有一些皱纹可以解决!

我现在可以提高我的易感人数,但是我的其他数据都没有超出第一组人的免疫力。

编辑#2 - 我已将问题缩小到我的tempArray元素值。当我在advanceFirstDay子中分配值时,它们似乎不会应用于全局数组,使newfirstDay和newtwoDays的值为0.我通过添加临时列表框和for循环来验证这一点,以显示值tempArray。结果是为tempList.Items.Add(i)

抛出NullException

我的代码在下面,您可以给我任何帮助/指出我的错误将非常感激,因为这变得越来越令人沮丧......

谢谢!

Public Class Form1

Dim population() As Single = {97, 2, 1, 0}
Dim susceptible As Integer = population(0)
Dim firstDay As Integer = population(1)
Dim twoDays As Integer = population(2)
Dim immune As Integer = population(3)
Dim currentDay, newfirstDay, newtwoDays, newImmune, newSusceptible
Dim tempArray() = {newSusceptible, newfirstDay, newtwoDays, newImmune}
Dim advanced1Day As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub day0_Click(sender As Object, e As EventArgs) Handles day0.Click

    'Create the graph
    Dim gr As Graphics = chart.CreateGraphics
    gr.DrawLine(Pens.Black, 60, 115, 475, 115) 'x-axis
    gr.DrawLine(Pens.Black, 60, 115, 60, 0)  'y-axis
    gr.DrawLine(Pens.Black, 55, 0, 65, 0) 'Tickmark
    gr.DrawString("10,000", Me.Font, Brushes.Black, 5, 0)
    gr.DrawString("Susceptible", Me.Font, Brushes.Black, 90, 125)
    gr.DrawString("Sick 1 Day", Me.Font, Brushes.Black, 177, 125)
    gr.DrawString("Sick 2 Days", Me.Font, Brushes.Black, 264, 125)
    gr.DrawString("Immune", Me.Font, Brushes.Black, 351, 125)
    gr.DrawString("Population * 100", Me.Font, Brushes.Black, 200, 140)

    'Display the values for Day 0 of the epidemic
    gr.FillRectangle(Brushes.Blue, 100, 18, 30, susceptible) 'suscpetible
    gr.FillRectangle(Brushes.Blue, 187, 113, 30, firstDay) 'sick one day
    gr.FillRectangle(Brushes.Blue, 274, 114, 30, twoDays) 'sick two days

End Sub

Private Sub advanceDay_Click(sender As Object, e As EventArgs) Handles advanceDay.Click

    advanceFirstDay()
    spreadIllness()

End Sub

Private Sub chart_Click(sender As Object, e As EventArgs) Handles chart.Click

End Sub

Sub spreadIllness()

    chart.Refresh()

    Dim gr As Graphics = chart.CreateGraphics
    gr.DrawLine(Pens.Black, 60, 115, 475, 115) 'x-axis
    gr.DrawLine(Pens.Black, 60, 115, 60, 0)  'y-axis
    gr.DrawLine(Pens.Black, 55, 0, 65, 0) 'Tickmark
    gr.DrawString("10,000", Me.Font, Brushes.Black, 5, 0)
    gr.DrawString("Susceptible", Me.Font, Brushes.Black, 90, 125)
    gr.DrawString("Sick 1 Day", Me.Font, Brushes.Black, 177, 125)
    gr.DrawString("Sick 2 Days", Me.Font, Brushes.Black, 264, 125)
    gr.DrawString("Immune", Me.Font, Brushes.Black, 351, 125)
    gr.DrawString("Population * 100", Me.Font, Brushes.Black, 200, 140)

    'Dim newSusceptible As Integer = susceptible - firstDay - twoDays

    newImmune += newtwoDays
    newtwoDays = 0 + newfirstDay
    newSusceptible = newSusceptible - newfirstDay - newtwoDays - newImmune
    currentDay = CInt(0.0001735 * newtwoDays * newSusceptible)
    newfirstDay = currentDay

    gr.FillRectangle(Brushes.Blue, 100, 114 - newSusceptible, 30, newSusceptible) 'suscpetible
    gr.FillRectangle(Brushes.Blue, 187, 114 - newfirstDay, 30, newfirstDay) 'sick one day
    gr.FillRectangle(Brushes.Blue, 274, 114 - newtwoDays, 30, newtwoDays) 'sick two days
    gr.FillRectangle(Brushes.Blue, 361, 114 - newImmune, 30, newImmune) 'sick two days

End Sub

Sub advanceFirstDay()

    If advanced1Day = True Then
        Exit Sub
    End If

    chart.Refresh()

    Dim gr As Graphics = chart.CreateGraphics
    gr.DrawLine(Pens.Black, 60, 115, 475, 115) 'x-axis
    gr.DrawLine(Pens.Black, 60, 115, 60, 0)  'y-axis
    gr.DrawLine(Pens.Black, 55, 0, 65, 0) 'Tickmark
    gr.DrawString("10,000", Me.Font, Brushes.Black, 5, 0)
    gr.DrawString("Susceptible", Me.Font, Brushes.Black, 90, 125)
    gr.DrawString("Sick 1 Day", Me.Font, Brushes.Black, 177, 125)
    gr.DrawString("Sick 2 Days", Me.Font, Brushes.Black, 264, 125)
    gr.DrawString("Immune", Me.Font, Brushes.Black, 351, 125)
    gr.DrawString("Population * 100", Me.Font, Brushes.Black, 200, 140)

    'Dim newSusceptible As Integer = susceptible - firstDay - twoDays

    newImmune += twoDays
    newtwoDays = 0 + firstDay
    newSusceptible = susceptible - firstDay - newtwoDays - newImmune
    currentDay = CInt(0.0001735 * newtwoDays * newSusceptible)
    newfirstDay = currentDay


    gr.FillRectangle(Brushes.Blue, 100, 114 - newSusceptible, 30, newSusceptible) 'suscpetible
    gr.FillRectangle(Brushes.Blue, 187, 114 - newfirstDay, 30, newfirstDay) 'sick one day
    gr.FillRectangle(Brushes.Blue, 274, 114 - newtwoDays, 30, newtwoDays) 'sick two days
    gr.FillRectangle(Brushes.Blue, 361, 114 - newImmune, 30, newImmune) 'sick two days

    advanced1Day = True

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

我相信我已经按照预期修复了图形,所有数据功能(或看起来都是)。谢谢Tony Hinkle的帮助!

Public Class Form1

Dim population() As Single = {97, 2, 1, 0}
Dim susceptible As Integer = population(0)
Dim firstDay As Integer = population(1)
Dim twoDays As Integer = population(2)
Dim immune As Integer = population(3)
Dim currentDay, newfirstDay, newtwoDays, newImmune, newSusceptible
Dim advanced1Day As Boolean = False
Dim tempArray()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub day0_Click(sender As Object, e As EventArgs) Handles day0.Click

    'Create the graph
    Dim gr As Graphics = chart.CreateGraphics
    gr.DrawLine(Pens.Black, 60, 115, 475, 115) 'x-axis
    gr.DrawLine(Pens.Black, 60, 115, 60, 0)  'y-axis
    gr.DrawLine(Pens.Black, 55, 0, 65, 0) 'Tickmark
    gr.DrawString("10,000", Me.Font, Brushes.Black, 5, 0)
    gr.DrawString("Susceptible", Me.Font, Brushes.Black, 90, 125)
    gr.DrawString("Sick 1 Day", Me.Font, Brushes.Black, 177, 125)
    gr.DrawString("Sick 2 Days", Me.Font, Brushes.Black, 264, 125)
    gr.DrawString("Immune", Me.Font, Brushes.Black, 351, 125)
    gr.DrawString("Population * 100", Me.Font, Brushes.Black, 200, 140)

    'Display the values for Day 0 of the epidemic
    gr.FillRectangle(Brushes.Blue, 100, 18, 30, susceptible) 'suscpetible
    gr.FillRectangle(Brushes.Blue, 187, 113, 30, firstDay) 'sick one day
    gr.FillRectangle(Brushes.Blue, 274, 114, 30, twoDays) 'sick two days

End Sub

Private Sub advanceDay_Click(sender As Object, e As EventArgs) Handles advanceDay.Click

    advanceFirstDay()
    spreadIllness()

End Sub

Private Sub chart_Click(sender As Object, e As EventArgs) Handles chart.Click

End Sub

Sub spreadIllness()

    chart.Refresh()

    Dim gr As Graphics = chart.CreateGraphics
    gr.DrawLine(Pens.Black, 60, 115, 475, 115) 'x-axis
    gr.DrawLine(Pens.Black, 60, 115, 60, 0)  'y-axis
    gr.DrawLine(Pens.Black, 55, 0, 65, 0) 'Tickmark
    gr.DrawString("10,000", Me.Font, Brushes.Black, 5, 0)
    gr.DrawString("Susceptible", Me.Font, Brushes.Black, 90, 125)
    gr.DrawString("Sick 1 Day", Me.Font, Brushes.Black, 177, 125)
    gr.DrawString("Sick 2 Days", Me.Font, Brushes.Black, 264, 125)
    gr.DrawString("Immune", Me.Font, Brushes.Black, 351, 125)
    gr.DrawString("Population * 100", Me.Font, Brushes.Black, 200, 140)

    newImmune += twoDays
    newtwoDays = firstDay
    newSusceptible = susceptible - firstDay - newtwoDays - newImmune
    currentDay = CInt(0.0001735 * (newtwoDays * 100) * (newSusceptible * 100))
    newfirstDay = CInt(currentDay / 100)

    gr.FillRectangle(Brushes.Blue, 100, 114 - newSusceptible, 30, newSusceptible) 'suscpetible
    gr.FillRectangle(Brushes.Blue, 187, 114 - newfirstDay, 30, newfirstDay) 'sick one day
    gr.FillRectangle(Brushes.Blue, 274, 114 - newtwoDays, 30, newtwoDays) 'sick two days
    gr.FillRectangle(Brushes.Blue, 361, 114 - newImmune, 30, newImmune) 'sick two days

End Sub

Sub advanceFirstDay()

    If advanced1Day = True Then
        Exit Sub
    End If

    chart.Refresh()

    Dim gr As Graphics = chart.CreateGraphics
    gr.DrawLine(Pens.Black, 60, 115, 475, 115) 'x-axis
    gr.DrawLine(Pens.Black, 60, 115, 60, 0)  'y-axis
    gr.DrawLine(Pens.Black, 55, 0, 65, 0) 'Tickmark
    gr.DrawString("10,000", Me.Font, Brushes.Black, 5, 0)
    gr.DrawString("Susceptible", Me.Font, Brushes.Black, 90, 125)
    gr.DrawString("Sick 1 Day", Me.Font, Brushes.Black, 177, 125)
    gr.DrawString("Sick 2 Days", Me.Font, Brushes.Black, 264, 125)
    gr.DrawString("Immune", Me.Font, Brushes.Black, 351, 125)
    gr.DrawString("Population * 100", Me.Font, Brushes.Black, 200, 140)

    newImmune += twoDays
    newtwoDays = firstDay
    newSusceptible = susceptible - firstDay - newtwoDays - newImmune
    currentDay = CInt(0.0001735 * (newtwoDays * 100) * (newSusceptible * 100))
    newfirstDay = CInt(currentDay / 100)

    gr.FillRectangle(Brushes.Blue, 100, 114 - newSusceptible, 30, newSusceptible) 'suscpetible
    gr.FillRectangle(Brushes.Blue, 187, 114 - newfirstDay, 30, newfirstDay) 'sick one day
    gr.FillRectangle(Brushes.Blue, 274, 114 - newtwoDays, 30, newtwoDays) 'sick two days
    gr.FillRectangle(Brushes.Blue, 361, 114 - newImmune, 30, newImmune) 'sick two days

    advanced1Day = True

End Sub

结束班