需要从这些结果VB.net直方图

时间:2017-01-17 18:51:02

标签: vb.net

我需要直方图的一些帮助。我的程序用三个,四个,五个和六个字母计算单词。现在我需要从收到的答案中制作简单的直方图。这是我的代码:

Public Class Form1
    Dim tekst As String
    Dim rijec() As String
    Dim trazena As String
    Dim brojac1 As Integer = 1
    Dim brojac2 As Integer = 0
    Dim brojac3 As Integer = 0
    Private Sub btnUcitaj_Click(sender As Object, e As EventArgs) Handles btnUcitaj.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
        End If
    End Sub
    Private Sub btnPrebroj1_Click(sender As Object, e As EventArgs) Handles btnPrebroj1.Click

        tekst = RichTextBox1.Text
        rijec = tekst.Split(CChar(" "))
        Dim count = From x In rijec Where x.Length = 3
        txtTri.Text = ("Number of words: " & count.Count.ToString())
    End Sub

    Private Sub btnPrebroj2_Click(sender As Object, e As EventArgs) Handles btnPrebroj2.Click
        tekst = RichTextBox1.Text
        rijec = tekst.Split(CChar(" "))
        Dim count = From x In rijec Where x.Length = 4
        txtCetiri.Text = ("Number of words: " & count.Count.ToString())
    End Sub

    Private Sub btnPrebroj3_Click(sender As Object, e As EventArgs) Handles btnPrebroj3.Click
        tekst = RichTextBox1.Text
        rijec = tekst.Split(CChar(" "))
        Dim count = From x In rijec Where x.Length = 5
        txtPet.Text = ("Number of words: " & count.Count.ToString())
    End Sub

    Private Sub btnPrebroj4_Click(sender As Object, e As EventArgs) Handles btnPrebroj4.Click
        tekst = RichTextBox1.Text
        rijec = tekst.Split(CChar(" "))
        Dim count = From x In rijec Where x.Length = 6
        txtSest.Text = ("Number of words: " & count.Count.ToString())
    End Sub

我需要从图片中得到这些结果的直方图。

enter image description here

1 个答案:

答案 0 :(得分:0)

Visual Basic实际上有一个名为图表的控件,对于这种情况很有用。如果您想查看,我找到了一个教程here 我编写了这个应该与您的代码一起使用的函数,您需要做的就是添加一个数据图表并将其命名为 Chart1 并调用此函数。

Private Sub ShowData()
    Chart1.Series.Clear() ' Delete the default data series.

    Dim tekst = RichTextBox1.Text
    Dim rijec = tekst.Split(CChar(" "))

    With Chart1.Series.Add("Word Lengths") ' Add a new series called Word Lengths

        For wordLength As Integer = 3 To 6 ' Check every word length from 3 to 6
            Dim count = From x In rijec Where x.Length = wordLength ' Count how many words there are
            .Points.AddXY(wordLength, count.Count) ' Add a new data point
        Next

    End With
End Sub
祝你好运,我希望这有所帮助!


〜尼克