Click here to Skip to main content
14,695,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, I am new in Charts in vb.net, i have this code and and these connection to get data from SQL Server, Here is the code in vb.net
 
Private Sub LoadDataGrocery()

       Chart1.DataSource = GetData_Grocery()
       Chart1.DataSource = GetData_Vacation()
       Chart1.DataSource = GetData_Electronics()
       Chart1.DataSource = GetData_FixCosts()
       Chart1.DataSource = GetData_Clothes()
       Chart1.DataSource = GetData_Other()

       'Grocery
       Chart1.Series("Grocery").Points.Clear()
       Chart1.Series("Grocery").Color = Color.FromArgb(172, 126, 241)
       Chart1.Series("Grocery").XValueMember = "Year"
       Chart1.Series("Grocery").YValueMembers = "Total"
       'Vacation
       Chart1.Series("Vacation").Points.Clear()
       Chart1.Series("Vacation").Color = Color.FromArgb(100, 200, 255)
       Chart1.Series("Vacation").XValueMember = "Year"
       Chart1.Series("Vacation").YValueMembers = "Total"
       'Electronics
       Chart1.Series("Electronics").Points.Clear()
       Chart1.Series("Electronics").Color = Color.FromArgb(249, 118, 176)
       Chart1.Series("Electronics").XValueMember = "Year"
       Chart1.Series("Electronics").YValueMembers = "Total"
       'Clothes
       Chart1.Series("Clothes").Points.Clear()
       Chart1.Series("Clothes").Color = Color.FromArgb(253, 138, 114)
       Chart1.Series("Clothes").XValueMember = "Year"
       Chart1.Series("Clothes").YValueMembers = "Total"
       'FixCost
       Chart1.Series("Fix Costs").Points.Clear()
       Chart1.Series("Fix Costs").Color = Color.FromArgb(220, 20, 60)
       Chart1.Series("Fix Costs").XValueMember = "Year"
       Chart1.Series("Fix Costs").YValueMembers = "Total"
       'Other
       Chart1.Series("Other").Points.Clear()
       Chart1.Series("Other").Color = Color.FromArgb(24, 161, 251)
       Chart1.Series("Other").XValueMember = "Year"
       Chart1.Series("Other").YValueMembers = "Total"


   End Sub


and this is the function that i get data :
Private Function GetData_Grocery() As DataTable

       Dim dtChartData As New DataTable()

       Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("db").ConnectionString)
           Using cmd As New SqlCommand("Chart_Grocery", conn) ' Total working hours in the month -- step3
               cmd.CommandType = CommandType.StoredProcedure

               conn.Open()

               cmd.Parameters.AddWithValue("@FromYear2", DateTimePickerfrom.Text) '--step3
               cmd.Parameters.AddWithValue("@toYear2", DateTimePickerto.Text) '--step3

               Dim reader As SqlDataReader = cmd.ExecuteReader()
               dtChartData.Load(reader)

           End Using
       End Using

       Return dtChartData

   End Function


And exactly like this, I have for each Chart in my database
Chart_Grocery
Chart_Clothes
Chart_Electronics
Chart_Vacation
Chart_Other
Chart_FixCost

What I have tried:

And in my SQL script this is what I have used to get the data :


ALTER Procedure [dbo].[Chart_Clothes]
(
@FromYear2 NVARCHAR(50)
,@toYear2 NVARCHAR(50)
)
AS

 Begin

   Select SUM(Price) AS 'Total'
   ,CONVERT(char(7), date, 120)  as 'year'
   --Department = 'Grocery'
   From [dbo].[Expenses]
   Where Department = 'Clothes'
   Group BY  CONVERT(char(7), date, 120)  
   Having CONVERT(char(7), date, 120)   >= @FromYear2 And  CONVERT(char(7), date, 120)   <= @toYear2
   
   End



For each chart, this function is used except the chart_name above and Department,
I don't know where is the issue why it shows me data only in one line.
There is no error just no information.

Thank you
Posted
Updated 16-Jul-20 4:19am
v2
Comments
Andre Oosthuizen 16-Jul-20 9:11am
   
You need to clarify your question as the header says all data shows in one line(not sure what this means) and then at the end you say it does not show data, very confusing.

You can edit your question by selecting the "Improve question" link when you hover over your post.
Member 13410460 16-Jul-20 9:20am
   
Thank you for your reply, I updated the question.

1 solution

Because you only provided a single datasource:
Chart1.DataSource = GetData_Grocery()
Chart1.DataSource = GetData_Vacation()
Chart1.DataSource = GetData_Electronics()
Chart1.DataSource = GetData_FixCosts()
Chart1.DataSource = GetData_Clothes()
Chart1.DataSource = GetData_Other()
Each time you provide another set of data, you overwrite and discard the previous one, leaving only the data from GetData_Other in you chart.

You will need to "combine" your data so you have an X axis series, with multiple Y axis series.
This may help: Create Multi-Series Line Chart (Graph) in Windows Forms Application using C# and VB.Net[^]
   
Comments
Member 13410460 16-Jul-20 9:20am
   
Thank you for your reply, I will have look.
Member 13410460 23-Jul-20 12:50pm
   
Thank you for your information , based on the link I figure it out and fix it.
Thanks
OriginalGriff 23-Jul-20 13:23pm
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900