📄 defaultvb.aspx.vb
字号:
Imports Telerik.QuickStart
Imports System.Data.OleDb
Imports Telerik.WebControls
Namespace Telerik.ChartExamplesVB.Programming.DatabaseBinding
Public Class DefaultVB
Inherits XhtmlPage
Private Shared colors As Color() = {Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, Color.Azure, Color.CornflowerBlue, Color.SteelBlue}
Protected WithEvents dropDownCategory As System.Web.UI.WebControls.DropDownList
Protected Label2 As System.Web.UI.WebControls.Label
Protected WithEvents dropDownYears As System.Web.UI.WebControls.DropDownList
Protected Label1 As System.Web.UI.WebControls.Label
Protected RadChart2 As Telerik.WebControls.RadChart
Protected RadChart1 As Telerik.WebControls.RadChart
Protected dbCon As OleDbConnection
#Region "Custom methods"
Private Sub InitDropDownCategory(ByVal dbCon As OleDbConnection)
Dim adapter As New OleDbDataAdapter("SELECT * FROM Category", dbCon)
Dim ds As New DataSet()
adapter.Fill(ds)
dropDownCategory.Items.Clear()
Dim dbRow As DataRow
For Each dbRow In ds.Tables(0).Rows
If Not (dbRow("name") Is Nothing) Then
dropDownCategory.Items.Add(CStr(dbRow("name")))
End If
Next dbRow
End Sub 'InitDropDownCategory
Private Sub InitDropDownYear(ByVal dbCon As OleDbConnection)
Dim adapter As New OleDbDataAdapter("SELECT DISTINCT(Year) FROM Data", dbCon)
Dim ds As New DataSet()
adapter.Fill(ds)
Dim dbRow As DataRow
For Each dbRow In ds.Tables(0).Rows
If Not (dbRow("Year") Is Nothing) Then
dropDownYears.Items.Add(CInt(dbRow("Year")).ToString())
End If
Next dbRow
End Sub 'InitDropDownYear
Private Sub InitRadChart1(ByVal dbCon As OleDbConnection)
UpdateRadChart1(dbCon)
RadChart1.Title1.Visible = True
End Sub 'InitRadChart1
Private Sub InitRadChart2(ByVal dbCon As OleDbConnection)
RadChart2.BarOverlapPercent = 40
RadChart2.BarWidthPercent = 80
RadChart2.Legend.Position = ChartPosition.Bottom
RadChart2.Legend.VSpacing = 10
RadChart2.Legend.HeightRatio = 0.75
RadChart2.Margins.Left = Unit.Percentage(10)
RadChart2.Margins.Bottom = Unit.Percentage(40)
RadChart2.Margins.Right = Unit.Percentage(2)
RadChart2.Gridlines.Visible = True
RadChart2.Gridlines.VerticalGridlines.Visible = False
' Loading all different years and initializing X axis items.
Dim adapter As New OleDbDataAdapter("SELECT Distinct(Year) FROM Data ORDER BY Year", dbCon)
Dim ds As New DataSet()
adapter.Fill(ds)
RadChart2.XAxis.Clear()
RadChart2.XAxis.AutoScale = False
Dim dbRow As DataRow
For Each dbRow In ds.Tables(0).Rows
RadChart2.XAxis.AddItem(CInt(dbRow("Year")).ToString())
Next dbRow
UpdateRadChart2(dbCon)
End Sub 'InitRadChart2
Private Sub UpdateRadChart1(ByVal dbCon As OleDbConnection)
' Set a query to database.
Dim sqlString As String = "SELECT C.ID, SUM(Value) AS [Sum] FROM (Category AS C INNER JOIN Subcategory AS SC ON C.Id=SC.Category_id) INNER JOIN [Data] AS D ON SC.ID=D.SubCategory_Id WHERE [Year]={0} GROUP BY C.ID ORDER BY C.ID"
sqlString = String.Format(sqlString, dropDownYears.SelectedItem.Value)
Dim adapter As New OleDbDataAdapter(sqlString, dbCon)
Dim ds As New DataSet()
adapter.Fill(ds)
' Gets the one and the only series in the chart.
Dim s0 As ChartSeries = RadChart1.GetChartSeries(0)
' If it doesn't exist - create and set it.
If s0 Is Nothing Then
s0 = RadChart1.CreateSeries(String.Empty, Color.Blue, ChartSeriesType.Pie)
End If
s0.Type = ChartSeriesType.Pie
s0.ShowLabels = True
s0.DefaultLabel = "#Y, #%"
s0.ValueFormat = ".00"
s0.LabelAppearance.Background.MainColor = Color.White
s0.LabelAppearance.Background.BorderColor = Color.Black
s0.LabelAppearance.TextColor = Color.Black
' Clear series items.
s0.Clear()
' Set new items for the series.
Dim i As Integer = 0
Dim dbRow As DataRow
For Each dbRow In ds.Tables(0).Rows
If Not (dbRow("Sum") Is Nothing) Then
'
' ChartSeriesItem seriesItem = new ChartSeriesItem((double) dbRow["Sum"], dropDownCategory.Items[i++].Text);
' seriesItem.Appearance.BorderColor = Color.Black;
' s0.Items.Add(seriesItem);
'
Dim seriesItem As New ChartSeriesItem()
seriesItem.YValue = CDbl(dbRow("Sum"))
seriesItem.Name = dropDownCategory.Items(i).Text
i = i + 1
seriesItem.Appearance.BorderColor = Color.Black
s0.Items.Add(seriesItem)
End If
Next dbRow
' Set additional chart properties and settings.
RadChart1.Title1.Text = "Gross Domestic Product By Categories For " + dropDownYears.SelectedItem.Text
RadChart1.Title1.Visible = True
End Sub 'UpdateRadChart1
Private Sub UpdateRadChart2(ByVal dbCon As OleDbConnection)
' Remove the previous series.
RadChart2.ChartSeriesCollection.Clear()
' Form sql query to the database.
Dim sqlString As String = "SELECT SC.Id, SC.Name, D.Year, D.Value FROM (Category AS C INNER JOIN Subcategory AS SC ON C.Id=SC.Category_id) INNER JOIN Data AS D ON SC.ID=D.SubCategory_Id WHERE C.ID={0} ORDER BY C.ID, SC.ID, Year;"
sqlString = String.Format(sqlString, dropDownCategory.SelectedIndex + 1)
Dim adapter As New OleDbDataAdapter(sqlString, dbCon)
Dim ds As New DataSet()
adapter.Fill(ds)
' Load data.
Dim oldsubcategory_id As Integer = -1
Dim subcategory_id As Integer
Dim currentSeries As ChartSeries = Nothing
Dim dbRow As DataRow
For Each dbRow In ds.Tables(0).Rows
subcategory_id = CInt(dbRow("Id"))
If subcategory_id <> oldsubcategory_id Then
currentSeries = RadChart2.CreateSeries(CStr(dbRow("Name")), Color.Blue, ChartSeriesType.Bar)
currentSeries.Appearance.BorderColor = Color.Black
currentSeries.ShowLabels = False
oldsubcategory_id = subcategory_id
End If
If Not (currentSeries Is Nothing) Then
currentSeries.AddItem(CDbl(dbRow("Value")))
End If
Next dbRow
' Set colors for the series in the series collection.
Dim i As Integer = 0
Dim series As ChartSeries
For Each series In RadChart2.ChartSeriesCollection
series.MainColor = colors((i Mod colors.Length))
series.Appearance.FillStyle = FillStyle.Solid
i = i + 1
Next series
' Set additional properties and settings for the chart.
RadChart2.Title1.Text = dropDownCategory.SelectedItem.Text
End Sub 'UpdateRadChart2
#End Region
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
dbCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("chart.mdb"))
dbCon.Open()
InitDropDownYear(dbCon)
InitDropDownCategory(dbCon)
InitRadChart1(dbCon)
InitRadChart2(dbCon)
dbCon.Close()
End If
End Sub 'Page_Load
#Region "Custom Events"
Private Sub dropDownCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropDownCategory.SelectedIndexChanged
dbCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("chart.mdb"))
dbCon.Open()
UpdateRadChart2(dbCon)
dbCon.Close()
End Sub 'dropDownCategory_SelectedIndexChanged
Private Sub dropDownYears_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropDownYears.SelectedIndexChanged
dbCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("chart.mdb"))
dbCon.Open()
UpdateRadChart1(dbCon)
dbCon.Close()
End Sub 'dropDownYears_SelectedIndexChanged
#End Region
#Region "Web Form Designer generated code"
Protected Overrides Sub OnInit(ByVal e As EventArgs)
'
' CODEGEN: This call is required by the ASP.NET Web Form Designer.
'
InitializeComponent()
MyBase.OnInit(e)
End Sub 'OnInit
'/ Required method for Designer support - do not modify
'/ the contents of this method with the code editor.
'/ </summary>
Private Sub InitializeComponent()
End Sub 'InitializeComponent
#End Region
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -