2016-04-01 8 views
0

Ich möchte ein Diagramm in VB.net erstellen, die X-und Y-Werte in Form einer Zeichenfolge hat. Beispiel unten: Sorry for the bad drawing, but this is what I'd like it to doErstellen eines Diagramms in VisualBasic.net mit einer Y-Achse bestehend aus String-Werten

Kann jemand mir helfen, die Einstellungen zu finden, um dies zu tun? Ich kann Daten in eine Grafik übertragen, aber es endet wie diese aufzublicken. This is what my current graph looks like. As you can see, I can't get the Y axis working.

(nicht auch um die X-Achse die Noten enthalten Sorge, das ist nur etwas, das ich reparieren müssen die Typen string() noch die enthält rechte Daten)

Dies ist ein Beispiel meines Codes: (mach dir keine Sorgen über delimitegrades(), die nur Daten für die Noten in 'A', 'B' usw. formatiert) Subjects is a string() Liste. Grades ist ein Array, das die Daten enthält, die ich einfügen muss. ChrtSubgrade ist das Diagramm selbst.

Public Sub CreateGraph(ByVal name As String, subjects() As String) 
    MsgBox("Generating graph for " & name) 
    chrtSubgrade.Series.Clear() 
    chrtSubgrade.Series.Add("Data") 
    chrtSubgrade.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Column 
    chrtSubgrade.Series(0).YValueType = DataVisualization.Charting.ChartValueType.String 
    chrtSubgrade.Series(0).IsValueShownAsLabel = True 


    delimitgrades(subjects) 

    For i = 0 To subjects.Count - 2 
     chrtSubgrade.Series(0).Points.AddXY(subjects(i), grades(i)) 
    Next 

Ich habe breakpointed den gesamten Code und die Arrays und Datenübertragung ist in Ordnung, so glaube ich, dass es nur darauf an, wie ich die Grafik bin zu schaffen.

Ich kann das Diagramm nicht mit einer Datenbank verknüpfen, da ich die Quelldaten aus einer XML-Datei zeichne.

Vielen Dank für jede Hilfe, die Sie möglicherweise geben können.

Antwort

0

Ich habe eine neue Methode gefunden. Anstatt die VB-Charting-Tools zu verwenden, habe ich meine eigenen erstellt. Hier ist der Code:

Public Sub CreateGraph(ByVal name As String, subjects() As String) 
    delimitgrades(subjects) 'collects the info from subjects which contains the grade data and make a new array from it containing only grades 
    MsgBox("Generating graph for " & name) 
    Dim mygraphics As Graphics = Graphics.FromHwnd(hwnd:=ActiveForm.Handle) 'defines a new graphics set on the Grades window 
    Dim axespen As New Pen(Color.Black, 5) 'makes a pen so I can draw the axes 
    Dim Xaxisleft As New Point(30, 350) 'defines the left point of the X axis 
    Dim Xaxisright As New Point(400, 350) 'defines the right point of the X axis 
    Dim Yaxisbottom As New Point(30, 350) 'defines the bottom point of the Y axis 
    Dim yaxistop As New Point(30, 80) 'defines the top point of the Y axis 

    For i = 0 To 4 'for each possible grade - A* through D 
     Dim labelgrade As New Label 'makes a label 
     With labelgrade 'with said label 
      .BackColor = Color.Transparent 'makes its background colourless 
      .AutoSize = True 'resizes the bounding box 
      Select Case i 'examines I - the counting variable 
       Case Is = 0 ' if its 0 
        .Text = "A*" ' sets the labels text to A* 
        .Location = New Point(2, 100) 'moves it to the right place 
       Case Is = 1 'etc 
        .Text = "A" 
        .Location = New Point(2, 140) 
       Case Is = 2 
        .Text = "B" 
        .Location = New Point(2, 180) 
       Case Is = 3 
        .Text = "C" 
        .Location = New Point(2, 220) 
       Case Is = 4 
        .Text = "D" 
        .Location = New Point(2, 260) 
      End Select '/etc 

     End With 
     Controls.Add(labelgrade) 'inserts the label into the form 

    Next 

    For i = 0 To subjects.Count - 2 'the last part of the subjects array is empty so it counts to the last position containing data 
     Dim labelsubject As New Label 'makes a new label 
     Dim labelxoffset As Integer = 30 'defines the variable xoffset which is used to determine where all labels should be placed 
     With labelsubject 'with this label 
      .BackColor = Color.Transparent 'make the background colourless 
      .AutoSize = True 'resize the bounding box 
      Select Case i 'examine i 
       Case Is = 0 'if this is the first label placed onto the form 
        .Text = subjects(i) 'take the first entry in the subjects array 
        .Location = New Point(30, 355) 'place the label on the X axis in the first position 
       Case Else 'otherwise 
        .Text = subjects(i) 'take the right name from the array and make the label reflect this name 
        .Location = New Point(labelxoffset + (i * 70), 365) 'set its location depending on which place it is in the array using xoffset 
      End Select 
     End With 
     Controls.Add(labelsubject) 'add the label to the form 
    Next 

    'Axes 
    mygraphics.DrawLine(axespen, Xaxisleft, Xaxisright) 'create 
    mygraphics.DrawLine(axespen, Yaxisbottom, yaxistop) 'the axes 

    'bars 
    For i = 0 To grades.Count - 1 'from 0 to the second to last entry in the grades array (this is because of how I formed the grades array. It still works. 
     Dim grade As String = grades(i) 'create a temp variable to store the correct entry from the grades array 
     Dim gradeindex As Integer = Nothing ' create the index integer, this is used to determine how high the bar should go 
     Dim gradepen As New Pen(Color.Green, 50) 'make the pen for the bars 
     Dim p1 As New Point(30, 350) 'define point 1 as above the first label 
     Dim p2 As New Point 'create point 2 
     Dim x As Integer = (58 + (70 * i)) 'create a new xoffset integer 
     Dim yoffset As Integer = 348 ' create the yoffset integer. This is to place the bottom point of the bar correctly 
     Select Case grade 'examine grade 
      Case Is = "A*" 'if its A* 
       gradeindex = 100 'the top y coord is 100 
       p1 = New Point(x, yoffset) 'p1 is now x, yoffset 
       p2 = New Point(x, gradeindex) 'p2 is now x, gradeindex 
      Case Is = "A" 'etc 
       gradeindex = 140 
       p1 = New Point(x, yoffset) 
       p2 = New Point(x, gradeindex) 
      Case Is = "B" 
       gradeindex = 180 
       p1 = New Point(x, yoffset) 
       p2 = New Point(x, gradeindex) 
      Case Is = "C" 
       gradeindex = 220 
       p1 = New Point(x, yoffset) 
       p2 = New Point(x, gradeindex) 
       gradepen = New Pen(Color.Orange, 50) 'make the grade pen orange 
      Case Is = "D" 
       gradeindex = 260 
       p1 = New Point(x, yoffset) 
       p2 = New Point(x, gradeindex) 
       gradepen = New Pen(Color.Red, 50) 'make the grade pen red 
     End Select '/etc 
     mygraphics.DrawLine(gradepen, p1, p2) 'draw the line from p1 to p2 
    Next 
End Sub 

Hier ist die Ausgabe für jemanden, der die folgenden Daten hat: Deutsch; A *, Mathematik; A, Französisch; A *, Bio, C, Bus (Business zum Beispiel), B

Output

Ich hoffe, dies hilft anderen, dieses Problem zu haben. Vielen Dank!

Verwandte Themen