none
Highlight The Chart Area of X-Y Chart using VB RRS feed

  • Question

  • Hello,

    I have designed one windows chart application in vb .net for that i have used line graph to plot 360 values. In that i have input the value of theta (0 to 360) and Radius. From that i have calculate the values of x and y. After that i have plotted it on the graph. I have also drawn the maximum, minimum and average values circle on the graph. Now i want to show the chart area of line graph with different colour which will go outside of the maximum circle. 

    For example- Maximum value = 270 and Minimum value = 90 if it is exceed above 270 then it will highlight with Chart Area with red different color .

    For that i have used, range series but it will highlight all the area.

    For reference, attached with screenshot of application and expected output of the application, code.

    Screenshot of application

    Expected output

    Imports System.Windows.Forms.DataVisualization.Charting
    Imports System.ComponentModel
    
    Public Class Form1
        Public App As New zenOn.Application
        Public Prj As zenOn.Project
        Public OriginDP As Double = 0.0
        Public scalefactor As Double
        Public meancaalcurve001 As Double = 0.0
        Public meancaalcurve002 As Double = 0.0
        Public myarray(360) As Integer
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Try
    
                Control.CheckForIllegalCrossThreadCalls = False         'Cross Thread Error Solution
    
                Prj = App.Projects.Item(0)                              'Connection with Zenon Runtime
                Me.ScanZenon.RunWorkerAsync()
    
                'To Enable Four Quandrants in Line Chart
                Chart1.ChartAreas("ChartArea1").AxisX.Minimum = -360
                Chart1.ChartAreas("ChartArea1").AxisX.Maximum = 360
                Chart1.ChartAreas("ChartArea1").AxisX.Interval = 45
                Chart1.ChartAreas("ChartArea1").AxisY.Minimum = -360
                Chart1.ChartAreas("ChartArea1").AxisY.Maximum = 360
                Chart1.ChartAreas("ChartArea1").AxisY.Interval = 45
                Chart1.ChartAreas("ChartArea1").AxisX.Crossing = 0
                Chart1.ChartAreas("ChartArea1").AxisY.Crossing = 0
                Chart1.Series("AxisSeries").Points.AddXY(0, 0)
    
                Dim t, t2, x, y, x1, y1, x2, y2, x3, y3, x4, y4, min, max, avg As Double
                min = 90
                avg = 180
                max = 270
    
                Chart1.Series("MinSeries").Points.Clear()
                Chart1.Series("MaxSeries").Points.Clear()
                Chart1.Series("AvgSeries").Points.Clear()
    
                'Min Circle
                For i = 1 To 360
                    t = 360 - i
                    If t < 0 Then t += 360
                    t2 = t + 90
                    If t2 > 360 Then t2 -= 360
                    x = min * Math.Cos((t2 / 57.3))
                    y = min * Math.Sin((t2 / 57.3))
                    Chart1.Series("MinSeries").Points.AddXY(x, y) 'For Mininum Value Circle
                Next
                'Max Circle
                For i = 1 To 360
                    t = 360 - i
                    If t < 0 Then t += 360
                    t2 = t + 90
                    If t2 > 360 Then t2 -= 360
                    x1 = max * Math.Cos((t2 / 57.3))
                    y1 = max * Math.Sin((t2 / 57.3))
                    Chart1.Series("MaxSeries").Points.AddXY(x1, y1) 'For Maximum Value Circle
                Next
                'Avg Circle
                For i = 1 To 360
                    t = 360 - i
                    If t < 0 Then t += 360
                    t2 = t + 90
                    If t2 > 360 Then t2 -= 360
                    x2 = avg * Math.Cos((t2 / 57.3))
                    y2 = avg * Math.Sin((t2 / 57.3))
                    Chart1.Series("AvgSeries").Points.AddXY(x2, y2) 'For Average Value Circle
                Next
                'Mean Calculated Curve - 1
                meancaalcurve001 = (avg + max) / 2
                For i = 1 To 360
                    t = 360 - i
                    If t < 0 Then t += 360
                    t2 = t + 90
                    If t2 > 360 Then t2 -= 360
                    x3 = meancaalcurve001 * Math.Cos((t2 / 57.3))
                    y3 = meancaalcurve001 * Math.Sin((t2 / 57.3))
                    Chart1.Series("MeanCalCurve001").Points.AddXY(x3, y3)
                Next
                'Mean Calculated Curve - 2
                meancaalcurve002 = (avg + min) / 2
                For i = 1 To 360
                    t = 360 - i
                    If t < 0 Then t += 360
                    t2 = t + 90
                    If t2 > 360 Then t2 -= 360
                    x4 = meancaalcurve002 * Math.Cos((t2 / 57.3))
                    y4 = meancaalcurve002 * Math.Sin((t2 / 57.3))
                    Chart1.Series("MeanCalCurve002").Points.AddXY(x4, y4)
                Next
    
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
        'Apply Button Click Event
        Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
            Try
                Dim min As Double
                Dim max As Double
                Dim avg As Double
    
                If Not String.IsNullOrEmpty(TextBox2.Text) And Not String.IsNullOrEmpty(TextBox4.Text) Then
                    avg = Convert.ToDouble(TextBox2.Text)
                    scalefactor = (Convert.ToDouble(TextBox2.Text) * (Convert.ToDouble(TextBox4.Text) / 100))
                    max = Convert.ToDouble(TextBox2.Text) + (Convert.ToDouble(TextBox2.Text) * (Convert.ToDouble(TextBox4.Text) / 100))
                    TextBox1.Text = max.ToString()
                    min = Convert.ToDouble(TextBox2.Text) - (Convert.ToDouble(TextBox2.Text) * (Convert.ToDouble(TextBox4.Text) / 100))
                    TextBox3.Text = min.ToString()
                    OriginDP = min - scalefactor
                End If
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
        'Plot Button Click Event
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                Chart1.Series("PointSeries").Points.Clear()
                Chart1.Series("HiglightSeries").Points.Clear()
                Dim i As Integer
                For i = 1 To 360
                    myarray(i - 1) = Prj.Variables.Item("Profile_RD[" + CStr(i) + "]").Value
                Next
    
                Dim count As Integer = myarray.Count
                Dim t, t2, x, y As Double
                For i = 0 To 359
                    t = 360 - i
                    If t < 0 Then t += 360
                    t2 = t + 90
                    If t2 > 360 Then t2 -= 360
                    myarray(i) = (myarray(i) * (90 / scalefactor)) - (OriginDP * (90 / scalefactor))
                    x = myarray(i) * Math.Cos((t2 / 57.3))
                    y = myarray(i) * Math.Sin((t2 / 57.3))
                    If myarray(i) > meancaalcurve001 And myarray(i) >= 89 Then
                        Chart1.Series("HiglightSeries").Points.AddXY(x, y)
                        Chart1.Series("PointSeries").Points.AddXY(x, y)
                    ElseIf myarray(i) < meancaalcurve002 And myarray(i) >= 89 Then
                        Chart1.Series("HiglightSeries").Points.AddXY(x, y)
                        Chart1.Series("PointSeries").Points.AddXY(x, y)
                    ElseIf myarray(i) >= 89 Then
                        Chart1.Series("PointSeries").Points.AddXY(x, y)
                    End If
    
                    'If myarray(i) > meancaalcurve001 Then
                    '    Chart1.Series("HiglightSeries").Points.AddXY(x, y)
                    '    Chart1.Series("PointSeries").Points.AddXY(x, y)
                    'ElseIf myarray(i) < meancaalcurve002 Then
                    '    Chart1.Series("HiglightSeries").Points.AddXY(x, y)
                    '    Chart1.Series("PointSeries").Points.AddXY(x, y)
                    'Else
                    '    Chart1.Series("PointSeries").Points.AddXY(x, y)
                    'End If
                Next
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
        Dim status As Boolean = True
        Private Sub ScanZenon_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles ScanZenon.DoWork
            Try
                While status = True
                    TextBox1.Invoke(DirectCast(Sub() TextBox1.Text = Prj.Variables.Item("Profile_Max").Value, Action))
                    TextBox2.Invoke(DirectCast(Sub() TextBox2.Text = Prj.Variables.Item("Profile_2_Average").Value, Action))
                    TextBox3.Invoke(DirectCast(Sub() TextBox3.Text = Prj.Variables.Item("Profile_Min").Value, Action))
                    TextBox4.Invoke(DirectCast(Sub() TextBox4.Text = Prj.Variables.Item("Pro_zoom").Value, Action))
                    Button8.PerformClick()
                    Threading.Thread.Sleep(5000)
                End While
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
    
        Private Sub Form1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
            Try
                GC.Collect()
                ScanZenon.Dispose()
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
        'Edit Button
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Try
                TextBox2.ReadOnly = False
                TextBox4.ReadOnly = False
                status = False
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
        'Update
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Try
                Dim min As Double
                Dim max As Double
                Dim avg As Double
    
                If Not String.IsNullOrEmpty(TextBox2.Text) And Not String.IsNullOrEmpty(TextBox4.Text) Then
                    If TextBox2.Text >= 0 And TextBox2.Text <= 2000 Then
                        avg = Convert.ToDouble(TextBox2.Text)
                        scalefactor = (Convert.ToDouble(TextBox2.Text) * (Convert.ToDouble(TextBox4.Text) / 100))
                        max = Convert.ToDouble(TextBox2.Text) + (Convert.ToDouble(TextBox2.Text) * (Convert.ToDouble(TextBox4.Text) / 100))
                        TextBox1.Text = max.ToString()
                        min = Convert.ToDouble(TextBox2.Text) - (Convert.ToDouble(TextBox2.Text) * (Convert.ToDouble(TextBox4.Text) / 100))
                        TextBox3.Text = min.ToString()
                        OriginDP = min - scalefactor
                    Else
                        MessageBox.Show("PLEASE ENTER THE VALUE BETWEEN 0 TO 2000")
                    End If
                End If
    
                Prj.Variables.Item("Profile_2_Average").Value = TextBox2.Text.Trim()
                Prj.Variables.Item("Pro_zoom").Value = TextBox4.Text.Trim()
                TextBox2.ReadOnly = True
                TextBox4.ReadOnly = True
            Catch ex As Exception
                Dim errormsg As String
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class
    

    Please do needful.

    regards

    rushuMT

    Wednesday, October 11, 2017 1:58 PM

All replies