Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
admin  
#1 Posted : Saturday, November 4, 2023 9:30:16 AM(UTC)
admin

Rank: Administration

Groups: Administrators
Joined: 12/24/2017(UTC)
Posts: 2,573


Legend: Phần chú thích nhận biết cột nào của Series nào (Loại dữ liệu duy nhất)

<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px">
    <Titles>
        <asp:Title ShadowOffset="3" Name="Items" />
    </Titles>
    <Legends>
        <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
            LegendStyle="Row" />
    </Legends>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1" BorderWidth="0" />
    </ChartAreas>
</asp:Chart>

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;
        }
    }
}
Users browsing this topic
Guest (3)
Similar Topics
Đặt màu sắc cột của chart theo nhóm cột (Chart)
by admin 11/4/2023 10:44:04 AM(UTC)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.