|
■No86534 (shu さん) に返信
お世話になります。
下記のようなことになりましたので投稿させて頂きます。
> row("マーク")はobject型なので文字列化すればその条件でよいと思います。
取り敢えずその部分は作ってみました。
> さらにループも時間毎にグループ化する必要がないのでループのネストが不要になります。
No86531でのこの部分は結局どうすれば良いのか分かりませんでした。
> dtRow(0) = Uridat(i).Jikoku '時刻
もしかしてこの時点で先に文字列型から日付型 (Date)に変換しておいた方がプログラムの簡素化には繋がるのでしょうか?
なお、↓のプログラムですと、60分を越える120分(1時間)、240分(2時間)などともなると表示は60分と同じともなってしまいます。
Public Class Form1
Private Structure datF
Dim Jikoku As String '時刻 (実際は "2018/01/30 16:30:00" などの日付も入った文字列)
Dim Suu1 As Integer '数量1
Dim Suu2 As Integer '数量2
Dim mark As String '連続した時間の最初のデータのみに '*' マークを付ける。
End Structure
Dim Uridat(500) As datF
Dim datS As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 1分間隔のサンプルデータをを成
datS = -1
' 8:10〜8:45
For i = 10 To 45 ' 8:10〜8:45
datS = datS + 1
Uridat(datS).Jikoku = $"8:{i:00}" '時刻
Uridat(datS).Suu1 = i * 10 '数量1
Uridat(datS).Suu2 = i * 5 '数量2
Uridat(datS).mark = " "
If i = 10 Then
Uridat(datS).mark = "" ' "*" マーク
End If
Next
' 10:20〜13:59 <------------ 時間を延長
For i = 20 To 59 ' 10:20〜10:59
datS = datS + 1
Uridat(datS).Jikoku = $"10:{i:00}" '時刻
Uridat(datS).Suu1 = i + 100 '数量1
Uridat(datS).Suu2 = i + 20 '数量2
Uridat(datS).mark = " "
Uridat(datS).mark = " "
If i = 20 Then
Uridat(datS).mark = "*" ' "*" マーク
End If
Next
For i = 0 To 59 ' 11:00〜11:59
datS = datS + 1
Uridat(datS).Jikoku = $"11:{i:00}" '時刻
Uridat(datS).Suu1 = i + 100 '数量1
Uridat(datS).Suu2 = i + 20 '数量2
Uridat(datS).mark = " "
Next
For i = 0 To 59 ' 12:00〜12:59
datS = datS + 1
Uridat(datS).Jikoku = $"12:{i:00}" '時刻
Uridat(datS).Suu1 = i + 100 '数量1
Uridat(datS).Suu2 = i + 20 '数量2
Uridat(datS).mark = " "
Next
For i = 0 To 59 ' 13:00〜13:59
datS = datS + 1
Uridat(datS).Jikoku = $"13:{i:00}" '時刻
Uridat(datS).Suu1 = i + 100 '数量1
Uridat(datS).Suu2 = i + 20 '数量2
Uridat(datS).mark = " "
Next
'==== Chart1 ====
Dim ds As New DataSet
Dim dt As New DataTable
Dim dtRow As DataRow
'列の作成
With dt.Columns
.Add("時刻", GetType(String))
.Add("数量1", GetType(Integer))
.Add("数量2", GetType(Integer))
.Add("マーク", GetType(String))
End With
ds.Tables.Add(dt)
'データのセット
For i = 0 To datS
dtRow = ds.Tables(0).NewRow
dtRow(0) = Uridat(i).Jikoku '時刻
dtRow(1) = Uridat(i).Suu1 '数量1
dtRow(2) = Uridat(i).Suu2 '数量2
dtRow(3) = Uridat(i).mark 'マーク
ds.Tables(0).Rows.Add(dtRow)
Next
'Chart に表示するデータソースを設定
Dim min = -1
Dim max = -1
Dim intr = 30 ' 60 120 240
Dim tbl = GetChartDs(ds, intr, min, max)
Chart1.DataSource = tbl.Select("", "x")
Chart1.Series.Clear()
'
With Chart1.ChartAreas(0)
With .AxisX
.Minimum = min
.Maximum = max
.Interval = 5
For Each row In tbl.Select("", "時刻")
Dim null = row.IsNull("数量1")
If null Then Continue For
Dim d = row.Field(Of Date)("時刻")
Dim x = row.Field(Of Integer)("x")
With .CustomLabels.Add(x - 2, x + 2, d.ToString("HH:mm"))
.RowIndex = 0
End With
Next
End With
End With
Dim ore = Chart1.Series.Add("折れ線")
With ore
.ChartType = DataVisualization.Charting.SeriesChartType.Line
.EmptyPointStyle.SetDefault(True)
'.XValueMember = "時刻"
.XValueMember = "x"
.YValueMembers = "数量1"
End With
Dim bou = Chart1.Series.Add("棒グラフ")
With bou
.ChartType = DataVisualization.Charting.SeriesChartType.Column
'.XValueMember = "時刻"
.XValueMember = "x"
.YValueMembers = "数量2"
End With
End Sub
Private Function GetChartDs(Src As DataSet, IntrMin As Integer,
ByRef Min As Integer, ByRef Max As Integer) As DataTable
Dim ret As New DataTable()
Min = Nothing
Max = Nothing
With ret.Columns()
.Add("x", GetType(Integer))
.Add("時刻", GetType(DateTime))
.Add("数量1", GetType(Integer))
.Add("数量2", GetType(Integer))
.Add("マーク", GetType(String))
End With
Dim x = 1
Min = x
x = 5
'一番左にスペース
'ret.Rows.Add(x, DBNull.Value, DBNull.Value, DBNull.Value) : x += 5 '特に必要とはしません。
'Dim isFirst2 = True
For Each itm In (From r In Src.Tables(0).Select()
Let d = Function(s As String) As Date
Dim d2 As Date = Nothing
If Date.TryParseExact(s, {"HH:mm", "HH: mm", "H:m"}, Nothing, Nothing, d2) Then
Return d2
Else
Return CDate(Nothing)
End If
End Function(r.Field(Of String)("時刻"))
Let h = d.Hour
Order By d
Group By h Into rows = Group)
'間
'If Not isFirst2 Then
'For i = 1 To 2
'ret.Rows.Add(x, DBNull.Value, DBNull.Value, DBNull.Value) : x += 5
'Next
'End If
'isFirst2 = False
Dim hour = itm.h
Dim isFirst = True
For Each rowItm In itm.rows
Dim row = rowItm.r
Dim d = rowItm.d
Dim disp = False
If isFirst Then disp = True : isFirst = False
If Not disp Then
If d.Minute Mod IntrMin = 0 Then
disp = True
End If
End If
If CType(row("マーク"), String) = "*" Then 'row("マーク")はobject型なので文字列化
For i = 1 To 2
ret.Rows.Add(x, DBNull.Value, DBNull.Value, DBNull.Value) : x += 5
Next
If disp Then
ret.Rows.Add(x, d, row("数量1"), row("数量2"))
x += 5
End If
Else
If disp Then
ret.Rows.Add(x, d, row("数量1"), row("数量2"))
x += 5
End If
End If
Next
Next
Max = x
ret.AcceptChanges()
Return ret
End Function
End Class
|