Overview



Crosshairs



Zoom1



Zoom2



Zoom3
FormatString = "hh:mm:ss.fff"



Code

' To test this code, the trial or full version of XYChart must be installed on your computer.
' 1. Start a new standard Visual Basic project
' 2. Copy the code below directly into the project
' 3. On the form, place a new XYChart control named XYChart4Ctl1 (default)
' 4. Save the project
' 5. Right click and "Save Target As..." to download and save the Data8.txt file in the same folder as the VB project
' 6. Run the project

Private Sub Form_Load()
    ' Configure XYChart control
    With XYChart4Ctl1
        .NumProfiles = 1

        .NumYScales = 1
        .NumXScales = 1

        ' Legend
        .Legend.Visible = True
        .Legend.BorderVisible = False
        .Legend.YScaleVisible = False

        .Toolbar.Visible = True

        ' Grids
        .YGrid(1).LineOption = loCustom
        .YGrid(1).LineColor = RGB(0, 170, 0)
        .XGrid(1).LineOption = loCustom
        .XGrid(1).LineColor = .YGrid(1).LineColor

        ' X Scale
        .XScale(1).Label = ""
        .XScale(1).TicksFont.Color = vbWhite
        .XScale(1).LabelFont.Color = .XScale(1).TicksFont.Color
        .XScale(1).FormatStyle = fsDateTime
        .XScale(1).DateTimeFormat = "hh:mm:ss AMPM"
        .XScale(1).ScaleMode = smManual
        .XScale(1).Min = 38045
        .XScale(1).Max = 38046

        ' Y Scale
        .YScale(1).Label = "MW"
        .YScale(1).TicksFont.Color = vbWhite
        .YScale(1).LabelFont.Color = .YScale(1).TicksFont.Color
        .YScale(1).LabelVertical = "Ontario Demand Power" + vbCrLf + "for February 28, 2004"
        .YScale(1).LabelVerticalFont.Size = 10
        .YScale(1).LabelVerticalFont.Bold = True
        .YScale(1).LabelVerticalFont.Color = .YScale(1).TicksFont.Color
        .YScale(1).Visible = True

        ' Chart and Plot formatting
        .BackColor = RGB(39, 80, 80)
        .Plot.BackColor = .BackColor
        .Plot.Border.LineOption = loNone

        ' Crosshairs
        .CrossHairs.YCoordInLegend = True
        .CrossHairs.Color = vbWhite
        .CrossHairs.Width = woTwoPoint
        .CrossHairs.CoordsBackcolor = RGB(64, 64, 128)
        .CrossHairs.HorizontalVisible = False
        .CrossHairs.VerticalVisible = True

        ' Profile
        .Profile(1).YScale = 1
        .Profile(1).LineOption = loCustom
        .Profile(1).LineWidth = woThreePoint
        .Profile(1).LineStyle = soSolid
        .Profile(1).MarkerOption = loNone
        .Profile(1).LineColor = RGB(255, 0, 0)
        .Profile(1).Label = "Demand"
        .Profile(1).NumSamples = 288

        ' Get data from csv file
        Dim Data() As Double
        GetDataFromFile Data, "Data8.txt", 288, 2
        .ChartData = Data

        .Refresh

    End With

End Sub


' Resize Event

Private Sub Form_Resize()
    With XYChart4Ctl1
    
    .Left = 0
    
    .Top = 0
    
    .Width = Me.ScaleWidth
    
    .Height = Me.ScaleHeight
    
    .Refresh
    End With
End Sub


Private Sub XYChart4Ctl1_ZoomInComplete()
    SetXScaleDateTimeFormat
End Sub


Private Sub XYChart4Ctl1_ZoomOutComplete()
    SetXScaleDateTimeFormat
End Sub


Private Sub XYChart4Ctl1_ZoomOutAllComplete()
    SetXScaleDateTimeFormat
End Sub


Private Sub SetXScaleDateTimeFormat()
    ' DateTime Formatting
    Dim strDateTimeFormat As String
    With XYChart4Ctl1
        If .XScale(1).FormatStyle = fsDateTime Then
            strDateTimeFormat = .XScale(1).DateTimeFormat
            If (.MaxXValue(1) - .MinXValue(1)) <= 1 / 24 / 60 / 60 * 10 Then
                ' X Scale range <= 10 seconds
                If InStr(strDateTimeFormat, ".fff") = 0 Then
                    strDateTimeFormat = Replace(strDateTimeFormat, ":ss", ":ss.fff")
                    .XScale(1).DateTimeFormat = strDateTimeFormat
                End If
            Else
                ' X Scale range > 10 seconds
                strDateTimeFormat = Replace(strDateTimeFormat, ":ss.fff", ":ss")
                .XScale(1).DateTimeFormat = strDateTimeFormat
            End If
            .Redraw
        End If
    End With
End Sub


' Get data from csv file

Private Sub GetDataFromFile(ByRef DataArray() As Double, FileName As String, Rows As Integer, Cols As Integer)
    Dim FilePathAndName As String
    FilePathAndName = App.Path + "\" + FileName

    Dim iFileNumber As Integer
    iFileNumber = FreeFile

    Open FilePathAndName For Input As #iFileNumber

    ReDim DataArray(1 To Rows, 1 To Cols)

    Dim Row As Integer, Col As Integer
    For Row = 1 To Rows
        For Col = 1 To Cols
            Input #iFileNumber, DataArray(Row, Col)
        Next Col
    Next Row

    Close #iFileNumber
End Sub