Author Topic: Stylizing the basicTrendChart  (Read 5584 times)

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Stylizing the basicTrendChart
« Reply #15 on: February 12, 2015, 10:03:23 PM »
Thanks for sharing.

I have tweaked it a bit more with original BackColor, DoubleBuffering and 3 new properties (DrawRightToLeft, GraphLineColor and GraphLineThickness) and here is the code:

Code: [Select]
Imports System
Imports System.ComponentModel

Public Class BasicTrendChartEx
    Inherits BasicTrendChart

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        '* Fill in the background color
        Me.DoubleBuffered = True  'Reduce or eliminate flicker
        e.Graphics.FillRectangle(New SolidBrush(BackColor), 0, 0, Me.Width, Me.Height)

        Dim x1, y1 As Integer
        Dim x2, y2 As Integer

        '* Draw the line connecting the points
        If Points.Count > 1 Then
            Dim index As Integer = 0
            While index < (Points.Count - 2)
                Try
                    '* calculate the x and y coordinates based on the Points collection

                    If DrawRightToLeft = True Then
                        x1 = Me.Width - Convert.ToInt32((Me.Width / MaxPoints) * index)
                        x2 = Me.Width - Convert.ToInt32((Me.Width / MaxPoints) * (index + 1))
                    Else
                        x1 = Convert.ToInt32((Me.Width / MaxPoints) * index)
                        x2 = Convert.ToInt32((Me.Width / MaxPoints) * (index + 1))
                        '*^ this was y1
                    End If
                    y1 = Me.Height - Convert.ToInt32((Me.Height / (YMaximum - YMinimum)) * (Points(index) - YMinimum))
                    y2 = Me.Height - Convert.ToInt32((Me.Height / (YMaximum - YMinimum)) * (Points(index + 1) - YMinimum))

                    e.Graphics.DrawLine(New Pen(GraphLineColor, GraphLineThickness), x1, y1, x2, y2)

                Catch ex As Exception
                    Dim dbg = 0
                End Try
                index += 1
            End While
            e.Graphics.DrawString(Now.ToString("dd-MMM-yy HH:mm:ss"), New Drawing.Font("Arial", 10), System.Drawing.Brushes.White, Me.Width - 116, Me.Height - 16)
            e.Graphics.DrawString(Points.Last.ToString, New Drawing.Font("Arial", 10), System.Drawing.Brushes.White, Me.Width - 40, 0)
        End If
        '* Draw the Axis
        e.Graphics.DrawLine(System.Drawing.Pens.White, 0, 0, 0, Me.Height)
        e.Graphics.DrawLine(System.Drawing.Pens.White, 0, Me.Height - 1, Me.Width, Me.Height - 1)
        e.Graphics.DrawString(YMaximum.ToString, New Drawing.Font("Arial", 10.0!), System.Drawing.Brushes.White, 0.0!, 0.0!)
        '*                    ^^^^^^^^^^^^^^^ this was MaxValue
        e.Graphics.DrawString(YMinimum.ToString, New Drawing.Font("Arial", 10), System.Drawing.Brushes.White, 0, Me.Height - 16)
    End Sub

    Public Property DrawRightToLeft As Boolean = False

    <DefaultValue(GetType(Color), "Black")> _
    Public Overridable Shadows Property BackColor() As Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal value As Color)
            MyBase.BackColor = value
            Refresh()
        End Set
    End Property

    Private _GraphLineColor As Drawing.Color = Drawing.Color.Blue
    Public Property GraphLineColor() As Drawing.Color
        Get
            Return _GraphLineColor
        End Get
        Set(ByVal value As Drawing.Color)
            _GraphLineColor = value
        End Set
    End Property

    Private _gLineThickness As Single = 1
    Public Property GraphLineThickness() As Single
        Get
            Return _gLineThickness
        End Get
        Set(ByVal value As Single)
            If Not IsNumeric(value) Then
                MsgBox("Valid values are numbers 1 to 3!")
                _gLineThickness = 1
                Exit Property
            Else
                If (value > 3) OrElse (value < 1) Then
                    MsgBox("Valid values are numbers 1 to 3!")
                    _gLineThickness = 1
                    Exit Property
                End If
            End If
            _gLineThickness = value
        End Set
    End Property

End Class


It also shows Date/Time and the last Value.
« Last Edit: February 14, 2015, 06:11:05 PM by Godra »