Hello,
I would like to display a Line Chart depending of the DropDownList control.
When I choose a value of this DropDown, the Line Chart display with the average of my value column group by the value of my DropDown.
This is my code behind :
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "select distinct Entite from reponse"; DataTable dt = GetData(query); NomEntite.DataSource = dt; NomEntite.DataTextField = "Entite"; NomEntite.DataValueField = "Entite"; NomEntite.DataBind(); NomEntite.Items.Insert(0, new ListItem("Selectionnez", "")); } } protected void entite_SelectedIndexChanged(object sender, EventArgs e) { string query = string.Format("select Entite, AVG(AvancementQuantitatifT1 * 100) FROM reponse WHERE Entite = '{0}' GROUP BY Entite", NomEntite.SelectedItem.Value); DataTable dt = GetData(query); string[] x = new string[dt.Rows.Count]; decimal[] y = new decimal[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { x[i] = dt.Rows[i][0].ToString(); y[i] = Convert.ToInt32(dt.Rows[i][1]); } LineChart1.Series.Add(new AjaxControlToolkit.LineChartSeries { Data = y }); LineChart1.CategoriesAxis = string.Join(",", x); LineChart1.ChartTitle = string.Format("{0}", NomEntite.SelectedItem.Value); if (x.Length > 3) { LineChart1.ChartWidth = (x.Length * 75).ToString(); } LineChart1.Visible = true; } private static DataTable GetData(string query) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["connexionBase"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } }
The column Entite is a Varchar type and AvancementQuantitatifT1 is a decimal (3,2) type.
Nothing display when I choose a value of my DropDownList.