Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching data from service and I am trying to bind data to bar chart I am unable to do this see my code
C#
protected async override void OnNavigatedTo(NavigationEventArgs e) 
{
       StatusServiceReferenceHost.Service1Client MyService = new StatusServiceReferenceHost.Service1Client();
       var data = await MyService.GetStatusAsync();

       foreach (var cchart in cidata)
       {
           //I am nt sure how to bind
           (dataChart.Series[0] as BarSeries).ItemsSource = cdata;
       }

in my get method I have more columns but I wanto bind only 2 columns suppose I have name and datacount columns pls help me onthis
Posted
Updated 11-Dec-14 0:11am
v2

hello jayantik ,
you have to dynamically add data to every series.I think below code is your solution(code is in vb.net).reply me if u need more help.

VB
If (dt.Rows.Count > 0) Then

                YourChart.DataSource = dt

                YourChart.Series.Clear()

                For colIndex As Integer = 1 To dt.Columns.Count - 1
                    ' For each column (column 1 and onward) add the value as a point
                    Dim Cseries As New Series(dt.Columns(colIndex).ColumnName)
                    Cseries.Points.Clear()
                    For Each dr In dt.Rows
                        Dim tempi As Integer = Convert.ToInt32(IIf(IsDBNull(dr(colIndex)), 0, dr(colIndex)))
                        Cseries.Points.AddXY(dr("Code"), tempi)
                    Next
                    YourChart.Series.Add(Cseries)
                    ' YourChart.Series(dt.Columns(colIndex).ColumnName)("PointWidth") = "2"
                    YourChart.Series(dt.Columns(colIndex).ColumnName).IsValueShownAsLabel = True

                    YourChart.Series(dt.Columns(colIndex).ColumnName).IsVisibleInLegend = True
                    YourChart.Series(dt.Columns(colIndex).ColumnName).Color = LegendColor(dt.Columns(colIndex).ColumnName)
                Next
                YourChart.DataBind()
                YourChart.Titles.Clear()
                ' YourChart.AlternateText.SmartLabel.DynamicDisplay = False
                YourChart.ChartAreas(0).AxisX.Interval = 1
                YourChart.ChartAreas(0).AxisX.Title = "Locations"
                YourChart.ChartAreas(0).AxisY.Title = "Numbers"
                shiftSelectedDate = DateTime.Now
                YourChart.Titles.Add("Summary")
                YourChart.Titles.Add("Record for the date:-" + " " + shiftSelectedDate.ToString("dd-MMM-yy"))

            Else
                YourChart.Titles.Clear()
                'YourChart.Series(0).Points.Clear()
                shiftSelectedDate = DateTime.Now
                YourChart.AlternateText = ""
                ' YourChart.Titles.Add("Summary")
                YourChart.Titles.Add("No Record for the date:-" + " " + shiftSelectedDate.ToString("dd-MMM-yy"))
            End If


Aditya Prakash
Software Enginner
Seed Management Services pvt ltd
 
Share this answer
 
Comments
jayanthik 12-Dec-14 0:45am    
hi adi thanks for ur reply.
will u pls send in C# and I need to bind data from collection to chart. if u send in c# it will be great help
Aditya Prakash Roy 12-Dec-14 2:29am    
you can get code in c#,i posted as per your requirement.
Thanku for your Response
here i my code in c#...i think this code can help you..
C#
if ((dt.Rows.Count > 0)) {

    YourChart.DataSource = dt;

    YourChart.Series.Clear();

    for (int colIndex = 1; colIndex <= dt.Columns.Count - 1; colIndex++) {
        // For each column (column 1 and onward) add the value as a point
        Series Cseries = new Series(dt.Columns(colIndex).ColumnName);
        Cseries.Points.Clear();
        foreach (void dr_loopVariable in dt.Rows) {
            dr = dr_loopVariable;
            int tempi = Convert.ToInt32((Information.IsDBNull(dr(colIndex)) ? 0 : dr(colIndex)));
            Cseries.Points.AddXY(dr("Code"), tempi);
        }
        YourChart.Series.Add(Cseries);
        // YourChart.Series(dt.Columns(colIndex).ColumnName)("PointWidth") = "2"
        YourChart.Series(dt.Columns(colIndex).ColumnName).IsValueShownAsLabel = true;

        YourChart.Series(dt.Columns(colIndex).ColumnName).IsVisibleInLegend = true;
        YourChart.Series(dt.Columns(colIndex).ColumnName).Color = LegendColor(dt.Columns(colIndex).ColumnName);
    }
    YourChart.DataBind();
    YourChart.Titles.Clear();
    // YourChart.AlternateText.SmartLabel.DynamicDisplay = False
    YourChart.ChartAreas(0).AxisX.Interval = 1;
    YourChart.ChartAreas(0).AxisX.Title = "Locations";
    YourChart.ChartAreas(0).AxisY.Title = "Numbers";
    shiftSelectedDate = DateTime.Now;
    YourChart.Titles.Add("Summary");
    YourChart.Titles.Add("Record for the date:-" + " " + shiftSelectedDate.ToString("dd-MMM-yy"));

} else {
    YourChart.Titles.Clear();
    //YourChart.Series(0).Points.Clear()
    shiftSelectedDate = DateTime.Now;
    YourChart.AlternateText = "";
    // YourChart.Titles.Add("Summary")
    YourChart.Titles.Add("No Record for the date:-" + " " + shiftSelectedDate.ToString("dd-MMM-yy"));
}
 
Share this answer
 
Comments
Deepu S Nair 17-Dec-14 6:59am    
You need to submit only one solution and if you want to update it just use improve solution link.

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