首页 > 解决方案 > 带渐近线的 VB 图表

问题描述

到目前为止,我已经创建了一个功能正常的余弦和正弦图,但是 tan 不起作用,因为它依赖于渐近线,我不太确定如何使用 VB 使用图表,因为图表将每个点输出为连接为一行而不是单独的行。如果有人对此有任何经验,我将不胜感激,因为我在网上找不到任何可以进一步帮助我的东西。

  For x As Integer = n To v Step 1
            Dim y As Single
            y = Math.Tan(x / 57.3)
            '57.3 is an approximation for pi/180
            Chart1.Series("plot4").Points.AddXY(x, y)
        Next
    End Sub

标签: vb.netcharts

解决方案


如果您可以检测到不连续性,则可以添加一个将.IsEmpty属性设置为 True 的数据点。该点不会被绘制,在图中给出一个中断:

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

    Sub DrawGraph()

        Chart1.Series.Clear()

        Dim pts As New Series("tan") With {.ChartType = SeriesChartType.Line}

        Dim nPoints = 541
        Dim xMin = 0.0
        Dim xMax = Math.PI * 3
        Dim xInc = (xMax - xMin) / nPoints

        For i = 0 To nPoints - 1
            Dim x = xMin + i * xInc
            Dim y = Math.Tan(x)
            If i > 0 Then
                ' Check if the sign of y has changed and its value is large enough to suggest a discontinuity
                If Math.Sign(pts.Points(i - 1).YValues(0)) = -Math.Sign(y) AndAlso Math.Abs(y) > 10 Then
                    pts.Points.Add(New DataPoint(x - xInc / 2, 0) With {.IsEmpty = True})
                End If
            End If

            pts.Points.AddXY(x, y)

        Next

        Chart1.Series.Add(pts)
        Chart1.ChartAreas(0).AxisY.Minimum = -50
        Chart1.ChartAreas(0).AxisY.Maximum = 50

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DrawGraph()
    End Sub

End Class

给出这样的东西:

在此处输入图像描述

(请原谅这张图表的粗俗。我没有时间很好地缩放它或给它上色。)


参考灵感:c# chart non Continuous


推荐阅读