C# Datatable过滤数据到--> Livechart(piechart)

C# Datatable filtered data to --> Livechart(piechart)

请先看图。我想做的是:将table中过滤后的数据按照饼图进行数值化传递。请阅读代码。我想将“GelirMiktari”列中的数据导出到饼图。为什么都写25%。 我希望根据“GiderMiktari”列中的数据对饼图进行整形。

enter image description here

private void btn_gider_bilgi_getir_Click(object sender, EventArgs e)
        {
            string veritabaniyolu = "Data source=veritabani.db";
            string ay = cbox_g_gun.Text;
            string yil = cbox_g_yil.Text;
            bunifuDataGridView1.DataSource = null;

            SQLiteConnection baglanti = new SQLiteConnection(veritabaniyolu);
            baglanti.Open();
            string sql_tarih_sorgula = $"SELECT * FROM Gelirler WHERE GelirTarihi BETWEEN '{yil}-{ay}-01' AND '{yil}-{ay}-31'";
            SQLiteDataAdapter da = new SQLiteDataAdapter(sql_tarih_sorgula, baglanti);
            DataTable dt = new DataTable();
            da.Fill(dt);
            bunifuDataGridView1.DataSource = dt;
            baglanti.Close();

                Func<ChartPoint, string> labelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
                SeriesCollection piechartData = new SeriesCollection();
                var collection = bunifuDataGridView1.Rows.Cast<DataGridViewRow>().GroupBy(x => x.Cells[1].Value).Where(g => g.Count() > 0).Select(y => new { Element = y.Key, Counter = y.Count() }).ToList();
                foreach (var item in collection)
                {
                    piechartData.Add(new PieSeries { Title = Convert.ToString(item.Element), Values = new ChartValues<int> { (int)item.Counter }, DataLabels = true, LabelPoint = labelPoint });
    
                }
    
                pieChart1.Series = piechartData;
                pieChart1.LegendLocation = LegendLocation.Right;}

您可以尝试使用以下代码创建一个饼图,用于根据日期时间列过滤数字列。

 private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
            m_dbConnection.Open();
            string sql = string.Format("Select * From example where Birthdate between '{0}-{1}-01' AND '{0}-{1}-31'", textBox1.Text,textBox2.Text);
            SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, m_dbConnection);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
            m_dbConnection.Close();
            Func<ChartPoint, string> labelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
            // Define a collection of items to display in the chart 
            SeriesCollection piechartData = new SeriesCollection();
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                piechartData.Add(new PieSeries { Title = Convert.ToDateTime(item.Cells["Birthdate"].Value).ToShortDateString(), Values = new ChartValues<double> {Convert.ToDouble(item.Cells["Number"].Value)}, DataLabels = true, LabelPoint = labelPoint });
            }
            pieChart1.Series = piechartData;
            // Set the legend location to appear in the Right side of the chart
            pieChart1.LegendLocation = LegendLocation.Right;
        }

结果: