如何在图表中显示来自访问数据库的特定数据?

How do I display specific data in a chart from access database?

我目前有一个带有 table 的访问数据库,它存储每个客户购买的商品以及所购买商品的数量。因此,多个客户可以购买不同数量的相同商品。我想显示所有客户为每件商品购买的总数量,并将其显示在 Vb.net 表格的图表中。这是我到目前为止所拥有的。当我 运行 这显示我的 table 中重复的数量数据时,当我希望它将所有数量加在一起以显示每个项目的一个值单个值时。有办法做到这一点吗?我对此进行了很多研究,但似乎找不到办法做到这一点。我是否必须先查询数据库以便它选择每个项目的数量,将它们加在一起然后显示在图表中?如果可以,我该怎么做?

非常感谢!

Me.OrderItemTableAdapter1.Fill(Me.POS_system1DataSet5.OrderItem)
        'TODO: This line of code loads data into the 'POS_system1DataSet3.OrderItem' table. You can move, or remove it, as needed.
        Me.OrderItemTableAdapter.Fill(Me.POS_system1DataSet3.OrderItem)


        OrdersGraph.ChartAreas(0).AxisX.LabelStyle.Interval = 1
        ' Axis X
        OrdersGraph.ChartAreas(0).AxisX.TitleFont = New Font("Oswald", 18.0F, FontStyle.Regular, GraphicsUnit.Pixel)
        OrdersGraph.ChartAreas(0).AxisX.Title = "Menu Items"
        ' Axis Y
        OrdersGraph.ChartAreas(0).AxisY.TitleFont = New Font("Oswald", 18.0F, FontStyle.Regular, GraphicsUnit.Pixel)
        OrdersGraph.ChartAreas(0).AxisY.Title = "Quantity"

        'Hide legend label
        OrdersGraph.Series("Orders").IsVisibleInLegend = False

        'Show number value in bar / column chart
        OrdersGraph.Series("Orders").IsValueShownAsLabel = True

        ' value member
        OrdersGraph.Series("Orders").XValueMember = "ItemID"
        OrdersGraph.Series("Orders").YValueMembers = "Quantity"

你的问题不在于图表。它是如何 select 从您的数据源中获取您需要的数据。所以我想当你 select 时,你有一些对应于 table 的属性。我做了一个 class 可以代表那个。用于测试

Public Class OrderItem
    Public Property ItemID As Integer
    Public Property OrderNo As Integer
    Public Property Quantity As Integer
    Public Property OrderID As Integer

    Public Sub New(itemID As Integer, orderNo As Integer, quantity As Integer, orderID As Integer)
        Me.ItemID = itemID
        Me.OrderNo = orderNo
        Me.Quantity = quantity
        Me.OrderID = orderID
    End Sub
End Class

然后我将你 table 中的数据添加到那个 class

的列表中
Dim orderItems As New List(Of OrderItem)()
orderItems.Add(New OrderItem(3, 84118, 33, 3))
orderItems.Add(New OrderItem(6, 63940, 4, 16))
orderItems.Add(New OrderItem(6, 77996, 6, 17))
orderItems.Add(New OrderItem(5, 77996, 8, 18))
orderItems.Add(New OrderItem(8, 48475, 3, 21))
orderItems.Add(New OrderItem(13, 48475, 2, 22))
orderItems.Add(New OrderItem(2, 93914, 4, 25))
orderItems.Add(New OrderItem(6, 93914, 4, 26))
orderItems.Add(New OrderItem(5, 77590, 5, 31))
orderItems.Add(New OrderItem(2, 93946, 4, 32))
orderItems.Add(New OrderItem(6, 93946, 3, 33))
orderItems.Add(New OrderItem(2, 89181, 4, 38))
orderItems.Add(New OrderItem(6, 89181, 4, 39))
orderItems.Add(New OrderItem(3, 98014, 4, 40))

现在是重要部分,按 ItemID 对数据进行分组并计算项目数。您可以为此使用 LINQ

Dim groupedOrderItems = orderItems.GroupBy(Function(oi) oi.ItemID).Select(Function(oig) New With {oig.Key, oig.Count()})

并将数据集应用于图表

OrdersGraph.Series("Orders").Points.DataBindXY(
    groupedOrderItems.Select(Function(oig) oig.Key).ToArray(),
    groupedOrderItems.Select(Function(oig) oig.Count).ToArray())