Author Topic: Chart Does Not Save Correctly When The Form Is Minimised  (Read 1166 times)

Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Chart Does Not Save Correctly When The Form Is Minimised
« on: June 04, 2019, 02:45:27 PM »
Hi Folks,

I'm developing an application where I can plot charts from PLC values.
The charts get saved as a .gif once the X axis plot value has been reached (NumericUpDown3.Value), points get cleared, new chart gets renamed and chart starts to fill again.
For development purposes away from the machine, I am using a timer to plot the data, instead of the SubscriptionDataReceived event.
All works great when the form is not minimised - all charts save perfectly (see FORM MAXIMISED attachment).
But when the form is minimised, it only plots a line 1 pixel wide x 324 pixels high (see FORM MINIMISED attachment).
If i maximise the form again, it plots again.
I need the data to still plot and save the chart when the form is minimised.
See simplified code below, I have omitted any code that is not needed to explain my issue:

Code: [Select]
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

Private Recording As Boolean
    Private Description As String
    Private Chart1 As Chart
    Private ChartDirectory As String
    Private ChartTitle As String
    Private ChartFileName As String

Private Sub CheckExistsCreateFolderSaveChart()
        Dim ChartDirectoryPath As String = ChartDirectory & "\" & Description
        If Not Directory.Exists(ChartDirectoryPath) Then
            Directory.CreateDirectory(ChartDirectoryPath)
        End If
        Chart1.SaveImage(ChartDirectoryPath & "\" & ChartFileName, ChartImageFormat.Gif)
    End Sub



    Private Sub SetChartTitleAndFilename()
        Dim DateTimeNow As Date = Now
        ChartTitle = Description & DateTimeNow.ToString(" (yyyy-MM-dd HH:mm:ss)")
        Chart1.Titles(0).Text = ChartTitle
        ChartFileName = Description & DateTimeNow.ToString(" (yyyy_MM_dd_HH_mm_ss)") & ".gif"
    End Sub



    Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Chart1 = New Chart
        GroupBox4.Controls.Add(Chart1)

        With Chart1
            .BorderlineColor = Color.Black
            .Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Right
            .Location = New Point(6, 25)
            .Size = New Size(1314, 324)
            .Visible = True
            .Enabled = True
        End With
    End Sub



Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If Button8.Text = "Start Recording" Then
            'Clear all
            Chart1.Titles.Clear()
            Chart1.Legends.Clear()
            Chart1.ChartAreas.Clear()
            Chart1.Series.Clear()

            'Title
            Dim Title1 As New Title
            Chart1.Titles.Add(Title1)

            Description = TextBox7.Text

            With Title1
                .Font = New Font("Arial", 10)
                .Alignment = ContentAlignment.TopCenter
                .Position.Auto = False
                .Position.X = 0
                .Position.Y = 1
                .Position.Width = 50
                .Position.Height = 6
                .Text = Description
            End With

            SetChartTitleAndFilename()

            'Legend
            Dim Legend1 As New Legend
            Chart1.Legends.Add(Legend1)

            With Legend1
                .TitleFont = New Font("Arial", 10)
                .Font = New Font("Arial", 10)
                .Docking = Docking.Top
                .Alignment = StringAlignment.Center
                .Position.Auto = False
                .Position.X = 50
                .Position.Y = 1
                .Position.Width = 50
                .Position.Height = 5
                .BackColor = Color.Transparent
                .LegendItemOrder = LegendItemOrder.ReversedSeriesOrder
            End With

            'Chart area
            Dim ChartArea1 As New ChartArea
            Chart1.ChartAreas.Add(ChartArea1)

            With ChartArea1
                .Position.Auto = False
                .Position.X = 0
                .Position.Y = 0
                .Position.Width = 100
                .Position.Height = 100

                .InnerPlotPosition.Auto = False
                .InnerPlotPosition.X = 5
                .InnerPlotPosition.Y = 7
                .InnerPlotPosition.Width = 90
                .InnerPlotPosition.Height = 76

                .IsSameFontSizeForAllAxes = True

                'Primary X axis
                .Axes(0).MajorGrid.Enabled = True
                .Axes(0).MajorGrid.LineColor = Color.Gainsboro
                .Axes(0).LabelStyle.Font = New Font("Arial", 8)
                .Axes(0).MajorTickMark.Size = 1.5
                .Axes(0).MajorTickMark.Enabled = False
                .Axes(0).IsMarginVisible = False
                .Axes(0).IsLabelAutoFit = False
                .Axes(0).LabelStyle.Angle = -90
                .Axes(0).LineWidth = 0
                .Axes(0).IntervalAutoMode = IntervalAutoMode.FixedCount
                .Axes(0).Interval = NumericUpDown3.Value / 25

                'Primary Y axis
                .Axes(1).MajorGrid.Enabled = False
                .Axes(1).LabelStyle.Font = New Font("Arial", 8)
                .Axes(1).MajorTickMark.Size = 0.3
                .Axes(1).MajorTickMark.LineColor = Color.Gainsboro
                .Axes(1).IsMarginVisible = False
                .Axes(1).IsLabelAutoFit = False
                .Axes(1).IsStartedFromZero = False
                .Axes(1).LineColor = Color.Gainsboro
                .Axes(1).IntervalAutoMode = IntervalAutoMode.VariableCount

                .Axes(1).TitleFont = New Font("Arial", 10)
                .Axes(1).Title = "Primary Title"

                'Secondary X axis
                .Axes(2).Enabled = AxisEnabled.False

                'Secondary Y axis
                .Axes(3).MajorGrid.Enabled = False
                .Axes(3).LabelStyle.Font = New Font("Arial", 8)
                .Axes(3).MajorTickMark.Size = 0.3
                .Axes(3).MajorTickMark.LineColor = Color.Gainsboro
                .Axes(3).IsMarginVisible = False
                .Axes(3).IsLabelAutoFit = False
                .Axes(3).IsStartedFromZero = False
                .Axes(3).LineColor = Color.Gainsboro
                .Axes(3).IntervalAutoMode = IntervalAutoMode.VariableCount

                .Axes(3).TitleFont = New Font("Arial", 10)
                .Axes(3).Title = "Secondary Title"
            End With

            'Set recording flag
            Recording = True

            'Series 1
            If Not String.IsNullOrEmpty(TextBox8.Text) Then
                Dim Series1 As New Series
                Chart1.Series.Add(Series1)

                With Series1
                    .Name = TextBox8.Text
                    .Color = Button2.BackColor
                    If ComboBox1.Text = "Primary" Then
                        .YAxisType = AxisType.Primary
                    ElseIf ComboBox1.Text = "Secondary" Then
                        .YAxisType = AxisType.Secondary
                    End If
                    .ChartType = SeriesChartType.Line
                End With
            End If

            'Series 2
            If Not String.IsNullOrEmpty(TextBox9.Text) Then
                Dim Series2 As New Series
                Chart1.Series.Add(Series2)

                With Series2
                    .Name = TextBox9.Text
                    .Color = Button3.BackColor
                    If ComboBox2.Text = "Primary" Then
                        .YAxisType = AxisType.Primary
                    ElseIf ComboBox2.Text = "Secondary" Then
                        .YAxisType = AxisType.Secondary
                    End If
                    .ChartType = SeriesChartType.Line
                End With
            End If

            'Series 3
            If Not String.IsNullOrEmpty(TextBox10.Text) Then
                Dim Series3 As New Series
                Chart1.Series.Add(Series3)

                With Series3
                    .Name = TextBox10.Text
                    .Color = Button4.BackColor
                    If ComboBox3.Text = "Primary" Then
                        .YAxisType = AxisType.Primary
                    ElseIf ComboBox3.Text = "Secondary" Then
                        .YAxisType = AxisType.Secondary
                    End If
                    .ChartType = SeriesChartType.Line
                End With
            End If

            'Series 4
            If Not String.IsNullOrEmpty(TextBox11.Text) Then
                Dim Series4 As New Series
                Chart1.Series.Add(Series4)

                With Series4
                    .Name = TextBox11.Text
                    .Color = Button5.BackColor
                    If ComboBox4.Text = "Primary" Then
                        .YAxisType = AxisType.Primary
                    ElseIf ComboBox4.Text = "Secondary" Then
                        .YAxisType = AxisType.Secondary
                    End If
                    .ChartType = SeriesChartType.Line
                End With
            End If
        ElseIf Button8.Text = "Stop Recording" Then
            If MsgBox("Are you sure you want to stop recording?", vbQuestion + vbYesNo + vbDefaultButton2, "PLC Tag Logger") = DialogResult.Yes Then
               
'Set recording flag
                Recording = False
                CheckExistsCreateFolderSaveChart()
            End If
        End If
    End Sub



Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        SevenSegment1.Value = 10
        SevenSegment2.Value = 90
        SevenSegment3.Value = GetRandom(30, 71)
        SevenSegment4.Value = GetRandom(45, 56)

        If Recording Then
            Dim SeriesCount As Integer = Chart1.Series(0).Points.Count

            If SeriesCount = NumericUpDown3.Value + 1 Then

                CheckExistsCreateFolderSaveChart()

                For Each Series In Chart1.Series
                    Series.Points.Clear()
                Next

                SetChartTitleAndFilename()
            End If

            Dim TimeNow As String = Now.ToString("HH:mm:ss")

            Chart1.Series(0).Points.AddXY(TimeNow, SevenSegment1.Value)
            Chart1.Series(1).Points.AddXY(TimeNow, SevenSegment2.Value)
            Chart1.Series(2).Points.AddXY(TimeNow, SevenSegment3.Value)
            Chart1.Series(3).Points.AddXY(TimeNow, SevenSegment4.Value)
        End If
    End Sub

End Class

Any help much appreciated as always,
Thanks a million,
Regards,
Conor
« Last Edit: June 05, 2019, 12:57:26 PM by Conor_Hyland »