Windows form chart control with multiple series not rendering

◇◆丶佛笑我妖孽 提交于 2019-12-11 21:42:56

问题


I have been researching chart controls for a few days and have seen that using DataBindXY with multiple series on one chart area should work. However mine is not working. I have tried both setting properties in the design form and creating the series in code behind on form page load. The first series will render but the second will not. I tried splitting into 2 sub routines and calling the subroutines on load but that did not work either.

Any ideas on how to get multiple series on the 1 line graph?

    Chart1.Series.Add("EndBearing")
    Chart1.Series(0).ChartType = SeriesChartType.Line
    Chart1.Series(0).Points.DataBindXY(array1, array2)

    Chart1.Series.Add("Ult")
    Chart1.Series(0).ChartType = SeriesChartType.Line
    Chart1.Series(0).Points.DataBindXY(array3, array2)

回答1:


Change Series(0) to Series("EndBearing") And also change the second one to Series("Ult"):

Chart1.Series.Add("EndBearing")
Chart1.Series("EndBearing").ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart1.Series("EndBearing").Points.DataBindXY(array1, array2)

Chart1.Series.Add("Ult")
Chart1.Series("Ult").ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart1.Series("Ult").Points.DataBindXY(array3, array2)

Reference: VB.NET Charts Tutorial




回答2:


Try this, first you must add these series from Chart's properties then add the following code :

Chart1.Series("EndBearing").ChartType = SeriesChartType.Line
Chart1.Series("EndBearing").Points.DataBindXY(array1, array2)

Chart1.Series("EndBearing").ChartType = SeriesChartType.Line
Chart1.Series("EndBearing").Points.DataBindXY(array3, array2)



回答3:


Please try this one:

            Dim myfont As New Font("Sans Serif", 20, FontStyle.Bold)
            dim rs as adodb.recordset  [connect your tablefield on table]
            'dim db as adodb.connection [connect your database]
            dim msg as string

            'Chart1
            Chart1.Titles.Add("")
            Chart1.Titles(0).Font = myfont

            Chart1.Series.Add("[series1]")
            Chart1.Series.Add("[series2]")
            Chart1.Series.Add("[series3]")
            Chart1.Series.Add("[series4]")

            Msg = "Select [grouping recored], Sum([series1]) As s1, Sum([series2]) As s2, Sum([series3]) As s3, Sum([series4]) As s4 "
            Msg = Msg & "From [table] "
            Msg = Msg & "Group By [grouping recored]"

            Rs = New ADODB.Recordset
            Rs.Open(Msg, Db, ADODB.CursorTypeEnum.adOpenStatic)
            While Rs.EOF <> True
                Chart1.Series("[series1]").Points.AddXY(Rs(0).Value, Rs(1).Value.ToString)
                Chart1.Series("[series2]").Points.AddXY(Rs(0).Value, Rs(2).Value)
                Chart1.Series("[series3]").Points.AddXY(Rs(0).Value, Rs(3).Value)
                Chart1.Series("[series4]").Points.AddXY(Rs(0).Value, Rs(4).Value)
                Rs.MoveNext()
            End While
            Rs.Close()                              

let me explain: database I call on form load so no need dim db on this case. it will cause an error.

    Dim Loc As String
    Dim Db As ADODB.Connection

        Loc = Application.StartupPath & "\Database\[databasename].mdb"
        Msg1 = "Provider=Microsoft.Jet.OLEDB.4.0;" & Chr(10)
        Msg1 = Msg1 & "Data Source='" & Loc & "';" & Chr(10)
        Msg1 = Msg1 & "Jet OLEDB:Database Password=[databasepass]& Chr(10)

    Try
        Db = New ADODB.Connection
        Db.Open(Msg1)
    Catch ex As Exception
        MsgBox("Can't connect to Database" & vbCrLf & ex.Message)
    End Try

put that on form_load source. (for sql connection or how to open excel file as reference you can search on internet how to used it.)

on this open case. you can put vertical value (y axis) as record index 0, and other index as horizontal (x axis)

so it could be like this:

    Chart1.Series("[series1]").Points.AddXY(Rs(1).Value, Rs(0).Value)
    Chart1.Series("[series2]").Points.AddXY(Rs(2).Value, Rs(0).Value)
    Chart1.Series("[series3]").Points.AddXY(Rs(3).Value, Rs(0).Value)
    Chart1.Series("[series3]").Points.AddXY(Rs(4).Value, Rs(0).Value)

you can try this code.

This is sample result Sample image

Hope it can help you and other. Thanks



来源:https://stackoverflow.com/questions/27448892/windows-form-chart-control-with-multiple-series-not-rendering

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!