I am an author, and working to make a system to track my details. This item is a chart where it has the 3 types of books, grouped by the edition type (ebook, hard cover, and paperback), and a sum of the sales for the month.
- Windows 10 Pro
- Visual Studio 2022 Community
- Windows Forms App (.NET Framework), using .NET Framework 4.7.2
- MS SQL Express 2022 (hosted locally on my PC)
- The used SQL View works and displays the data correctly
| Field | Format |
| ————– | ——– |
| SaleWhen | YYYY-MM |
| EdType | String |
| SumOfSaleCount | Integer |
EdType has 3 distinct values, for 3 book types. This column is used to generate the names and to group the 3 series together.
SaleWhen is generated by the grouping. In the form, it runs through a function I made to parse the data, and generate a date for the first of the month, so it sees a valid date.
SumOfSaleCount is a SQL sum of entries in the table for the month.
I have a working DataSet to access the data.
The Form contains a Chart element, docked to fill the form, and is called by the MDI interface (which is in the Project). The form also has the DataSet, BindingSource, and TableAdapter. The only change from the default for the chart is the binding source is set.
I have re-made the form, as Test1, to make sure I hadn’t messed anything up, but it does the same thing.
Imports System.Windows.Forms.DataVisualization.Charting
Imports GUI.MiscFunctions
Public Class Test1
Private Sub Test1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SalesByDateMonth_ByEditionTypeTableAdapter.Fill(Me.BooksDataSet.SalesByDateMonth_ByEditionType)
Dim dataTable As DataTable = BooksDataSet.Tables("SalesByDateMonth_ByEditionType")
' Initialize chart
Dim Chart1 As New Chart()
' Add chart area
Dim chartArea1 As New ChartArea()
Chart1.ChartAreas.Add(chartArea1)
' Create series for each EdType
Dim RowXLabel = "SaleWhen" 'The Column name with the value for the X Axis (Date)
Dim RowYLabel = "SumOfSaleCount" 'The column name with the value for the Y Axis (Integer)
Dim SeriesLabel = "EdType" 'The column name with the label for the series (String, with each series having a value)
For Each SeriesValue As String In GetDistinctSeries(dataTable, SeriesLabel)
Dim series As New Series(SeriesValue)
series.ChartType = SeriesChartType.Bar
series.Label = SeriesValue
' Add data points to the series; if the SeriesLabel column has the selected SeriesValue
For Each row As DataRow In dataTable.Rows
If row(SeriesLabel).ToString() = SeriesValue Then
Dim XValue As Date = ParseDate(row(RowXLabel).ToString())
Dim YValue As Integer = Convert.ToInt32(row(RowYLabel))
series.Points.AddXY(XValue, YValue)
End If
Next
Chart1.Series.Add(series)
Next
' Add chart to form
Me.Controls.Add(Chart1)
End Sub
End Class
MiscFunctions items used:
Public Class MiscFunctions
Public Shared Function ParseDate(ByVal DateString As String) As Date
Dim ReturnError As Date = New Date(2000, 1, 1)
' Check if the input string is empty
If String.IsNullOrEmpty(DateString) Then
MessageBox.Show("Empty date string. Please provide a valid date string in yyyy-mm or yyyy-m format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ReturnError
End If
' Split the string into year and month parts
Dim parts() As String = DateString.Split("-"c)
' Check if the number of parts is not 2
If parts.Length <= 2 And parts.Length >= 3 Then
MessageBox.Show("Invalid date format. Please provide a valid date string in yyyy-mm or yyyy-mm-dd format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ReturnError
End If
Dim yearPart As Integer
Dim monthPart As Integer
Dim dayPart As Integer
' Attempt to parse year part
If Not Integer.TryParse(parts(0), yearPart) Then
MessageBox.Show("Invalid year format. Please provide a valid year in yyyy format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ReturnError
End If
' Attempt to parse month part
If Not Integer.TryParse(parts(1), monthPart) Then
MessageBox.Show("Invalid month format. Please provide a valid month in mm or m format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ReturnError
End If
If parts.Length = 3 Then
If Not Integer.TryParse(parts(2), dayPart) Then
MessageBox.Show("Invalid month format. Please provide a valid month in mm or m format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ReturnError
End If
Else
dayPart = 1
End If
' Check if year and month parts are within valid ranges
If yearPart < 1 OrElse yearPart > 9999 OrElse monthPart < 1 OrElse monthPart > 12 OrElse dayPart < 1 OrElse dayPart > 31 Then
MessageBox.Show("Invalid year or month value. Please provide valid year (1-9999) and month (1-12) values, and optionally day (1-31).", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ReturnError
End If
' Create a Date variable using the year and month parts
Dim dateVariable As Date = New Date(yearPart, monthPart, dayPart)
Return dateVariable
End Function
Public Shared Function GetDistinctSeries(dataTable As DataTable, SeriesLabel As String) As List(Of String)
Dim LabelList As New List(Of String)()
For Each row As DataRow In dataTable.Rows
Dim LabelEntry As String = row(SeriesLabel).ToString()
If Not LabelList.Contains(LabelEntry) Then
LabelList.Add(LabelEntry)
End If
Next
Return LabelList
End Function
End Class
What I am expecting is a chart with data. (30, 20, 0 for the first month for the series)
What I get when launched
Entering the series information manually and disabling the Load details so it doesn’t overwrite the data for some reason still generates the same issue, so not sure what is causing the form to not load the chart right.
I do have other charts with one series, where I had connected the data using the designer worked correctly.
Having a breakpoint at the Me.Controls.Add(Chart1), shows the Chart1 with all the data loaded into the series correctly, but it’s still not displaying, so the data is being pulled from the database correctly.
I did try ChatGPT, but it didn’t give me a solution for it. Anyone’s help would be appreciated.
Wolphin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.