Hi vishalkal,
Using the below article i have created an example.
Check this example. Now please take its reference and correct your code.
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms.DataVisualization.Charting;
C#
private void Form1_Load(object sender, EventArgs e)
{
string query = "SELECT ShipCity, COUNT(OrderId) [Total]";
query += " FROM Orders WHERE ShipCountry = 'Brazil'";
query += " GROUP BY ShipCity";
DataTable dt = GetData(query);
string[] x = (from p in dt.AsEnumerable()
orderby p.Field<string>("ShipCity") ascending
select p.Field<string>("ShipCity")).ToArray();
int[] y = (from p in dt.AsEnumerable()
orderby p.Field<string>("ShipCity") ascending
select p.Field<int>("Total")).ToArray();
Chart1.Series[0].LegendText = "Brazil Order";
Chart1.Series[0].ChartType = SeriesChartType.Line;
Chart1.Series[0].BorderWidth = 3;
Chart1.Series[0].IsValueShownAsLabel = true;
Chart1.Series[0].Points.DataBindXY(x, y);
this.Chart1.Titles.Add("Graph Analysis");
}
private static DataTable GetData(string query)
{
string constr = @"Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
private void Chart1_MouseMove(object sender, MouseEventArgs e)
{
HitTestResult result = Chart1.HitTest(e.X, e.Y);
if (result.ChartElementType == ChartElementType.DataPoint)
{
this.Cursor = Cursors.Hand;
lblValue.Text = Chart1.Series[0].Points[result.PointIndex].YValues[0].ToString();
}
else
{
this.Cursor = Cursors.Default;
}
}
Screenshot