【问题标题】:C# Datatable filtered data to --> Livechart(piechart)C# Datatable 过滤数据到 --> Livechart(piechart)
【发布时间】:2021-05-12 21:18:29
【问题描述】:

请先看图。我要做的是:根据饼图将表格中过滤后的数据以数字方式传递。请阅读代码。我想将“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;}

【问题讨论】:

    标签: c# visual-studio winforms c#-4.0 charts


    【解决方案1】:

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

     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;
            }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多