几天来我一直在寻找如何使用MS Chart修复此问题的方法,不幸的是,到目前为止我发现的所有内容都无济于事。
我有一个水平条形图,正在特定时间范围内加载销售数据。使用“日期维”表,此数据已经“按摩”到我想要的方式,这样我可以在没有数据的情况下将零推入。
这意味着我的数据始终始终精确地要求有要求的点数,无论是每天,每周,每月还是每年。您要求7天获得7行,您要求12周获得12行,要求3个月获得3行。始终(目前可能是每年的开始情况,因此我们不再赘述。)
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9QRXlpMFl5LnBuZyJ9” alt =“按预期工作”>
现在,Daily可以正常工作,但是当我切换到其他模式时,图表完全忽略了我给它指定了非常特定数量的点的事实,而是将日历中的每个日期都放在我的数据点之间。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9GTzN6NEhRLnBuZyJ9” alt =“无法按预期工作”>正如我说过的,我尝试了各种解决方案,但它们会使情况变得更糟或导致错误。
Private Sub LoadChart()
Dim oDT As New DataTable
Dim sqlCmd As New SqlCommand
Dim sSQLDateCTE As String = "WITH Date_CTE as ( " & _
"SELECT [Date] as SaleDate " & _
"FROM [DateDimension] "
Dim sWHEREDateCTE As String = "WHERE [Date] BETWEEN DATEADD({0},-{1},GETDATE()) AND GETDATE() "
Dim sSQLSUM As String = "), SUM_CTE as ( " & _
"SELECT F01 as UPCCode, F254 as SaleDate, SUM(F64) as UnitsSold " & _
"FROM {0} " & _
"WHERE F1034 = 3 AND F64 <> 0 AND F01 = @UPCCode " & _
"AND F254 IN (SELECT saledate FROM Date_CTE) " & _
"GROUP BY F01,F254) " & _
"Select DC.SaleDate ,ISNULL(SC.UnitsSold,0) as UnitsSold " & _
"FROM Date_CTE as DC " & _
"LEFT OUTER JOIN SUM_CTE as SC " & _
"ON SC.SaleDate = DC.SaleDate " & _
"ORDER BY DC.SaleDate ASC "
Dim sSQL As String = ""
Try
Dim PeriodRow As DataRow = oAppEnv.dtPeriods.Select(String.Format("DPPeriodID = {0}", cboPeriods.SelectedValue)).FirstOrDefault
Dim DurationRow As DataRow = oAppEnv.dtDurations.Select(String.Format("DDDurationID = '{0}'", cboDuration.SelectedValue)).FirstOrDefault
Select Case PeriodRow.Item("DPPeriodAbbrev")
Case "D"
sWHEREDateCTE = String.Format(sWHEREDateCTE, "DD", DurationRow.Item("DDDurationLength"))
sSQLSUM = String.Format(sSQLSUM, "RPT_ITM_D")
sSQL = sSQLDateCTE & sWHEREDateCTE & sSQLSUM
Case "W"
sWHEREDateCTE = String.Format(sWHEREDateCTE, "WW", DurationRow.Item("DDDurationLength")) & " AND Weekday = 1 "
sSQLSUM = String.Format(sSQLSUM, "RPT_ITM_W")
sSQL = sSQLDateCTE & sWHEREDateCTE & sSQLSUM
Case "M"
sWHEREDateCTE = String.Format(sWHEREDateCTE, "MM", DurationRow.Item("DDDurationLength")) & " AND [DAY] = DATEPART(Day,getdate()) "
sSQLSUM = String.Format(sSQLSUM, "RPT_ITM_M")
sSQL = sSQLDateCTE & sWHEREDateCTE & sSQLSUM
Case "Y"
'????
End Select
sqlCmd.CommandText = sSQL
sqlCmd.Parameters.AddWithValue("@UPCCode", sUPCCode)
oDT = oAppEnv.oLogiDM.GetSQLData(sqlCmd)
ctSalesData.ChartAreas.Clear()
ctSalesData.ChartAreas.Add("Sales")
ctSalesData.Series.Clear()
ctSalesData.Series.Add("Sales")
ctSalesData.Series("Sales").ChartType = Charting.SeriesChartType.Bar
'ctSalesData.Series(0).IsXValueIndexed = True
'ctSalesData.ChartAreas(0).AxisX.IntervalOffsetType = Charting.DateTimeIntervalType.Days
'ctSalesData.ChartAreas(0).AxisX.IntervalOffset = -1
If oDT IsNot Nothing Then
'//Get the Date range.
Dim oSortDV As DataView = oDT.DefaultView
oSortDV.Sort = "UnitsSold DESC"
'ctSalesData.DataSource = oDT
For Each row In oDT.Rows
ctSalesData.Series(0).Points.AddXY(row.item("SaleDate"), row.item("UnitsSold"))
Next
'ctSalesData.AlignDataPointsByAxisLabel()
ctSalesData.Series(0).CustomProperties = "PixelPointWidth = 15"
ctSalesData.Series(0).IsValueShownAsLabel = True
Dim MinDate As Date = oDT.Rows(0).Item("SaleDate")
Dim Maxdate As Date = oDT.Rows(oDT.Rows.Count - 1).Item("SaleDate")
ctSalesData.ChartAreas(0).AxisX.Minimum = MinDate.ToOADate
ctSalesData.ChartAreas(0).AxisX.Maximum = Maxdate.ToOADate
If oDT.Rows.Count > 14 Then
Dim ZoomDate As Date = oDT.Rows(14).Item("SaleDate")
ctSalesData.ChartAreas(0).AxisX.ScaleView.Zoom(MinDate.ToOADate, ZoomDate.ToOADate)
End If
ctSalesData.Series(0).XValueType = Charting.ChartValueType.Date
ctSalesData.ChartAreas(0).AxisX.Interval = 1
ctSalesData.ChartAreas(0).AxisY.Minimum = 0
ctSalesData.ChartAreas(0).AxisY.Maximum = Math.Ceiling(oSortDV(0).Item("UnitsSold") / 10) * 10
If ctSalesData.ChartAreas(0).AxisY.Maximum = oSortDV(0).Item("UnitsSold") Then
ctSalesData.ChartAreas(0).AxisY.Maximum += 10
End If
ctSalesData.ChartAreas(0).CursorX.AutoScroll = True
ctSalesData.ChartAreas(0).AxisX.ScaleView.SizeType = Charting.DateTimeIntervalType.Number
ctSalesData.ChartAreas(0).AxisX.ScrollBar.ButtonStyle = Charting.ScrollBarButtonStyles.SmallScroll
ctSalesData.ChartAreas(0).AxisX.ScaleView.SmallScrollSize = 20
'ctSalesData.Series(0).XValueMember = "SaleDate"
'ctSalesData.Series(0).YValueMembers = "UnitsSold"
ctSalesData.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim iCnt As Integer = 0
For Each row In oDT.Rows
ctSalesData.Series(0).Points.AddXY(iCnt, row.item("UnitsSold"))
ctSalesData.Series(0).Points(iCnt).AxisLabel = row.item("SaleDate")
iCnt += 1
Next
但是我认为这只是打破了我的缩放代码,因为它基于所有日期。
所以;我该如何摆脱这些空白的空间浪费,只显示我插入的数据点?
因此,对于正在/曾经/将要遇到相同问题的任何人,到目前为止,我能够解决这一烦恼的唯一方法是执行我之前提到的Generic Counter事情,但要重做我的缩放代码。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9EcjB2Rk1ILnBuZyJ9” alt =“正在使用” >>>
这是任何有兴趣的人的重做代码。
Private Sub LoadChart() Dim oDT As New DataTable Dim sqlCmd As New SqlCommand Dim sSQLDateCTE As String = "WITH Date_CTE as ( " & _ "SELECT [Date] as SaleDate " & _ "FROM [DateDimension] " Dim sWHEREDateCTE As String = "WHERE [Date] BETWEEN DATEADD({0},-{1},GETDATE()) AND GETDATE() " Dim sSQLSUM As String = "), SUM_CTE as ( " & _ "SELECT F01 as UPCCode, F254 as SaleDate, SUM(F64) as UnitsSold " & _ "FROM {0} " & _ "WHERE F1034 = 3 AND F64 <> 0 AND F01 = @UPCCode " & _ "AND F254 IN (SELECT saledate FROM Date_CTE) " & _ "GROUP BY F01,F254) " & _ "Select DC.SaleDate ,ISNULL(SC.UnitsSold,0) as UnitsSold " & _ "FROM Date_CTE as DC " & _ "LEFT OUTER JOIN SUM_CTE as SC " & _ "ON SC.SaleDate = DC.SaleDate " & _ "ORDER BY DC.SaleDate ASC " Dim sSQL As String = "" Try Dim PeriodRow As DataRow = oAppEnv.dtPeriods.Select(String.Format("DPPeriodID = {0}", cboPeriods.SelectedValue)).FirstOrDefault Dim DurationRow As DataRow = oAppEnv.dtDurations.Select(String.Format("DDDurationID = '{0}'", cboDuration.SelectedValue)).FirstOrDefault Select Case PeriodRow.Item("DPPeriodAbbrev") Case "D" sWHEREDateCTE = String.Format(sWHEREDateCTE, "DD", DurationRow.Item("DDDurationLength")) sSQLSUM = String.Format(sSQLSUM, "RPT_ITM_D") sSQL = sSQLDateCTE & sWHEREDateCTE & sSQLSUM Case "W" sWHEREDateCTE = String.Format(sWHEREDateCTE, "WW", DurationRow.Item("DDDurationLength")) & " AND Weekday = 1 " sSQLSUM = String.Format(sSQLSUM, "RPT_ITM_W") sSQL = sSQLDateCTE & sWHEREDateCTE & sSQLSUM Case "M" sWHEREDateCTE = String.Format(sWHEREDateCTE, "MM", DurationRow.Item("DDDurationLength")) & " AND [DAY] = DATEPART(Day,getdate()) " sSQLSUM = String.Format(sSQLSUM, "RPT_ITM_M") sSQL = sSQLDateCTE & sWHEREDateCTE & sSQLSUM Case "Y" '???? End Select sqlCmd.CommandText = sSQL sqlCmd.Parameters.AddWithValue("@UPCCode", sUPCCode) oDT = oAppEnv.oLogiDM.GetSQLData(sqlCmd) ctSalesData.ChartAreas.Clear() ctSalesData.ChartAreas.Add("Sales") ctSalesData.Series.Clear() ctSalesData.Series.Add("Sales") ctSalesData.Series("Sales").ChartType = Charting.SeriesChartType.Bar ctSalesData.Series(0).IsXValueIndexed = True 'ctSalesData.ChartAreas(0).AxisX.IntervalOffsetType = Charting.DateTimeIntervalType.Days 'ctSalesData.ChartAreas(0).AxisX.IntervalOffset = -1 If oDT IsNot Nothing Then '//Get the highest sold item so we can set the Y axis to bigger than the biggest Dim oSortDV As DataView = oDT.DefaultView oSortDV.Sort = "UnitsSold DESC" 'ctSalesData.DataSource = oDT Dim iCnt As Integer = 0 For Each row In oDT.Rows ctSalesData.Series(0).Points.AddXY(iCnt, row.item("UnitsSold")) ctSalesData.Series(0).Points(iCnt).AxisLabel = row.item("SaleDate") iCnt += 1 Next 'ctSalesData.AlignDataPointsByAxisLabel() ctSalesData.Series(0).CustomProperties = "PixelPointWidth = 15" ctSalesData.Series(0).IsValueShownAsLabel = True 'Dim MinDate As Date = oDT.Rows(0).Item("SaleDate") 'Dim Maxdate As Date = oDT.Rows(oDT.Rows.Count - 1).Item("SaleDate") ctSalesData.ChartAreas(0).AxisX.Minimum = 0 ctSalesData.ChartAreas(0).AxisX.Maximum = oDT.Rows.Count + 1 If oDT.Rows.Count > 21 Then Dim ZoomDate As Date = oDT.Rows(14).Item("SaleDate") ctSalesData.ChartAreas(0).AxisX.ScaleView.Zoom(0, 22) End If 'ctSalesData.Series(0).XValueType = Charting.ChartValueType.Date ctSalesData.ChartAreas(0).AxisX.Interval = 1 ctSalesData.ChartAreas(0).AxisY.Minimum = 0 ctSalesData.ChartAreas(0).AxisY.Maximum = Math.Ceiling(oSortDV(0).Item("UnitsSold") / 10) * 10 If ctSalesData.ChartAreas(0).AxisY.Maximum = oSortDV(0).Item("UnitsSold") Then ctSalesData.ChartAreas(0).AxisY.Maximum += 10 End If ctSalesData.ChartAreas(0).CursorX.AutoScroll = True ctSalesData.ChartAreas(0).AxisX.ScaleView.SizeType = Charting.DateTimeIntervalType.Number ctSalesData.ChartAreas(0).AxisX.ScrollBar.ButtonStyle = Charting.ScrollBarButtonStyles.SmallScroll ctSalesData.ChartAreas(0).AxisX.ScaleView.SmallScrollSize = 21 ''ctSalesData.Series(0).XValueMember = "SaleDate" ''ctSalesData.Series(0).YValueMembers = "UnitsSold" ctSalesData.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False End If Catch ex As Exception MsgBox(ex.Message) End Try
尽管这是一个令人讨厌的解决方法,但是仍然可以使Date方法正常运行,还是很好的。