Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,

So I want to plot a bar graph, with X- values A,B,C,D against Y-values from dataset fill by a sql query.

How do i plot this graph using Chart control in VS.

Would appreciate direction to an example or a good tutorial.
Posted

the best:is this one[^]
 
Share this answer
 
C#
private void button1_Click(object sender, EventArgs e)
        {
            chart1.Series.Clear();

            string test ="" ,test2 = "",test3 = "";
            datatb2 = new DataTable();

            int index = 0;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[1].Value != null && (bool)row.Cells[1].FormattedValue)
                {
                    String idtoselect = row.Cells[0].Value.ToString();
                    try
                    {
                        string sql2 = "select NAME_Product,Unit.NAME_Unit from Product,unit  where Product.ID_Unit = Unit.ID_Unit AND Product.ID_Product = '" + idtoselect + "' ";
                        SqlCommand testcommand2 = new SqlCommand(sql2, conn);
                        rdr = testcommand2.ExecuteReader();
                        if (rdr.Read())
                        {
                            test = rdr["NAME_Product"].ToString();
                            test2 = rdr["NAME_Unit"].ToString();
                        }
                        rdr.Close();
                        test3 = test +"("+ test2+")";

                        try
                        {
                            string sql = "SELECT Product.NAME_Product AS test1, SUM(Reproduct.QUANTITY_Reproduct) AS quan1, CONVERT(VARCHAR(10), Reproduct.DATE_Reproduct, 103) AS Expr1 FROM  Reproduct INNER JOIN Product ON Reproduct.ID_Product = Product.ID_Product where Reproduct.ID_Product = @ID AND Reproduct.ID_Product = Product.ID_Product  AND DATE_Reproduct BETWEEN @D1 AND @D2 GROUP BY Reproduct.ID_Product,Product.ID_Product, Product.NAME_Product, Reproduct.DATE_Reproduct";
                            da = new SqlDataAdapter(sql, conn);

                            da.SelectCommand.Parameters.AddWithValue("@ID", idtoselect);
                            da.SelectCommand.Parameters.AddWithValue("@D1", startDate.Value);
                            da.SelectCommand.Parameters.AddWithValue("@D2", finDate.Value);

                            datatb = new DataTable();

                            da.Fill(datatb);
                            da.Fill(datatb2);


                            //chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
                            chart1.Series.Add(test3);
                            for (int i = 0; i < datatb.Rows.Count; i++)
                            { 
                                chart1.Series[test3].Points.AddXY(datatb.Rows[i].ItemArray.GetValue(0), datatb.Rows[i].ItemArray.GetValue(1));
                            }
                            
                            chart1.Series[test3].ChartArea = "ChartArea1";
                            chart1.Series[test3].BorderWidth = 2;
                            chart1.Series[test3].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
                            chart1.Series[test3].YValueMembers = "quan1";
                            chart1.Series[test3].XValueMember = "test1";
                            chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = false;



                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("test :" + ex);
                        }

                    }
                    catch
                    {
                        MessageBox.Show("Exception");
                    }


                }
            }

            chart1.DataBind();
            dataGridView2.DataSource = datatb2;
            setdatagrid2();
        }


this is my code
 
Share this answer
 
Comments
nikki88 17-Feb-12 2:22am    
Thank you, this was very helpful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900