How to set x-axis Min and Max value from Database table in MSChart Control ASP.Net

Last Reply on Jul 04, 2014 03:01 AM By Priyanka12

Posted on Jul 03, 2014 06:30 AM

Suppose if DB table has x-axis values as below for particular Id:


65    
67.5
68.5  
70.0
78.5
80.0   
81.5
82.5  
83.0  
83.5  
84.0  
84.5  
85.5  
86.5 

Then it should select x-axis Min value as 65 (for that particular Id), in below line:

//Chart1.ChartAreas["ChartArea1"].AxisX.Minimum 

and it should select x-axis Max value as 86.5 (for that particular Id, in below line:

//Chart1.ChartAreas["ChartArea1"].AxisX.Maximum 

These values differ everytime for different Id. ( particular Id has multiple x-
So everytime for particular Id, it should take x-axis Min and Max value from Database.

I tried below code, but its not working.
(Also this code enables MajorGrid = true dont know how. I had set it Enable = false from code behind.)

string query = "SELECT max(x) from Tables where Param_ID= '" + id + "' and DeletionDate is null";
DataTable dt2 = GetData(query);
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = Convert.ToDouble(dt2);

Please reply how to solve it.

Posted on Jul 03, 2014 07:03 AM

You are retreiving values from database for X and Y axis. So you can find the max and min value of X and Y axis from DataTable.

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("X",typeof(decimal));
        dt.Columns.Add("Y", typeof(decimal));
        dt.Rows.Add(65,10);
        dt.Rows.Add(88,45);
        dt.Rows.Add(45,50);
        dt.Rows.Add(12,78);
        dt.Rows.Add(80,75);
        decimal Xmax = dt.Select().Select(c => Convert.ToDecimal(c["X"])).Max();
        decimal Xmin = dt.Select().Select(c => Convert.ToDecimal(c["X"])).Min();

        decimal Ymax = dt.Select().Select(c => Convert.ToDecimal(c["Y"])).Max();
        decimal Ymin = dt.Select().Select(c => Convert.ToDecimal(c["Y"])).Min();
    }
}

 


Posted on Jul 04, 2014 03:01 AM

Used below code and is working fine:

string id = Request.QueryString[1].ToString();

string query = "select X,Y from Table where DeletionDate is null order by X";
DataTable dt = GetData(query);

double[] x = new double[dt.Rows.Count];
double[] y = new double[dt.Rows.Count];

for (int i = 0; i < dt.Rows.Count; i++)
{
    x[i] = Convert.ToDouble(dt.Rows[i][0].ToString());
    y[i] = Convert.ToDouble(dt.Rows[i][1].ToString());
}
Chart1.Series[0].Points.DataBindXY(x, y);

double Xmax = dt.Select().Select(c => Convert.ToDouble(c["X"])).Max();
double Xmin = dt.Select().Select(c => Convert.ToDouble(c["X"])).Min();

Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = Xmax;  // for x min value from DB
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = Xmin;  // for x max value from DB