Hello,
previously I was using SfChart on Xamarin.Android with the DataSource property of the LineSeries with an ObservableArrayList. I was adding ChartDataPoints to this list on runtime and the chart refreshed itself properly.
However this did only work on Android devices with API <= 25. On API 26+ (Oreo) the chart always stayed empty.
Beacuse of this and th fact that the DataSource property is marked as deprecated, I tried to use the ItemsSource property of LineSeries with an ObservableCollection, but I can't get it to work on all APIs.
Please find the following example and note that it does not work with ChartDataPoint, either.
public class ChartFragment : Fragment
{
private SfChart _chart;
private ObservableCollection _chartData;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.Chart, container, false);
_chartData = new ObservableCollection();
LineSeries dataSeries = new LineSeries();
dataSeries.XBindingPath = nameof(MeasurementPoint.Timestamp);
dataSeries.YBindingPath = nameof(MeasurementPoint.Value);
dataSeries.DataSource.ItemsSource = _chartData;
_chart = new SfChart(Activity);
_chart.Series.Add(dataSeries);
DateTimeAxis primaryAxis = new DateTimeAxis();
primaryAxis.Title.Text = "Time";
primaryAxis.LabelRotationAngle = -45;
primaryAxis.LabelStyle.LabelFormat = "HH:mm:ss";
_chart.PrimaryAxis = primaryAxis;
NumericalAxis secondaryAxis = new NumericalAxis();
secondaryAxis.Title.Text = "Value";
_chart.SecondaryAxis = secondaryAxis;
LinearLayout layout = view.FindViewById(Resource.Id.chartLinearLayout2);
layout.AddView(_chart);
return view;
}
public void AddValues(DateTime dateTime, double value)
{
_chartData.Add(new MeasurementPoint(dateTime, value));
}
private class MeasurementPoint
{
public DateTime Timestamp { get; set; }
public double Value { get; set; }
public MeasurementPoint(DateTime timestamp, double value)
{
Timestamp = timestamp;
Value = value;
}
}
}
Thanks, Florian