This is a migrated thread and some comments may be shown as answers.

Binding Data to a RadCartesianChart

3 Answers 497 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Stephan
Top achievements
Rank 1
Stephan asked on 28 Nov 2012, 06:26 PM
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;











3 Answers, 1 is accepted

Sort by
0
Petar Kirov
Telerik team
answered on 03 Dec 2012, 03:35 PM
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.

0
Stephan
Top achievements
Rank 1
answered on 03 Dec 2012, 04:37 PM

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; }
0
Petar Kirov
Telerik team
answered on 06 Dec 2012, 01:36 PM
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.

Tags
ChartView
Asked by
Stephan
Top achievements
Rank 1
Answers by
Petar Kirov
Telerik team
Stephan
Top achievements
Rank 1
Share this question
or