Visual Studio .NET 图表错误

问题描述 投票:0回答:1

这篇文章是关于我认为的 VS bug 的。 问题在于当我将具有以下格式的单行数据表加载为数据源时图表给出的视图:

DATE;doubleVAR;doubleVAR;doubleVAR;

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

this

而不是

this

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

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

附注奇怪的是,当日期变量存储为字符串时,我正确地看到了该块,但显然我的 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 charts series
1个回答
0
投票

我可以确认你的问题。

可以通过显式将

Series.XValueType
设置为
ChartValueType.Date
ChartValueType.Datetime
来解决。默认值为
ChartValueType.Auto
,由于某种原因,仅绘制一条数据记录时会失败。

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

对每个系列执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.