【问题标题】:C# how to make a live bar chart (Livecharts library)C# 如何制作实时条形图(Livecharts 库)
【发布时间】:2023-03-08 03:00:01
【问题描述】:

我正在将数值数据传输到笛卡尔图中的图表。但是,从数据库中提取数据名称时出现问题。名称不出现。我希望我的数据在笛卡尔图旁边显示为饼图。我想将“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());
    }
}

【问题讨论】:

  • 你的标签太没用了。你用什么图表库?
  • 我使用实时图表库
  • @BrktrL,有更新吗?请检查我的答案是否适合您。
  • @JackJJun-MSFT 非常感谢你。你帮了我很多。
  • @JackJJun-MSFT 我能问最后一件事吗?它在图形名称的开头显示“系列”。示例=“系列名称”,“系列 Blabla”等。为什么会有“系列”文本。如何在笛卡尔图中删除此“系列”文本?

标签: c# visual-studio winforms visual-studio-2010 livecharts


【解决方案1】:

根据我的测试,你可以使用linq动态设置labelpoint的tooltip。

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

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

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多