Bind Pie Chart to Linq

5 posts, 0 answers
  1. Andy
    Andy avatar
    12 posts
    Member since:
    Oct 2011

    Posted 23 Nov 2012 Link to this post

    From what i understand this should be possible and very easy, but i must be missing something...

    Dim SumData = From S In _DataStockInfoList Group S By Key = S.CategoryDescription Into Group Select Supplier = Key, StockValue = Group.Sum(Function(S) S.TotalCost)

    Me.StockValueSeries.ItemsSource = SumData *** Gives me a runtime error Null object !!

    And my xaml

                      <telerik:RadPieChart x:Name="PieChartStockValue" Palette="Metro" >
                            <telerik:PieSeries x:Name="StockValueSeries" ShowLabels="True" RadiusFactor="0.7" ValueBinding="StockValue" ItemsSource="{Binding}" >
                                <telerik:PieSeries.LabelDefinitions>
                                    <telerik:ChartSeriesLabelDefinition Margin="-30,0,0,0"/>
                                </telerik:PieSeries.LabelDefinitions>
                                <telerik:PieSeries.AngleRange>
                                    <telerik:AngleRange  SweepAngle="360" />
                                </telerik:PieSeries.AngleRange>
                            </telerik:PieSeries>
                        </telerik:RadPieChart>
  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 27 Nov 2012 Link to this post

    Hi Andy,

    I believe that the null-reference error you are getting is caused not by the binding of the ItemSource (or the LINQ), but the the fact that StockValueSeries is null at runtime.

    The reason for the error is that you can't refer to a series object by its x:Name. Here you can find more information on the matter.

    You can fix that by either:
    • Setting SumData to the DataContext of the UserControl or the RadPieChart
    • Referencing the series by its index in the chart.Series collection. For example: 
      Me.PieChartStockValue.Series(0).ItemsSource = SumData

    I hope this helps.

    Regards,
    Petar Kirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Andy
    Andy avatar
    12 posts
    Member since:
    Oct 2011

    Posted 28 Nov 2012 Link to this post

    Still Not there

    Code behind is
    Dim SumData = From S In _DataStockInfoList Group S By Key = S.CategoryDescription Into Group Select Supplier = Key, StockValue = Group.Sum(Function(S) S.TotalCost)
     
    Me.PieChartStockValue.Series(0).ItemsSource = SumData
    Xaml is
    <telerik:RadPieChart x:Name="PieChartStockValue" Palette="Metro" >
                            <telerik:PieSeries  ShowLabels="True" RadiusFactor="0.7" ValueBinding="StockValue" ItemsSource="{Binding}">
                                <telerik:PieSeries.LabelDefinitions>
                                    <telerik:ChartSeriesLabelDefinition Margin="-30,0,0,0"/>
                                </telerik:PieSeries.LabelDefinitions>
                                <telerik:PieSeries.AngleRange>
                                    <telerik:AngleRange  SweepAngle="360" />
                                </telerik:PieSeries.AngleRange>
                            </telerik:PieSeries>
                        </telerik:RadPieChart>

    And I now get the following error at runtime

    Attempt by security transparent method 'DynamicClass.Telerik_DynamicGetter_StockValue(System.Object)' to access security critical type 'VB$AnonymousType_6`2<System.String,System.Decimal>' failed.
  5. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 03 Dec 2012 Link to this post

    Hi Andy,

    The problem is related to anonymous classes. Please, create a small class containing the properties you need as a result of the LINQ expression and pass the resulting collection to chart series ItemsSource. For example: 
    Public Class DataItem
        Public Property Supplier As String
        Public Property StockValue As Double
    End Class
        
        
    'Old:
    Dim SumData = From S In _DataStockInfoList Group S By Key = S.CategoryDescription Into Group
       Select Supplier = Key, StockValue = Group.Sum(Function(S) S.TotalCost)
        
    'New:
    Dim sumData = From S In _DataStockInfoList _
             Group S By Key = S.CategoryDescription Into Group _
             Select New DataItem With { _
              .Supplier = Key, _
              .StockValue = Group.Sum(Function(s) s.TotalCost)}
      
    Me.PieChartStockValue.Series(0).ItemsSource = sumData

    Regards,
    Petar Kirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Andy
    Andy avatar
    12 posts
    Member since:
    Oct 2011

    Posted 06 Dec 2012 Link to this post

    That work around works

    Thank you
Back to Top
DevCraft banner