Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
That my data (sp) from my database and i wanna display it in a chart when a button is clicked.

I coding on the run. I'm getting only the Q4 displayed on the x-axis when the btn is clicked (just one bar) instead of getting all Quaters displayed for Number1

Quater  Number1   Number2
  Q1     10         30
  Q2     20         40
  Q3     25         35
  Q4     80         55


My code
VB
private void InitializeChart()
        {
            this.components = new System.ComponentModel.Container();
            ChartArea chartArea1 = new ChartArea();
            Legend legend1 = new Legend() { BackColor = Color.Green, ForeColor = Color.Black, Title = "Salary" };
            Legend legend2 = new Legend() { BackColor = Color.Green, ForeColor = Color.Black, Title = "Anzahl" };
            
            barChart = new Chart();

            ((ISupportInitialize)(barChart)).BeginInit();

            SuspendLayout();

            //====Bar Chart
            chartArea1 = new ChartArea();
            chartArea1.Name = "BarChartArea";
            barChart.ChartAreas.Add(chartArea1);
            barChart.Dock = System.Windows.Forms.DockStyle.Fill;
            legend2.Name = "Legend2";
            barChart.Legends.Add(legend2);

            AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
            this.Load += new EventHandler(Form2_Load);
            ((ISupportInitialize)(this.barChart)).EndInit();
            this.ResumeLayout(false);
        }


VB
private void LoadBarChart()
        {

            string CS = ConfigurationManager.ConnectionStrings["BD"].ConnectionString;
            SqlConnection con = new SqlConnection(CS);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = ("[dbo].[spGetQuarter]");
            
            SqlDataReader myReader            
try
            {
                //DO SOMETHING
                con.Open();
                myReader = cmd.ExecuteReader();

                while (myReader.Read())
                {
                    barChart.Series.Clear();
                    barChart.BackColor = Color.LightYellow;
                    barChart.Palette = ChartColorPalette.Fire;
                    barChart.ChartAreas[0].BackColor = Color.Transparent;
                    barChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
                    barChart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;

                    Series series = new Series
                    {
                        Name = "series1",
                        IsVisibleInLegend = true,
                        ChartType = SeriesChartType.Column
                    };


                    barChart.Series.Add(series);
                    
    //Parameters (Seriesname, x-axis data & y-axis data)
this.barChart.Series["Series1"].Points.AddXY(myReader["Quarter"],myReader["Number1"]);

  barChart.Invalidate();
  pnlChart.Controls.Add(barChart);


                    

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }

           
        }


VB
private void btnGet_Click(object sender, EventArgs e)
        {

            
                barChart.Titles.Add("Title ");
                LoadBarChart();   
   
        }



Will be glad for any help
Posted

ahumm...
C#
while (myReader.Read())
                {
                    barChart.Series.Clear();


for each new datarow you clear all the chart series ending up with 1 bar....
clear before the while.
 
Share this answer
 
Comments
mikybrain1 25-Mar-15 9:56am    
Hi Digimanus
I' ve tried that but it' still populating the chart with only Q4

barChart.Series.Clear();
while (myReader.Read())
{
Herman<T>.Instance 25-Mar-15 10:01am    
Name = "series1" --> foreach serie the name is identical!
Herman<T>.Instance 25-Mar-15 10:03am    
anyway: Clear before the while!!!
Herman<T>.Instance 25-Mar-15 10:12am    
what does this do?

barChart.Invalidate();
mikybrain1 25-Mar-15 10:22am    
It just updates the chart area. it's not all the important. I can comment it out.

By the way my problem still not solved. I have cleared as u suggested without any success :(
I solved it through the property panel. Added a new series and then
edited my existing code on scratch.
 
Share this answer
 

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



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