I am trying to display Multiple Series in a ListView, My requirement is that each cell's seriescollections may vary in number. How can I achieve this? I've shared the code which I have done so far.
I hope I've stated my query clearly.
var source = new ObservableCollection<MyChartData> ();
source.Add (new MyChartData {Name = "A"});
source.Add (new MyChartData {Name = "B"});
source.Add (new MyChartData {Name = "C"});
source.Add (new MyChartData {Name = "D"});
source.Add (new MyChartData {Name = "E"});
var listView = new ListView (ListViewCachingStrategy.RecycleElement) {
ItemsSource = source,
RowHeight = 200,
HasUnevenRows = true,
ItemTemplate = new DataTemplate(()=>{
var chart = new CustomChart{
HeightRequest = 200,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
PrimaryAxis = new CategoryAxis(),
SecondaryAxis = new NumericalAxis(),
};
chart.SetBinding(CustomChart.TitleProperty,"Name");
chart.SetBinding(CustomChart.DataSourceProperty,"BarData");
return new ViewCell{
View = chart,
};
}),
};
Chart Data:
public class MyChartData{
public string Name {get; set;}
public ObservableCollection<ObservableCollection<ChartDataPoint>> BarData {get; set;}
public MyChartData(){
BarData = new ObservableCollection<ObservableCollection<ChartDataPoint>> ();
ObservableCollection<ChartDataPoint> Data1 = new ObservableCollection<ChartDataPoint> ();
ObservableCollection<ChartDataPoint> Data2 = new ObservableCollection<ChartDataPoint> ();
Random r = new Random();
Data1.Add(new ChartDataPoint("Jan",r.Next(1,100)));
Data1.Add(new ChartDataPoint("Feb",r.Next(1,100)));
Data1.Add(new ChartDataPoint("Mar",r.Next(1,100)));
Data1.Add(new ChartDataPoint("Apr",r.Next(1,100)));
Data1.Add(new ChartDataPoint("May",r.Next(1,100)));
Data2.Add(new ChartDataPoint("Jan",r.Next(1,100)));
Data2.Add(new ChartDataPoint("Feb",r.Next(1,100)));
Data2.Add(new ChartDataPoint("Mar",r.Next(1,100)));
Data2.Add(new ChartDataPoint("Apr",r.Next(1,100)));
Data2.Add(new ChartDataPoint("May",r.Next(1,100)));
BarData.Add (Data1);
BarData.Add (Data2);
}
}
Custom Chart Code:
public class CustomChart : SfChart
{
public CustomChart ()
{
}
/// <summary>
/// The SeriesCollection property
/// </summary>
public static readonly BindableProperty DataSourceProperty = BindableProperty.Create("DataSource", typeof(IEnumerable<IEnumerable<object>>), typeof(CustomChart), default(IEnumerable<IEnumerable<object>>));
/// <summary>
/// Gets or sets the DataSource
/// </summary>
public IEnumerable<IEnumerable<object>> DataSource
{
get { return (IEnumerable<IEnumerable<object>>)GetValue(DataSourceProperty); }
set {
if (value != null) {
base.Series.Clear ();
foreach (var s in value) {
var barSeries = new ColumnSeries{ ItemsSource = s};
base.Series.Add (barSeries);
}
}
SetValue(DataSourceProperty, value);
}
}
}
Thanks,
Femil Shajin C