数据集中的图表显示问题包含多个系列

时间:2015-06-08 20:08:07

标签: vb.net charts dataset series graphing

Noob to chart,这是我第一次尝试,所以这里。我根据Separating Chart Series

中给出的答案构建了我的代码

我使用的图表仅被修改为添加2-7系列,并且还为所有给定的系列更改了从bar到spline的系列类型。通过GUI进行的所述更改。

执行后,数据集被正确检索,图表关键区域中的系列名称更新并正确显示,但图表本身不显示,仍然是右边的颜色键以外的空白框。

数据类型:DateTime = DateTime,MCEC& AECC =十进制,所有其他=整数

编辑:

  • 第2天:尝试(多次)不同的代码尝试,最新的是每次更新图表时擦除并重新建立系列的代码(通过屏幕上的组合框),没有变化。
  • 第3天:
    • 进一步替代代码尝试,最新迭代如下所示
    • 尝试删除并添加新图表(完全未修改)
  • 第14天:
    • 自上次编辑以来,我尝试了大约30种不同的变种,但仍然没有成功。
    • 已经征求了我认识的开发人员的帮助,尝试了他的代码变体(下面),我们两个都受到了阻碍。变体代码会显示一对图表,但不会显示任何系列。在这一点上,我正在寻找一个插件,希望能够使这项工作。

第14天代码:

Private Sub T3UpdateDisplay()
    '/////////////////////////////////
    'Query database and display results in the chart
    '/////////////////////////////////
    'build sql for processing
    sql = "SELECT DateTime, AECCRate1 AS AECC, MCECRate1 AS MCEC, Manila, Gosnell, Lepanto, Reginold, Promised_Land " _
     & "FROM pt_demand WHERE DateTime BETWEEN '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString & "' " _
     & "ORDER BY DateTime ASC;"

    SetUpChart()
    GetAllDemands()

End Sub

Private Sub SetUpChart()

    Chart1.Series.Clear()
    Chart1.ChartAreas.Clear()

    Chart1.Series.Add("AECC")
    Chart1.ChartAreas.Add("AD")
    Chart1.Series("AECC").ChartArea = "AD"

    Chart1.Series.Add("MCEC")
    Chart1.ChartAreas.Add("MD")
    Chart1.Series("MCEC").ChartArea = "MD"

    Chart1.Series.Add("Manila")
    Chart1.ChartAreas.Add("M")
    Chart1.Series("Manila").ChartArea = "M"

    Chart1.Series.Add("Gosnell")
    Chart1.ChartAreas.Add("G")
    Chart1.Series("Gosnell").ChartArea = "G"

    Chart1.Series.Add("Lepanto")
    Chart1.ChartAreas.Add("L")
    Chart1.Series("Lepanto").ChartArea = "L"

    Chart1.Series.Add("Reginold")
    Chart1.ChartAreas.Add("R")
    Chart1.Series("Reginold").ChartArea = "R"

    Chart1.Series.Add("PromisedLand")
    Chart1.ChartAreas.Add("P")
    Chart1.Series("PromisedLand").ChartArea = "P"

    Chart1.Series("AECC").Color = Color.Green
    Chart1.Series("MCEC").Color = Color.Blue
    Chart1.Series("Manila").Color = Color.Red
    Chart1.Series("Gosnell").Color = Color.Gray
    Chart1.Series("Lepanto").Color = Color.Purple
    Chart1.Series("Reginold").Color = Color.Orange
    Chart1.Series("PromisedLand").Color = Color.Yellow

    Chart1.Series("AECC").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
    Chart1.Series("MCEC").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
    Chart1.Series("Manila").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
    Chart1.Series("Gosnell").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
    Chart1.Series("Lepanto").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
    Chart1.Series("Reginold").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
    Chart1.Series("PromisedLand").ChartType = DataVisualization.Charting.SeriesChartType.FastLine

    Chart1.Series("AECC").XValueType = DataVisualization.Charting.ChartValueType.Time
    Chart1.Series("MCEC").XValueType = DataVisualization.Charting.ChartValueType.Time
    Chart1.Series("Manila").XValueType = DataVisualization.Charting.ChartValueType.Time
    Chart1.Series("Gosnell").XValueType = DataVisualization.Charting.ChartValueType.Time
    Chart1.Series("Lepanto").XValueType = DataVisualization.Charting.ChartValueType.Time
    Chart1.Series("Reginold").XValueType = DataVisualization.Charting.ChartValueType.Time
    Chart1.Series("PromisedLand").XValueType = DataVisualization.Charting.ChartValueType.Time

End Sub

Private Sub GetAllDemands()
    'Connect to database do SQL for Dates Between
    On Error GoTo Err_Control
    Dim myDBCS As String = Main.DBCS    'create local copy of database connection string
    Dim ThisRecKey As Guid
    Dim sqlConnectionString As String = myDBCS

    Dim strGetDemandsSQL As String = sql
    Using sConnection As New SqlConnection(sqlConnectionString)
        sConnection.Open()
        Dim GetRecKeySQLCommand As New SqlCommand(strGetDemandsSQL, sConnection)
        'Get the Rec_Key
        Dim reader As SqlDataReader = GetRecKeySQLCommand.ExecuteReader()
        Dim ThisDateTime As Date

        While reader.Read()
            'Add Points
            Debug.Print(reader(0) & "    " & reader(1) & "    " & reader(2) & "    " & reader(3) & "    " & reader(4) & "    " & reader(5) & "    " & reader(6) & "    " & reader(7))
            ThisDateTime = reader(0)
            Chart1.Series("AECC").Points.AddXY(ThisDateTime.ToOADate(), CDbl(reader(1)))
            Chart1.Series("MCEC").Points.AddXY(ThisDateTime.ToOADate(), CDbl(reader(2)))
            Chart1.Series("M").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(3)))
            Chart1.Series("G").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(4)))
            Chart1.Series("L").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(5)))
            Chart1.Series("R").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(6)))
            Chart1.Series("P").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(7)))

        End While
        reader.Close()
        sConnection.Close()
    End Using

    Dim dblMin, dblMax As Double
    SetMinMax(dblMin, dblMax, "AECCRate1")
    Chart1.ChartAreas("AD").AxisY.Minimum = dblMin
    Chart1.ChartAreas("AD").AxisY.Maximum = dblMax
    Chart1.ChartAreas("AD").AxisX.Title = "AECC: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

    SetMinMax(dblMin, dblMax, "MCECRate1")
    Chart1.ChartAreas("MD").AxisY.Minimum = dblMin
    Chart1.ChartAreas("MD").AxisY.Maximum = dblMax
    Chart1.ChartAreas("MD").AxisX.Title = "MCEC: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

    SetMinMax(dblMin, dblMax, "Manila")
    Chart1.ChartAreas("M").AxisY.Minimum = dblMin
    Chart1.ChartAreas("M").AxisY.Maximum = dblMax
    Chart1.ChartAreas("M").AxisX.Title = "Manila: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

    SetMinMax(dblMin, dblMax, "Gosnell")
    Chart1.ChartAreas("G").AxisY.Minimum = dblMin
    Chart1.ChartAreas("G").AxisY.Maximum = dblMax
    Chart1.ChartAreas("G").AxisX.Title = "Gosnell: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

    SetMinMax(dblMin, dblMax, "Lepanto")
    Chart1.ChartAreas("L").AxisY.Minimum = dblMin
    Chart1.ChartAreas("L").AxisY.Maximum = dblMax
    Chart1.ChartAreas("L").AxisX.Title = "Lepanto: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

    SetMinMax(dblMin, dblMax, "Reginold")
    Chart1.ChartAreas("R").AxisY.Minimum = dblMin
    Chart1.ChartAreas("R").AxisY.Maximum = dblMax
    Chart1.ChartAreas("R").AxisX.Title = "Reginold: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

    SetMinMax(dblMin, dblMax, "Promised_Land")
    Chart1.ChartAreas("P").AxisY.Minimum = dblMin
    Chart1.ChartAreas("P").AxisY.Maximum = dblMax
    Chart1.ChartAreas("P").AxisX.Title = "Promised Land: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

Exit_Here:

    Exit Sub
Err_Control:
    Select Case Err.Number
        Case Else
            Resume Exit_Here
    End Select
End Sub

Private Sub SetMinMax(ByRef Min As Double, ByRef Max As Double, FieldName As String)
    On Error GoTo Err_Control
    Dim myDBCS As String = Main.DBCS    'create local copy of database connection string
    Dim sqlConnectionString As String = myDBCS
    Dim strGetMaxSQL As String = "Select MAX(" & FieldName & ") FROM pt_demand WHERE DateTime Between '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString & "'"
    Dim strGetMinSQL As String = "Select MIN(" & FieldName & ") FROM pt_demand WHERE DateTime Between '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString & "'"
    Using sConnection As New SqlConnection(sqlConnectionString)
        sConnection.Open()
        'MAX
        Dim GetMaxSQLCommand As New SqlCommand(strGetMaxSQL, sConnection)
        Dim readerMax As SqlDataReader = GetMaxSQLCommand.ExecuteReader()

        While readerMax.Read()
            Max = CDbl(CInt(readerMax(0) + (0.01 * readerMax(0))))
        End While
        readerMax.Close()
        'MIN
        Dim GetMinSQLCommand As New SqlCommand(strGetMinSQL, sConnection)
        'Get the Rec_Key
        Dim readerMin As SqlDataReader = GetMinSQLCommand.ExecuteReader()

        While readerMin.Read()
            Min = CDbl(CInt(readerMin(0) - (0.01 * readerMin(0))))
        End While
        readerMin.Close()

        sConnection.Close()
    End Using

Exit_Here:

    Exit Sub
Err_Control:
    Select Case Err.Number
        Case Else
            Resume Exit_Here
    End Select
End Sub

第3天代码:

Private Sub T3UpdateDisplay()
            '/////////////////////////////////
    'Query database and display results in the chart
    '  borrowed from https://stackoverflow.com/questions/22638639/separating-chart-series
    '/////////////////////////////////
    'build sql for processing
    sql = "SELECT DateTime, MCECRate1 AS MCEC, AECCRate1 AS AECC, Manila, Gosnell, Lepanto, Reginold, Promised_Land " _
     & "FROM pt_demand WHERE DateTime BETWEEN '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString & "' " _
     & "ORDER BY DateTime ASC;"

    '---- REMOVE FROM PRODUCTION ----
    'Main.Msg2User(sql, "query")

    Using ds = dbSelect_DataSet(sql)
        Chart1.Series.Clear()       'dump all series and recreate
        Chart1.Titles.Clear()       'dump title for recreation later

        Chart1.Titles.Add("System Demands: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm"))

        Dim mcec As New Series()
        mcec.ChartArea = "ChartArea1"
        mcec.Name = "MCEC"
        Chart1.Series.Add(mcec)
        Chart1.Series(mcec.Name).Color = Color.Green
        Chart1.Series(mcec.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(mcec.Name).XValueMember = "DateTime"
        Chart1.Series(mcec.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(mcec.Name).YValueMembers = "MCEC"
        Chart1.Series(mcec.Name).YValueType = ChartValueType.Double

        Dim aecc As New Series()
        aecc.ChartArea = "ChartArea1"
        aecc.Name = "AECC"
        Chart1.Series.Add(aecc)
        Chart1.Series(aecc.Name).Color = Color.DarkGray
        Chart1.Series(aecc.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(aecc.Name).XValueMember = "DateTime"
        Chart1.Series(aecc.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(aecc.Name).YValueMembers = "AECC"
        Chart1.Series(aecc.Name).YValueType = ChartValueType.Double

        Dim m As New Series()
        m.ChartArea = "ChartArea1"
        m.Name = "Manila"
        Chart1.Series.Add(m)
        Chart1.Series(m.Name).Color = Color.Blue
        Chart1.Series(m.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(m.Name).XValueMember = "DateTime"
        Chart1.Series(m.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(m.Name).YValueMembers = "Manila"
        Chart1.Series(m.Name).YValueType = ChartValueType.Int32

        Dim g As New Series()
        g.ChartArea = "ChartArea1"
        g.Name = "Gosnell"
        Chart1.Series.Add(g)
        Chart1.Series(g.Name).Color = Color.Gold
        Chart1.Series(g.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(g.Name).XValueMember = "DateTime"
        Chart1.Series(g.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(g.Name).YValueMembers = "Gosnell"
        Chart1.Series(g.Name).YValueType = ChartValueType.Int32

        Dim l As New Series()
        l.ChartArea = "ChartArea1"
        l.Name = "Lepanto"
        Chart1.Series.Add(l)
        Chart1.Series(l.Name).Color = Color.Red
        Chart1.Series(l.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(l.Name).XValueMember = "DateTime"
        Chart1.Series(l.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(l.Name).YValueMembers = "Lepanto"
        Chart1.Series(l.Name).YValueType = ChartValueType.Int32

        Dim r As New Series()
        r.ChartArea = "ChartArea1"
        r.Name = "Reginold"
        Chart1.Series.Add(r)
        Chart1.Series(r.Name).Color = Color.Orange
        Chart1.Series(r.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(r.Name).XValueMember = "DateTime"
        Chart1.Series(r.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(r.Name).YValueMembers = "Reginold"
        Chart1.Series(r.Name).YValueType = ChartValueType.Int32

        Dim p As New Series()
        p.ChartArea = "ChartArea1"
        p.Name = "Promised Land"
        Chart1.Series.Add(p)
        Chart1.Series(p.Name).Color = Color.Purple
        Chart1.Series(p.Name).ChartType = SeriesChartType.Spline
        Chart1.Series(p.Name).XValueMember = "DateTime"
        Chart1.Series(p.Name).XValueType = ChartValueType.DateTime
        Chart1.Series(p.Name).YValueMembers = "Promised_Land"
        Chart1.Series(p.Name).YValueType = ChartValueType.Int32

        Chart1.Size = New System.Drawing.Size(973, 467)
        'Chart1.BackColor = Color.WhiteSmoke
        Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "MM/dd HH:mm"

        Chart1.DataSource = ds.Tables("pt_demand")
        Chart1.DataBind()
        Chart1.Visible = True

    End Using

End Sub

1 个答案:

答案 0 :(得分:0)

我仍然不完全确定为什么它在我尝试的前几次都不会工作,但我怀疑次要数据类型差异之间可能存在问题(前两个系列是十进制的,其余是整数)。

最终解决方案(下面的删节代码)是为两种不同类型的数据生成两个不同的图表,将图表保存到用户配置文件目录中的png,敲出一个html文件来处理最终格式,显示和打印需求,然后以编程方式打开文件。

以下示例代码仅包含:

  1. Chart2的生成,因为它是两个图表中更复杂的
  2. 根据需要设置图表的y轴和
  3. 的过程
  4. 将图表保存到磁盘的过程。

    dt.toString('dd-MM-yyyy')