📄 defaultcs.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.QuickStart;
using Telerik.WebControls;
namespace Telerik.ChartExamplesCS.DatabaseBinding
{
/// <summary>
/// Summary description for _Default.
/// </summary>
public class DefaultCS: XhtmlPage
{
static Color[] colors = {Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, Color.Azure, Color.CornflowerBlue, Color.SteelBlue};
protected System.Web.UI.WebControls.DropDownList dropDownCategory;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.DropDownList dropDownYears;
protected System.Web.UI.WebControls.Label Label1;
protected Telerik.WebControls.RadChart RadChart2;
protected Telerik.WebControls.RadChart RadChart1;
protected OleDbConnection dbCon;
#region Custom methods
private void InitDropDownCategory(OleDbConnection dbCon)
{
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Category", dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
dropDownCategory.Items.Clear();
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if (dbRow["name"] != null)
{
dropDownCategory.Items.Add( (string) dbRow["name"]);
}
}
}
private void InitDropDownYear(OleDbConnection dbCon)
{
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT DISTINCT(Year) FROM Data", dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if (dbRow["Year"] != null)
{
dropDownYears.Items.Add( ( (int) dbRow["Year"] ).ToString());
}
}
}
private void InitRadChart1(OleDbConnection dbCon)
{
UpdateRadChart1(dbCon);
RadChart1.Title1.Visible = true;
}
private void InitRadChart2(OleDbConnection dbCon)
{
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.
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT Distinct(Year) FROM Data ORDER BY Year", dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
RadChart2.XAxis.Clear();
RadChart2.XAxis.AutoScale = false;
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
RadChart2.XAxis.AddItem( ((int) dbRow["Year"]).ToString() );
}
UpdateRadChart2(dbCon);
}
private void UpdateRadChart1(OleDbConnection dbCon)
{
// Set a query to database.
string sqlString = "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);
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlString, dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
// Gets the one and the only series in the chart.
ChartSeries s0 = RadChart1.GetChartSeries(0);
// If it doesn't exist - create and set it.
if (s0 == null)
{
s0 = RadChart1.CreateSeries(string.Empty, Color.Blue, ChartSeriesType.Pie);
}
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.
int i = 0;
foreach (DataRow dbRow in ds.Tables[0].Rows)
{
if (dbRow["Sum"] != null)
{
/*
ChartSeriesItem seriesItem = new ChartSeriesItem((double) dbRow["Sum"], dropDownCategory.Items[i++].Text);
seriesItem.Appearance.BorderColor = Color.Black;
s0.Items.Add(seriesItem);
*/
ChartSeriesItem seriesItem = new ChartSeriesItem();
seriesItem.YValue = (double) dbRow["Sum"];
seriesItem.Name = dropDownCategory.Items[i++].Text;
seriesItem.Appearance.BorderColor = Color.Black;
s0.Items.Add(seriesItem);
}
}
// Set additional chart properties and settings.
RadChart1.Title1.Text = "Gross Domestic Product By Categories For " + dropDownYears.SelectedItem.Text;
RadChart1.Title1.Visible = true;
}
private void UpdateRadChart2(OleDbConnection dbCon)
{
// Remove the previous series.
RadChart2.ChartSeriesCollection.Clear();
// Form sql query to the database.
string sqlString = "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);
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlString, dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
// Load data.
int oldsubcategory_id = -1;
int subcategory_id;
ChartSeries currentSeries = null;
foreach (DataRow dbRow in ds.Tables[0].Rows)
{
subcategory_id = (int) dbRow["Id"];
if (subcategory_id != oldsubcategory_id)
{
currentSeries = RadChart2.CreateSeries((string) dbRow["Name"], Color.Blue, ChartSeriesType.Bar);
currentSeries.Appearance.BorderColor = Color.Black;
currentSeries.ShowLabels = false;
oldsubcategory_id = subcategory_id;
}
if (currentSeries != null)
{
currentSeries.AddItem( (double) dbRow["Value"]);
}
}
// Set colors for the series in the series collection.
int i = 0;
foreach (ChartSeries series in RadChart2.ChartSeriesCollection)
{
series.MainColor = colors[i++ % colors.Length];
series.Appearance.FillStyle = FillStyle.Solid;
}
// Set additional properties and settings for the chart.
RadChart2.Title1.Text = dropDownCategory.SelectedItem.Text;
}
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
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();
}
}
#region Custom Events
private void dropDownCategory_SelectedIndexChanged(object sender, System.EventArgs e)
{
dbCon = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("chart.mdb"));
dbCon.Open();
UpdateRadChart2(dbCon);
dbCon.Close();
}
private void dropDownYears_SelectedIndexChanged(object sender, System.EventArgs e)
{
dbCon = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("chart.mdb"));
dbCon.Open();
UpdateRadChart1(dbCon);
dbCon.Close();
}
#endregion
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dropDownYears.SelectedIndexChanged += new System.EventHandler(this.dropDownYears_SelectedIndexChanged);
this.dropDownCategory.SelectedIndexChanged += new System.EventHandler(this.dropDownCategory_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -