以编程方式将Chart添加到程序中,但不显示图表输出

时间:2019-04-02 11:48:53

标签: c# winforms charts

我正在动态创建图表,并将其与数据库表连接以显示图表输出,但是我的图表数据源无法获取数据库表,或者我不知道为什么不获得任何输出。数据源或数据绑定有什么问题?显示图表

 Chart addchart()
        {
            Chart chartA = new Chart();
            SqlConnection cs = new SqlConnection(@"Data Source=LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Task_2\Task_2\Database.mdf;Integrated Security=True");
            cs.Open();
            SqlCommand md=new SqlCommand("Select*from Agestatistics",cs);
            md.ExecuteNonQuery();
            cs.Close();
            try
            { Legend legend = new Legend();
            chartA.BackColor = Color.White;
            chartA.Location =new Point(t,l);
            chartA.Width = 370;
            chartA.Height = 250;
            chartA.Name = ""+x;
                chartA.Legends.Add(legend);
                legend.TitleFont = new Font(legend.Font.FontFamily, 7);
                //legend.Font = new Font("Arial", 8, FontStyle.Bold);
                legend.Title = "Age of the Employees";
                Series series;
                 series = new Series(""+x );
                 series[""+x] = "Disabled";
                series.IsValueShownAsLabel = true;
                series.LabelAngle = 0;
                series.Font = new Font("Arial", 8, FontStyle.Bold);
                // series.SmartLabelStyle.Enabled = true;
                series.ChartType = SeriesChartType.Pie;
                series.LegendToolTip = ("Age of The Employees");
                series.IsVisibleInLegend = true;
                chartA.Series.Add(series);
            ChartArea chartArea = new ChartArea();
            Axis yAxis = new Axis(chartArea, AxisName.Y);
            Axis xAxis = new Axis(chartArea, AxisName.X);
                 chartA.Series["" + x].XValueMember = "" + x;
                    chartA.Series["" + x].YValueMembers = "" + y;
                    chartArea.AxisX.Title = "Age";
                    chartArea.AxisY.Title = "Total";
                    chartA.ChartAreas.Add(chartArea);
                    chartA.Legends[0].ForeColor = Color.Blue;
                    chartA.Series["" + x].LabelForeColor=Color.DarkGreen;
                    chartA.Titles.Add("Age Statistics of The Employees");
                    chartA.DataSource = md;                        
                    chartA.DataBind();                        
                    chartA.Show();
                    control.Controls.Add(chartA);
                    chartA.Show();                     
            }
            catch
            {
                 MessageBox.Show("Connection Problem", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return chartA;

        }

0 个答案:

没有答案