Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to set X-axis and Y-axis values from code behind to chart control in asp.net c#
Posted
Comments
Sunasara Imdadhusen 27-Jun-13 6:31am    
Try google!
Member 13209478 2-Jun-17 16:18pm    
People like you, writing "try google", are the worst. Sincerely, Google user.

Here set your X-axis and Y-axis value set to array and bind chart control

C#
int[] yValues = {10,25,100}; // Here y values is related to display three month values
 string[] xValues = {"one","two","three"};
 Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);




Add this to front end

XML
<asp:Chart ID="Chart1" runat="server" Height="200px">
       <Series>
           <asp:Series Name="Series1" XValueMember="MonthName" YValueMembers="Mavailable">
           </asp:Series>
       </Series>
       <ChartAreas>
           <asp:ChartArea Name="ChartArea1">
           </asp:ChartArea>
       </ChartAreas>
   </asp:Chart>
 
Share this answer
 
v3
Try this code....

C#
connect.Close();

connect.Open();
DataSet ds = new DataSet();

string query = "SELECT SUM(Quantity) as Quan,Itemname   FROM Orderdetails where hotelid='" + hotelid + "'    and datetime between '" + inputField.Text + "' and '" + inputField1.Text + "'  GROUP BY  Itemname,Quantity";
SqlCommand cmd = new SqlCommand(query, connect);

cmd.ExecuteNonQuery();
connect.Close();
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds, "Orderdetails");
if (ds.Tables["Orderdetails"].Rows.Count > 1)
{
    Chart1.DataSource = ds.Tables["Orderdetails"];
    Chart1.ChartAreas[0].AxisX.Title = "Itemname";
    Chart1.ChartAreas[0].AxisY.Title = "Quantity";
    Chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;

    Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
    Chart1.ChartAreas["ChartArea1"].AxisY.IsLabelAutoFit = true;

    Chart1.ChartAreas["ChartArea1"].AxisY.Interval = 1;
    Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "Itemname";
    Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "Quan";
    Chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
    Chart1.ChartAreas[0].AxisY.IntervalOffset = 1;
}

C#
Chart1.Series["Count"].XValueMember = "Itemname";

Chart1.Series["Count"].YValueMembers = "Quan";
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900