public class PieChartExt : SfChart
{
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(object), typeof(PieChartExt), null, BindingMode.Default, null, OnItemsSourceChanged);
public static readonly BindableProperty XBindingPathProperty =
BindableProperty.Create("XBindingPath", typeof(string), typeof(PieChartExt), "XValue", BindingMode.Default, null);
public static readonly BindableProperty YBindingPathProperty =
BindableProperty.Create(nameof(YBindingPath), typeof(string), typeof(PieChartExt), "YValue", BindingMode.Default, null);
public object ItemsSource
{
get { return (object)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public string XBindingPath
{
get { return (string)GetValue(XBindingPathProperty); }
set { SetValue(XBindingPathProperty, value); }
}
public string YBindingPath
{
get { return (string)GetValue(YBindingPathProperty); }
set { SetValue(YBindingPathProperty, value); }
}
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
(bindable as PieChartExt).GenerateSeries(newValue);
}
private void GenerateSeries(object newValue)
{
if (ItemsSource != null)
{
var commonItemsSource = (ItemsSource as IEnumerable).GetEnumerator();
if (newValue is INotifyCollectionChanged)
(newValue as INotifyCollectionChanged).CollectionChanged += DataPoint_CollectionChanged;
while (commonItemsSource.MoveNext())
{
CreateSeries(commonItemsSource.Current);
}
}
}
…
} |
<local:PieChartExt x:Name="Chart" ItemsSource="{Binding Data}" XBindingPath="Component" YBindingPath="Price" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<chart:SfChart.Title>
<chart:ChartTitle Text="A Linear PieChart" />
</chart:SfChart.Title>
<chart:SfChart.BindingContext>
<local:ViewModel x:Name="viewModel" />
</chart:SfChart.BindingContext>
<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis IsVisible="False" ShowMajorGridLines="False" >
</chart:CategoryAxis>
</chart:SfChart.PrimaryAxis>
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis IsVisible="False" ShowMajorGridLines="False">
</chart:NumericalAxis>
</chart:SfChart.SecondaryAxis>
<chart:SfChart.ColorModel>
<chart:ChartColorModel Palette="Natural"/>
</chart:SfChart.ColorModel>
</local:PieChartExt> |