none
Plot the X-Y Graph(Line Graph) in Clockwise Direction using VB .Net 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. Now i want to plot the 360 points in clockwise direction that is from(Quadrant- I, IV, III, II). Currently i have plotted the points in anticlockwise direction. 

    For reference, attached with screenshot of application and code.Anticlockwise Graph Plot(By default)Clockwise Plot

    Imports System.Windows.Forms.DataVisualization.Charting Public Class Form2 Public App As New zenOn.Application Public Prj As zenOn.Project Dim myarray(360) As Integer Dim maxyarray(360) As Double Dim xarray(360) As Double Dim yarray(360) As Double Dim position As Integer = 0 Dim i As Integer Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Prj = App.Projects.Item(0) Chart1.Series("Series5").SmartLabelStyle.Enabled = False Chart1.ChartAreas("ChartArea1").AxisX.Minimum = -360 Chart1.ChartAreas("ChartArea1").AxisX.Maximum = 360 Chart1.ChartAreas("ChartArea1").AxisX.Interval = 40 Chart1.ChartAreas("ChartArea1").AxisY.Minimum = -360 Chart1.ChartAreas("ChartArea1").AxisY.Maximum = 360 Chart1.ChartAreas("ChartArea1").AxisY.Interval = 40 Chart1.ChartAreas("ChartArea1").AxisX.Crossing = 0 Chart1.ChartAreas("ChartArea1").AxisY.Crossing = 0 Chart1.Series("Series3").Points.AddXY(0, 0) Chart1.Series("Series5").Points.Clear() For i = 0 To 359 Prj.Variables.Item("radius[" + CStr(i) + "]").Value = Prj.Variables.Item("radius[" + CStr(i) + "]").Value + (Val(TextBox5.Text) / 100) myarray(i) = Prj.Variables.Item("radius[" + CStr(i) + "]").Value xarray(i) = myarray(i) * Math.Cos((i * Math.PI / 180)) yarray(i) = myarray(i) * Math.Sin((i * Math.PI / 180)) Chart1.Series("Series5").Points.AddXY(xarray(i), yarray(i)) Me.TopMost = False Chart1.Update() Dim maxDataPoint As DataPoint = Chart1.Series("Series5").Points.FindMaxByValue() TextBox3.Text = maxDataPoint.ToString Dim minDataPoint As DataPoint = Chart1.Series("Series5").Points.FindMinByValue() TextBox4.Text = minDataPoint.ToString Next Dim myTextBoxArray As TextBox() = ControlArrayUtils.getControlArray(Me, "myTextBox") For i = 0 To 18 If i = 0 Then myTextBoxArray(i).Text = Chart1.ChartAreas(0).AxisY.Minimum Else myTextBoxArray(i).Text = myTextBoxArray(i - 1).Text + 40 End If Next Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Enabled = False 'Chart1.ChartAreas(0).AxisX.ScaleView.Zoomable = True 'Chart1.ChartAreas(0).AxisY.ScaleView.Zoomable = True End Sub 'Apply Button Click Event Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click If TextBox1.Text <> "" And TextBox2.Text <> "" Then If Val(TextBox2.Text) > Val(TextBox1.Text) Then MessageBox.Show("Minimum Value is Greater then Maximum Value ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information) Else Chart1.Series("Series2").Points.Clear() Chart1.Series("Series1").Points.Clear() Chart1.Series("Series4").Points.Clear() For i = 0 To 359 'Minimum circle Dim x As Double = Val(TextBox2.Text) * Math.Cos((i * Math.PI) / 180) Dim y As Double = Val(TextBox2.Text) * Math.Sin((i * Math.PI) / 180) Chart1.Series("Series2").Points.AddXY(x, y) Next For i = 0 To 359 'Maximum circle Dim x As Double = Val(TextBox1.Text) * Math.Cos((i * Math.PI) / 180) Dim y As Double = Val(TextBox1.Text) * Math.Sin((i * Math.PI) / 180) Chart1.Series("Series1").Points.AddXY(x, y) Next For i = 0 To 359 'Average circle Dim calcavg As Integer calcavg = ((Val(TextBox1.Text) - Val(TextBox2.Text)) / 2) + Val(TextBox2.Text) Dim x As Double = calcavg * Math.Cos((i * Math.PI) / 180) Dim y As Double = calcavg * Math.Sin((i * Math.PI) / 180) Chart1.Series("Series4").Points.AddXY(x, y) Next End If End If For j = 0 To 359 If ((myarray(j) > Val(TextBox1.Text))) Then Chart1.Series("Series6").Points.AddXY(xarray(j), yarray(j)) ' Hightlight the points with red color ElseIf ((myarray(j) < Val(TextBox2.Text))) Then Chart1.Series("Series7").Points.AddXY(xarray(j), yarray(j)) End If Chart1.Series("Series5").Points.AddXY(xarray(j), yarray(j)) Chart1.Series("Series5").Label = myarray(j).ToString Next End Sub

    'Timer1 tick click event Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick Chart1.Series("Series5").Points.Clear() For i = 0 To 359 myarray(i) = Prj.Variables.Item("radius[" + CStr(i) + "]").Value + Val(TextBox5.Text) xarray(i) = myarray(i) * Math.Cos((i * Math.PI / 180)) yarray(i) = myarray(i) * Math.Sin((i * Math.PI / 180)) Next For k = 0 To 359 myarray(k) = Prj.Variables.Item("radius[" + CStr(k) + "]").Value Next

    Timer1.Stop() End Sub 'Recalculate button click event Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim newy As Double = Chart1.ChartAreas("ChartArea1").AxisY.ScaleView.Position + 10 Chart1.ChartAreas("ChartArea1").AxisY.Crossing = Val(TextBox5.Text) Chart1.Series("Series5").Points.Clear() For i = 0 To 359 myarray(i) = Prj.Variables.Item("radius[" + CStr(i) + "]").Value + Val(TextBox5.Text) xarray(i) = myarray(i) * Math.Cos((i * Math.PI / 180)) yarray(i) = myarray(i) * Math.Sin((i * Math.PI / 180)) Next For j = 0 To 359 Chart1.Series("Series5").Points.AddXY(xarray(j), yarray(j)) Next Dim myTextBoxArray As TextBox() = ControlArrayUtils.getControlArray(Me, "myTextBox") For i = 0 To 18 If i = 0 Then myTextBoxArray(i).Text = Chart1.ChartAreas(0).AxisY.Minimum Else myTextBoxArray(i).Text = myTextBoxArray(i - 1).Text + 40 End If Next For j = 0 To 18 myTextBoxArray(j).Text = myTextBoxArray(j).Text - Val(TextBox5.Text) Next If TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox5.Text <> "" Then Chart1.Series("Series2").Points.Clear() Chart1.Series("Series1").Points.Clear() Chart1.Series("Series4").Points.Clear() TextBox1.Text = ((Val(TextBox1.Text)) + Val(TextBox1.Text) * (Val(TextBox5.Text) / 100)) TextBox2.Text = ((Val(TextBox2.Text)) + Val(TextBox2.Text) * (Val(TextBox5.Text) / 100)) Dim j As Integer For j = 0 To 359 Dim x As Double = (Val(TextBox2.Text) + Val(TextBox5.Text)) * Math.Cos((j * Math.PI) / 180) Dim y As Double = (Val(TextBox2.Text) + Val(TextBox5.Text)) * Math.Sin((j * Math.PI) / 180) Chart1.Series("Series2").Points.AddXY(x, y) Next For j = 0 To 359 Dim x As Double = (Val(TextBox1.Text) + Val(TextBox5.Text)) * Math.Cos((j * Math.PI) / 180) Dim y As Double = (Val(TextBox1.Text) + Val(TextBox5.Text)) * Math.Sin((j * Math.PI) / 180) Chart1.Series("Series1").Points.AddXY(x, y) Next For j = 0 To 359 Dim calcavg As Integer calcavg = ((Val(TextBox1.Text) - Val(TextBox2.Text)) / 2) + Val(TextBox2.Text) calcavg = calcavg + Val(TextBox5.Text) Dim x As Double = calcavg * Math.Cos((j * Math.PI) / 180) Dim y As Double = calcavg * Math.Sin((j * Math.PI) / 180) Chart1.Series("Series4").Points.AddXY(x, y) Next End If Chart1.Series("Series6").Points.Clear() Chart1.Series("Series7").Points.Clear() For j = 0 To 359 If ((myarray(j) > Val(TextBox1.Text))) Then Chart1.Series("Series6").Points.AddXY(xarray(j), yarray(j)) ' Hightlight the points with red color ElseIf ((myarray(j) < Val(TextBox2.Text))) Then Chart1.Series("Series7").Points.AddXY(xarray(j), yarray(j)) End If Chart1.Series("Series5").Points.AddXY(xarray(j), yarray(j)) Chart1.Series("Series5").Label = myarray(j).ToString Next End Sub Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick For i = 0 To 359 If myarray(i) = Prj.Variables.Item("radius[" + CStr(i) + "]").Value Then 'Timer1.Stop() Else myarray(i) = Prj.Variables.Item("radius[" + CStr(i) + "]").Value Prj.Variables.Item("BoolVariable").Value = 1 Timer1.Start() End If Next End Sub Private Sub Chart1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, Chart1.MouseMove Dim result As HitTestResult = Chart1.HitTest(e.X, e.Y) If (result.ChartElementType = ChartElementType.DataPoint) Then Chart1.Series("Series5").Points(result.PointIndex).Label = myarray(result.PointIndex) Chart1.Series("Series5").Points(result.PointIndex).LabelForeColor = Color.Black Chart1.Series("Series5").ToolTip = myarray(result.PointIndex) End If End Sub Private Sub Chart1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles Chart1.MouseEnter Me.Chart1.Focus() End Sub End Class



    Tell me the solution how to do this.

    Regards 

    Rushali


    • Edited by rushuMT Monday, January 25, 2016 9:32 AM
    Monday, January 25, 2016 9:25 AM

Answers

  • @Acamar:- Hello sir, i have tried with changing the loop from 359 to 0, but it not changes the direction and  not getting desired output. Simply i want to change the starting point of the graph. 

    Do help me.

    Thanks & Regards

    Rushali

    Here are a couple ways to change it.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form5
        Friend WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 150, .Enabled = True}
    
        'create the data
        Private MyData() As Double = {2.0, 6, 1, 4.5, 6, 7, 5, 4, 6, 3, 1, 3, 2.0}
        Private MaxAngle As Double
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Sample Chart"
    
            'setup the chart
            With Chart1.ChartAreas(0)
                .AxisX.Title = "X"
                .AxisX.MajorGrid.LineColor = Color.LightBlue
                .AxisX.Minimum = -10
                .AxisX.Maximum = 10
                .AxisX.Interval = 2
                .AxisX.Crossing = 0
    
                .AxisY.Title = "Y"
                .AxisY.MajorGrid.LineColor = Color.LightGray
                .AxisY.Minimum = -10
                .AxisY.Maximum = 10
                .AxisY.Interval = 2
                .AxisY.Crossing = 0
    
                .BackColor = Color.FloralWhite
                .BackSecondaryColor = Color.White
                .BackGradientStyle = GradientStyle.HorizontalCenter
                .BorderColor = Color.Blue
                .BorderDashStyle = ChartDashStyle.Solid
                .BorderWidth = 1
                .ShadowOffset = 2
            End With
    
            RadioButton1.Checked = True
    
        End Sub
    
        Private Sub DrawChart()
            Chart1.Series.Clear()
            Chart1.Series.Add("Y = f(x)")
            With Chart1.Series(0)
                .ChartType = DataVisualization.Charting.SeriesChartType.Line
                .IsVisibleInLegend = False
                .BorderWidth = 1
                .Color = Color.Red
                .BorderDashStyle = ChartDashStyle.Dash
                .MarkerStyle = DataVisualization.Charting.MarkerStyle.Square
                .MarkerSize = 4
    
                Dim x, y, t, t2 As Double
    
                For theta = 0 To MaxAngle Step 30
    
                    If RadioButton1.Checked Then
                        'plot normal counterclockwise
                        x = MyData(theta / 30) * Math.Cos(theta / 57.3)
                        y = MyData(theta / 30) * Math.Sin(theta / 57.3)
                    ElseIf RadioButton2.Checked Then
                        'rotate all points 90 degree counterclockwise
                        t = theta + 90
                        If t > 360 Then t -= 360
                        x = MyData(theta / 30) * Math.Cos(t / 57.3)
                        y = MyData(theta / 30) * Math.Sin(t / 57.3)
                    ElseIf RadioButton3.Checked Then
                        'reverse plot clockwise
                        t = 360 - theta
                        If t < 0 Then t += 360
                        x = MyData(theta / 30) * Math.Cos(t / 57.3)
                        y = MyData(theta / 30) * Math.Sin(t / 57.3)
                    Else
                        'both
                        t = 360 - theta
                        If t < 0 Then t += 360
                        t2 = t + 90
                        If t2 > 360 Then t2 -= 360
                        x = MyData(t / 30) * Math.Cos(t2 / 57.3)
                        y = MyData(t / 30) * Math.Sin(t2 / 57.3)
                    End If
    
                    .Points.AddXY(x, y)
    
                Next
            End With
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            MaxAngle = 0
            DrawChart()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            MaxAngle += 30
            If MaxAngle > 360 Then
                MaxAngle = 0
            End If
            DrawChart()
        End Sub
    End Class

    Thursday, January 28, 2016 1:01 PM

All replies

  • Perhaps I don't understand the question, but, cant you just reverse your for loops?

            For i = 359 To 0 step -1

            next   

    Monday, January 25, 2016 2:47 PM
  • @tommytwotrain:- Hello sir, I want to plot the points in clockwise direction as from quadrant I, IV,III,II. Currently it plot the points in anticlockwise direction as from quadrant I, II, III, IV.  In my windows application, i have plotted the x,y values and for that i have input the radius and theta(0 to 360) which is constant, from that i have calculate the values of x and y and plot the points on the graph. Now i want to plot the points in clockwise direction as i shown in second screenshot.

    Please do help me.

    Thanks & Regards

    Rushali

    Thursday, January 28, 2016 4:23 AM
  • I want to plot the points in clockwise direction as from quadrant I, IV,III,II. Currently it plot the points in anticlockwise direction as from quadrant I, II, III, IV.

    Your two screenshots are the same, other than for the circle you have drawn indicating clockwise and anticlockwise.  So it is not possible to tell from those images what is the change that you want to make.

    What exactly do you mean by the 'direction' of plotting?  If it is the sequence in which the points are actually placed on the chart, then change the direction of your for/next loops as suggested. If it's something else, provide a more complete description.

    Thursday, January 28, 2016 5:09 AM
  • @Acamar:- Hello Sir,

    For reference, again i am adding two screenshot to show you where i want to change. In First screenshot, i have shown the direction of points which is currently plotted on the graph i.e in Anticlockwise direction. The arrow shown in the screenshots where it plot the last point and i want to plot the points in clockwise direction which i have shown in second screenshot.

    Please do refer and do help me.

    Thanks & Regards

    Rushali

    Thursday, January 28, 2016 5:32 AM
  • In First screenshot, i have shown the direction of points which is currently plotted on the graph i.e in Anticlockwise direction. The arrow shown in the screenshots where it plot the last point and i want to plot the points in clockwise direction which i have shown in second screenshot.

    Your screenshots look the same to me, so I can only conclude that it is the sequence in which the points are plotted that you are referring to, in which case you should try the suggestion already provided of changing the direction of your for/next loops.

    Thursday, January 28, 2016 6:34 AM
  • @Acamar:- Hello sir, i have tried with changing the loop from 359 to 0, but it not changes the direction and  not getting desired output. Simply i want to change the starting point of the graph. 

    Do help me.

    Thanks & Regards

    Rushali

    Thursday, January 28, 2016 6:55 AM
  • Simply i want to change the starting point of the graph.

    What does this mean?  What do you want to change it to?

    Do you mean that you want to change the position in the graph at which the drawing of the graph starts, or do you mean that you want to rotate the graph around the (0,0) point?

    If you have made a change that didn't produce the desired output, you should show what changed and explained why it was different than what you expected.

    Thursday, January 28, 2016 7:37 AM
  • @Acamar:- Hello sir, i have tried with changing the loop from 359 to 0, but it not changes the direction and  not getting desired output. Simply i want to change the starting point of the graph. 

    Do help me.

    Thanks & Regards

    Rushali

    Here are a couple ways to change it.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form5
        Friend WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 150, .Enabled = True}
    
        'create the data
        Private MyData() As Double = {2.0, 6, 1, 4.5, 6, 7, 5, 4, 6, 3, 1, 3, 2.0}
        Private MaxAngle As Double
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Sample Chart"
    
            'setup the chart
            With Chart1.ChartAreas(0)
                .AxisX.Title = "X"
                .AxisX.MajorGrid.LineColor = Color.LightBlue
                .AxisX.Minimum = -10
                .AxisX.Maximum = 10
                .AxisX.Interval = 2
                .AxisX.Crossing = 0
    
                .AxisY.Title = "Y"
                .AxisY.MajorGrid.LineColor = Color.LightGray
                .AxisY.Minimum = -10
                .AxisY.Maximum = 10
                .AxisY.Interval = 2
                .AxisY.Crossing = 0
    
                .BackColor = Color.FloralWhite
                .BackSecondaryColor = Color.White
                .BackGradientStyle = GradientStyle.HorizontalCenter
                .BorderColor = Color.Blue
                .BorderDashStyle = ChartDashStyle.Solid
                .BorderWidth = 1
                .ShadowOffset = 2
            End With
    
            RadioButton1.Checked = True
    
        End Sub
    
        Private Sub DrawChart()
            Chart1.Series.Clear()
            Chart1.Series.Add("Y = f(x)")
            With Chart1.Series(0)
                .ChartType = DataVisualization.Charting.SeriesChartType.Line
                .IsVisibleInLegend = False
                .BorderWidth = 1
                .Color = Color.Red
                .BorderDashStyle = ChartDashStyle.Dash
                .MarkerStyle = DataVisualization.Charting.MarkerStyle.Square
                .MarkerSize = 4
    
                Dim x, y, t, t2 As Double
    
                For theta = 0 To MaxAngle Step 30
    
                    If RadioButton1.Checked Then
                        'plot normal counterclockwise
                        x = MyData(theta / 30) * Math.Cos(theta / 57.3)
                        y = MyData(theta / 30) * Math.Sin(theta / 57.3)
                    ElseIf RadioButton2.Checked Then
                        'rotate all points 90 degree counterclockwise
                        t = theta + 90
                        If t > 360 Then t -= 360
                        x = MyData(theta / 30) * Math.Cos(t / 57.3)
                        y = MyData(theta / 30) * Math.Sin(t / 57.3)
                    ElseIf RadioButton3.Checked Then
                        'reverse plot clockwise
                        t = 360 - theta
                        If t < 0 Then t += 360
                        x = MyData(theta / 30) * Math.Cos(t / 57.3)
                        y = MyData(theta / 30) * Math.Sin(t / 57.3)
                    Else
                        'both
                        t = 360 - theta
                        If t < 0 Then t += 360
                        t2 = t + 90
                        If t2 > 360 Then t2 -= 360
                        x = MyData(t / 30) * Math.Cos(t2 / 57.3)
                        y = MyData(t / 30) * Math.Sin(t2 / 57.3)
                    End If
    
                    .Points.AddXY(x, y)
    
                Next
            End With
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            MaxAngle = 0
            DrawChart()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            MaxAngle += 30
            If MaxAngle > 360 Then
                MaxAngle = 0
            End If
            DrawChart()
        End Sub
    End Class

    Thursday, January 28, 2016 1:01 PM