【发布时间】:2022-01-23 02:35:58
【问题描述】:
一点信息:
我想用过去 12 个月的交易填充堆积柱形图。 每列应该是一年中的一个月,交易当然是相互叠加的。
目前,我只有一个测试文档,可以从中提取一些数据。我首先尝试将数据(来自 csv)直接放入图表中,但这只会导致多列具有多个重复的 x 轴标签,并且没有堆叠。
我做了一些挖掘并找到了这个解决方案。不幸的是,结果不是我想要的。事务堆叠但x轴标签不正确
目前有
Dim rowsTra() As String = File.ReadAllLines(".\data\transactions.csv")
Dim traVal() As String
Dim preYear As DateTime = DateTime.Now.AddYears(-1)
Dim j As Integer = 0
Dim dtTest As DataTable = New DataTable
dtTest.Columns.Add("col1", GetType(Double))
dtTest.Columns.Add("col2", GetType(String))
dtTest.Columns.Add("col3", GetType(String))
For i As Integer = 0 To rowsTra.Length - 1 Step +1 ' Looping through all transactions
traVal = rowsTra(j).ToString().Split(",")
Dim traDate As String = Convert.ToDateTime(traVal(1))
If (traDate >= preYear) Then ' Check if date is not older than 1 year
Dim conMonth As Date = CDate(traVal(1))
Dim month = conMonth.ToString("MMM yyyy")
dtTest.Rows.Add(traVal(6), month, traVal(4))
Else
i = rowsTra.Length - 1 ' Quit loop if year ends (will only work if csv is chronological
End If
j += 1
Next
Dim dv As DataView = New DataView(dtTest)
dv.Sort = "col2 asc"
chTrend.Series.Clear()
chTrend.Titles.Clear() ' Clear
chTrend.DataBindCrossTable(dv, "col3", "col2", "col1", "Label=col1") ' Populate chart
For Each cs As Series In chTrend.Series ' Stack values
cs.ChartType = SeriesChartType.StackedColumn
Next
通过使用这个 csv 文件,我得到了这个结果:
Account 1,19 Dec 2021,Man 1,Cat 1,Subcat 1,test,5
Account 2,01 Dec 2021,Man 2,Cat 2,Subcat 2,test,10
Account 5,01 Nov 2021,Man 4,Cat 2,Subcat 2,test,10
Account 4,27 Oct 2021,Man 4,Cat 4,Subcat 4,test,20
Account 3,10 Oct 2021,Man 3,Cat 3,Subcat 3,test,15
Account 1,03 Sep 2020,Man 1,Cat 1,Subcat 1,test,25
= col2 =col3 =col1
为什么在这种情况下会在“2021 年 12 月”下添加 4 笔交易,而实际上只有 2 笔交易? 我注意到,当我将第三个列出的交易更改为另一个 Subcat 时,所有交易都在 2021 年 12 月以下。
我也尝试在 dtTest 中为 col2 提供 Date 类型,但这只是给出了一个更奇怪的图表,我不知道如何将日期格式化为“MMM yyyy”。这是我从日期时间转移到字符串的原因
我不确定自己做错了什么。
感谢您的意见。抱歉代码乱七八糟,还在学习中。
【问题讨论】:
-
好的,所以我可能已经想出了一个解决方案,但我不确定它是否可行。我认为它打印出一个不正确的图表,因为几个月来并不是所有现有的子类别都被赋予了一个值。是否有一种有效的方法可以为每个月生成所有现有子类别,或者最好单独存储子类别并循环遍历它们以在 dataTable 中创建所需的行?