Visual Studio.net 图表错误

Visual Studio .NET Chart Bug

提问人:sickboy 提问时间:9/25/2023 最后编辑:Panagiotis Kanavossickboy 更新时间:9/25/2023 访问量:37

问:

这篇文章是关于我认为是 VS 错误的内容。 问题出在当我将包含这些列的单行查询结果作为 DataSource 加载时图表提供的视图

  • 日期 MYDATE
  • 双重负债
  • 双倍保证金
  • 双NET_INTEREST

我的堆积柱形图中有 3 个系列,但是当我加载新的 DataSource(单行时)时,我看到

this

而不是

this

由于我找不到关于这种不便的合理解释,我想问你是否曾经经历过类似的事情。

另外,我知道有人会抱怨缺少代码,但我真的不知道它是否隐藏在我的代码中(而且真的很多),哪个部分可能是相关的。 如果有人有线索,请随时写。

P.S. 奇怪的是,当 Date 变量存储为字符串时,我正确地看到了块,但显然我的 X 轴没有标签。

谢谢大家

编辑:

在“干净”代码下方

Imports System.Windows.Forms.DataVisualization.Charting
Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class Form1

    Dim ConnectionString As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=MyAddress\File.mdf;Integrated Security=True;"

    Private Sub Form5_Load(sender As Object, e As EventArgs) Handles Me.Load

        '// Initial Load

        Dim dt As New DataTable
        Using cn = New SqlClient.SqlConnection
            cn.ConnectionString = ConnectionString
            cn.Open()
            Using cmd = cn.CreateCommand
                cmd.CommandText = "SELECT MYDATE,SUM(LIABILITIES) AS LIABILITIES, 
                                   SUM(MARGIN) AS MARGIN,
                                   SUM(NET_INTEREST) AS NET_INTEREST
                                   FROM CHART 
                                   GROUP BY MYDATE
                                   ORDER BY MYDATE
                                   "
                Using adap As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd)
                    adap.Fill(dt)
                End Using
            End Using
        End Using

        '// Data Source

        Me.Chart1.DataSource = dt
        Me.Chart1.DataBind()

        '// Series

#Region "Series Liabilities"
        '// Series 1 - Liabilities
        Dim Liabilities As Series = New Series("Liabilities")
        Me.Chart1.Series.Add(Liabilities)
        Me.Chart1.Series("Liabilities").ChartType = SeriesChartType.StackedColumn
        Me.Chart1.Series("Liabilities").XValueMember = "MYDATE"
        Me.Chart1.Series("Liabilities").YValueMembers = "LIABILITIES"
#End Region

#Region "Series Margin"
        '// Series 2 - Margin
        Dim Margin_Interest As Series = New Series("Margin")
        Me.Chart1.Series.Add(Margin_Interest)
        Me.Chart1.Series("Margin").ChartType = SeriesChartType.StackedColumn
        Me.Chart1.Series("Margin").XValueMember = "MYDATE"
        Me.Chart1.Series("Margin").YValueMembers = "MARGIN"
#End Region

#Region "Series Net Interest"
        '// Series 3 - NET_INTEREST
        Dim NET_INTEREST As Series = New Series("Net Interest")
        Me.Chart1.Series.Add(NET_INTEREST)
        Me.Chart1.Series("Net Interest").ChartType = SeriesChartType.StackedColumn
        Me.Chart1.Series("Net Interest").XValueMember = "MYDATE"
        Me.Chart1.Series("Net Interest").YValueMembers = "NET_INTEREST"
#End Region

        '// AxisX

        'Me.Chart1.ChartAreas(0).AxisX.Interval = 1       
        'Me.Chart1.ChartAreas(0).AxisX.IsLabelAutoFit = True
        'Me.Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "MMMM"

    End Sub

    Private Sub btnLast30Days_Click(sender As Object, e As EventArgs) Handles btnLast30Days.Click
        UpdateFilters()
    End Sub

    Sub UpdateFilters()
        Dim str As String = "SELECT MYDATE, 
                            SUM(LIABILITIES) AS LIABILITIES,
                            SUM(MARGIN) AS MARGIN,
                            SUM(NET_INTEREST) AS NET_INTEREST
                            FROM CHART 
                            WHERE MYDATE = '2023-08-31'
                            GROUP BY MYDATE
                            ORDER BY MYDATE"

        Dim dt As New DataTable
        Using cn = New SqlClient.SqlConnection
            cn.ConnectionString = ConnectionString
            cn.Open()
            Using cmd = cn.CreateCommand
                cmd.CommandText = str
                Using adap As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd)
                    adap.Fill(dt)
                End Using
            End Using
        End Using
        Me.Chart1.DataSource = dt
        Me.Chart1.DataBind()
    End Sub

End Class
SQL vb.net Visual-Studio 图表 系列

评论

0赞 djv 9/25/2023
您是否尝试过将 x 轴设置为日期时间

答:

1赞 TnTinMn 9/25/2023 #1

我可以确认你的问题。

可以通过显式设置 to 或 来解决此问题。默认值为 和 由于某种原因,当仅绘制一条数据记录时失败。Series.XValueTypeChartValueType.DateChartValueType.DatetimeChartValueType.Auto

Me.Chart1.Series("Liabilities").XValueType = ChartValueType.DateTime

对每个系列执行此操作。

评论

0赞 sickboy 9/26/2023
这不是唯一的问题,但这解决了列宽不同的问题。错误选项仍然存在,因为日期是唯一的,并且所有系列都相同,因此没有理由采取不同的措施。