Binding Data to a RadCartesianChart

4 posts, 0 answers
  1. Stephan
    Stephan avatar
    33 posts
    Member since:
    Jun 2012

    Posted 28 Nov 2012 Link to this post

    I have some data that is coming from a Stored procedure which i have been able to display in a non-Telerik DataGrid.  using the following code.

    CBFdataDataContext conn = new CBFdataDataContext();
    List<spTotalRevByZipResult> sptotalrevbyzipresult = (from s in conn.spTotalRevByZip()
    select s).ToList();
    ZipGrid.ItemsSource = sptotalrevbyzipresult;

    But with using the radCartesianChart, It says that there is no ItemSource.
    <telerik:RadCartesianChart HorizontalAlignment="Left" Margin="10,104,0,0" Grid.Row="1" VerticalAlignment="Top" Width="723" Height="218" Name="ZipGrid">
               <telerik:RadCartesianChart.VerticalAxis>
                   <telerik:LinearAxis/>
               </telerik:RadCartesianChart.VerticalAxis>
               <telerik:RadCartesianChart.HorizontalAxis>
                   <telerik:CategoricalAxis/>
               </telerik:RadCartesianChart.HorizontalAxis>
               <telerik:RadCartesianChart.Series>
                   <telerik:BarSeries CategoryBinding="subTotal"
                                      ValueBinding="custzip"
                                      ItemsSource="{Binding}"/>
               </telerik:RadCartesianChart.Series>
           </telerik:RadCartesianChart>
    So How do i load the chart with Data?
    CBFsqldataDataContext conn = new  CBFsqldataDataContext();
                List<spTotalRevByZipResult> sptotalrevbyzipresult = (from s in conn.spTotalRevByZip()
                                                                     select s).ToList();
                 
                ZipGrid????????? = sptotalrevbyzipresult;











  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 03 Dec 2012 Link to this post

    Hi Stephan,

    You can use the DataContext property of the RadCartesianChart to set the data collection, that you want the chart to visualize. Or you can use the ItemSource of the individual series, if you want to have different ItemSources for each series.

    I have attached an example.
     
    Greetings,
    Petar Kirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Stephan
    Stephan avatar
    33 posts
    Member since:
    Jun 2012

    Posted 03 Dec 2012 Link to this post

    Ok, I am truly missing something here, your example appears to me as though your data is hard coded.  All the examples I see the dtat looks hard coded.
    So are you saying that I have to loop through all the rows of data and create 460
    do while not eof()
    new spTotalRevByZipResult { subTotal = 1, custzip = 20},
               Next Row
    end Do

    public partial class MainWindow : Window
       {
           public MainWindow()
           {
               InitializeComponent();
     
               CBFdataDataContext conn = new CBFdataDataContext();
               List<spTotalRevByZipResult> sptotalrevbyzipresult = (from s in conn.spTotalRevByZip()
                                                                    select s).ToList();
               ZipChart.DataContext = sptotalrevbyzipresult;
           }
       }
     
       public class CBFdataDataContext
       {
           public IEnumerable<spTotalRevByZipResult> spTotalRevByZip()
           {
               return new spTotalRevByZipResult[]
               {
                   new spTotalRevByZipResult { subTotal = 1, custzip = 20},
                   new spTotalRevByZipResult { subTotal = 2, custzip = 55},
                   new spTotalRevByZipResult { subTotal = 3, custzip = 30},
                   new spTotalRevByZipResult { subTotal = 4, custzip = 40},
               };
           }
       }
     
       public class spTotalRevByZipResult
       {
           public int subTotal { get; set; }
           public double custzip { get; set; }
  5. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 06 Dec 2012 Link to this post

    Hello Stephan,

    Yes the data in this particular example is hard-coded, but this is only for simplicity and portability. This allows you to easily plug-in your data-retrieval logic. 

    That said, there is no need to manually create data points. You just need to tell the RadCartesianChart how to interpret the data collection you receive as a result of a Stored Procedure (or whatever is your source of data). Note that at least one of the two columns should be numeric, or the class that represents (models) a row of your data base should store that data in a numeric form.
     
    All the best,
    Petar Kirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top