Reputation: 135
I am transferring the numerical data to the graph in a Cartesian graph. However, I have a problem when extracting the data names from the database. Names do not appear. I want my data to appear like a pie chart next to the Cartesian graph. I want to match the data in the "GelirAdi" column to the chart. In summary "GelirAdi" column will be name, "GelirMiktari" will be numeric value in cartesien graph but how can I do that?
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());
}
}
Upvotes: 0
Views: 3953
Reputation: 5986
Based on my test, you can set the labelpoint's tooltip dynamically by using linq.
You can try the following code to achieve it.
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);
}
}
Result:
Upvotes: 2