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

Bind Pie Chart to Linq

4 Answers 113 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 23 Nov 2012, 10:15 AM
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>

4 Answers, 1 is accepted

Sort by
0
Petar Kirov
Telerik team
answered on 27 Nov 2012, 05:34 PM
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.

0
Andrew
Top achievements
Rank 1
answered on 28 Nov 2012, 08:35 PM
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.
0
Petar Kirov
Telerik team
answered on 03 Dec 2012, 09:17 PM
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.

0
Andrew
Top achievements
Rank 1
answered on 06 Dec 2012, 09:42 AM
That work around works

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