2014年7月13日 星期日

[RESOLVED] Data binding Bar Chart c#


Hi everyone,


I want to populate a Bar Chart from an sql database,  and also to make the bar chart changes as my data changes.


 I have a dropdownlist that is used to select the ID of the element which I want to bind its data to the chart.


Code C#:


protected void Page_Load(object sender, EventArgs e)
{

//constring, sqlconnection instantiation

SqlDataSource da = new SqlDataSource(constring, "select [idacp] from [VALEUR]");

DropDownList1.DataSource = da;
DropDownList1.DataTextField = "idacp";
DropDownList1.DataValueField = "idacp";
DropDownList1.DataBind();

}

and the structure of the table which i want to use for databinding is :


TABLE : [dbo].[VALEUR](
[IDVAL] [numeric] IDENTITY(1,1) //primary key

[IDACP] [int] // foreign key

// each ID ACP have a maximum of 12 values, each "VALM*" represent a monthly value
[VALM1] [int] // first month
[VALM2] [int] // second month
[VALM3] [int]
[VALM4] [int]
[VALM5] [int]
[VALM6] [int]
[VALM7] [int]
[VALM8] [int]
[VALM9] [int]
[VALM10] [int]
[VALM11] [int]
[VALM12] [int]

I need to display all the 12 values of the chosen element from the ddl in the chart ( in the X-Asis), and some integer values in the the Y-Axis.


I have already created a chart (all the configuration is working) but i don't know how to populate it, and how to set xvaluemember and yvaluemeber, datapoint etc ... 







<%-- Do i need to set the datapoint, and how ? --%>











and what to add or to change in this C# code ?


{
DataTable dt = new DataTable();
using (SqlConnection cn = new SqlConnection(Constring))
{
string sql = "select * from [VALEUR] where IDACP =" + DropDownList1.SelectedValue;
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}

}
}
Chart1.DataSource = dt;
Chart1.DataBind();
}



If anyone can help me, i'll be grateful !



hi CameliaRR ,


   From what I understand you want to databinding Bar Chart  using c# code.you don’t need to set the datapoint in html source code.you can  add points  in the  code-behind.


There is demo as below:



BorderWidth = "2" BorderColor = "#cc9900" >


















Code-behind as below:


// data bing Chart

public void BindChart()

{

//get Sales value

int[] arraySell = new int[] { 10 };

//get month

string[] arrayMonths = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };

//get Department

string[] arrayDeparts = new string[] { "Department1", "Department2", "Department3" };

Series seriess = new Series("Sales");

seriess.BorderWidth = 3;

seriess.ShadowOffset = 2;

this.ChartBar.Series.Add(seriess);


Title tBar = new Title("Sales Histogram");

this.ChartBar.Titles.Add(tBar);

this.ChartBar.ChartAreas["ChartArea1"].AxisX.Interval = 1;//x value

this.ChartBar.ChartAreas["ChartArea1"].AxisY.Interval = 50;//y value

//Draw Chart bar

foreach (string d in arrayDeparts)

{

Series tempseries = new Series(string.Format("{0}", d));

this.ChartBar.Series.Add(tempseries);

foreach (string m in arrayMonths)

{

foreach (int n in arraySell)

{

tempseries.Points.AddXY(m, n);

}

}

}

}

Hope it can help you.



Thanks everyone for your help !


Actually, I figured out another way to do it by converting my DataRow into an int array and then binding the chart with it.


Cause afterall I didn't get the classic method.



沒有留言:

張貼留言