Results 1 to 28 of 28

Thread: [RESOLVED] Getting time and date in chart

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Resolved [RESOLVED] Getting time and date in chart

    Hi guys.

    I have made a chart to read values from soil-moisture sensor connected to Arduino, and everything works fine..

    I would like to change the x-axis to show current date and time. Now it only shows seconds from when i start the readings.
    If it's also possible to get a zoom in/ zoom out I would like to know how..

    I have tried to set Labelstyle -> Format dd-mm-yy hh:mm, but it does not show real time, and every second it starts on a new month :O

    I will paste the code and a screenshot, hopefully i have explained my problem good enough for you to understand

    Code:
    Imports System.IO.Ports
    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
        Private Delegate Sub myDelegate(ByVal Buffer As String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
            Me.WindowState = FormWindowState.Maximized
            ConnectionPanel.Focus()
            ComboBoxBaudRate.SelectedIndex = 0
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.Escape Then Me.Close()
        End Sub
    
    
        Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            SerialPort1.ReadTimeout = 50
    
            Dim sData As String = Nothing
            Try
                sData = SerialPort1.ReadLine
            Catch ex As Exception
    
            End Try
    
            Me.BeginInvoke((New myDelegate(AddressOf DisplayData)), sData)
    
        End Sub
    
        Private Sub DisplayData(ByVal sdata As String)
            Dim chartvalue As Integer
            Dim txtarray As String() = Split(sdata, ",")
    
    
            If txtarray(0) = "Sensor1" Then
                TextBox1.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor1").Points.Add(chartvalue)
            End If
    
            If txtarray(0) = "Sensor2" Then
                TextBox2.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor2").Points.Add(chartvalue)
    
            End If
    
            If txtarray(0) = "Sensor3" Then
                TextBox3.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor3").Points.Add(chartvalue)
    
            End If
    
            If txtarray(0) = "Sensor4" Then
                TextBox4.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor4").Points.Add(chartvalue)
    
            End If
    
            If txtarray(0) = "Sensor5" Then
                TextBox5.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor5").Points.Add(chartvalue)
    
            End If
    
        End Sub
    
        Private Sub ButtonScanPort_Click(sender As Object, e As EventArgs) Handles ButtonScanPort.Click
            ConnectionPanel.Focus()
            If LabelStatus.Text = "Status : Connected" Then
                MsgBox("Conncetion in progress, please Disconnect to scan the new port.", MsgBoxStyle.Critical, "Warning !!!")
                Return
            End If
            ComboBoxPort.Items.Clear()
            Dim myPort As Array
            Dim i As Integer
            myPort = IO.Ports.SerialPort.GetPortNames()
            ComboBoxPort.Items.AddRange(myPort)
            i = ComboBoxPort.Items.Count
            i = i - i
            Try
                ComboBoxPort.SelectedIndex = i
                ButtonConnect.Enabled = True
            Catch ex As Exception
                MsgBox("Com port not detected", MsgBoxStyle.Critical, "Warning !!!")
                ComboBoxPort.Text = ""
                ComboBoxPort.Items.Clear()
                Return
            End Try
            ComboBoxPort.DroppedDown = True
        End Sub
    
        Private Sub ComboBoxPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxPort.SelectedIndexChanged
            ConnectionPanel.Focus()
        End Sub
    
        Private Sub ComboBoxPort_DropDown(sender As Object, e As EventArgs) Handles ComboBoxPort.DropDown
            ConnectionPanel.Focus()
        End Sub
    
        Private Sub LinkLabel_LinkClicked_1(sender As Object, e As LinkLabelLinkClickedEventArgs)
    
        End Sub
    
        Private Sub ComboBoxPort_Click(sender As Object, e As EventArgs) Handles ComboBoxPort.Click
            If LabelStatus.Text = "Status : Connected" Then
                MsgBox("Connection in progress, please Disconnect to change COM.", MsgBoxStyle.Critical, "Warning !!!")
                Return
            End If
        End Sub
    
        Private Sub ComboBoxBaudRate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxBaudRate.SelectedIndexChanged
            ConnectionPanel.Focus()
        End Sub
    
    
    
        Private Sub ComboBoxBaudRate_DropDown(sender As Object, e As EventArgs) Handles ComboBoxBaudRate.DropDown
            ConnectionPanel.Focus()
        End Sub
    
    
        Private Sub ComboBoxBaudRate_Click(sender As Object, e As EventArgs) Handles ComboBoxBaudRate.Click
            If LabelStatus.Text = "Status : Connected" Then
                MsgBox("Conncetion in progress, please Disconnect to change Baud Rate.", MsgBoxStyle.Critical, "Warning !!!")
                Return
            End If
        End Sub
    
        Private Sub ButtonConnect_Click(sender As Object, e As EventArgs) Handles ButtonConnect.Click
            ConnectionPanel.Focus()
            Try
                SerialPort1.BaudRate = ComboBoxBaudRate.SelectedItem
                SerialPort1.PortName = ComboBoxPort.SelectedItem
                SerialPort1.Open()
                TimerSerial.Start()
    
                LabelStatus.Text = "Status : Connected"
                ButtonConnect.SendToBack()
                ButtonDisconnect.BringToFront()
                PictureBoxConnectionStatus.BackColor = Color.Green
            Catch ex As Exception
                MsgBox("Please check the Hardware, COM, Baud Rate and try again.", MsgBoxStyle.Critical, "Connection failed !!!")
            End Try
        End Sub
        Private Sub ButtonDisconnect_Click(sender As Object, e As EventArgs) Handles ButtonDisconnect.Click
            ConnectionPanel.Focus()
            TimerSerial.Stop()
            SerialPort1.Close()
            ButtonDisconnect.SendToBack()
            ButtonConnect.BringToFront()
            LabelStatus.Text = "Status : Disconnect"
            PictureBoxConnectionStatus.Visible = True
            PictureBoxConnectionStatus.BackColor = Color.Red
    
        End Sub
    
        Private Sub ChartSettings()
    
            Chart1.BackColor = Color.Bisque
    
            Chart1.Series("Series1").ChartType = SeriesChartType.Spline
            Chart1.Series("Series2").ChartType = SeriesChartType.Spline
    
            Chart1.Series("Series1").BorderWidth = 2
            Chart1.Series("Series2").BorderWidth = 2
    
            Chart1.Series("Series1").Color = Color.Red
            Chart1.Series("Series2").Color = Color.DarkBlue
    
            With Chart1.ChartAreas(0).AxisX
    
    
    
                .Maximum = 300
                .Minimum = 0
    
                .LabelStyle.Format = "0S"
                .Title = "Humidity"
                .TitleFont = New Font(New FontFamily("Arial"), 9, FontStyle.Bold)
    
                .MajorGrid.Interval = 1
                .MajorGrid.Enabled = True
    
                .MajorTickMark.Enabled = True
                .MajorTickMark.Interval = 1
                .MajorTickMark.Size = 2
    
                .LabelStyle.Enabled = True
                .LabelStyle.Interval = 10
    
    
            End With
    
            With Chart1.ChartAreas(0).AxisY
    
                .Maximum = 100
                .Minimum = 0
    
                .MajorGrid.Interval = 10
                .MajorGrid.Enabled = True
    
                .MajorTickMark.Enabled = True
                .MajorTickMark.Interval = 10
                .MajorTickMark.Size = 2
    
                .LabelStyle.Enabled = True
                .LabelStyle.Interval = 10
    
            End With
    
    
    
            Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
            Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(1, 59)
    
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim frm As New Form1
            frm.Show()
    
            Me.Close()
        End Sub
    
    
    
    End Class
    Name:  Skjermbilde.jpg
Views: 418
Size:  21.3 KB

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Hi Fedda,

    You might find your answer here:


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Thank for reply Poppa!

    The problem is that I'm in the learning process, and really not sure what changes to make in my coding..
    Learning new things everyday, but here I'm stuck..
    If you have the knowledge (and time) would you mind take a look at my code and tell me what to change?

    Probably here I need to make some changes?
    Name:  Skjermbilde.jpg
Views: 324
Size:  18.8 KB

    I might be wrong, but if this could be made into a code, it might work?
    When Button1 is pushed, Then read systemDateAndTimeNow (from windows) to X-Axis start.
    And When values updated (every 1000ms) add 1 second to a pre-generated timeline. (Not shure if it makes any sense, just a thought)
    Last edited by Fedda; Dec 1st, 2021 at 01:38 PM.

  4. #4
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    If you have a (Say) Label to display the time (and date ?) your Button1 could call a small subroutine to display the time, an example might look like this:
    Code:
       Dim txt as string
       txt = Now.Hour.ToString & ":"
       txt &= Now.Minute.ToString & ":"
       txt &= Now.Second.ToString
    
    'If you want to add the date as well you could (add " & " (no quotes) 
    'to the last line above) and use:
    
       txt &= Now.ToString(".  dd MM yyyy") 
       Label1.Text = txt
    Your Button1 could, at the same time, start a timer (Say) Timer1.
    Timer1 interval would be set to 1,000. and its Tick would also call your small subroutine (once every minute).

    If you'd prefer the date to be first I'm sure you can see how to do that.

    Poppa
    Last edited by Poppa Mintin; Dec 1st, 2021 at 07:53 PM. Reason: Date option added
    Along with the sunshine there has to be a little rain sometime.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Great, this worked just as I wanted, got the time and date to display in a Label
    Now, I would like to get the readings from Label6 to be displayed in X-Axis, and I'm sure you know how that is done also
    And one more thing, seconds and hour displays with only 1 value when its under 10 (like 9:2:3 PM, and i would like it to display 09:02:03 PM)
    Attachment 183180

  6. #6
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    I'm sorry but I can't help with your X axis problem. I can't even see where you add the time in your code.

    To display leading zeros is a simple matter. An example would be:-
    Code:
    txt = Now.Hour.ToString("00") & ":"
    This will display 01 to 09, and then 10 to 23 (in this case).
    If you were dealing with numbers of up to (say) 001 to 099 then you'd simply add another zero to the string in the parentheses, and so on for larger sums.


    Poppa

    PS.

    Oh! I've just seen the 'PM' part of your question.

    'Now' always deals with the 24 hour clock, you could read the Now.Hour part into an integer variable, subtract 12 where necessary and use that for: -
    Code:
    txt = hourValue.ToString("00") & ":"
    Then just add & " AM" or & " PM" to the relevant part of the string. (txt)


    Pop.
    Last edited by Poppa Mintin; Dec 2nd, 2021 at 07:51 PM. Reason: PS added
    Along with the sunshine there has to be a little rain sometime.

  7. #7
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Hi Fedda,

    Here's an example.

    Code:
       Dim txt As String, hours as Integer, flag as Boolean
    
       hours = Now.Hour
       If hours > 12 Then
          flag = true
          hours -= 12
       End If
       txt = hours.ToString("00") & ":"
       txt &= Now.Minute.ToString("00") & ":"
       txt &= Now.Second.ToString("00") & " AM"
       If flag then txt = txt.Replace("AM", "PM")
       Label1.Text = txt
    Remember that boolean variables are set to false by default each time you call the subroutine.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Even tidier...

    Code:
       Dim txt As String
       Dim apm As String = " AM"
       Dim hrs As Integer = Now.Hour
    
       If hrs > 12 Then
          apm = " PM"
          hrs -= 12
       End If
    
       txt = hrs.ToString("00") & ":"
       txt &= Now.Minute.ToString("00") & ":"
       txt &= Now.Second.ToString("00") & apm
       Label1.Text = txt

    Poppa
    Last edited by Poppa Mintin; Dec 2nd, 2021 at 08:31 PM.
    Along with the sunshine there has to be a little rain sometime.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Thanks for all your help @Poppa

    Sry for the misunderstanding and waste of your time, I will keep the 24h and not change to AM PM. Maybe i formulated a little bit wrong.

    Appreciate the help with ("00") and the other stuff

    Now my only issue is to work with the chart and get the X-Axis to display time and date, and some kind of zoom in/out..

  10. #10
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    That's ok, no problem.

    I still can't see where you are trying to enter the time.
    Maybe it's in your attachment in Post 5 ?
    Sadly the attachment fails. (invalid attachment)
    You might like to post just the relevant part of your code ?


    Poppa

    PS
    You might also like to make the string variable which I've been calling 'txt' into a global variable so that it will be calculated within the subroutine but you can use it anywhere in your code. I suspect you'd still have to refresh the control in which you use it though.

    Pop
    Last edited by Poppa Mintin; Dec 4th, 2021 at 07:13 AM. Reason: PS added
    Along with the sunshine there has to be a little rain sometime.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Made a new project to get it a little tidier.
    This is a part of the code I have now, and Date and Time is displayed in a label, but not at the X-Axis. So my problem now is to get the date and time in the X-Axis..

    Code:
    Imports System.IO.Ports
    Imports System.Windows.Forms.DataVisualization.Charting
    
    
    Public Class Form1
        Private Delegate Sub myDelegate(Buffer As String)
        Dim txt As String
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FormBorderStyle = FormBorderStyle.None
            WindowState = FormWindowState.Maximized
            ConnectionPanel.Focus()
            ComboBoxBaudRate.SelectedIndex = 0
    
            Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
            Chart1.ChartAreas(0).AxisX.ScaleView.SmallScrollMinSize = 5
            Chart1.Series(0).XValueType = ChartValueType.DateTime
            Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "hh:mm:ss"
        End Sub
    
    
        Private Sub TimerSerial_Tick(sender As Object, e As EventArgs) Handles TimerSerial.Tick
            txt = Now.Hour.ToString("00") & ":"
            txt &= Now.Minute.ToString("00") & ":"
            txt &= Now.Second.ToString("00")
            txt &= Now.ToString(".  dd MM yyyy")
            Label6.Text = txt
        End Sub
    Name:  Skjermbilde.jpg
Views: 254
Size:  14.3 KB

  12. #12
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Getting time and date in chart

    if you want a real time update, in your timer tick you need to update your chart so you should have something like that :

    Code:
    Chart1.Series(x).Points.AddXY(time, value) 'x in the number of the serie
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Think I might be close to a solution now.

    Changed the;
    Code:
    Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "hh:mm:ss"
    to
    Code:
    Chart1.ChartAreas(0).AxisX.LabelStyle.Format = Now.ToString(txt)
    in the Form1.load Sub

    Axis-X shows the current date, but problem now it that it only shows the time when i started the program. (Picture in link)
    https://i.imgur.com/vzR8Z0J.png

    Then i tried to cut/paste the line into the TimerSerial.Tick Sub. But that didn't work as planned either.
    Not sure how to explain, but it doesn't save the time, it continuously updates all the "writings" to current time (Picture in link)
    https://i.imgur.com/kW2QWVN.png

  14. #14
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Getting time and date in chart

    what is the interval of your timer or every how much millisecond do you update your chart ? if it is to fast, you will get the same value for .now
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  15. #15
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Hi Fedda,

    I notice from your image that the traces don't start until the 2nd time axis, I suspect that this is because the traces are triggered by your timer ?
    In an earlier post you mentioned starting with Button6, the following minor change would fix that.
    Code:
    Imports System.IO.Ports
    Imports System.Windows.Forms.DataVisualization.Charting
    
    
    Public Class Form1
        Private Delegate Sub myDelegate(Buffer As String)
        Dim txt As String
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FormBorderStyle = FormBorderStyle.None
            WindowState = FormWindowState.Maximized
            ConnectionPanel.Focus()
            ComboBoxBaudRate.SelectedIndex = 0
    
            Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
            Chart1.ChartAreas(0).AxisX.ScaleView.SmallScrollMinSize = 5
            Chart1.Series(0).XValueType = ChartValueType.DateTime
            Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "hh:mm:ss"
        End Sub
    
        
        Private Sub Button6_Click() Handles Button1.Click
            GetTime()
            TimerSerial.Start
            ' Plus whatever is in here.
        End Sub 'Exit
    
    
        Private Sub TimerSerial_Tick(sender As Object, e As EventArgs) Handles TimerSerial.Tick
            GetTime()
        End Sub
    
    
        Private Sub GetTime()
            txt = Now.Hour.ToString("00") & ":"
            txt &= Now.Minute.ToString("00") & ":"
            txt &= Now.Second.ToString("00")
            txt &= Now.ToString(".  dd MM yyyy")
            Label6.Text = txt
        End Sub
    As Delaney says, getting the time into your axis would involve telling your code at which time axis to place 'txt'.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Quote Originally Posted by Delaney View Post
    what is the interval of your timer or every how much millisecond do you update your chart ? if it is to fast, you will get the same value for .now

    I think they are both set at 1000ms..

  17. #17

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Still can't figure it out, although I think I'm very close..

    Thanks for all the tips!

    The problem is that I have only had this hobby for a couple of months, and struggle to understand how to do the things you mention.
    Like:
    "if you want a real time update, in your timer tick you need to update your chart so you should have something like that"
    "what is the interval of your timer or every how much millisecond do you update your chart ? if it is to fast, you will get the same value for .now"
    "As Delaney says, getting the time into your axis would involve telling your code at which time axis to place 'txt'"

    I know the day I get this working is near, just need things explained "as a beginner" "VB for dummies"

    Refer to post 13, where I managed to get the reading to the Chart, but it is not useful since it updates all the date and time it already wrote..

  18. #18

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Quote Originally Posted by Poppa Mintin View Post
    Hi Fedda,

    I notice from your image that the traces don't start until the 2nd time axis, I suspect that this is because the traces are triggered by your timer ?
    In an earlier post you mentioned starting with Button6, the following minor change would fix that.
    Code:
    Imports System.IO.Ports
    Imports System.Windows.Forms.DataVisualization.Charting
    
    
    Public Class Form1
        Private Delegate Sub myDelegate(Buffer As String)
        Dim txt As String
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FormBorderStyle = FormBorderStyle.None
            WindowState = FormWindowState.Maximized
            ConnectionPanel.Focus()
            ComboBoxBaudRate.SelectedIndex = 0
    
            Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
            Chart1.ChartAreas(0).AxisX.ScaleView.SmallScrollMinSize = 5
            Chart1.Series(0).XValueType = ChartValueType.DateTime
            Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "hh:mm:ss"
        End Sub
    
        
        Private Sub Button6_Click() Handles Button1.Click
            GetTime()
            TimerSerial.Start
            ' Plus whatever is in here.
        End Sub 'Exit
    
    
        Private Sub TimerSerial_Tick(sender As Object, e As EventArgs) Handles TimerSerial.Tick
            GetTime()
        End Sub
    
    
        Private Sub GetTime()
            txt = Now.Hour.ToString("00") & ":"
            txt &= Now.Minute.ToString("00") & ":"
            txt &= Now.Second.ToString("00")
            txt &= Now.ToString(".  dd MM yyyy")
            Label6.Text = txt
        End Sub
    As Delaney says, getting the time into your axis would involve telling your code at which time axis to place 'txt'.


    Poppa
    I also remember something about Button6, but cant find back to that post :O

    Now, the chart starts when i click ButtonConnect, but as you say there might be some problems since it doesn't write anything before reading 2

  19. #19
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Getting time and date in chart

    the problem is here :

    Code:
     If txtarray(0) = "Sensor1" Then
                TextBox1.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor1").Points.Add(chartvalue)
            End If
    you add to your chart only the value and not the time. what kind of chart do you have? your chart type should be .ChartType = SeriesChartType.Line

    so you can replace
    Code:
    Chart1.Series ("Sensor1").Points.Add(chartvalue) by Chart1.Series ("Sensor1").Points.AddXY(time, chartvalue) (time is a datatime variable)
    I think but I am not sure, if you replace "time" by Now, you should get exactly what you want.

    I have a program similar to your and this is what I use (well, I don't use real time but the idea is there) (sorry it is in french) the same button is used to start and stop so the select case :


    Code:
    Private Sub SerialPort_arduino_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort_arduino.DataReceived
            Dim Incoming As String = Nothing
            Incoming = SerialPort_arduino.ReadLine()
            Incoming = Incoming.Replace(vbCr, "").Replace(vbLf, "")
    
            If Incoming <> Nothing Then
                Dim valeur = CDbl(Incoming) * tension_application / resolution * conversion
                Me.BeginInvoke(Sub()
                                   Chart1.Series(0).Points.AddXY(Tps * pas_temps, valeur)
                               End Sub)
                courbe_acquise.ligne.Add(New Courbe.PointD(Tps * pas_temps, valeur))
                Tps += 1
            End If
    
        End Sub
    
    Private Sub B_activation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_activation.Click
            Select Case reccord
                Case False
    
                    'initialisation courbe de mesure
                    courbe_acquise = New Courbe With {
                    .nom = TB_titre.Text,
                    .nom_axe_X = "Temps en s",
                    .nom_axe_Y = TB_type_données.Text}
    
                    tension_application = CDbl(TB_tension_application.Text)
                    resolution = CInt(TB_resolution.Text)
                    pas_temps = CDbl(NTB__interval_acquisition.Text)
    
                    'initialisation du graphique
                    Chart1.Series.Clear()
                    Chart1.ChartAreas.Clear()
                    Chart1.Titles.Clear()
    
                    Dim serie = "courbe"
                    Dim Area As New ChartArea With {.Name = "ChartArea1"}
                    Chart1.Series.Add(serie)
                    Chart1.ChartAreas.Add(Area)
                    Chart1.Titles.Add(courbe_acquise.nom)
                    With Chart1.Series(serie)
                        .ChartType = SeriesChartType.Line
                        .ChartArea = Chart1.ChartAreas(0).Name
                        .IsVisibleInLegend = False
                    End With
    
                    With Chart1.ChartAreas(0)
                        .AxisX.Title = courbe_acquise.nom_axe_X
                        .AxisX.LabelStyle.Format = chart_precision(CInt(NUD_X.Value))
                        .AxisX.RoundAxisValues()
                        .AxisX.Minimum = 0
                        .AxisY.Title = courbe_acquise.nom_axe_Y
                        .AxisY.RoundAxisValues()
                        .AxisY.LabelStyle.Format = chart_precision(CInt(NUD_Y.Value))
                    End With
                    reccord = True
                    B_activation.Text = "Arrêter l'enregistrement"
                    Tps = 0
                    Dim message = "S" & CStr(pas_temps * 1000) & "!"
                    SerialPort_arduino.Write(message)
    
                Case True
    
                    reccord = False
                    B_activation.Text = "Démarrer l'enregistrement"
                    SerialPort_arduino.Write("A!")
            End Select
    
    Private Function chart_precision(ByVal p As Integer) As String
            Dim precision As String = "0"
            If p > 0 Then
                precision += "."
                For i = 1 To p
                    precision += "0"
                Next
            End If
    
            Return precision
    
        End Function
    Last edited by Delaney; Dec 5th, 2021 at 04:34 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  20. #20
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Quote Originally Posted by Fedda View Post
    I also remember something about Button6, but cant find back to that post :O

    Now, the chart starts when i click ButtonConnect, but as you say there might be some problems since it doesn't write anything before reading 2
    Ah... Sorry, that's my mistake, it ought to have been Button1, but now I see that you seem to've called it ButtonConnect.

    So... The relevant part of the suggestion in Post #15 ought to've been:-
    Code:
        
        Private Sub ButtonConnect_Click() Handles ButtonConnect.Click
            TimerSerial.Stop()    ' Add this line if you need to re-set the timer for each click.
            GetTime()
            TimerSerial.Start()
            ' Plus whatever else is in here.
        End Sub 'Exit
    
        Private Sub TimerSerial_Tick(sender As Object, e As EventArgs) Handles TimerSerial.Tick
            GetTime()
            ' Plus whatever else is in here.
        End Sub
    You'll notice some changes. (Corrections)


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  21. #21

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Quote Originally Posted by Delaney View Post
    the problem is here :



    you add to your chart only the value and not the time. what kind of chart do you have? your chart type should be .ChartType = SeriesChartType.Line

    so you can replace
    Code:
    Chart1.Series ("Sensor1").Points.Add(chartvalue) by Chart1.Series ("Sensor1").Points.AddXY(time, chartvalue) (time is a datatime variable)
    I think but I am not sure, if you replace "time" by Now, you should get exactly what you want.

    I have a program similar to your and this is what I use (well, I don't use real time but the idea is there) (sorry it is in french) the same button is used to start and stop so the select case :


    Code:
    Private Sub SerialPort_arduino_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort_arduino.DataReceived
            Dim Incoming As String = Nothing
            Incoming = SerialPort_arduino.ReadLine()
            Incoming = Incoming.Replace(vbCr, "").Replace(vbLf, "")
    
            If Incoming <> Nothing Then
                Dim valeur = CDbl(Incoming) * tension_application / resolution * conversion
                Me.BeginInvoke(Sub()
                                   Chart1.Series(0).Points.AddXY(Tps * pas_temps, valeur)
                               End Sub)
                courbe_acquise.ligne.Add(New Courbe.PointD(Tps * pas_temps, valeur))
                Tps += 1
            End If
    
        End Sub
    
    Private Sub B_activation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_activation.Click
            Select Case reccord
                Case False
    
                    'initialisation courbe de mesure
                    courbe_acquise = New Courbe With {
                    .nom = TB_titre.Text,
                    .nom_axe_X = "Temps en s",
                    .nom_axe_Y = TB_type_données.Text}
    
                    tension_application = CDbl(TB_tension_application.Text)
                    resolution = CInt(TB_resolution.Text)
                    pas_temps = CDbl(NTB__interval_acquisition.Text)
    
                    'initialisation du graphique
                    Chart1.Series.Clear()
                    Chart1.ChartAreas.Clear()
                    Chart1.Titles.Clear()
    
                    Dim serie = "courbe"
                    Dim Area As New ChartArea With {.Name = "ChartArea1"}
                    Chart1.Series.Add(serie)
                    Chart1.ChartAreas.Add(Area)
                    Chart1.Titles.Add(courbe_acquise.nom)
                    With Chart1.Series(serie)
                        .ChartType = SeriesChartType.Line
                        .ChartArea = Chart1.ChartAreas(0).Name
                        .IsVisibleInLegend = False
                    End With
    
                    With Chart1.ChartAreas(0)
                        .AxisX.Title = courbe_acquise.nom_axe_X
                        .AxisX.LabelStyle.Format = chart_precision(CInt(NUD_X.Value))
                        .AxisX.RoundAxisValues()
                        .AxisX.Minimum = 0
                        .AxisY.Title = courbe_acquise.nom_axe_Y
                        .AxisY.RoundAxisValues()
                        .AxisY.LabelStyle.Format = chart_precision(CInt(NUD_Y.Value))
                    End With
                    reccord = True
                    B_activation.Text = "Arrêter l'enregistrement"
                    Tps = 0
                    Dim message = "S" & CStr(pas_temps * 1000) & "!"
                    SerialPort_arduino.Write(message)
    
                Case True
    
                    reccord = False
                    B_activation.Text = "Démarrer l'enregistrement"
                    SerialPort_arduino.Write("A!")
            End Select
    
    Private Function chart_precision(ByVal p As Integer) As String
            Dim precision As String = "0"
            If p > 0 Then
                precision += "."
                For i = 1 To p
                    precision += "0"
                Next
            End If
    
            Return precision
    
        End Function
    Finally got it working! So thankful for all the help!
    Did some changes to the part you mentioned, and zim-zalla-bim!
    Code:
        Private Sub DisplayData(ByVal sdata As String)
            Dim chartvalue As Integer
            Dim txtarray As String() = Split(sdata, ",")
    
    
    
    
            If txtarray(0) = "Sensor1" Then
                TextBox1.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor1").Points.AddXY(txt, chartvalue)
            End If
    
            If txtarray(0) = "Sensor2" Then
                TextBox2.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor2").Points.AddXY(txt, chartvalue)
    
            End If
    
            If txtarray(0) = "Sensor3" Then
                TextBox3.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor3").Points.AddXY(txt, chartvalue)
    
            End If
    
            If txtarray(0) = "Sensor4" Then
                TextBox4.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor4").Points.AddXY(txt, chartvalue)
    
            End If
    
            If txtarray(0) = "Sensor5" Then
                TextBox5.Text = txtarray(1) & " %"
                Integer.TryParse(txtarray(1), chartvalue)
                Chart1.Series("Sensor5").Points.AddXY(txt, chartvalue)
    
            End If
    
        End Sub
    Name:  Skjermbilde.jpg
Views: 203
Size:  15.4 KB

    Now my next step is to work with the fonts (at least get the DateTime vertical instead of horizontal), and try to set some kind of zoom properties..

    Thanks again, so glad we got this sorted

  22. #22
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Quote Originally Posted by Fedda View Post
    Now my next step is to work with the fonts (at least get the DateTime vertical instead of horizontal)
    Use a slightly smaller font size, use a 'Narrow' font (Arial Narrow? Niagara Engraved?) and put date beneath time?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  23. #23

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    I'm in for the challenge "put date beneath time"
    Managed vertical text
    Name:  Skjermbilde.jpg
Views: 247
Size:  17.1 KB

    Is it possible to put a "breakline" code into this?:
    Code:
        Private Sub GetTime()
            txt = Now.Hour.ToString("00") & ":"
            txt &= Now.Minute.ToString("00") & ":"
            txt &= Now.Second.ToString("00")
            txt &= Now.ToString(". dd-MM-yyyy")
            Label6.Text = txt
    
    
        End Sub
    Last edited by Fedda; Dec 6th, 2021 at 12:39 PM.

  24. #24
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Getting time and date in chart

    you can try
    Code:
    Private Sub GetTime()
            txt = Now.Hour.ToString("00") & ":" & Now.Minute.ToString("00") & ":" & Now.Second.ToString("00") & Environment.NewLine & Now.ToString("dd-MM-yyyy")
            Label6.Text = txt
        End Sub
    Last edited by Delaney; Dec 7th, 2021 at 10:52 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  25. #25
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Getting time and date in chart

    Quote Originally Posted by Fedda View Post
    Is it possible to put a "breakline" code into this?
    Define "breakline" ?


    I think you know that I meant like this:

    This is using the default font of the forum.

    xxxxxxxx10:28:15
    xxxxxxi06-12-2021


    This is using the Arial Narrow font.

    xxxxxxxx10:28:15
    xxxxxxi06-12-2021


    This is using the Niagara Engraved font.

    xxxxxxxx10:28:15
    xxxxxxi06-12-2021


    Easier to read than the vertical don't you agree?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  26. #26

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    "Breakline" = "NewLine"

    Yeah, the Narrow looks much better, thanks

  27. #27
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Getting time and date in chart

    Quote Originally Posted by Fedda View Post
    "Breakline" = "NewLine"
    that's what I understood. My post #24 should do the job
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  28. #28

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    14

    Re: Getting time and date in chart

    Quote Originally Posted by Delaney View Post
    that's what I understood. My post #24 should do the job
    Yeah, worked just fine
    Just answered Poppa's post, to define what I ment with Breakline

    Again, thanks for all the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width