C# 如何制作实时条形图(Livecharts 库)

C# how to make a live bar chart (Livecharts library)

我正在将数值数据传输到笛卡尔图中的图形。但是,从数据库中提取数据名称时遇到问题。名字不会出现。我希望我的数据显示为笛卡尔图旁边的饼图。我想将“GelirAdi”列中的数据与图表匹配。总之,“GelirAdi”列将是名称,“GelirMiktari”将是笛卡尔图中的数值,但我该怎么做?

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

    SQLiteConnection baglanti = new SQLiteConnection(veritabaniyolu);
    baglanti.Open();
    string sql_tarih_sorgula = "SELECT * FROM Gelirler";
    SQLiteDataAdapter da = new SQLiteDataAdapter(sql_tarih_sorgula, baglanti);
    DataTable dt = new DataTable();
    da.Fill(dt);
    bunifuDataGridView1.DataSource = dt;
    baglanti.Close();

    ColumnSeries series2 = new ColumnSeries()
    {
        DataLabels = true,
        Values = new ChartValues<int>(),
        LabelPoint = point => point.Y.ToString()
    };
    Axis axisX = new Axis() {
        Separator = new Separator() { Step = 1, IsEnabled = false },
        Labels=new List<string>()
    };

    Axis axisY = new Axis()
    {
        LabelFormatter=y=>y.ToString(),
        Separator=new Separator()

    };
    cartesianChart1.Series.Add(series2);
    cartesianChart1.AxisX.Add(axisX);
    cartesianChart1.AxisY.Add(axisY);
    foreach (DataGridViewRow item in bunifuDataGridView1.Rows) {

        int a = Convert.ToInt32(item.Cells["GelirMiktari"].Value);
        series2.Values.Add(a);
        axisX.Labels.Add(item.Cells["GelirAdi"].Value.ToString());
    }
}

根据我的测试,您可以使用 linq 动态设置标签点的工具提示。

你可以试试下面的代码来实现

 private void Form1_Load(object sender, EventArgs e)
        {

            SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
            m_dbConnection.Open();
            SQLiteDataAdapter da = new SQLiteDataAdapter("select * from Product", m_dbConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void button1_Click(object sender, EventArgs e)
    {
        ColumnSeries series2 = new ColumnSeries()
        {
            DataLabels = true,
            Values = new ChartValues<int>(),
            LabelPoint = point => dataGridView1.Rows.Cast<DataGridViewRow>().Where(i => Convert.ToDouble(i.Cells["number"].Value) == point.Y).Select(i => i.Cells["name"].Value.ToString()).First(),
            Title = ""               //Here you added to remove  "Series" text
            
        };
        Axis axisX = new Axis()
        {
            Separator = new Separator() { Step = 1, IsEnabled = false },
            Labels = new List<string>()
            
        };

        Axis axisY = new Axis()
        {
            LabelFormatter = y => y.ToString(),
            Separator = new Separator()

        };
        cartesianChart1.Series.Add(series2);
        cartesianChart1.AxisX.Add(axisX);
        cartesianChart1.AxisY.Add(axisY);
        foreach (DataGridViewRow item in dataGridView1.Rows)
        {

            int a = Convert.ToInt32(item.Cells["number"].Value);
            series2.Values.Add(a);
        }
    }

结果: