winforms图表仅显示数据库中的一个条形图 [英] winforms chart shows only one bar from database

查看:66
本文介绍了winforms图表仅显示数据库中的一个条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库(sp)来自我的数据库,我想在点击按钮时将其显示在图表中。



我在运行时编码。单击btn时,我只在x轴上显示Q4(只有一个条),而不是为Number1显示所有的Quater



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

推荐答案

ahumm ...

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




每个新数据的
你清除所有以1 bar结尾的图表系列....

在此之前清除。



for each new datarow you clear all the chart series ending up with 1 bar....
clear before the while.


I solved it through the property panel. Added a new series and then
edited my existing code on scratch.


这篇关于winforms图表仅显示数据库中的一个条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆