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
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);
}
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();
}
}
private void btnGet_Click(object sender, EventArgs e)
{
barChart.Titles.Add("Title ");
LoadBarChart();
}
Will be glad for any help