简体   繁体   中英

Unable To Change SeriesChartType Dynamically in MS Chart Control

I am using the MS Chart control (version 3.5.0). I have added it by using the designer (drag and drop). I removed the default "Series1" from the Properties -> Series -> Collection so that the chart contains no data.

I am adding the data at runtime based on a canned query to a SQLite DB. Like so:

Dim SQL As String = "SELECT * FROM ageLength ORDER BY month"
Dim cmd As New SQLiteCommand(SQL)
Dim SqLiteConnection1 As SQLiteConnection = New SQLiteConnection()
SqLiteConnection1.ConnectionString = "Data Source=" & My.Application.Info.DirectoryPath & "\Data\UserData.db3;"
cmd.Connection = SqLiteConnection1
Dim da As New SQLiteDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "ageLength")

Dim Series1 As Series = New Series()
Dim Series2 As Series = New Series()
Series1.Name = "Pcnt2nd"
Series1.ChartType = SeriesChartType.Spline
Series2.Name = "Pcnt98th"
Series2.ChartType = SeriesChartType.Spline
Chart1.Series.Add("Pcnt2nd")
Chart1.Series.Add("Pcnt98th")

Chart1.Series("Pcnt2nd").XValueMember = "month"
Chart1.Series("Pcnt2nd").YValueMembers = "Pcnt2nd"
Chart1.Series("Pcnt98th").XValueMember = "month"
Chart1.Series("Pcnt98th").YValueMembers = "Pcnt98th"
Chart1.DataSource = ds.Tables(0)

The data is displayed on the Chart, however, it is a Bar type graph. I set it to use the Spline type for both series. I am not sure what I missed. Any input is greatly appreciated.

After doing a lot more messing around I found the solution: I remove these 2 lines:

Series1.ChartType = SeriesChartType.Spline
Series2.ChartType = SeriesChartType.Spline

And then I add these 2 lines:

Chart1.Series("Pcnt2nd").ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart1.Series("Pcnt98th").ChartType = DataVisualization.Charting.SeriesChartType.Line

Not sure why that is...but there you have it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM