protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Fetch the Statistical data from database.
string query = "SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total]";
query += " FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil')";
query += " GROUP BY ShipCountry, DATEPART(Year, OrderDate)";
DataTable dt = GetData(query);
//Xóa hết các Series đang có
Chart1.Series.Clear();
//Get the DISTINCT Countries.
List<string> countries = (from p in dt.AsEnumerable()
select p.Field<string>("ShipCountry")).Distinct().ToList();
//Loop through the Countries.
foreach (string country in countries)
{
//Get the Year for each Country.
int[] x = (from p in dt.AsEnumerable()
where p.Field<string>("ShipCountry") == country
orderby p.Field<int>("Year") ascending //Sắp xếp thứ tự theo 'Year' kiểu int
select p.Field<int>("Year")).ToArray(); //Lấy dữ liệu'Year' kiểu int cho mảng x
//Get the Total of Orders for each Country.
int[] y = (from p in dt.AsEnumerable()
where p.Field<string>("ShipCountry") == country
orderby p.Field<int>("Year") ascending
select p.Field<int>("Total")).ToArray(); //Lấy dữ liệu 'Total' kiểu int cho mảng y
//Add Series to the Chart.
Chart1.Series.Add(new Series(country));
Chart1.Series[country].IsValueShownAsLabel = true;
Chart1.Series[country].ChartType = SeriesChartType.Bar;
Chart1.Series[country].Points.DataBindXY(x, y);
}
Chart1.Legends[0].Enabled = true;
}
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}