Creating a chart in VisualBasic.net which has a Y axis comprising of string values - vb.net

I'd like to create a chart in VB.net which has X and Y values in the form of a string.
Example below:
Sorry for the bad drawing, but this is what I'd like it to do
Can anybody help me find the settings to do this?
I can transfer data to a graph but it ends up looking like this:
This is what my current graph looks like. As you can see, I can't get the Y axis working.
(also dont worry about the X axis containing the grades, that's just something I need to fix. The grades string() still contains the right data)
This is a sample of my code: (don't worry about delimitegrades(), that just formats data for the grades into 'A', 'B', etc)
Subjects is a string() list.
Grades is an array which contains the data I need to insert. ChrtSubgrade is the chart itself.
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
I've breakpointed all the code and the arrays and data transfer is fine so I believe it's just down to how I'm creating the graph.
I can't link the chart to a database since I'm drawing the source data from an XML file.
Thanks very much for any help you might be able to give.

I figured out a new method. Instead of using the VB charting tools I made my own. Here's the 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
Here's the output for someone who has the following data: German;A* , Maths;A , French;A* , Bio;C , Bus(business for example);B
Output
Hope this helps anyone else having this issue. Thanks!

Related

How to dynamicallty create multiple controls at runtime

I am trying to add multiple labels to a userform at runtime
It's for the player names of a board game; and until the game starts the number of players are not known. I have managed to figure out for myself how to use the dynamic array function to create the list of players. I used a For.....Next loop to add the player names. I thought I could do that to add the labels to the form, but it only adds one. Depending on where the new control type is declared, it either adds the first player only, or the last player
This code produces one label only within the groupbox, the last player
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Players_Num As Integer = InputBox("Enter the number of players")
Dim Players(Players_Num) As String
Dim newText As New Label
For i = 0 To Players_Num - 1
Players(i) = InputBox("Enter player name")
Next
'This piece of code was jsut for me to test that I was successfully using a For...Loop
'to add the players names, and will be deleted later on
For x = 0 To Players_Num - 1
MessageBox.Show(Players(x))
Next
For z = 0 To Players_Num - 1
newText.Name = "txt" & Players(z)
newText.Text = Players(z)
newText.Size = New Size(170, 20)
newText.Location = New Point(12 + 5, 12 + 5)
GroupBox1.Controls.Add(newText)
Next
End Sub
End Class
This one places only the first player
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Players_Num As Integer = InputBox("Enter the number of players")
Dim Players(Players_Num) As String
For i = 0 To Players_Num - 1
Players(i) = InputBox("Enter player name")
Next
'This piece of code was jsut for me to test that I was successfully using a For...Loop
'to add the players names, and will be deleted later on
For x = 0 To Players_Num - 1
MessageBox.Show(Players(x))
Next
For z = 0 To Players_Num - 1
Dim newText As New Label
newText.Name = "txt" & Players(z)
newText.Text = Players(z)
newText.Size = New Size(170, 20)
newText.Location = New Point(12 + 5, 12 + 5)
GroupBox1.Controls.Add(newText)
Next
End Sub
End Class
I've tried this in vs 2015 and 2019 Community
Where is it going wrong?
From the looks of the code, you are correctly creating the controls but their location is the same for all of them, essentially, they are being place one of top of the other, the first is hidden with the second, which is hidden with the third.
The line
newText.Location = New Point(12 + 5, 12 + 5)
needs to be modified to place the labels at different locations.
Perhaps, something like:
newText.Location = New Point(12 + 5, 12 + (z * 25))
This will vertically align the labels with a gap of 25 between them
You are placing them all in the same location
newText.Location = New Point(12 + 5, 12 + 5)
Use your 'z' index to place them at different locations in order to be able to see them
For me it is easier to contain controls in a TableLayoutPanel then add the TLP to what ever control collection, such as a GroupBox This way you can couple a Label with TextBox, for example. Here's an example how you can create controls from a DataTable. In your case you would only need 1 ColumnStyle for labels, I just thought I would show you a good practice for future shortcuts. (I rarely use the designer to place controls)
'Start test data
Dim DtTable As New DataTable
With DtTable
Dim NewDtRow As DataRow = .NewRow
For i As Integer = 0 To 25
Dim DtCol As New DataColumn With {.ColumnName = "Col" & i, .DataType = GetType(String)}
.Columns.Add(DtCol)
NewDtRow(DtCol.ColumnName) = "Test" & i
Next
.Rows.Add(NewDtRow)
End With
'End test data
Dim TLP1 As New TableLayoutPanel With {.Name = "TlpFields"}
With TLP1
.BorderStyle = BorderStyle.Fixed3D
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
.AutoScroll = True
.AutoSize = True
.RowStyles.Clear()
.ColumnStyles.Clear()
.Dock = DockStyle.Fill
.ColumnCount = 2
.ColumnStyles.Add(New ColumnStyle With {.SizeType = SizeType.AutoSize})
End With
For Each DtCol As DataColumn In DtTable.Columns
With TLP1
.RowCount += 1
.RowStyles.Add(New RowStyle With {.SizeType = SizeType.AutoSize})
'create labels
.Controls.Add(New Label With {
.Text = DtCol.ColumnName,
.Anchor = AnchorStyles.Right}, 0, .RowCount)
'create textboxs
Dim TxtBox As New TextBox With {
.Name = "TextBox" & DtCol.ColumnName,
.Size = New Size(170, 20),
.Anchor = AnchorStyles.Left}
'add binding
TxtBox.DataBindings.Add("Text", DtTable, DtCol.ColumnName)
.Controls.Add(TxtBox, 1, .RowCount)
End With
Next
Controls.Add(TLP1)

Size of labels in a TableLayoutPanel

I've been stuck in this for two hours now. I googled for the answer a lot, and still can't figure out what's wrong with my program.
I have a TableLayoutPanel1 in a form2. This is the code in which I create labels, a list and then add all of those labels to the TableLayOutPanel1:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lbl0, lbl1, lbl11, lbl2, lbl22, lbl3, lbl33, lbl4, lbl44, lbl5, lbl55, lbl6, lbl66, lbl7, lbl77 As New Label
lbl0.Text = "ACCESORIOS"
lbl0.Font = New System.Drawing.Font("MS Reference Sans Serif", 15.75, FontStyle.Bold)
lbl0.Location = New Point(110, 12)
lbl0.AutoSize = True
Me.Controls.Add(lbl0)
lbl1.Text = "Té 180°"
lbl11.Text = Te180
lbl2.Text = "Té 90° Empalme - Codo Triple"
If Global1 <> 0 Then
lbl22.Text = 0
Else
lbl22.Text = Global2 - 1
End If
lbl3.Text = "Soporte 90° T/T"
lbl33.Text = SoporteTT90
lbl4.Text = "Base para tubo de 1 1/2 - 3 agujeros"
lbl44.Text = Int(lbl11.Text) + Int(lbl22.Text) + (lbl33.Text)
lbl5.Text = "Base para tubo de 1 1/2 - 1 agujero"
lbl55.Text = 2
lbl6.Text = "Tapas para base de tubo de 1 1/2"
lbl66.Text = lbl44.Text
lbl7.Text = "Tornillos y Tarugos 5 x 50 y N°8 Ladrillo Hueco"
lbl7.Font = New System.Drawing.Font("MS Reference Sans Serif", 15.75, FontStyle.Bold)
lbl77.Text = (Int(lbl44.Text) + Int(lbl55.Text)) * 3
Dim labellist As New List(Of Label)()
labellist.Add(lbl1)
labellist.Add(lbl2)
labellist.Add(lbl3)
labellist.Add(lbl4)
labellist.Add(lbl5)
labellist.Add(lbl6)
labellist.Add(lbl7)
labellist.Add(lbl11)
labellist.Add(lbl22)
labellist.Add(lbl33)
labellist.Add(lbl44)
labellist.Add(lbl55)
labellist.Add(lbl66)
labellist.Add(lbl77)
Dim h = 0
For i = 0 To 1
For j = 0 To 6
Dim etiqueta As New Label
etiqueta.Text = labellist(h).Text
TableLayoutPanel1.Controls.Add(etiqueta, i, j)
h = h + 1
Next
Next
End Sub
End Class
The problem is that the the full label doesn't appear in the table.[enter image description here][1]
I have tried Autosizing the labels, the tablelayout. Nothing works. It's like there's an invisible line that doesn't let the text of the label get past a line in the table. Any help appreciated. Thanks.
The layout in form2. enter image description here
What happens when I make the font in the TLP 5pts. The whole label appears in two lines. enter image description here
Mainly, you create all those labels, store them in a list. Then all you use them for is to set the text on another new label you create to add to the controls collection.
Here is a shorter, simpler way to do all that with no extra list, and just one scratch label variable for the TLP set:
Dim texts = {"Te 180", "Te 90 - blah blah blah", "Soprte 90 T/T",
"Torillas y salsa", "Torillas y salsa y guacamole"}
Dim lbl As Label
For n As Int32 = 0 To texts.Length - 1
lbl = New Label
lbl.Text = texts(n)
' more important than autozise, probably:
lbl.Dock = DockStyle.Fill
' debug: to see the border for tweaking
lbl.BorderStyle = BorderStyle.FixedSingle
' add the one you created to the controls collection
tlp1.Controls.Add(lbl, 0, n)
Next

Custom colors on bar chart using Zedgraph library

Am using Zedgraph chart library but I can't seem to be able to color a bar graph depending on a specific condition. Am following along with the example found on this tutorial.
In my case, if the value isn't above 50 -which is the student.pass_mark variable-, I want to color the bar red and if its above 50 I want to color it green. Below is my code. Which so far only gives me red even though I have values of 100, 80, 110 etc.
Dim subject As String
Dim grade As Decimal
Dim colors As Color() = {}
Dim subject_names As String() = {}
For i = 0 To student.no_of_subjects
ReDim Preserve colors(i)
ReDim Preserve subject_names(i)
subject = student.subject_name
grade = student.grade
Dim x As Double = CDbl(i) + 1
Dim y As Double = grade
Dim z As Double = 0
list.Add(x, y, z)
If grade < student.pass_mark Then
colors(i) = Color.Red
Else
colors(i) = Color.Green
End If
subject_names(i) = subject
Next
Dim myCurve As BarItem = myPane.AddBar("Student Subject", list, Color.Blue)
'Dim colors As Color() = {Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Purple}
myCurve.Bar.Fill = New Fill(colors)
myCurve.Bar.Fill.Type = FillType.Solid
myCurve.Bar.Fill.RangeMin = 0
myCurve.Bar.Fill.RangeMax = 4
myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45)
myPane.Fill = New Fill(Color.White, Color.FromArgb(255, 255, 225), 45)
' Tell ZedGraph to calculate the axis ranges
' Set the XAxis labels
myPane.XAxis.Scale.TextLabels = subject_names
' Set the XAxis to Text type
myPane.XAxis.Type = ZedGraph.AxisType.Text
ZedChart.IsShowPointValues = True
ZedChart.AxisChange()
ZedChart.Refresh()
Also, I want to draw a line across the whole chart that shows the pass_mark so that it is quickly visible that a student' has or hasn't passed a certain subject in comparison to the passmark
I don't know whether it is still relative but you can do multi-colored bar plots in ZedGraph.
You can find the tutorial here:
http://zedgraph.dariowiz.com/indexb806.html?title=Multi-Colored_Bar_Demo
In your case you can use FillType.GradientByColorValue I assume.
For the line you can simply add a LineObj to the myPane.GraphObjList

string with surrounding color

i want to make an image in vb.net which is a string
it should be made of 2 colors one as forecolor the other as a color surrounding the first one
how should i make it using code?
my result must be some thing like this image(yellow as forecolor and red! as background)
[the string is in persian]
right now i first make the string using
Dim result As New Bitmap(100, 100)
Dim g As Graphics = Graphics.FromImage(result)
g.DrawString("My string", New Font("Arial", 40), New SolidBrush(Color.yellow), 22, 22)
and then process this image by checking every single pixel and if they are close to the string i color them as red , the code is this
kr = font_color.R
kg = font_color.G
kb = font_color.B
For i = 0 To (img.Height - 1) Step 1
prg.Value = prg.Value + 1
For j = 0 To (img.Width - 1)
If (kr = img.GetPixel(j, i).R And kg = img.GetPixel(j, i).G And kb = img.GetPixel(j, i).B) Then
'some code
ElseIf (isnabor(j, i) = True) Then'checks if it is close enough or not
img.SetPixel(j, i, back_color)
Else
img.SetPixel(j, i, Color.Transparent)
End If
Next
Next
The problem is that it takes a long time for a large image
any better way?
Try using GraphicsPath. Check the following links for more information
www.codeproject.com/Articles/42529/Outline-Text
www.java2s.com/Tutorial/VB/0300__2D-Graphics/Textoutline.htm
www.java2s.com/Tutorial/VB/0300__2D-Graphics/AddstringtoGraphicsPath.htm
Bob Powell: Text Effects
by the help of my friend i found the answer here it is:
Dim result As New Bitmap(1000, 1000)
Dim grp As Graphics = Graphics.FromImage(result)
Dim gp As New Drawing2D.GraphicsPath
Dim useFont As Font = New Font("IranNastaliq", 100, FontStyle.Regular)
grp.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gp.AddString(rr.Lines(aa), useFont.FontFamily, FontStyle.Regular, 100, New Point(0, 0), StringFormat.GenericTypographic)
useFont.Dispose()
grp.FillPath(Brushes.White, gp)
grp.DrawPath(Pens.Black, gp)
gp.Dispose()
pic.Image = result

Overwrite a string from the middle

I am trying to make a function to create textboxes. I need to know if there is a way to replace the end of a string with the new number.
While x <= tbnumberofitems
Dim x As Integer = 0 ' looop count '
Dim y As Integer = 1 ' name count'
Dim label1name As String = "label"
Dim textbox1name As String = "textbox"
While x <= tbnumberofitems
y = y + 1
If x = 0 Then y = 1
Convert.ToString(y)
Dim label1 As New Label
label1.Name = label1name & y
'Code to create label box
Dim textbox1 As New TextBox
textbox1.Name = textbox1name & y
'code to create text box
x = x + 1
End While
This is what I currently have. What I need now is a way to make it where when the loop runs the next time, it changes the name to textbox2 and label2 on 3rd loop label 3 and textbox 3, etc. If that is not clear enough what I am trying to do is make it where the numberofitems, make it 5, creates 5 labels and 5 textboxes through the program.
Basicly Like this ..
For x as Integer = 1 to tbnumberofitems
'Code to create label
Dim lbl As New Label
lbl.Name = "label" & format(x)
lbl.Location = New Point(10, x*20)
Me.Controls.Add(lbl)
'Code to create textbox
Dim tb As New TextBox
tb.Name = "TextBox" & format(x)
tb.Location = New Point(100, x*20)
Me.Controls.Add(tb)
Next